23 public static byte[]
Sign(
byte[] Data,
byte[] PrivateKey,
byte[] Prefix,
28 int ScalarBytes = PrivateKey.Length;
30 if (Prefix.Length != ScalarBytes)
31 throw new ArgumentException(
"Invalid prefix.", nameof(Prefix));
35 byte[] A =
Encode(P, Curve);
37 byte[] Bin =
new byte[ScalarBytes + c];
38 Array.Copy(Prefix, 0, Bin, 0, ScalarBytes);
39 Array.Copy(Data, 0, Bin, ScalarBytes, c);
44 byte[] Rs =
Encode(R, Curve);
46 Bin =
new byte[(ScalarBytes << 1) + c];
47 Array.Copy(Rs, 0, Bin, 0, ScalarBytes);
48 Array.Copy(A, 0, Bin, ScalarBytes, ScalarBytes);
49 Array.Copy(Data, 0, Bin, ScalarBytes << 1, c);
56 Bin = s.ToByteArray();
57 if (Bin.Length != ScalarBytes)
58 Array.Resize(ref Bin, ScalarBytes);
60 byte[] Signature =
new byte[ScalarBytes << 1];
62 Array.Copy(Rs, 0, Signature, 0, ScalarBytes);
63 Array.Copy(Bin, 0, Signature, ScalarBytes, ScalarBytes);
77 public static byte[]
Sign(Stream Data,
byte[] PrivateKey,
byte[] Prefix,
82 int ScalarBytes = PrivateKey.Length;
84 if (Prefix.Length != ScalarBytes)
85 throw new ArgumentException(
"Invalid prefix.", nameof(Prefix));
89 byte[] A =
Encode(P, Curve);
94 TempFile.
Write(Prefix, 0, ScalarBytes);
97 Data.CopyTo(TempFile);
99 TempFile.Position = 0;
105 byte[] Rs =
Encode(R, Curve);
109 TempFile.
Write(Rs, 0, ScalarBytes);
110 TempFile.
Write(A, 0, ScalarBytes);
113 Data.CopyTo(TempFile);
115 TempFile.Position = 0;
122 byte[] Bin = s.ToByteArray();
123 if (Bin.Length != ScalarBytes)
124 Array.Resize(ref Bin, ScalarBytes);
126 byte[] Signature =
new byte[ScalarBytes << 1];
128 Array.Copy(Rs, 0, Signature, 0, ScalarBytes);
129 Array.Copy(Bin, 0, Signature, ScalarBytes, ScalarBytes);
143 int ScalarBytes = (ScalarBits + 9) >> 3;
145 byte[] y = P.
Y.ToByteArray();
146 if (y.Length != ScalarBytes)
147 Array.Resize(ref y, ScalarBytes);
149 byte[] x = P.
X.ToByteArray();
150 int Msb = (ScalarBits + 1) & 7;
152 byte Mask = (byte)(0xff >> (8 - Msb));
153 y[ScalarBytes - 1] &= Mask;
156 y[ScalarBytes - 1] |= 0x80;
170 int ScalarBytes = (ScalarBits + 9) >> 3;
172 if (Encoded.Length != ScalarBytes)
173 throw new ArgumentException(
"Not encoded properly.", nameof(Encoded));
175 bool x0 = (Encoded[ScalarBytes - 1] & 0x80) != 0;
177 Encoded[ScalarBytes - 1] &= 0x7f;
180 if (y >= Curve.
Prime)
181 throw new ArgumentException(
"Not a valid point.", nameof(Encoded));
184 Encoded[ScalarBytes - 1] |= 0x80;
186 BigInteger x = Curve.
GetX(y, x0);
205 int ScalarBytes = Signature.Length;
206 if ((ScalarBytes & 1) != 0)
211 byte[] R =
new byte[ScalarBytes];
212 Array.Copy(Signature, 0, R, 0, ScalarBytes);
214 byte[] S =
new byte[ScalarBytes];
215 Array.Copy(Signature, ScalarBytes, S, 0, ScalarBytes);
218 if (s >= Curve.
Order)
222 byte[] Bin =
new byte[(ScalarBytes << 1) + c];
223 Array.Copy(R, 0, Bin, 0, ScalarBytes);
224 Array.Copy(PublicKey, 0, Bin, ScalarBytes, ScalarBytes);
225 Array.Copy(Data, 0, Bin, ScalarBytes << 1, c);
232 Curve.
AddTo(ref P2, r);
239 catch (ArgumentException)
259 int ScalarBytes = Signature.Length;
260 if ((ScalarBytes & 1) != 0)
265 byte[] R =
new byte[ScalarBytes];
266 Array.Copy(Signature, 0, R, 0, ScalarBytes);
268 byte[] S =
new byte[ScalarBytes];
269 Array.Copy(Signature, ScalarBytes, S, 0, ScalarBytes);
273 if (s >= Curve.
Order)
278 TempFile.
Write(R, 0, ScalarBytes);
279 TempFile.
Write(PublicKey, 0, ScalarBytes);
282 Data.CopyTo(TempFile);
284 TempFile.Position = 0;
291 Curve.
AddTo(ref P2, r);
298 catch (ArgumentException)
Manages a temporary stream. Contents is kept in-memory, if below a memory threshold,...
override void Write(byte[] buffer, int offset, int count)
When overridden in a derived class, writes a sequence of bytes to the current stream and advances the...
Implements the Edwards curve Digital Signature Algorithm (EdDSA), as defined in RFC 8032....
static byte[] Encode(PointOnCurve P, EdwardsCurveBase Curve)
Encodes a point on the curve in accordance with §5.1.2 of RFC 8032.
static byte[] Sign(Stream Data, byte[] PrivateKey, byte[] Prefix, HashFunctionStream HashFunction, EdwardsCurveBase Curve)
Signs data using the EdDSA algorithm.
static byte[] Sign(byte[] Data, byte[] PrivateKey, byte[] Prefix, HashFunctionArray HashFunction, EdwardsCurveBase Curve)
Signs data using the EdDSA algorithm.
static bool Verify(byte[] Data, byte[] PublicKey, HashFunctionArray HashFunction, EdwardsCurveBase Curve, byte[] Signature)
Verifies a signature of Data made by the EdDSA algorithm.
static PointOnCurve Decode(byte[] Encoded, EdwardsCurveBase Curve)
Decodes a point on the curve in accordance with §5.1.3 of RFC 8032.
static bool Verify(Stream Data, byte[] PublicKey, HashFunctionStream HashFunction, EdwardsCurveBase Curve, byte[] Signature)
Verifies a signature of Data made by the EdDSA algorithm.
Base class of different types of Edwards curves over a prime field.
override PointOnCurve Decode(byte[] Point)
Decodes an encoded point on the curve.
abstract BigInteger GetX(BigInteger Y, bool X0)
Gets the X-coordinate that corresponds to a given Y-coordainte, and the first bit of the X-coordinate...
abstract int CoordinateBits
Number of bits used to encode the y-coordinate.
Abstract base class for elliptic curves.
BigInteger Order
Order of 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 Multiply(BigInteger a, BigInteger b)
Multiplies two numbers, modulus p
BigInteger Add(BigInteger a, BigInteger b)
Adds two numbers, modulus p
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)
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.
override bool Equals(object obj)