25 BigInteger e = CalcE(Data,
HashFunction, ScalarBytes, MsbMask);
39 r = BigInteger.Remainder(P1.
X, Curve.
Order);
50 byte[] Signature =
new byte[ScalarBytes << 1];
52 byte[] S = r.ToByteArray();
53 if (S.Length != ScalarBytes)
54 Array.Resize(ref S, ScalarBytes);
56 Array.Copy(S, 0, Signature, 0, ScalarBytes);
59 if (S.Length != ScalarBytes)
60 Array.Resize(ref S, ScalarBytes);
62 Array.Copy(S, 0, Signature, ScalarBytes, ScalarBytes);
68 int ScalarBytes,
byte MsbMask)
74 Array.Resize(ref Hash, ScalarBytes);
76 Hash[ScalarBytes - 1] &= MsbMask;
94 BigInteger e = CalcE(Data,
HashFunction, ScalarBytes, MsbMask);
108 r = BigInteger.Remainder(P1.
X, Curve.
Order);
119 byte[] Signature =
new byte[ScalarBytes << 1];
121 byte[] S = r.ToByteArray();
122 if (S.Length != ScalarBytes)
123 Array.Resize(ref S, ScalarBytes);
125 Array.Copy(S, 0, Signature, 0, ScalarBytes);
128 if (S.Length != ScalarBytes)
129 Array.Resize(ref S, ScalarBytes);
131 Array.Copy(S, 0, Signature, ScalarBytes, ScalarBytes);
137 int ScalarBytes,
byte MsbMask)
142 if (c != ScalarBytes)
143 Array.Resize(ref Hash, ScalarBytes);
145 Hash[ScalarBytes - 1] &= MsbMask;
162 int ScalarBytes,
byte MsbMask,
PrimeFieldCurve Curve,
byte[] Signature)
164 int c = Signature.Length;
165 if (c != ScalarBytes << 1)
170 byte[] Bin =
new byte[c];
171 Array.Copy(Signature, 0, Bin, 0, c);
176 Array.Copy(Signature, c, Bin, 0, c);
181 if (!PublicKeyPoint.
NonZero || r.IsZero || s.IsZero || r >= Curve.
Order || s >= Curve.
Order)
184 BigInteger e = CalcE(Data,
HashFunction, ScalarBytes, MsbMask);
190 Curve.
AddTo(ref P2, P3);
197 BigInteger Compare = BigInteger.Remainder(P2.
X, Curve.
Order);
198 if (Compare.Sign < 0)
199 Compare += Curve.
Order;
216 int ScalarBytes,
byte MsbMask,
PrimeFieldCurve Curve,
byte[] Signature)
218 int c = Signature.Length;
219 if (c != ScalarBytes << 1)
224 byte[] Bin =
new byte[c];
225 Array.Copy(Signature, 0, Bin, 0, c);
230 Array.Copy(Signature, c, Bin, 0, c);
235 if (!PublicKeyPoint.
NonZero || r.IsZero || s.IsZero || r >= Curve.
Order || s >= Curve.
Order)
238 BigInteger e = CalcE(Data,
HashFunction, ScalarBytes, MsbMask);
244 Curve.
AddTo(ref P2, P3);
251 BigInteger Compare = BigInteger.Remainder(P2.
X, Curve.
Order);
252 if (Compare.Sign < 0)
253 Compare += Curve.
Order;
Implements the Elliptic Curve Digital Signature Algorithm (ECDSA).
static byte[] Sign(Stream Data, byte[] PrivateKey, HashFunctionStream HashFunction, int ScalarBytes, byte MsbMask, PrimeFieldCurve Curve)
Signs data using the ECDSA algorithm.
static bool Verify(Stream Data, byte[] PublicKey, HashFunctionStream HashFunction, int ScalarBytes, byte MsbMask, PrimeFieldCurve Curve, byte[] Signature)
Verifies a signature of Data made by the ECDSA algorithm.
static bool Verify(byte[] Data, byte[] PublicKey, HashFunctionArray HashFunction, int ScalarBytes, byte MsbMask, PrimeFieldCurve Curve, byte[] Signature)
Verifies a signature of Data made by the ECDSA algorithm.
static byte[] Sign(byte[] Data, byte[] PrivateKey, HashFunctionArray HashFunction, int ScalarBytes, byte MsbMask, PrimeFieldCurve Curve)
Signs data using the ECDSA algorithm.
Abstract base class for elliptic curves.
BigInteger Order
Order of curve.
virtual PointOnCurve Decode(byte[] Point)
Decodes an encoded point on the curve.
static BigInteger ToInt(byte[] Binary)
Converts a little-endian binary representation of a big integer to a BigInteger.
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).