Remove Hash, Hash256, Hash160 and ScryptHash256 classes.
[NovacoinLibrary.git] / Novacoin / CKey.cs
1 \feff/**
2  *  Novacoin classes library
3  *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
4
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.
9
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.
14
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/>.
17  */
18
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;
25
26 using System.Collections.Generic;
27 using System.Linq;
28
29
30 namespace Novacoin
31 {
32     /// <summary>
33     /// Basic pubkey functionality
34     /// </summary>
35     public abstract class CKey
36     {
37         // These fields are inherited by CPubKey and CKeyPair
38         protected ECPublicKeyParameters _Public;
39
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());
42
43         /// <summary>
44         /// Regenerate public key parameters (ECPoint compression)
45         /// </summary>
46         /// <param name="pubKeyParams">Non-compressed key parameters</param>
47         /// <returns>Parameters for compressed key</returns>
48         protected ECPublicKeyParameters Compress(ECPublicKeyParameters pubKeyParams)
49         {
50             if (pubKeyParams.Q.IsCompressed)
51             {
52                 // Already compressed
53                 return pubKeyParams;
54             }
55
56             ECPoint q = new FpPoint(curve.Curve, pubKeyParams.Q.X, pubKeyParams.Q.Y, true);
57
58             return new ECPublicKeyParameters(q, domain);
59         }
60
61         /// <summary>
62         /// Regenerate public key parameters (ECPoint decompression)
63         /// </summary>
64         /// <param name="pubKeyParams">Compressed key parameters</param>
65         /// <returns>Parameters for non-compressed key</returns>
66         protected ECPublicKeyParameters Decompress(ECPublicKeyParameters pubKeyParams)
67         {
68             if (!pubKeyParams.Q.IsCompressed)
69             {
70                 // Isn't compressed
71                 return pubKeyParams;
72             }
73
74             var q = new FpPoint(curve.Curve, pubKeyParams.Q.X, pubKeyParams.Q.Y, false);
75
76             return new ECPublicKeyParameters(q, domain);
77         }
78
79         /// <summary>
80         /// Does the signature matches our public key?
81         /// </summary>
82         /// <param name="sigHash">Data hash</param>
83         /// <param name="signature">Signature bytes</param>
84         /// <returns>Checking result</returns>
85         public bool VerifySignature(uint256 sigHash, byte[] signature)
86         {
87             var signer = SignerUtilities.GetSigner("NONEwithECDSA");
88             signer.Init(false, _Public);
89             signer.BlockUpdate(sigHash, 0, sigHash.Size);
90
91             return signer.VerifySignature(signature);
92         }
93
94         /// <summary>
95         /// Calculate Hash160 and create new CKeyID instance.
96         /// </summary>
97         /// <returns>New key ID</returns>
98         public CKeyID KeyID
99         {
100             get { return new CKeyID(CryptoUtils.ComputeHash160(_Public.Q.GetEncoded())); }
101         }
102
103         /// <summary>
104         /// Is this a compressed public key?
105         /// </summary>
106         /// <returns></returns>
107         public bool IsCompressed
108         {
109             get { return _Public.Q.IsCompressed; }
110         }
111
112     }
113 }