Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
WeierstrassCurve.cs
1using System.IO;
2using System.Numerics;
3
5{
9 public abstract class WeierstrassCurve : PrimeFieldCurve
10 {
11 private readonly BigInteger a;
12 private readonly BigInteger b;
13
23 public WeierstrassCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger a,
24 BigInteger b, BigInteger Order, int Cofactor)
25 : this(Prime, BasePoint, a, b, Order, Cofactor, (byte[])null)
26 {
27 }
28
39 public WeierstrassCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger a,
40 BigInteger b, BigInteger Order, int Cofactor, byte[] Secret)
41 : base(Prime, BasePoint, Order, Cofactor, Secret)
42 {
43 this.a = a;
44 this.b = b;
45 }
46
57 public WeierstrassCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger a,
58 BigInteger b, BigInteger Order, int Cofactor, uint[] Secret)
59 : base(Prime, BasePoint, Order, Cofactor, Secret)
60 {
61 this.a = a;
62 this.b = b;
63 }
64
68 public BigInteger A => this.a;
69
73 public BigInteger B => this.b;
74
79 public void Negate(ref PointOnCurve P)
80 {
81 P.Y = this.p - P.Y;
82 }
83
90 public override void AddTo(ref PointOnCurve P, PointOnCurve Q)
91 {
92 if (P.NonZero)
93 {
94 if (Q.NonZero)
95 {
96 BigInteger sDividend = this.modP.Subtract(P.Y, Q.Y);
97 BigInteger sDivisor = this.modP.Subtract(P.X, Q.X);
98 BigInteger s, xR, yR;
99
100 if (sDivisor.IsZero)
101 {
102 if (sDividend.IsZero) // P=Q
103 this.Double(ref P);
104 else
105 P = this.Zero;
106 }
107 else
108 {
109 s = this.modP.Divide(sDividend, sDivisor);
110 xR = this.modP.Subtract(this.modP.Multiply(s, s), this.modP.Add(P.X, Q.X));
111 yR = this.modP.Add(P.Y, this.modP.Multiply(s, this.modP.Subtract(xR, P.X)));
112
113 P.X = xR;
114 P.Y = this.p - yR;
115 }
116 }
117 }
118 else
119 P.CopyFrom(Q);
120 }
121
126 public override void Double(ref PointOnCurve P)
127 {
128 if (P.NonZero)
129 {
130 BigInteger sDividend = this.modP.Add(3 * this.modP.Multiply(P.X, P.X), this.a);
131 BigInteger sDivisor = this.modP.Multiply(Two, P.Y);
132
133 BigInteger s = this.modP.Divide(sDividend, sDivisor);
134 BigInteger xR = this.modP.Subtract(this.modP.Multiply(s, s), this.modP.Add(P.X, P.X));
135 BigInteger yR = this.modP.Add(P.Y, this.modP.Multiply(s, this.modP.Subtract(xR, P.X)));
136
137 P.X = xR;
138 P.Y = this.p - yR;
139 }
140 }
141
147 public override bool IsPoint(PointOnCurve Point)
148 {
149 // Check if point matches y²=x³+ax+b=x(x²+a)+b
150
151 BigInteger Left = this.modP.Multiply(Point.Y, Point.Y);
152
153 BigInteger Right = this.modP.Add(
154 this.modP.Multiply(
155 Point.X,
156 this.modP.Add(
157 this.modP.Multiply(Point.X, Point.X),
158 this.a)),
159 this.b);
160
161 return Left == Right;
162 }
163
170 public override byte[] Sign(byte[] Data, bool BigEndian)
171 {
172 return ECDSA.Sign(Data, BigEndian, this.PrivateKey, this.HashFunction, this);
173 }
174
181 public override byte[] Sign(Stream Data, bool BigEndian)
182 {
183 return ECDSA.Sign(Data, BigEndian, this.PrivateKey, this.HashFunctionStream, this);
184 }
185
194 public override bool Verify(byte[] Data, byte[] PublicKey, bool BigEndian, byte[] Signature)
195 {
196 return ECDSA.Verify(Data, PublicKey, BigEndian, this.HashFunction, this, Signature);
197 }
198
207 public override bool Verify(Stream Data, byte[] PublicKey, bool BigEndian, byte[] Signature)
208 {
209 return ECDSA.Verify(Data, PublicKey, BigEndian, this.HashFunctionStream, this, Signature);
210 }
211 }
212}
Implements the Elliptic Curve Digital Signature Algorithm (ECDSA).
Definition: ECDSA.cs:11
static bool Verify(byte[] Data, byte[] PublicKey, bool BigEndian, HashFunctionArray HashFunction, PrimeFieldCurve Curve, byte[] Signature)
Verifies a signature of Data made by the ECDSA algorithm.
Definition: ECDSA.cs:184
static byte[] Sign(byte[] Data, bool BigEndian, byte[] PrivateKey, HashFunctionArray HashFunction, PrimeFieldCurve Curve)
Signs data using the ECDSA algorithm.
Definition: ECDSA.cs:21
virtual byte[] PublicKey
Encoded public key
virtual PointOnCurve Zero
Neutral point.
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 Divide(BigInteger a, BigInteger b)
Divides two numbers, modulus p
Definition: ModulusP.cs:91
BigInteger Subtract(BigInteger a, BigInteger b)
Subtracts two numbers, modulus p
Definition: ModulusP.cs:51
Base class of Elliptic curves over a prime field.
readonly ModulusP modP
Arithmetic modulus p
virtual HashFunctionStream HashFunctionStream
Hash function to use in signatures for data streams.
Base class of Weierstrass curves (y²=x³+ax+b) over a prime field.
void Negate(ref PointOnCurve P)
Negates a point on the curve.
override byte[] Sign(byte[] Data, bool BigEndian)
Creates a signature of Data using the ECDSA algorithm.
WeierstrassCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger a, BigInteger b, BigInteger Order, int Cofactor)
Base class of Weierstrass curves (y²=x³+ax+b) over a prime field.
override void Double(ref PointOnCurve P)
Doubles a point on the curve.
WeierstrassCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger a, BigInteger b, BigInteger Order, int Cofactor, byte[] Secret)
Base class of Weierstrass curves (y²=x³+ax+b) over a prime field.
override void AddTo(ref PointOnCurve P, PointOnCurve Q)
Adds Q to P .
override byte[] Sign(Stream Data, bool BigEndian)
Creates a signature of Data using the ECDSA algorithm.
override bool IsPoint(PointOnCurve Point)
Checks if a point is on the curve.
WeierstrassCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger a, BigInteger b, BigInteger Order, int Cofactor, uint[] Secret)
Base class of Weierstrass curves (y²=x³+ax+b) over a prime field.
override bool Verify(Stream Data, byte[] PublicKey, bool BigEndian, byte[] Signature)
Verifies a signature of Data made by the ECDSA algorithm.
override bool Verify(byte[] Data, byte[] PublicKey, bool BigEndian, byte[] Signature)
Verifies a signature of Data made by the ECDSA algorithm.
HashFunction
Hash method enumeration.
Definition: Hashes.cs:26
Represents a point on a curve.
Definition: PointOnCurve.cs:10
bool NonZero
If the point is not zero (infinity).