Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Edwards448.cs
1using System;
2using System.Globalization;
3using System.IO;
4using System.Numerics;
5using System.Text;
8
10{
16 public class Edwards448 : EdwardsCurve
17 {
18 private static readonly BigInteger p0 = BigInteger.Pow(2, 448) - BigInteger.Pow(2, 224) - 1;
19 private static readonly BigInteger d0 = p0 - 39081;
20 private static readonly BigInteger n0 = BigInteger.Pow(2, 446) - BigInteger.Parse("008335dc163bb124b65129c96fde933d8d723a70aadc873d6d54a7bb0d", NumberStyles.HexNumber);
21 private static readonly BigInteger BasePointX = BigInteger.Parse("224580040295924300187604334099896036246789641632564134246125461686950415467406032909029192869357953282578032075146446173674602635247710");
22 private static readonly BigInteger BasePointY = BigInteger.Parse("298819210078481492676017930443930673437544040154080242095928241372331506189835876003536878655418784733982303233503462500531545062832660");
23 private readonly SHAKE256 shake256_114;
24 private readonly bool hashSecret;
25
31 public Edwards448()
32 : this(null, true)
33 {
34 }
35
42 public Edwards448(byte[] Secret)
43 : this(Secret, true)
44 {
45 }
46
54 public Edwards448(byte[] Secret, bool HashSecret)
55 : base(p0, new PointOnCurve(BasePointX, BasePointY), d0, n0, 4, Secret)
56 {
57 this.shake256_114 = new SHAKE256(114 << 3);
58 this.hashSecret = HashSecret;
59 }
60
64 public override string CurveName => "Edwards448";
65
69 protected override BigInteger D => d;
70
74 public override int CoordinateBits => 447;
75
81 public override Tuple<byte[], byte[]> CalculatePrivateKey(byte[] Secret)
82 {
83 byte[] Bin = this.shake256_114.ComputeVariable(Secret);
84 byte[] AdditionalInfo = new byte[57];
85 byte[] PrivateKey = new byte[57];
86
87 if (this.hashSecret)
88 Array.Copy(Bin, 0, PrivateKey, 0, 57);
89 else
90 Array.Copy(Secret, 0, PrivateKey, 0, Math.Min(57, Secret.Length));
91
92 Array.Copy(Bin, 57, AdditionalInfo, 0, 57);
93
94 PrivateKey[0] &= 0xfc;
95 PrivateKey[55] |= 0x80;
96 PrivateKey[56] = 0;
97
98 return new Tuple<byte[], byte[]>(PrivateKey, AdditionalInfo);
99 }
100
106 public override byte[] Sign(byte[] Data)
107 {
108 return EdDSA.Sign(Data, this.PrivateKey, this.AdditionalInfo, this.H_dom4, this);
109 }
110
111
117 public override byte[] Sign(Stream Data)
118 {
119 return EdDSA.Sign(Data, this.PrivateKey, this.AdditionalInfo, this.H_dom4, this);
120 }
121
129 public override bool Verify(byte[] Data, byte[] PublicKey, byte[] Signature)
130 {
131 return EdDSA.Verify(Data, PublicKey, this.H_dom4, this, Signature);
132 }
133
141 public override bool Verify(Stream Data, byte[] PublicKey, byte[] Signature)
142 {
143 return EdDSA.Verify(Data, PublicKey, this.H_dom4, this, Signature);
144 }
145
146 private byte[] H_dom4(byte[] Data)
147 {
148 int c = Data.Length;
149 byte[] Bin = new byte[10 + c];
150
151 Array.Copy(preamble, 0, Bin, 0, 8);
152 Bin[8] = 0; // x=phflag=0
153 Bin[9] = 0; // y=context=empty string
154 Array.Copy(Data, 0, Bin, 10, c);
155
156 return this.shake256_114.ComputeVariable(Bin);
157 }
158
159 private byte[] H_dom4(Stream Data)
160 {
161 using (TemporaryStream TempFile = new TemporaryStream())
162 {
163 TempFile.Write(preamble, 0, 8);
164 TempFile.WriteByte(0); // x=phflag=0
165 TempFile.WriteByte(0); // y=context=empty string
166
167 Data.Position = 0;
168 Data.CopyTo(TempFile);
169
170 TempFile.Position = 0;
171 return this.shake256_114.ComputeVariable(TempFile);
172 }
173 }
174
175 private static readonly byte[] preamble = Encoding.ASCII.GetBytes("SigEd448");
176
177 }
178}
Manages a temporary stream. Contents is kept in-memory, if below a memory threshold,...
override void Write(byte[] buffer, int offset, int count)
When overridden in a derived class, writes a sequence of bytes to the current stream and advances the...
override void WriteByte(byte value)
Writes a byte to the current position in the stream and advances the position within the stream by on...
Implements the Edwards curve Digital Signature Algorithm (EdDSA), as defined in RFC 8032....
Definition: EdDSA.cs:13
static byte[] Sign(byte[] Data, byte[] PrivateKey, byte[] Prefix, HashFunctionArray HashFunction, EdwardsCurveBase Curve)
Signs data using the EdDSA algorithm.
Definition: EdDSA.cs:23
static bool Verify(byte[] Data, byte[] PublicKey, HashFunctionArray HashFunction, EdwardsCurveBase Curve, byte[] Signature)
Verifies a signature of Data made by the EdDSA algorithm.
Definition: EdDSA.cs:200
Edwards448 Elliptic Curve, as defined in RFC7748 and RFC8032: https://tools.ietf.org/html/rfc7748 htt...
Definition: Edwards448.cs:17
override Tuple< byte[], byte[]> CalculatePrivateKey(byte[] Secret)
Calculates a private key from a secret.
Definition: Edwards448.cs:81
override bool Verify(Stream Data, byte[] PublicKey, byte[] Signature)
Verifies a signature of Data made by the EdDSA algorithm.
Definition: Edwards448.cs:141
Edwards448(byte[] Secret)
Edwards448 Elliptic Curve, as defined in RFC7748 and RFC8032: https://tools.ietf.org/html/rfc7748 htt...
Definition: Edwards448.cs:42
override BigInteger D
d coefficient of Edwards curve.
Definition: Edwards448.cs:69
override bool Verify(byte[] Data, byte[] PublicKey, byte[] Signature)
Verifies a signature of Data made by the EdDSA algorithm.
Definition: Edwards448.cs:129
override byte[] Sign(byte[] Data)
Creates a signature of Data using the EdDSA algorithm.
Definition: Edwards448.cs:106
override byte[] Sign(Stream Data)
Creates a signature of Data using the EdDSA algorithm.
Definition: Edwards448.cs:117
Edwards448()
Edwards448 Elliptic Curve, as defined in RFC7748 and RFC8032: https://tools.ietf.org/html/rfc7748 htt...
Definition: Edwards448.cs:31
Edwards448(byte[] Secret, bool HashSecret)
Edwards448 Elliptic Curve, as defined in RFC7748 and RFC8032: https://tools.ietf.org/html/rfc7748 htt...
Definition: Edwards448.cs:54
override int CoordinateBits
Number of bits used to encode the y-coordinate.
Definition: Edwards448.cs:74
override string CurveName
Name of curve.
Definition: Edwards448.cs:64
BigInteger d
Edwards curve coefficient
Base class of Edwards curves (x²+y²=1+dx²y²) over a prime field.
Definition: EdwardsCurve.cs:11
byte[] AdditionalInfo
Curve-specific additional information
virtual byte[] PublicKey
Encoded public key
Implements the SHA3 SHAKE256 extendable-output functions, as defined in section 6....
Definition: SHAKE256.cs:13
Represents a point on a curve.
Definition: PointOnCurve.cs:11