Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
EdwardsCurve.cs
1using System;
2using System.Numerics;
3
5{
9 public abstract class EdwardsCurve : EdwardsCurveBase
10 {
11 private readonly BigInteger p34;
12
21 public EdwardsCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger d,
22 BigInteger Order, int Cofactor)
23 : this(Prime, BasePoint, d, Order, Cofactor, null)
24 {
25 }
26
36 public EdwardsCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger d,
37 BigInteger Order, int Cofactor, byte[] Secret)
38 : base(Prime, BasePoint, d, Order, Cofactor, Secret)
39 {
40 this.p34 = (this.p - 3) / 4;
41 }
42
46 public override PointOnCurve Zero
47 {
48 get
49 {
50 return new PointOnCurve(BigInteger.Zero, BigInteger.One);
51 }
52 }
53
60 public override void AddTo(ref PointOnCurve P, PointOnCurve Q)
61 {
62 if (!P.IsHomogeneous)
63 P.Z = BigInteger.One;
64
65 if (!Q.IsHomogeneous)
66 Q.Z = BigInteger.One;
67
68 BigInteger A = this.modP.Multiply(P.Z, Q.Z);
69 BigInteger B = this.modP.Multiply(A, A);
70 BigInteger C = this.modP.Multiply(P.X, Q.X);
71 BigInteger D = this.modP.Multiply(P.Y, Q.Y);
72 BigInteger E = this.modP.Multiply(this.modP.Multiply(this.d, C), D);
73 BigInteger F = this.modP.Subtract(B, E);
74 BigInteger G = this.modP.Add(B, E);
75 BigInteger H = this.modP.Multiply(P.X + P.Y, Q.X + Q.Y);
76
77 P.X = this.modP.Multiply(A, this.modP.Multiply(F, H - C - D));
78 P.Y = this.modP.Multiply(A, this.modP.Multiply(G, D - C));
79 P.Z = this.modP.Multiply(F, G);
80 }
81
86 public override void Double(ref PointOnCurve P)
87 {
88 if (!P.IsHomogeneous)
89 P.Z = BigInteger.One;
90
91 BigInteger A = this.modP.Add(P.X, P.Y);
92 BigInteger B = this.modP.Multiply(A, A);
93 BigInteger C = this.modP.Multiply(P.X, P.X);
94 BigInteger D = this.modP.Multiply(P.Y, P.Y);
95 BigInteger E = this.modP.Add(C, D);
96 BigInteger H = this.modP.Multiply(P.Z, P.Z);
97 BigInteger J = this.modP.Subtract(E, H << 1);
98
99 P.X = this.modP.Multiply(B - E, J);
100 P.Y = this.modP.Multiply(E, C - D);
101 P.Z = this.modP.Multiply(E, J);
102 }
103
111 public override BigInteger GetX(BigInteger Y, bool X0)
112 {
113 BigInteger y2 = this.modP.Multiply(Y, Y);
114 BigInteger u = y2 - BigInteger.One;
115 if (u.Sign < 0)
116 u += this.p;
117
118 BigInteger v = this.modP.Multiply(this.d, y2) - BigInteger.One;
119 BigInteger v2 = this.modP.Multiply(v, v);
120 BigInteger v3 = this.modP.Multiply(v, v2);
121 BigInteger u2 = this.modP.Multiply(u, u);
122 BigInteger u3 = this.modP.Multiply(u, u2);
123 BigInteger u5 = this.modP.Multiply(u2, u3);
124 BigInteger x = this.modP.Multiply(this.modP.Multiply(u3, v),
125 BigInteger.ModPow(this.modP.Multiply(u5, v3), this.p34, this.p));
126
127 BigInteger x2 = this.modP.Multiply(x, x);
128 BigInteger Test = this.modP.Multiply(v, x2);
129 if (Test.Sign < 0)
130 Test += this.p;
131
132 if (Test != u)
133 throw new ArgumentException("Not a valid point.", nameof(Y));
134
135 if (X0)
136 {
137 if (x.IsZero)
138 throw new ArgumentException("Not a valid point.", nameof(Y));
139
140 if (x.IsEven)
141 x = this.p - x;
142 }
143 else if (!x.IsEven)
144 x = this.p - x;
145
146 return x;
147 }
148
154 public override bool IsPoint(PointOnCurve Point)
155 {
156 // Check if point matches x²+y²=1+dx²y²
157
158 BigInteger X2 = this.modP.Multiply(Point.X, Point.X);
159 BigInteger Y2 = this.modP.Multiply(Point.Y, Point.Y);
160
161 BigInteger Left = this.modP.Add(X2, Y2);
162
163 BigInteger Right = this.modP.Add(
164 1,
165 this.modP.Multiply(
166 this.modP.Multiply(this.d, X2),
167 Y2));
168
169 return Left == Right;
170 }
171 }
172}
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 Edwards curves (x²+y²=1+dx²y²) over a prime field.
Definition: EdwardsCurve.cs:10
override void Double(ref PointOnCurve P)
Doubles a point on the curve.
Definition: EdwardsCurve.cs:86
EdwardsCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger d, BigInteger Order, int Cofactor)
Base class of Edwards curves (x²+y²=1+dx²y²) over a prime field.
Definition: EdwardsCurve.cs:21
override PointOnCurve Zero
Neutral point.
Definition: EdwardsCurve.cs:47
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...
EdwardsCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger d, BigInteger Order, int Cofactor, byte[] Secret)
Base class of Edwards curves (x²+y²=1+dx²y²) over a prime field.
Definition: EdwardsCurve.cs:36
override void AddTo(ref PointOnCurve P, PointOnCurve Q)
Adds Q to P .
Definition: EdwardsCurve.cs:60
override bool IsPoint(PointOnCurve Point)
Checks if a point is on the curve.
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.