21 public static byte[]
Sign(
byte[] Data,
bool BigEndian,
byte[] PrivateKey,
39 r = BigInteger.Remainder(P1.
X, Curve.
Order);
50 byte[] Signature =
new byte[OrderBytes << 1];
52 byte[] S = r.ToByteArray();
53 if (S.Length != OrderBytes)
54 Array.Resize(ref S, OrderBytes);
59 Buffer.BlockCopy(S, 0, Signature, 0, OrderBytes);
62 if (S.Length != OrderBytes)
63 Array.Resize(ref S, OrderBytes);
68 Buffer.BlockCopy(S, 0, Signature, OrderBytes, OrderBytes);
80 PrimeFieldCurve Curve)
85 private static BigInteger CalcE(
byte[] Hash, PrimeFieldCurve Curve)
90 int MaxC = Curve.OrderBytes;
94 byte[] Hash2 =
new byte[MaxC];
97 Buffer.BlockCopy(Hash, 0, Hash2, MaxC - c, c);
99 Buffer.BlockCopy(Hash, 0, Hash2, 0, MaxC);
106 BigInteger e = EllipticCurve.ToInt(Hash,
true);
122 public static byte[]
Sign(Stream Data,
bool BigEndian,
byte[] PrivateKey,
140 r = BigInteger.Remainder(P1.
X, Curve.
Order);
151 byte[] Signature =
new byte[OrderBytes << 1];
153 byte[] S = r.ToByteArray();
154 if (S.Length != OrderBytes)
155 Array.Resize(ref S, OrderBytes);
160 Buffer.BlockCopy(S, 0, Signature, 0, OrderBytes);
163 if (S.Length != OrderBytes)
164 Array.Resize(ref S, OrderBytes);
169 Buffer.BlockCopy(S, 0, Signature, OrderBytes, OrderBytes);
184 public static bool Verify(
byte[] Data,
byte[] PublicKey,
bool BigEndian,
187 int c = Signature.Length;
193 byte[] Bin =
new byte[c];
194 Buffer.BlockCopy(Signature, 0, Bin, 0, c);
199 Buffer.BlockCopy(Signature, c, Bin, 0, c);
221 if (!PublicKeyPoint.
NonZero || r.IsZero || s.IsZero || r >= Curve.
Order || s >= Curve.
Order)
230 Curve.
AddTo(ref P2, P3);
237 BigInteger Compare = BigInteger.Remainder(P2.
X, Curve.
Order);
238 if (Compare.Sign < 0)
239 Compare += Curve.
Order;
254 public static bool Verify(Stream Data,
byte[] PublicKey,
bool BigEndian,
257 int c = Signature.Length;
263 byte[] Bin =
new byte[c];
264 Buffer.BlockCopy(Signature, 0, Bin, 0, c);
269 Buffer.BlockCopy(Signature, c, Bin, 0, c);
274 if (!PublicKeyPoint.
NonZero || r.IsZero || s.IsZero || r >= Curve.
Order || s >= Curve.
Order)
283 Curve.
AddTo(ref P2, P3);
290 BigInteger Compare = BigInteger.Remainder(P2.
X, Curve.
Order);
291 if (Compare.Sign < 0)
292 Compare += Curve.
Order;
Implements the Elliptic Curve Digital Signature Algorithm (ECDSA).
static bool Verify(Stream Data, byte[] PublicKey, bool BigEndian, HashFunctionStream HashFunction, PrimeFieldCurve Curve, byte[] Signature)
Verifies a signature of Data made by the ECDSA algorithm.
static bool Verify(byte[] Data, byte[] PublicKey, bool BigEndian, HashFunctionArray HashFunction, PrimeFieldCurve Curve, byte[] Signature)
Verifies a signature of Data made by the ECDSA algorithm.
static byte[] Sign(byte[] Data, bool BigEndian, byte[] PrivateKey, HashFunctionArray HashFunction, PrimeFieldCurve Curve)
Signs data using the ECDSA algorithm.
static byte[] Sign(Stream Data, bool BigEndian, byte[] PrivateKey, HashFunctionStream HashFunction, PrimeFieldCurve Curve)
Signs data using the ECDSA algorithm.
static bool Verify(byte[] Data, PointOnCurve PublicKeyPoint, HashFunctionArray HashFunction, PrimeFieldCurve Curve, BigInteger r, BigInteger s)
Verifies a signature of Data made by the ECDSA algorithm.
Abstract base class for elliptic curves.
BigInteger Order
Order of curve.
static BigInteger ToInt(byte[] Binary, bool BigEndian)
Converts a little-endian binary representation of a big integer to a BigInteger.
int OrderBytes
Number of bytes required to represent the order of the curve.
PointOnCurve Decode(byte[] Point)
Decodes an encoded point on the curve.
PointOnCurve BasePoint
Base-point of curve.
abstract void AddTo(ref PointOnCurve P, PointOnCurve Q)
Adds Q to P .
BigInteger Invert(BigInteger x)
Inverts a number in the field Z[p].
BigInteger Multiply(BigInteger a, BigInteger b)
Multiplies two numbers, modulus p
BigInteger Add(BigInteger a, BigInteger b)
Adds two numbers, modulus p
BigInteger Divide(BigInteger a, BigInteger b)
Divides two numbers, modulus p
Base class of Elliptic curves over a prime field.
BigInteger Prime
Prime of curve.
override PointOnCurve ScalarMultiplication(byte[] N, PointOnCurve P, bool Normalize)
Performs the scalar multiplication of N *P .
ModulusP ModulusN
Arithmetic modulus n (the order)
override byte[] GenerateSecret()
Generates a new secret.
delegate byte[] HashFunctionStream(Stream Data)
Delegate to hash function.
delegate byte[] HashFunctionArray(byte[] Data)
Delegate to hash function.
HashFunction
Hash method enumeration.
Represents a point on a curve.
void Normalize(PrimeFieldCurve Curve)
Normalizes a point, if in homogeneous coorinates.
bool IsXZero
If the X-coordinate is zero.
bool NonZero
If the point is not zero (infinity).