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 public override int CoordinateBits => 447;
70
76 public override Tuple<byte[], byte[]> CalculatePrivateKey(byte[] Secret)
77 {
78 byte[] Bin = this.shake256_114.ComputeVariable(Secret);
79 byte[] AdditionalInfo = new byte[57];
80 byte[] PrivateKey = new byte[57];
81
82 if (this.hashSecret)
83 Buffer.BlockCopy(Bin, 0, PrivateKey, 0, 57);
84 else
85 Buffer.BlockCopy(Secret, 0, PrivateKey, 0, Math.Min(57, Secret.Length));
86
87 Buffer.BlockCopy(Bin, 57, AdditionalInfo, 0, 57);
88
89 PrivateKey[0] &= 0xfc;
90 PrivateKey[55] |= 0x80;
91 PrivateKey[56] = 0;
92
93 return new Tuple<byte[], byte[]>(PrivateKey, AdditionalInfo);
94 }
95
102 public override byte[] Sign(byte[] Data, bool BigEndian)
103 {
104 return EdDSA.Sign(Data, BigEndian, this.PrivateKey, this.AdditionalInfo, this.H_dom4, this);
105 }
106
107
114 public override byte[] Sign(Stream Data, bool BigEndian)
115 {
116 return EdDSA.Sign(Data, BigEndian, this.PrivateKey, this.AdditionalInfo, this.H_dom4, this);
117 }
118
127 public override bool Verify(byte[] Data, byte[] PublicKey, bool BigEndian, byte[] Signature)
128 {
129 return EdDSA.Verify(Data, PublicKey, BigEndian, this.H_dom4, this, Signature);
130 }
131
140 public override bool Verify(Stream Data, byte[] PublicKey, bool BigEndian, byte[] Signature)
141 {
142 return EdDSA.Verify(Data, PublicKey, BigEndian, this.H_dom4, this, Signature);
143 }
144
145 private byte[] H_dom4(byte[] Data)
146 {
147 int c = Data.Length;
148 byte[] Bin = new byte[10 + c];
149
150 Buffer.BlockCopy(preamble, 0, Bin, 0, 8);
151 Bin[8] = 0; // x=phflag=0
152 Bin[9] = 0; // y=context=empty string
153 Buffer.BlockCopy(Data, 0, Bin, 10, c);
154
155 return this.shake256_114.ComputeVariable(Bin);
156 }
157
158 private byte[] H_dom4(Stream Data)
159 {
160 using (TemporaryStream TempFile = new TemporaryStream())
161 {
162 TempFile.Write(preamble, 0, 8);
163 TempFile.WriteByte(0); // x=phflag=0
164 TempFile.WriteByte(0); // y=context=empty string
165
166 Data.Position = 0;
167 Data.CopyTo(TempFile);
168
169 TempFile.Position = 0;
170 return this.shake256_114.ComputeVariable(TempFile);
171 }
172 }
173
174 private static readonly byte[] preamble = Encoding.ASCII.GetBytes("SigEd448");
175
176 }
177}
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 bool Verify(byte[] Data, byte[] PublicKey, bool BigEndian, HashFunctionArray HashFunction, EdwardsCurveBase Curve, byte[] Signature)
Verifies a signature of Data made by the EdDSA algorithm.
Definition: EdDSA.cs:214
static byte[] Sign(byte[] Data, bool BigEndian, byte[] PrivateKey, byte[] Prefix, HashFunctionArray HashFunction, EdwardsCurveBase Curve)
Signs data using the EdDSA algorithm.
Definition: EdDSA.cs:24
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:76
Edwards448(byte[] Secret)
Edwards448 Elliptic Curve, as defined in RFC7748 and RFC8032: https://tools.ietf.org/html/rfc7748 htt...
Definition: Edwards448.cs:42
override bool Verify(byte[] Data, byte[] PublicKey, bool BigEndian, byte[] Signature)
Verifies a signature of Data made by the EdDSA algorithm.
Definition: Edwards448.cs:127
override byte[] Sign(byte[] Data, bool BigEndian)
Creates a signature of Data using the EdDSA algorithm.
Definition: Edwards448.cs:102
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 bool Verify(Stream Data, byte[] PublicKey, bool BigEndian, byte[] Signature)
Verifies a signature of Data made by the EdDSA algorithm.
Definition: Edwards448.cs:140
override int CoordinateBits
Number of bits used to encode the y-coordinate.
Definition: Edwards448.cs:69
override string CurveName
Name of curve.
Definition: Edwards448.cs:64
override byte[] Sign(Stream Data, bool BigEndian)
Creates a signature of Data using the EdDSA algorithm.
Definition: Edwards448.cs:114
Base class of Edwards curves (x²+y²=1+dx²y²) over a prime field.
Definition: EdwardsCurve.cs:10
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:9
Represents a point on a curve.
Definition: PointOnCurve.cs:10