24 public static byte[]
Sign(
byte[] Data,
bool BigEndian,
byte[] PrivateKey,
byte[] Prefix,
29 int ScalarBytes = PrivateKey.Length;
31 if (Prefix.Length != ScalarBytes)
32 throw new ArgumentException(
"Invalid prefix.", nameof(Prefix));
36 byte[] A =
Encode(P, BigEndian, Curve);
38 byte[] Bin =
new byte[ScalarBytes + c];
39 Buffer.BlockCopy(Prefix, 0, Bin, 0, ScalarBytes);
40 Buffer.BlockCopy(Data, 0, Bin, ScalarBytes, c);
45 byte[] Rs =
Encode(R, BigEndian, Curve);
47 Bin =
new byte[(ScalarBytes << 1) + c];
48 Buffer.BlockCopy(Rs, 0, Bin, 0, ScalarBytes);
49 Buffer.BlockCopy(A, 0, Bin, ScalarBytes, ScalarBytes);
50 Buffer.BlockCopy(Data, 0, Bin, ScalarBytes << 1, c);
57 Bin = s.ToByteArray();
58 if (Bin.Length != ScalarBytes)
59 Array.Resize(ref Bin, ScalarBytes);
61 byte[] Signature =
new byte[ScalarBytes << 1];
63 Buffer.BlockCopy(Rs, 0, Signature, 0, ScalarBytes);
64 Buffer.BlockCopy(Bin, 0, Signature, ScalarBytes, ScalarBytes);
79 public static byte[]
Sign(Stream Data,
bool BigEndian,
byte[] PrivateKey,
84 int ScalarBytes = PrivateKey.Length;
86 if (Prefix.Length != ScalarBytes)
87 throw new ArgumentException(
"Invalid prefix.", nameof(Prefix));
91 byte[] A =
Encode(P, BigEndian, Curve);
96 TempFile.
Write(Prefix, 0, ScalarBytes);
99 Data.CopyTo(TempFile);
101 TempFile.Position = 0;
107 byte[] Rs =
Encode(R, BigEndian, Curve);
111 TempFile.
Write(Rs, 0, ScalarBytes);
112 TempFile.
Write(A, 0, ScalarBytes);
115 Data.CopyTo(TempFile);
117 TempFile.Position = 0;
124 byte[] Bin = s.ToByteArray();
125 if (Bin.Length != ScalarBytes)
126 Array.Resize(ref Bin, ScalarBytes);
128 byte[] Signature =
new byte[ScalarBytes << 1];
130 Buffer.BlockCopy(Rs, 0, Signature, 0, ScalarBytes);
131 Buffer.BlockCopy(Bin, 0, Signature, ScalarBytes, ScalarBytes);
146 int ScalarBytes = (ScalarBits + 9) >> 3;
148 byte[] y = P.
Y.ToByteArray();
149 if (y.Length != ScalarBytes)
150 Array.Resize(ref y, ScalarBytes);
152 byte[] x = P.
X.ToByteArray();
153 int Msb = (ScalarBits + 1) & 7;
155 byte Mask = (byte)(0xff >> (8 - Msb));
156 y[ScalarBytes - 1] &= Mask;
159 y[ScalarBytes - 1] |= 0x80;
177 int ScalarBytes = (ScalarBits + 9) >> 3;
179 if (Encoded.Length != ScalarBytes)
180 throw new ArgumentException(
"Not encoded properly.", nameof(Encoded));
184 Encoded = (
byte[])Encoded.Clone();
185 Array.Reverse(Encoded);
188 bool x0 = (Encoded[ScalarBytes - 1] & 0x80) != 0;
190 Encoded[ScalarBytes - 1] &= 0x7f;
193 if (y >= Curve.
Prime)
194 throw new ArgumentException(
"Not a valid point.", nameof(Encoded));
197 Encoded[ScalarBytes - 1] |= 0x80;
199 BigInteger x = Curve.
GetX(y, x0);
219 int ScalarBytes = Signature.Length;
220 if ((ScalarBytes & 1) != 0)
225 byte[] R =
new byte[ScalarBytes];
226 Buffer.BlockCopy(Signature, 0, R, 0, ScalarBytes);
228 byte[] S =
new byte[ScalarBytes];
229 Buffer.BlockCopy(Signature, ScalarBytes, S, 0, ScalarBytes);
232 if (s >= Curve.
Order)
236 byte[] Bin =
new byte[(ScalarBytes << 1) + c];
237 Buffer.BlockCopy(R, 0, Bin, 0, ScalarBytes);
238 Buffer.BlockCopy(PublicKey, 0, Bin, ScalarBytes, ScalarBytes);
239 Buffer.BlockCopy(Data, 0, Bin, ScalarBytes << 1, c);
246 Curve.
AddTo(ref P2, r);
253 catch (ArgumentException)
269 public static bool Verify(Stream Data,
byte[] PublicKey,
bool BigEndian,
274 int ScalarBytes = Signature.Length;
275 if ((ScalarBytes & 1) != 0)
280 byte[] R =
new byte[ScalarBytes];
281 Buffer.BlockCopy(Signature, 0, R, 0, ScalarBytes);
283 byte[] S =
new byte[ScalarBytes];
284 Buffer.BlockCopy(Signature, ScalarBytes, S, 0, ScalarBytes);
288 if (s >= Curve.
Order)
293 TempFile.
Write(R, 0, ScalarBytes);
294 TempFile.
Write(PublicKey, 0, ScalarBytes);
297 Data.CopyTo(TempFile);
299 TempFile.Position = 0;
306 Curve.
AddTo(ref P2, r);
313 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[] Sign(Stream Data, bool BigEndian, byte[] PrivateKey, byte[] Prefix, HashFunctionStream HashFunction, EdwardsCurveBase Curve)
Signs data using the EdDSA algorithm.
static bool Verify(byte[] Data, byte[] PublicKey, bool BigEndian, HashFunctionArray HashFunction, EdwardsCurveBase Curve, byte[] Signature)
Verifies a signature of Data made by the EdDSA algorithm.
static byte[] Sign(byte[] Data, bool BigEndian, byte[] PrivateKey, byte[] Prefix, HashFunctionArray HashFunction, EdwardsCurveBase Curve)
Signs data using the EdDSA algorithm.
static bool Verify(Stream Data, byte[] PublicKey, bool BigEndian, HashFunctionStream HashFunction, EdwardsCurveBase Curve, byte[] Signature)
Verifies a signature of Data made by the EdDSA algorithm.
static PointOnCurve Decode(byte[] Encoded, bool BigEndian, EdwardsCurveBase Curve)
Decodes a point on the curve in accordance with §5.1.3 of RFC 8032.
static byte[] Encode(PointOnCurve P, bool BigEndian, EdwardsCurveBase Curve)
Encodes a point on the curve in accordance with §5.1.2 of RFC 8032.
Base class of different types of Edwards curves over a prime field.
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...
override PointOnCurve Decode(byte[] Point, bool BigEndian)
Decodes an encoded point on the curve.
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, bool BigEndian)
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)