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.Security.Cryptography;
3using System.Numerics;
4
6{
10 public abstract class EdwardsTwistedCurve : EdwardsCurveBase
11 {
12 private readonly BigInteger p58;
13 private readonly BigInteger twoP14;
14
24 BigInteger d, BigInteger Order, int Cofactor)
25 : this(Prime, BasePoint, d, Order, Cofactor, null)
26 {
27 }
28
38 public EdwardsTwistedCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger d,
39 BigInteger Order, int Cofactor, byte[] Secret)
40 : base(Prime, BasePoint, d, Order, Cofactor, Secret)
41 {
42 this.p58 = (this.p - 5) / 8;
43 this.twoP14 = BigInteger.ModPow(Two, (this.p - 1) / 4, this.p);
44 }
45
52 public override void AddTo(ref PointOnCurve P, PointOnCurve Q)
53 {
54 if (!P.IsHomogeneous)
55 P.T = P.X * P.Y;
56
57 if (!Q.IsHomogeneous)
58 Q.T = Q.X * Q.Y;
59
60 BigInteger A = this.modP.Multiply(P.Y - P.X, Q.Y - Q.X);
61 BigInteger B = this.modP.Multiply(P.Y + P.X, Q.Y + Q.X);
62 BigInteger C = this.modP.Multiply(this.modP.Multiply(d2, P.T), Q.T);
63 BigInteger D = this.modP.Multiply(P.Z << 1, Q.Z);
64 BigInteger E = this.modP.Subtract(B, A);
65 BigInteger F = this.modP.Subtract(D, C);
66 BigInteger G = this.modP.Add(D, C);
67 BigInteger H = this.modP.Add(B, A);
68
69 P.X = this.modP.Multiply(E, F);
70 P.Y = this.modP.Multiply(G, H);
71 P.T = this.modP.Multiply(E, H);
72 P.Z = this.modP.Multiply(F, G);
73 }
74
79 public override void Double(ref PointOnCurve P)
80 {
81 if (!P.IsHomogeneous)
82 P.T = P.X * P.Y;
83
84 BigInteger A = P.Y - P.X;
85 A = this.modP.Multiply(A, A);
86
87 BigInteger B = P.Y + P.X;
88 B = this.modP.Multiply(B, B);
89
90 BigInteger C = this.modP.Multiply(this.modP.Multiply(d2, P.T), P.T);
91 BigInteger D = this.modP.Multiply(P.Z << 1, P.Z);
92 BigInteger E = this.modP.Subtract(B, A);
93 BigInteger F = this.modP.Subtract(D, C);
94 BigInteger G = this.modP.Add(D, C);
95 BigInteger H = this.modP.Add(B, A);
96
97 P.X = this.modP.Multiply(E, F);
98 P.Y = this.modP.Multiply(G, H);
99 P.T = this.modP.Multiply(E, H);
100 P.Z = this.modP.Multiply(F, G);
101 }
102
110 public override BigInteger GetX(BigInteger Y, bool X0)
111 {
112 BigInteger y2 = this.modP.Multiply(Y, Y);
113 BigInteger u = y2 - BigInteger.One;
114 if (u.Sign < 0)
115 u += this.p;
116
117 BigInteger v = this.modP.Multiply(this.d, y2) + BigInteger.One;
118 BigInteger v2 = this.modP.Multiply(v, v);
119 BigInteger v3 = this.modP.Multiply(v, v2);
120 BigInteger v4 = this.modP.Multiply(v2, v2);
121 BigInteger v7 = this.modP.Multiply(v3, v4);
122 BigInteger x = this.modP.Multiply(this.modP.Multiply(u, v3),
123 BigInteger.ModPow(this.modP.Multiply(u, v7), this.p58, this.Prime));
124
125 BigInteger x2 = this.modP.Multiply(x, x);
126 BigInteger Test = this.modP.Multiply(v, x2);
127 if (Test.Sign < 0)
128 Test += this.Prime;
129
130 if (Test != u)
131 {
132 if (Test == this.Prime - u)
133 x = this.modP.Multiply(x, this.twoP14);
134 else
135 throw new ArgumentException("Not a valid point.", nameof(Y));
136 }
137
138 if (X0)
139 {
140 if (x.IsZero)
141 throw new ArgumentException("Not a valid point.", nameof(Y));
142
143 if (x.IsEven)
144 x = this.Prime - x;
145 }
146 else if (!x.IsEven)
147 x = this.Prime - x;
148
149 return x;
150 }
151
152 }
153}
Base class of different types of Edwards curves over a prime field.
BigInteger d
Edwards curve coefficient
BigInteger d2
Edwards curve coefficient * 2 mod p
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 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:11
bool IsHomogeneous
If the point is in homogeneous coordinates.