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;
2using System.Security.Cryptography;
3using System.Numerics;
4
6{
10 public abstract class WeierstrassCurve : PrimeFieldCurve
11 {
12 private readonly BigInteger a;
13
22 public WeierstrassCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger a,
23 BigInteger Order, int Cofactor)
24 : this(Prime, BasePoint, a, Order, Cofactor, null)
25 {
26 }
27
37 public WeierstrassCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger a,
38 BigInteger Order, int Cofactor, byte[] Secret)
39 : base(Prime, BasePoint, Order, Cofactor, Secret)
40 {
41 this.a = a;
42 }
43
48 public void Negate(ref PointOnCurve P)
49 {
50 P.Y = this.p - P.Y;
51 }
52
59 public override void AddTo(ref PointOnCurve P, PointOnCurve Q)
60 {
61 if (P.NonZero)
62 {
63 if (Q.NonZero)
64 {
65 BigInteger sDividend = this.modP.Subtract(P.Y, Q.Y);
66 BigInteger sDivisor = this.modP.Subtract(P.X, Q.X);
67 BigInteger s, xR, yR;
68
69 if (sDivisor.IsZero)
70 {
71 if (sDividend.IsZero) // P=Q
72 this.Double(ref P);
73 else
74 P = this.Zero;
75 }
76 else
77 {
78 s = this.modP.Divide(sDividend, sDivisor);
79 xR = this.modP.Subtract(this.modP.Multiply(s, s), this.modP.Add(P.X, Q.X));
80 yR = this.modP.Add(P.Y, this.modP.Multiply(s, this.modP.Subtract(xR, P.X)));
81
82 P.X = xR;
83 P.Y = this.p - yR;
84 }
85 }
86 }
87 else
88 P.CopyFrom(Q);
89 }
90
95 public override void Double(ref PointOnCurve P)
96 {
97 if (P.NonZero)
98 {
99 BigInteger sDividend = this.modP.Add(3 * this.modP.Multiply(P.X, P.X), this.a);
100 BigInteger sDivisor = this.modP.Multiply(Two, P.Y);
101
102 BigInteger s = this.modP.Divide(sDividend, sDivisor);
103 BigInteger xR = this.modP.Subtract(this.modP.Multiply(s, s), this.modP.Add(P.X, P.X));
104 BigInteger yR = this.modP.Add(P.Y, this.modP.Multiply(s, this.modP.Subtract(xR, P.X)));
105
106 P.X = xR;
107 P.Y = this.p - yR;
108 }
109 }
110
111 }
112}
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
Base class of Weierstrass curves (y²=x³+ax+b) over a prime field.
void Negate(ref PointOnCurve P)
Negates a point on the curve.
WeierstrassCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger a, 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.
override void AddTo(ref PointOnCurve P, PointOnCurve Q)
Adds Q to P .
WeierstrassCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger a, BigInteger Order, int Cofactor, byte[] Secret)
Base class of Weierstrass curves (y²=x³+ax+b) over a prime field.
Represents a point on a curve.
Definition: PointOnCurve.cs:11
bool NonZero
If the point is not zero (infinity).