From: CryptoManiac Date: Tue, 18 Aug 2015 13:04:37 +0000 (+0300) Subject: Remove unnecessary usings, rewrite pubkey serialization code X-Git-Url: https://git.novaco.in/?p=NovacoinLibrary.git;a=commitdiff_plain;h=ee051cb2ce668aefdc603d7c096cd84ac4f5b54f Remove unnecessary usings, rewrite pubkey serialization code --- diff --git a/Novacoin/CKey.cs b/Novacoin/CKey.cs new file mode 100644 index 0000000..e06086c --- /dev/null +++ b/Novacoin/CKey.cs @@ -0,0 +1,58 @@ +using System.Collections.Generic; +using System.Linq; + +using Org.BouncyCastle.Crypto; +using Org.BouncyCastle.Crypto.Parameters; + +using Org.BouncyCastle.Asn1.X9; +using Org.BouncyCastle.Security; +using Org.BouncyCastle.Asn1.Sec; + +namespace Novacoin +{ + /// + /// Basic pubkey functionality + /// + public abstract class CKey + { + // These fields are inherited by CPubKey and CKeyPair + protected ECPublicKeyParameters _Public; + + protected static X9ECParameters curve = SecNamedCurves.GetByName("secp256k1"); + protected static ECDomainParameters domain = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H, curve.GetSeed()); + + /// + /// Does the signature matches our public key? + /// + /// Data bytes + /// Signature bytes + /// Checking result + public bool VerifySignature(IEnumerable data, IEnumerable signature) + { + byte[] dataBytes = data.ToArray(); + + ISigner signer = SignerUtilities.GetSigner("SHA-256withECDSA"); + signer.Init(false, _Public); + signer.BlockUpdate(dataBytes, 0, dataBytes.Length); + + return signer.VerifySignature(signature.ToArray()); + } + + /// + /// Calculate Hash160 and create new CKeyID instance. + /// + /// New key ID + public CKeyID GetKeyID() + { + return new CKeyID(Hash160.Compute160(Public)); + } + + /// + /// Public part of key pair + /// + public IEnumerable Public + { + get { return _Public.Q.GetEncoded(); } + } + } +} diff --git a/Novacoin/CKeyPair.cs b/Novacoin/CKeyPair.cs index 0abbb25..f5b778f 100644 --- a/Novacoin/CKeyPair.cs +++ b/Novacoin/CKeyPair.cs @@ -1,8 +1,6 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Text; -using System.Threading.Tasks; using Org.BouncyCastle.Math; using Org.BouncyCastle.Math.EC; @@ -10,20 +8,14 @@ using Org.BouncyCastle.Math.EC; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Generators; using Org.BouncyCastle.Crypto.Parameters; - -using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Security; -using Org.BouncyCastle.Asn1.Sec; + namespace Novacoin { - public class CKeyPair + public class CKeyPair : CKey { - private BigInteger D; - private ECPoint Q; - - private static X9ECParameters curve = SecNamedCurves.GetByName("secp256k1"); - private static ECDomainParameters domain = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H, curve.GetSeed()); + private ECPrivateKeyParameters _Private; /// /// Initialize new CKeyPair instance with random secret. @@ -31,13 +23,12 @@ namespace Novacoin public CKeyPair() { ECKeyGenerationParameters genParams = new ECKeyGenerationParameters(domain, new SecureRandom()); - ECKeyPairGenerator generator = new ECKeyPairGenerator("ECDSA"); generator.Init(genParams); AsymmetricCipherKeyPair ecKeyPair = generator.GenerateKeyPair(); - Q = ((ECPublicKeyParameters)ecKeyPair.Public).Q; - D = ((ECPrivateKeyParameters)ecKeyPair.Private).D; + _Public = (ECPublicKeyParameters)ecKeyPair.Public; + _Private = (ECPrivateKeyParameters)ecKeyPair.Private; } /// @@ -46,8 +37,14 @@ namespace Novacoin /// Byte sequence public CKeyPair(IEnumerable secretBytes) { - D = new BigInteger(secretBytes.ToArray()); - Q = curve.G.Multiply(D); + // Deserialize secret value + BigInteger D = new BigInteger(secretBytes.ToArray()); + + // Calculate public key + ECPoint Q = curve.G.Multiply(D); + + _Public = new ECPublicKeyParameters(Q, domain); + _Private = new ECPrivateKeyParameters(D, domain); } /// @@ -60,37 +57,15 @@ namespace Novacoin byte[] dataBytes = data.ToArray(); ISigner signer = SignerUtilities.GetSigner("SHA-256withECDSA"); - ECPrivateKeyParameters keyParameters = new ECPrivateKeyParameters(D, domain); - signer.Init(true, keyParameters); + signer.Init(true, _Private); signer.BlockUpdate(dataBytes, 0, dataBytes.Length); return signer.GenerateSignature(); } - public bool Verify(IEnumerable data, IEnumerable signature) - { - byte[] dataBytes = data.ToArray(); - - ISigner signer = SignerUtilities.GetSigner("SHA-256withECDSA"); - ECPublicKeyParameters keyParameters = new ECPublicKeyParameters(Q, domain); - signer.Init(false, keyParameters); - signer.BlockUpdate(dataBytes, 0, dataBytes.Length); - - return signer.VerifySignature(signature.ToArray()); - } - public CPubKey GetPubKey() { - return new CPubKey(Q); - } - - /// - /// Calculate Hash160 and create new CKeyID instance. - /// - /// New key ID - public CKeyID GetKeyID() - { - return new CKeyID(Hash160.Compute160(Public)); + return new CPubKey(Public); } /// @@ -98,15 +73,7 @@ namespace Novacoin /// public IEnumerable Secret { - get { return D.ToByteArray(); } - } - - /// - /// Public part of key pair - /// - public IEnumerable Public - { - get { return Q.GetEncoded(); } + get { return _Private.D.ToByteArray(); } } public override string ToString() diff --git a/Novacoin/CPubKey.cs b/Novacoin/CPubKey.cs index a6227a6..8ad4c3e 100644 --- a/Novacoin/CPubKey.cs +++ b/Novacoin/CPubKey.cs @@ -1,38 +1,22 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; - -using Org.BouncyCastle.Math; using Org.BouncyCastle.Math.EC; - -using Org.BouncyCastle.Crypto; -using Org.BouncyCastle.Crypto.Generators; using Org.BouncyCastle.Crypto.Parameters; -using Org.BouncyCastle.Asn1.X9; -using Org.BouncyCastle.Security; -using Org.BouncyCastle.Asn1.Sec; - namespace Novacoin { /// /// Representation of ECDSA public key /// - public class CPubKey + public class CPubKey : CKey { - private ECPoint Q; - private static X9ECParameters curve = SecNamedCurves.GetByName("secp256k1"); - private static ECDomainParameters domain = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H, curve.GetSeed()); - /// /// Initializes a new instance of CPubKey class as the copy of another instance /// /// Another CPubKey instance public CPubKey(CPubKey pubKey) { - Q = pubKey.Q; + _Public = pubKey._Public; } /// @@ -41,12 +25,8 @@ namespace Novacoin /// Byte sequence public CPubKey(IEnumerable bytes) { - Q = ((ECPublicKeyParameters)PublicKeyFactory.CreateKey(bytes.ToArray())).Q; - } - - public CPubKey(ECPoint pQ) - { - Q = pQ; + ECPoint pQ = curve.Curve.DecodePoint(bytes.ToArray()); + _Public = new ECPublicKeyParameters(pQ, domain); } /// @@ -55,7 +35,7 @@ namespace Novacoin /// Validation result public bool IsValid { - get { return !Q.IsInfinity; } + get { return !_Public.Q.IsInfinity; } } /// @@ -64,41 +44,12 @@ namespace Novacoin /// public bool IsCompressed { - get { return Q.IsCompressed; } - } - - /// - /// Calculate Hash160 and create new CKeyID instance. - /// - /// New key ID - public CKeyID GetKeyID() - { - return new CKeyID(Hash160.Compute160(Raw)); - } - - public bool Verify(IEnumerable data, IEnumerable signature) - { - byte[] dataBytes = data.ToArray(); - - ISigner signer = SignerUtilities.GetSigner("SHA-256withECDSA"); - ECPublicKeyParameters keyParameters = new ECPublicKeyParameters(Q, domain); - signer.Init(false, keyParameters); - signer.BlockUpdate(dataBytes, 0, dataBytes.Length); - - return signer.VerifySignature(signature.ToArray()); - } - - /// - /// Accessor for internal representation - /// - public IEnumerable Raw - { - get { return Q.GetEncoded(); } + get { return _Public.Q.IsCompressed; } } public override string ToString() { - return Interop.ToHex(Raw); + return Interop.ToHex(Public); } } } diff --git a/Novacoin/CScript.cs b/Novacoin/CScript.cs index 65b824a..f77b8c8 100644 --- a/Novacoin/CScript.cs +++ b/Novacoin/CScript.cs @@ -1,8 +1,6 @@ using System; using System.Linq; using System.Text; - -using System.Collections; using System.Collections.Generic; namespace Novacoin @@ -352,7 +350,7 @@ namespace Novacoin foreach (CPubKey key in keys) { - PushData(key.Raw.ToList()); + PushData(key.Public.ToList()); } AddOp(ScriptOpcode.EncodeOP_N(keys.Count())); AddOp(opcodetype.OP_CHECKMULTISIG); diff --git a/Novacoin/Interop.cs b/Novacoin/Interop.cs index 2405890..a2ead39 100644 --- a/Novacoin/Interop.cs +++ b/Novacoin/Interop.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Text; -using System.Threading.Tasks; namespace Novacoin { diff --git a/Novacoin/Novacoin.csproj b/Novacoin/Novacoin.csproj index c8d8539..5e158c0 100644 --- a/Novacoin/Novacoin.csproj +++ b/Novacoin/Novacoin.csproj @@ -35,6 +35,7 @@ + diff --git a/Novacoin/ScriptOpcode.cs b/Novacoin/ScriptOpcode.cs index a5a524c..436de24 100644 --- a/Novacoin/ScriptOpcode.cs +++ b/Novacoin/ScriptOpcode.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; -using System.Threading.Tasks; namespace Novacoin { diff --git a/NovacoinTest/Program.cs b/NovacoinTest/Program.cs index 2270b8c..fe54e2a 100644 --- a/NovacoinTest/Program.cs +++ b/NovacoinTest/Program.cs @@ -1,8 +1,6 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Text; -using System.Threading.Tasks; namespace NovacoinTest @@ -58,8 +56,8 @@ namespace NovacoinTest byte[] signature = keyPair1.Sign(dataBytes).ToArray(); Console.WriteLine("Signature: {0}", Interop.ToHex(signature)); - Console.WriteLine("Signature is OK: {0} (CKeyPair)", keyPair1.Verify(dataBytes, signature)); - Console.WriteLine("Signature is OK: {0} (CPubKey)", pubKey.Verify(dataBytes, signature)); + Console.WriteLine("Signature is OK: {0} (CKeyPair)", keyPair1.VerifySignature(dataBytes, signature)); + Console.WriteLine("Signature is OK: {0} (CPubKey)", pubKey.VerifySignature(dataBytes, signature)); Console.ReadLine(); }