/**
* Novacoin classes library
* Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
using Org.BouncyCastle.Asn1.Sec;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math.EC;
using Org.BouncyCastle.Security;
using System.Collections.Generic;
using System.Linq;
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());
///
/// Regenerate public key parameters (ECPoint compression)
///
/// Non-compressed key parameters
/// Parameters for compressed key
protected ECPublicKeyParameters Compress(ECPublicKeyParameters pubKeyParams)
{
if (pubKeyParams.Q.IsCompressed)
{
// Already compressed
return pubKeyParams;
}
ECPoint q = new FpPoint(curve.Curve, pubKeyParams.Q.X, pubKeyParams.Q.Y, true);
return new ECPublicKeyParameters(q, domain);
}
///
/// Regenerate public key parameters (ECPoint decompression)
///
/// Compressed key parameters
/// Parameters for non-compressed key
protected ECPublicKeyParameters Decompress(ECPublicKeyParameters pubKeyParams)
{
if (!pubKeyParams.Q.IsCompressed)
{
// Isn't compressed
return pubKeyParams;
}
var q = new FpPoint(curve.Curve, pubKeyParams.Q.X, pubKeyParams.Q.Y, false);
return new ECPublicKeyParameters(q, domain);
}
///
/// Does the signature matches our public key?
///
/// Data hash
/// Signature bytes
/// Checking result
public bool VerifySignature(Hash sigHash, byte[] signature)
{
var signer = SignerUtilities.GetSigner("NONEwithECDSA");
signer.Init(false, _Public);
signer.BlockUpdate(sigHash, 0, sigHash.hashSize);
return signer.VerifySignature(signature);
}
///
/// Calculate Hash160 and create new CKeyID instance.
///
/// New key ID
public CKeyID KeyID
{
get { return new CKeyID(Hash160.Compute160(_Public.Q.GetEncoded())); }
}
///
/// Is this a compressed public key?
///
///
public bool IsCompressed
{
get { return _Public.Q.IsCompressed; }
}
}
}