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), A0, n0, 4)
27 {
28 }
29
35 public Curve448(byte[] Secret)
36 : base(p0, new PointOnCurve(BasePointU, BasePointV), A0, n0, 4, Secret)
37 {
38 }
39
43 public override string CurveName => "Curve448";
44
51 public override PointOnCurve ToXY(PointOnCurve UV)
52 {
53 BigInteger U2 = this.modP.Multiply(UV.X, UV.X);
54 BigInteger U3 = this.modP.Multiply(U2, UV.X);
55 BigInteger U4 = this.modP.Multiply(U2, U2);
56 BigInteger U5 = this.modP.Multiply(U3, U2);
57 BigInteger V2 = this.modP.Multiply(UV.Y, UV.Y);
58
59 BigInteger TwoU2 = U2 << 1;
60 BigInteger TwoU3 = U3 << 1;
61
62 BigInteger X = this.modP.Divide(this.modP.Multiply(UV.Y << 2, U2 - 1),
63 (U4 - TwoU2 + (V2 << 2) + BigInteger.One));
64
65 BigInteger Y = this.modP.Divide(-(U5 - TwoU3 -
66 this.modP.Multiply((UV.X << 2), V2) + UV.X),
67 (U5 - this.modP.Multiply(TwoU2, V2) - TwoU3 - (V2 << 1) + UV.X));
68
69 if (X.Sign < 0)
70 X += this.p;
71
72 if (Y.Sign < 0)
73 Y += this.p;
74
75 return new PointOnCurve(X, Y);
76 }
77
84 public override PointOnCurve ToUV(PointOnCurve XY)
85 {
86 BigInteger X2 = this.modP.Multiply(XY.X, XY.X);
87 BigInteger Y2 = this.modP.Multiply(XY.Y, XY.Y);
88 BigInteger U = this.modP.Divide(Y2, X2);
89 BigInteger X3 = this.modP.Multiply(XY.X, X2);
90 BigInteger V = this.modP.Divide(this.modP.Multiply(Two - X2 - Y2, XY.Y), X3);
91
92 if (U.Sign < 0)
93 U += this.p;
94
95 if (V.Sign < 0)
96 V += this.p;
97
98 return new PointOnCurve(U, V);
99 }
100
107 public override BigInteger ScalarMultiplication(byte[] N, BigInteger U)
108 {
109 return XFunction(N, U, A24, this.p, 448);
110 }
111
117 public override Tuple<byte[], byte[]> CalculatePrivateKey(byte[] Secret)
118 {
119 byte[] Bin = Secret;
120
121 switch (Bin.Length)
122 {
123 case 56:
124 Array.Resize(ref Bin, 57);
125 break;
126
127 case 57:
128 break;
129
130 default:
131 Bin = Hashes.ComputeSHA512Hash(Secret);
132 Array.Resize(ref Bin, 57);
133 break;
134 }
135
136 Bin[0] &= 0xfc;
137 Bin[55] |= 0x80;
138 Bin[56] |= 0;
139
140 return new Tuple<byte[], byte[]>(Bin, null);
141 }
142
147 public override EdwardsCurveBase CreatePair()
148 {
149 PointOnCurve PublicKeyUV = this.PublicKeyPoint;
150 PointOnCurve PublicKeyXY = this.ToXY(PublicKeyUV);
151
152 Edwards448 Candidate = new Edwards448(this.PrivateKey, false);
153 PointOnCurve PublicKeyXY2 = Candidate.PublicKeyPoint;
154 PublicKeyXY2 = Candidate.ScalarMultiplication(4, PublicKeyXY2, true);
155
156 if (PublicKeyXY.Y.Equals(PublicKeyXY2.Y))
157 return Candidate;
158 else
159 throw new InvalidOperationException("Unable to create pair curve.");
160 }
161
168 public override byte[] Sign(byte[] Data, bool BigEndian)
169 {
170 throw new NotSupportedException("Signatures not supported.");
171 //return XEdDSA.Sign(Data, this.PrivateKey, Hashes.ComputeSHA512Hash, this);
172 }
173
180 public override byte[] Sign(Stream Data, bool BigEndian)
181 {
182 throw new NotSupportedException("Signatures not supported.");
183 //return XEdDSA.Sign(Data, this.PrivateKey, Hashes.ComputeSHA512Hash, this);
184 }
185
194 public override bool Verify(byte[] Data, byte[] PublicKey, bool BigEndian, byte[] Signature)
195 {
196 throw new NotSupportedException("Signatures not supported.");
197 //return XEdDSA.Verify(Data, PublicKey, Hashes.ComputeSHA512Hash, this,
198 // Signature, 448, 446);
199 }
200
209 public override bool Verify(Stream Data, byte[] PublicKey, bool BigEndian, byte[] Signature)
210 {
211 throw new NotSupportedException("Signatures not supported.");
212 //return XEdDSA.Verify(Data, PublicKey, Hashes.ComputeSHA512Hash, this,
213 // Signature, 448, 446);
214 }
215
216 }
217}
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:107
override bool Verify(Stream Data, byte[] PublicKey, bool BigEndian, byte[] Signature)
Verifies a signature of Data made by the EdDSA algorithm.
Definition: Curve448.cs:209
override byte[] Sign(Stream Data, bool BigEndian)
Creates a signature of Data using the XEdDSA algorithm.
Definition: Curve448.cs:180
override bool Verify(byte[] Data, byte[] PublicKey, bool BigEndian, byte[] Signature)
Verifies a signature of Data made by the EdDSA algorithm.
Definition: Curve448.cs:194
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:84
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:51
Curve448()
Curve448 (Goldilocks), as defined in RFC 7748: https://tools.ietf.org/html/rfc7748
Definition: Curve448.cs:25
override Tuple< byte[], byte[]> CalculatePrivateKey(byte[] Secret)
Calculates a private key from a secret.
Definition: Curve448.cs:117
override string CurveName
Name of curve.
Definition: Curve448.cs:43
override byte[] Sign(byte[] Data, bool BigEndian)
Creates a signature of Data using the XEdDSA algorithm.
Definition: Curve448.cs:168
override EdwardsCurveBase CreatePair()
Creates the Edwards Curve pair.
Definition: Curve448.cs:147
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 birational 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:57
static byte[] ComputeSHA512Hash(byte[] Data)
Computes the SHA-512 hash of a block of binary data.
Definition: Hashes.cs:577
Represents a point on a curve.
Definition: PointOnCurve.cs:10