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.Security.Cryptography;
3using System.Numerics;
4
6{
10 public abstract class EdwardsCurve : EdwardsCurveBase
11 {
12 private readonly BigInteger p34;
13
22 public EdwardsCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger d,
23 BigInteger Order, int Cofactor)
24 : this(Prime, BasePoint, d, Order, Cofactor, null)
25 {
26 }
27
37 public EdwardsCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger d,
38 BigInteger Order, int Cofactor, byte[] Secret)
39 : base(Prime, BasePoint, d, Order, Cofactor, Secret)
40 {
41 this.p34 = (this.p - 3) / 4;
42 }
43
47 protected abstract BigInteger D
48 {
49 get;
50 }
51
55 public override PointOnCurve Zero
56 {
57 get
58 {
59 return new PointOnCurve(BigInteger.Zero, BigInteger.One);
60 }
61 }
62
69 public override void AddTo(ref PointOnCurve P, PointOnCurve Q)
70 {
71 if (!P.IsHomogeneous)
72 P.Z = BigInteger.One;
73
74 if (!Q.IsHomogeneous)
75 Q.Z = BigInteger.One;
76
77 BigInteger A = this.modP.Multiply(P.Z, Q.Z);
78 BigInteger B = this.modP.Multiply(A, A);
79 BigInteger C = this.modP.Multiply(P.X, Q.X);
80 BigInteger D = this.modP.Multiply(P.Y, Q.Y);
81 BigInteger E = this.modP.Multiply(this.modP.Multiply(this.D, C), D);
82 BigInteger F = this.modP.Subtract(B, E);
83 BigInteger G = this.modP.Add(B, E);
84 BigInteger H = this.modP.Multiply(P.X + P.Y, Q.X + Q.Y);
85
86 P.X = this.modP.Multiply(A, this.modP.Multiply(F, H - C - D));
87 P.Y = this.modP.Multiply(A, this.modP.Multiply(G, D - C));
88 P.Z = this.modP.Multiply(F, G);
89 }
90
95 public override void Double(ref PointOnCurve P)
96 {
97 if (!P.IsHomogeneous)
98 P.Z = BigInteger.One;
99
100 BigInteger A = this.modP.Add(P.X, P.Y);
101 BigInteger B = this.modP.Multiply(A, A);
102 BigInteger C = this.modP.Multiply(P.X, P.X);
103 BigInteger D = this.modP.Multiply(P.Y, P.Y);
104 BigInteger E = this.modP.Add(C, D);
105 BigInteger H = this.modP.Multiply(P.Z, P.Z);
106 BigInteger J = this.modP.Subtract(E, H << 1);
107
108 P.X = this.modP.Multiply(B - E, J);
109 P.Y = this.modP.Multiply(E, C - D);
110 P.Z = this.modP.Multiply(E, J);
111 }
112
120 public override BigInteger GetX(BigInteger Y, bool X0)
121 {
122 BigInteger y2 = this.modP.Multiply(Y, Y);
123 BigInteger u = y2 - BigInteger.One;
124 if (u.Sign < 0)
125 u += this.p;
126
127 BigInteger v = this.modP.Multiply(this.D, y2) - BigInteger.One;
128 BigInteger v2 = this.modP.Multiply(v, v);
129 BigInteger v3 = this.modP.Multiply(v, v2);
130 BigInteger u2 = this.modP.Multiply(u, u);
131 BigInteger u3 = this.modP.Multiply(u, u2);
132 BigInteger u5 = this.modP.Multiply(u2, u3);
133 BigInteger x = this.modP.Multiply(this.modP.Multiply(u3, v),
134 BigInteger.ModPow(this.modP.Multiply(u5, v3), this.p34, this.p));
135
136 BigInteger x2 = this.modP.Multiply(x, x);
137 BigInteger Test = this.modP.Multiply(v, x2);
138 if (Test.Sign < 0)
139 Test += this.p;
140
141 if (Test != u)
142 throw new ArgumentException("Not a valid point.", nameof(Y));
143
144 if (X0)
145 {
146 if (x.IsZero)
147 throw new ArgumentException("Not a valid point.", nameof(Y));
148
149 if (x.IsEven)
150 x = this.p - x;
151 }
152 else if (!x.IsEven)
153 x = this.p - x;
154
155 return x;
156 }
157
158 }
159}
Base class of different types of Edwards curves over a prime field.
BigInteger d
Edwards curve coefficient
Base class of Edwards curves (x²+y²=1+dx²y²) over a prime field.
Definition: EdwardsCurve.cs:11
override void Double(ref PointOnCurve P)
Doubles a point on the curve.
Definition: EdwardsCurve.cs:95
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:22
override PointOnCurve Zero
Neutral point.
Definition: EdwardsCurve.cs:56
abstract BigInteger D
d coefficient of Edwards curve.
Definition: EdwardsCurve.cs:48
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:37
override void AddTo(ref PointOnCurve P, PointOnCurve Q)
Adds Q to P .
Definition: EdwardsCurve.cs:69
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:11
bool IsHomogeneous
If the point is in homogeneous coordinates.