Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
EdwardsTwistedCurve.cs
1using System;
2using System.Numerics;
3
5{
9 public abstract class EdwardsTwistedCurve : EdwardsCurveBase
10 {
11 private readonly BigInteger p58;
12 private readonly BigInteger twoP14;
13
23 BigInteger d, BigInteger Order, int Cofactor)
24 : this(Prime, BasePoint, d, Order, Cofactor, null)
25 {
26 }
27
37 public EdwardsTwistedCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger d,
38 BigInteger Order, int Cofactor, byte[] Secret)
39 : base(Prime, BasePoint, d, Order, Cofactor, Secret)
40 {
41 this.p58 = (this.p - 5) / 8;
42 this.twoP14 = BigInteger.ModPow(Two, (this.p - 1) / 4, this.p);
43 }
44
51 public override void AddTo(ref PointOnCurve P, PointOnCurve Q)
52 {
53 if (!P.IsHomogeneous)
54 P.T = P.X * P.Y;
55
56 if (!Q.IsHomogeneous)
57 Q.T = Q.X * Q.Y;
58
59 BigInteger A = this.modP.Multiply(P.Y - P.X, Q.Y - Q.X);
60 BigInteger B = this.modP.Multiply(P.Y + P.X, Q.Y + Q.X);
61 BigInteger C = this.modP.Multiply(this.modP.Multiply(this.d2, P.T), Q.T);
62 BigInteger D = this.modP.Multiply(P.Z << 1, Q.Z);
63 BigInteger E = this.modP.Subtract(B, A);
64 BigInteger F = this.modP.Subtract(D, C);
65 BigInteger G = this.modP.Add(D, C);
66 BigInteger H = this.modP.Add(B, A);
67
68 P.X = this.modP.Multiply(E, F);
69 P.Y = this.modP.Multiply(G, H);
70 P.T = this.modP.Multiply(E, H);
71 P.Z = this.modP.Multiply(F, G);
72 }
73
78 public override void Double(ref PointOnCurve P)
79 {
80 if (!P.IsHomogeneous)
81 P.T = P.X * P.Y;
82
83 BigInteger A = P.Y - P.X;
84 A = this.modP.Multiply(A, A);
85
86 BigInteger B = P.Y + P.X;
87 B = this.modP.Multiply(B, B);
88
89 BigInteger C = this.modP.Multiply(this.modP.Multiply(this.d2, P.T), P.T);
90 BigInteger D = this.modP.Multiply(P.Z << 1, P.Z);
91 BigInteger E = this.modP.Subtract(B, A);
92 BigInteger F = this.modP.Subtract(D, C);
93 BigInteger G = this.modP.Add(D, C);
94 BigInteger H = this.modP.Add(B, A);
95
96 P.X = this.modP.Multiply(E, F);
97 P.Y = this.modP.Multiply(G, H);
98 P.T = this.modP.Multiply(E, H);
99 P.Z = this.modP.Multiply(F, G);
100 }
101
109 public override BigInteger GetX(BigInteger Y, bool X0)
110 {
111 BigInteger y2 = this.modP.Multiply(Y, Y);
112 BigInteger u = y2 - BigInteger.One;
113 if (u.Sign < 0)
114 u += this.p;
115
116 BigInteger v = this.modP.Multiply(this.d, y2) + BigInteger.One;
117 BigInteger v2 = this.modP.Multiply(v, v);
118 BigInteger v3 = this.modP.Multiply(v, v2);
119 BigInteger v4 = this.modP.Multiply(v2, v2);
120 BigInteger v7 = this.modP.Multiply(v3, v4);
121 BigInteger x = this.modP.Multiply(this.modP.Multiply(u, v3),
122 BigInteger.ModPow(this.modP.Multiply(u, v7), this.p58, this.Prime));
123
124 BigInteger x2 = this.modP.Multiply(x, x);
125 BigInteger Test = this.modP.Multiply(v, x2);
126 if (Test.Sign < 0)
127 Test += this.Prime;
128
129 if (Test != u)
130 {
131 if (Test == this.Prime - u)
132 x = this.modP.Multiply(x, this.twoP14);
133 else
134 throw new ArgumentException("Not a valid point.", nameof(Y));
135 }
136
137 if (X0)
138 {
139 if (x.IsZero)
140 throw new ArgumentException("Not a valid point.", nameof(Y));
141
142 if (x.IsEven)
143 x = this.Prime - x;
144 }
145 else if (!x.IsEven)
146 x = this.Prime - x;
147
148 return x;
149 }
150
156 public override bool IsPoint(PointOnCurve Point)
157 {
158 // Check if point matches -x²+y²=1+dx²y²
159
160 BigInteger X2 = this.modP.Multiply(Point.X, Point.X);
161 BigInteger Y2 = this.modP.Multiply(Point.Y, Point.Y);
162
163 BigInteger Left = this.modP.Subtract(Y2, X2);
164
165 BigInteger Right = this.modP.Add(
166 1,
167 this.modP.Multiply(
168 this.modP.Multiply(this.d, X2),
169 Y2));
170
171 return Left == Right;
172 }
173 }
174}
Base class of different types of Edwards curves over a prime field.
BigInteger D
d coefficient of Edwards curve.
BigInteger d
Edwards curve coefficient
Base class of Twisted Edwards curves (-x²+y²=1+dx²y²) over a prime field.
override void AddTo(ref PointOnCurve P, PointOnCurve Q)
Adds Q to P .
EdwardsTwistedCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger d, BigInteger Order, int Cofactor, byte[] Secret)
Base class of Twisted Edwards curves (-x²+y²=1+dx²y²) over a prime field.
override 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 bool IsPoint(PointOnCurve Point)
Checks if a point is on the curve.
override void Double(ref PointOnCurve P)
Doubles a point on the curve.
EdwardsTwistedCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger d, BigInteger Order, int Cofactor)
Base class of Twisted Edwards curves (-x²+y²=1+dx²y²) over a prime field.
PointOnCurve BasePoint
Base-point of curve.
BigInteger Multiply(BigInteger a, BigInteger b)
Multiplies two numbers, modulus p
Definition: ModulusP.cs:80
BigInteger Add(BigInteger a, BigInteger b)
Adds two numbers, modulus p
Definition: ModulusP.cs:31
BigInteger Subtract(BigInteger a, BigInteger b)
Subtracts two numbers, modulus p
Definition: ModulusP.cs:51
readonly ModulusP modP
Arithmetic modulus p
Represents a point on a curve.
Definition: PointOnCurve.cs:10
bool IsHomogeneous
If the point is in homogeneous coordinates.