Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Curve448.cs
1using System;
2using System.Globalization;
3using System.IO;
4using System.Numerics;
5
7{
13 {
14 private static readonly BigInteger p0 = BigInteger.Pow(2, 448) - BigInteger.Pow(2, 224) - 1;
15 private static readonly BigInteger A0 = 156326;
16 private static readonly BigInteger A24 = (A0 - 2) / 4;
17 private static readonly BigInteger n0 = BigInteger.Pow(2, 446) - BigInteger.Parse("008335dc163bb124b65129c96fde933d8d723a70aadc873d6d54a7bb0d", NumberStyles.HexNumber);
18 private static readonly BigInteger BasePointU = 5;
19 private static readonly BigInteger BasePointV = BigInteger.Parse("355293926785568175264127502063783334808976399387714271831880898435169088786967410002932673765864550910142774147268105838985595290606362");
20
25 public Curve448()
26 : base(p0, new PointOnCurve(BasePointU, BasePointV), n0, 4)
27 {
28 }
29
35 public Curve448(byte[] Secret)
36 : base(p0, new PointOnCurve(BasePointU, BasePointV), n0, 4, Secret)
37 {
38 }
39
43 public override string CurveName => "Curve448";
44
48 protected override BigInteger A => A0;
49
56 public override PointOnCurve ToXY(PointOnCurve UV)
57 {
58 BigInteger U2 = this.modP.Multiply(UV.X, UV.X);
59 BigInteger U3 = this.modP.Multiply(U2, UV.X);
60 BigInteger U4 = this.modP.Multiply(U2, U2);
61 BigInteger U5 = this.modP.Multiply(U3, U2);
62 BigInteger V2 = this.modP.Multiply(UV.Y, UV.Y);
63
64 BigInteger TwoU2 = U2 << 1;
65 BigInteger TwoU3 = U3 << 1;
66
67 BigInteger X = this.modP.Divide(this.modP.Multiply(UV.Y << 2, U2 - 1),
68 (U4 - TwoU2 + (V2 << 2) + BigInteger.One));
69
70 BigInteger Y = this.modP.Divide(-(U5 - TwoU3 -
71 this.modP.Multiply((UV.X << 2), V2) + UV.X),
72 (U5 - this.modP.Multiply(TwoU2, V2) - TwoU3 - (V2 << 1) + UV.X));
73
74 if (X.Sign < 0)
75 X += this.p;
76
77 if (Y.Sign < 0)
78 Y += this.p;
79
80 return new PointOnCurve(X, Y);
81 }
82
89 public override PointOnCurve ToUV(PointOnCurve XY)
90 {
91 BigInteger X2 = this.modP.Multiply(XY.X, XY.X);
92 BigInteger Y2 = this.modP.Multiply(XY.Y, XY.Y);
93 BigInteger U = this.modP.Divide(Y2, X2);
94 BigInteger X3 = this.modP.Multiply(XY.X, X2);
95 BigInteger V = this.modP.Divide(this.modP.Multiply(Two - X2 - Y2, XY.Y), X3);
96
97 if (U.Sign < 0)
98 U += this.p;
99
100 if (V.Sign < 0)
101 V += this.p;
102
103 return new PointOnCurve(U, V);
104 }
105
112 public override BigInteger ScalarMultiplication(byte[] N, BigInteger U)
113 {
114 return XFunction(N, U, A24, this.p, 448);
115 }
116
122 public override Tuple<byte[], byte[]> CalculatePrivateKey(byte[] Secret)
123 {
124 byte[] Bin = Secret;
125
126 switch (Bin.Length)
127 {
128 case 56:
129 Array.Resize(ref Bin, 57);
130 break;
131
132 case 57:
133 break;
134
135 default:
136 Bin = Hashes.ComputeSHA512Hash(Secret);
137 Array.Resize(ref Bin, 57);
138 break;
139 }
140
141 Bin[0] &= 0xfc;
142 Bin[55] |= 0x80;
143 Bin[56] |= 0;
144
145 return new Tuple<byte[], byte[]>(Bin, null);
146 }
147
152 public override EdwardsCurveBase CreatePair()
153 {
154 PointOnCurve PublicKeyUV = this.PublicKeyPoint;
155 PointOnCurve PublicKeyXY = this.ToXY(PublicKeyUV);
156
157 Edwards448 Candidate = new Edwards448(this.PrivateKey, false);
158 PointOnCurve PublicKeyXY2 = Candidate.PublicKeyPoint;
159 PublicKeyXY2 = Candidate.ScalarMultiplication(4, PublicKeyXY2, true);
160
161 if (PublicKeyXY.Y.Equals(PublicKeyXY2.Y))
162 return Candidate;
163 else
164 throw new InvalidOperationException("Unable to create pair curve.");
165 }
166
172 public override byte[] Sign(byte[] Data)
173 {
174 throw new NotSupportedException("Signatures not supported.");
175 //return XEdDSA.Sign(Data, this.PrivateKey, Hashes.ComputeSHA512Hash, this);
176 }
177
183 public override byte[] Sign(Stream Data)
184 {
185 throw new NotSupportedException("Signatures not supported.");
186 //return XEdDSA.Sign(Data, this.PrivateKey, Hashes.ComputeSHA512Hash, this);
187 }
188
196 public override bool Verify(byte[] Data, byte[] PublicKey, byte[] Signature)
197 {
198 throw new NotSupportedException("Signatures not supported.");
199 //return XEdDSA.Verify(Data, PublicKey, Hashes.ComputeSHA512Hash, this,
200 // Signature, 448, 446);
201 }
202
210 public override bool Verify(Stream Data, byte[] PublicKey, byte[] Signature)
211 {
212 throw new NotSupportedException("Signatures not supported.");
213 //return XEdDSA.Verify(Data, PublicKey, Hashes.ComputeSHA512Hash, this,
214 // Signature, 448, 446);
215 }
216
217 }
218}
Curve448 (Goldilocks), as defined in RFC 7748: https://tools.ietf.org/html/rfc7748
Definition: Curve448.cs:13
Curve448(byte[] Secret)
Curve448 (Goldilocks), as defined in RFC 7748: https://tools.ietf.org/html/rfc7748
Definition: Curve448.cs:35
override BigInteger ScalarMultiplication(byte[] N, BigInteger U)
Performs the scalar multiplication of N *U .
Definition: Curve448.cs:112
override byte[] Sign(Stream Data)
Creates a signature of Data using the XEdDSA algorithm.
Definition: Curve448.cs:183
override PointOnCurve ToUV(PointOnCurve XY)
Converts a pair of (X,Y) coordinates for the birational Edwards curve to a pair of (U,...
Definition: Curve448.cs:89
override PointOnCurve ToXY(PointOnCurve UV)
Converts a pair of (U,V) coordinates to a pair of (X,Y) coordinates in the birational Edwards curve.
Definition: Curve448.cs:56
Curve448()
Curve448 (Goldilocks), as defined in RFC 7748: https://tools.ietf.org/html/rfc7748
Definition: Curve448.cs:25
override bool Verify(byte[] Data, byte[] PublicKey, byte[] Signature)
Verifies a signature of Data made by the EdDSA algorithm.
Definition: Curve448.cs:196
override Tuple< byte[], byte[]> CalculatePrivateKey(byte[] Secret)
Calculates a private key from a secret.
Definition: Curve448.cs:122
override string CurveName
Name of curve.
Definition: Curve448.cs:43
override bool Verify(Stream Data, byte[] PublicKey, byte[] Signature)
Verifies a signature of Data made by the EdDSA algorithm.
Definition: Curve448.cs:210
override byte[] Sign(byte[] Data)
Creates a signature of Data using the XEdDSA algorithm.
Definition: Curve448.cs:172
override BigInteger A
a Coefficient in the definition of the curve E: v²=u³+A*u²+u
Definition: Curve448.cs:48
override EdwardsCurveBase CreatePair()
Creates the Edwards Curve pair.
Definition: Curve448.cs:152
Edwards448 Elliptic Curve, as defined in RFC7748 and RFC8032: https://tools.ietf.org/html/rfc7748 htt...
Definition: Edwards448.cs:17
Base class of different types of Edwards curves over a prime field.
virtual byte[] PublicKey
Encoded public key
virtual PointOnCurve PublicKeyPoint
Public key, as a point on the elliptic curve.
BigInteger Multiply(BigInteger a, BigInteger b)
Multiplies two numbers, modulus p
Definition: ModulusP.cs:80
BigInteger Divide(BigInteger a, BigInteger b)
Divides two numbers, modulus p
Definition: ModulusP.cs:91
Base class of Montgomery curves (y²=x³+Ax²+x), with biratinal Edwards equivalent over a prime field.
static BigInteger XFunction(byte[] N, BigInteger U, BigInteger A24, BigInteger p, int Bits)
Performs the scalar multiplication of N *U .
override PointOnCurve PublicKeyPoint
Public key.
readonly ModulusP modP
Arithmetic modulus p
override PointOnCurve ScalarMultiplication(byte[] N, PointOnCurve P, bool Normalize)
Performs the scalar multiplication of N *P .
Contains methods for simple hash calculations.
Definition: Hashes.cs:59
static byte[] ComputeSHA512Hash(byte[] Data)
Computes the SHA-512 hash of a block of binary data.
Definition: Hashes.cs:456
Represents a point on a curve.
Definition: PointOnCurve.cs:11