51 public BigInteger
A => this.a;
86 throw new NotSupportedException(
"Scalar multiplication is performed using a Montgomery ladder.");
129 public static BigInteger
XFunction(
byte[] N, BigInteger U,
130 BigInteger A24, BigInteger
p,
int Bits)
133 BigInteger x2 = BigInteger.One;
134 BigInteger z2 = BigInteger.Zero;
136 BigInteger z3 = BigInteger.One;
137 BigInteger
A, AA, B, BB, E, C, D, DA, CB;
141 kt = (Bits + 7) >> 3;
143 Array.Resize(ref N, kt);
147 kt = (N[Bits >> 3] >> (Bits & 7)) & 1;
149 ConditionalSwap(swap, ref x2, ref x3);
150 ConditionalSwap(swap, ref z2, ref z3);
153 A = BigInteger.Remainder(x2 + z2,
p);
154 AA = BigInteger.Remainder(
A *
A,
p);
155 B = BigInteger.Remainder(x2 - z2,
p);
156 BB = BigInteger.Remainder(B * B,
p);
157 E = BigInteger.Remainder(AA - BB,
p);
158 C = BigInteger.Remainder(x3 + z3,
p);
159 D = BigInteger.Remainder(x3 - z3,
p);
160 DA = BigInteger.Remainder(D *
A,
p);
161 CB = BigInteger.Remainder(C * B,
p);
164 x3 = BigInteger.Remainder(x3 * x3,
p);
166 z3 = BigInteger.Remainder(x1 * BigInteger.Remainder(z3 * z3,
p),
p);
167 x2 = BigInteger.Remainder(AA * BB,
p);
168 z2 = BigInteger.Remainder(E * (AA + BigInteger.Remainder(A24 * E,
p)),
p);
171 ConditionalSwap(swap, ref x2, ref x3);
172 ConditionalSwap(swap, ref z2, ref z3);
174 BigInteger Result = BigInteger.Remainder(x2 * BigInteger.ModPow(z2,
p -
Two,
p),
p);
190 private static void ConditionalSwap(
int swap, ref BigInteger I2, ref BigInteger I3)
192 byte[] x2 = I2.ToByteArray();
193 byte[] x3 = I3.ToByteArray();
194 int i, c = x2.Length, d = x3.Length;
201 Sign = (x2[c - 1] & 0x80) != 0;
202 Array.Resize(ref x2, d);
214 Sign = (x3[d - 1] & 0x80) != 0;
215 Array.Resize(ref x3, c);
226 Mask = (byte)(0xff * swap);
228 for (i = 0; i < c; i++)
230 Dummy = (byte)(Mask & (x2[i] ^ x3[i]));
235 I2 =
new BigInteger(x2);
236 I3 =
new BigInteger(x3);
246 if (this.pair is
null)
270 PublicKey.Y = this.
CalcV(PublicKey.
X);
283 public BigInteger
CalcV(BigInteger U)
287 BigInteger V2 = BigInteger.Remainder(U3 + this.
modP.
Multiply(
this.a, U2) + U,
this.Prime);
293 BigInteger V = this.Prime - V1;
308 byte[] Bin = Point.
X.ToByteArray();
310 if (Bin.Length !=
this.orderBytes)
327 BigInteger U =
ToInt(Point, BigEndian);
339 base.SetPrivateKey(Secret);
364 return Left == Right;
Base class of different types of Edwards curves over a prime field.
byte[] Sign(byte[] Data)
Creates a signature of Data using the ECDSA algorithm.
BigInteger Order
Order of curve.
readonly int orderBytes
Number of bytes used for the order of the curve.
int Cofactor
Cofactor of curve.
static BigInteger ToInt(byte[] Binary, bool BigEndian)
Converts a little-endian binary representation of a big integer to a BigInteger.
virtual byte[] PublicKey
Encoded public key
static readonly BigInteger Two
2
PointOnCurve BasePoint
Base-point of curve.
BigInteger Sqrt(BigInteger N)
Computes sqrt(N) mod p.
BigInteger Multiply(BigInteger a, BigInteger b)
Multiplies two numbers, modulus p
Base class of Montgomery curves (y²=x³+Ax²+x), with birational Edwards equivalent over a prime field.
static BigInteger XFunction(byte[] N, BigInteger U, BigInteger A24, BigInteger p, int Bits)
Performs the scalar multiplication of N *U .
override void SetPrivateKey(byte[] Secret)
Sets the private key (and therefore also the public key) of the curve.
abstract PointOnCurve ToUV(PointOnCurve XY)
Converts a pair of (X,Y) coordinates for the birational Edwards curve to a pair of (U,...
override PointOnCurve ScalarMultiplication(byte[] N, PointOnCurve P, bool Normalize)
Performs the scalar multiplication of N *P .
BigInteger A
Coefficient a.
override void Double(ref PointOnCurve P)
Doubles a point on the curve.
override void AddTo(ref PointOnCurve P, PointOnCurve Q)
Adds Q to P .
BigInteger ScalarMultiplication(BigInteger N, BigInteger U)
Performs the scalar multiplication of N *U .
abstract EdwardsCurveBase CreatePair()
Creates the Edwards Curve pair.
MontgomeryCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger A, BigInteger Order, int Cofactor, byte[] Secret)
Base class of Montgomery curves, with birational Edwards equivalent over a prime field.
abstract PointOnCurve ToXY(PointOnCurve UV)
Converts a pair of (U,V) coordinates to a pair of (X,Y) coordinates in the birational Edwards curve.
override bool IsPoint(PointOnCurve Point)
Checks if a point is on the curve.
override PointOnCurve Decode(byte[] Point, bool BigEndian)
Decodes an encoded point on the curve.
MontgomeryCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger A, BigInteger Order, int Cofactor)
Base class of Montgomery curves (y²=x³+Ax²+x), with birational Edwards equivalent over a prime field.
BigInteger CalcV(BigInteger U)
Calculates the V-coordinate, given the corresponding U-coordinate.
abstract BigInteger ScalarMultiplication(byte[] N, BigInteger U)
Performs the scalar multiplication of N *U .
override PointOnCurve PublicKeyPoint
Public key.
EdwardsCurveBase Pair
Edwards Curve pair.
override byte[] Encode(PointOnCurve Point, bool BigEndian)
Encodes a point on the curve.
Base class of Elliptic curves over a prime field.
readonly ModulusP modP
Arithmetic modulus p
BigInteger Prime
Prime of curve.
readonly BigInteger p
Prime p
Represents a point on a curve.