2 * Novacoin classes library
3 * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 using Org.BouncyCastle.Asn1.Sec;
20 using Org.BouncyCastle.Asn1.X9;
21 using Org.BouncyCastle.Crypto;
22 using Org.BouncyCastle.Crypto.Parameters;
23 using Org.BouncyCastle.Math.EC;
24 using Org.BouncyCastle.Security;
26 using System.Collections.Generic;
33 /// Basic pubkey functionality
35 public abstract class CKey
37 // These fields are inherited by CPubKey and CKeyPair
38 protected ECPublicKeyParameters _Public;
40 protected static X9ECParameters curve = SecNamedCurves.GetByName("secp256k1");
41 protected static ECDomainParameters domain = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H, curve.GetSeed());
44 /// Regenerate public key parameters (ECPoint compression)
46 /// <param name="pubKeyParams">Non-compressed key parameters</param>
47 /// <returns>Parameters for compressed key</returns>
48 protected ECPublicKeyParameters Compress(ECPublicKeyParameters pubKeyParams)
50 if (pubKeyParams.Q.IsCompressed)
56 ECPoint q = new FpPoint(curve.Curve, pubKeyParams.Q.X, pubKeyParams.Q.Y, true);
58 return new ECPublicKeyParameters(q, domain);
62 /// Regenerate public key parameters (ECPoint decompression)
64 /// <param name="pubKeyParams">Compressed key parameters</param>
65 /// <returns>Parameters for non-compressed key</returns>
66 protected ECPublicKeyParameters Decompress(ECPublicKeyParameters pubKeyParams)
68 if (!pubKeyParams.Q.IsCompressed)
74 var q = new FpPoint(curve.Curve, pubKeyParams.Q.X, pubKeyParams.Q.Y, false);
76 return new ECPublicKeyParameters(q, domain);
80 /// Does the signature matches our public key?
82 /// <param name="sigHash">Data hash</param>
83 /// <param name="signature">Signature bytes</param>
84 /// <returns>Checking result</returns>
85 public bool VerifySignature(Hash sigHash, byte[] signature)
87 var signer = SignerUtilities.GetSigner("NONEwithECDSA");
88 signer.Init(false, _Public);
89 signer.BlockUpdate(sigHash, 0, sigHash.hashSize);
91 return signer.VerifySignature(signature);
95 /// Calculate Hash160 and create new CKeyID instance.
97 /// <returns>New key ID</returns>
100 get { return new CKeyID(Hash160.Compute160(_Public.Q.GetEncoded())); }
104 /// Is this a compressed public key?
106 /// <returns></returns>
107 public bool IsCompressed
109 get { return _Public.Q.IsCompressed; }