Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Edwards25519.cs
1using System;
2using System.Globalization;
3using System.IO;
4using System.Numerics;
5
7{
14 {
15 private static readonly BigInteger p0 = BigInteger.Pow(2, 255) - 19;
16 private static readonly BigInteger d0 = BigInteger.Parse("37095705934669439343138083508754565189542113879843219016388785533085940283555");
17 private static readonly BigInteger n0 = BigInteger.Pow(2, 252) + BigInteger.Parse("14def9dea2f79cd65812631a5cf5d3ed", NumberStyles.HexNumber);
18 private static readonly BigInteger BasePointX = BigInteger.Parse("15112221349535400772501151409588531511454012693041857206046113283949847762202");
19 private static readonly BigInteger BasePointY = BigInteger.Parse("46316835694926478169428394003475163141307993866256225615783033603165251855960");
20 private readonly bool hashSecret;
21
27 public Edwards25519()
28 : this(null, true)
29 {
30 }
31
38 public Edwards25519(byte[] Secret)
39 : this(Secret, true)
40 {
41 }
42
50 public Edwards25519(byte[] Secret, bool HashSecret)
51 : base(p0, new PointOnCurve(BasePointX, BasePointY), d0, n0, 8, Secret)
52 {
53 this.hashSecret = HashSecret;
54 }
55
59 public override string CurveName => "Edwards25519";
60
64 public override int CoordinateBits => 254;
65
71 public override Tuple<byte[], byte[]> CalculatePrivateKey(byte[] Secret)
72 {
73 byte[] Bin = Hashes.ComputeSHA512Hash(Secret);
74 byte[] AdditionalInfo = new byte[32];
75 byte[] PrivateKey = new byte[32];
76
77 if (this.hashSecret)
78 Array.Copy(Bin, 0, PrivateKey, 0, 32);
79 else
80 Array.Copy(Secret, 0, PrivateKey, 0, Math.Min(32, Secret.Length));
81
82 Array.Copy(Bin, 32, AdditionalInfo, 0, 32);
83
84 PrivateKey[0] &= 0xf8;
85 PrivateKey[31] &= 0x3f;
86 PrivateKey[31] |= 0x40;
87
88 return new Tuple<byte[], byte[]>(PrivateKey, AdditionalInfo);
89 }
90
96 public override byte[] Sign(byte[] Data)
97 {
98 return EdDSA.Sign(Data, this.PrivateKey, this.AdditionalInfo, Hashes.ComputeSHA512Hash, this);
99 }
100
106 public override byte[] Sign(Stream Data)
107 {
108 return EdDSA.Sign(Data, this.PrivateKey, this.AdditionalInfo, Hashes.ComputeSHA512Hash, this);
109 }
110
118 public override bool Verify(byte[] Data, byte[] PublicKey, byte[] Signature)
119 {
120 return EdDSA.Verify(Data, PublicKey, Hashes.ComputeSHA512Hash, this, Signature);
121 }
122
130 public override bool Verify(Stream Data, byte[] PublicKey, byte[] Signature)
131 {
132 return EdDSA.Verify(Data, PublicKey, Hashes.ComputeSHA512Hash, this, Signature);
133 }
134
135 }
136}
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
Edwards25519 Elliptic Curve, as defined in RFC7748 and RFC8032: https://tools.ietf....
Definition: Edwards25519.cs:14
override bool Verify(byte[] Data, byte[] PublicKey, byte[] Signature)
Verifies a signature of Data made by the EdDSA algorithm.
Edwards25519(byte[] Secret)
Edwards25519 Elliptic Curve, as defined in RFC7748 and RFC8032: https://tools.ietf....
Definition: Edwards25519.cs:38
override bool Verify(Stream Data, byte[] PublicKey, byte[] Signature)
Verifies a signature of Data made by the EdDSA algorithm.
override byte[] Sign(Stream Data)
Creates a signature of Data using the EdDSA algorithm.
override byte[] Sign(byte[] Data)
Creates a signature of Data using the EdDSA algorithm.
Definition: Edwards25519.cs:96
Edwards25519(byte[] Secret, bool HashSecret)
Edwards25519 Elliptic Curve, as defined in RFC7748 and RFC8032: https://tools.ietf....
Definition: Edwards25519.cs:50
Edwards25519()
Edwards25519 Elliptic Curve, as defined in RFC7748 and RFC8032: https://tools.ietf....
Definition: Edwards25519.cs:27
override Tuple< byte[], byte[]> CalculatePrivateKey(byte[] Secret)
Calculates a private key from a secret.
Definition: Edwards25519.cs:71
override string CurveName
Name of curve.
Definition: Edwards25519.cs:59
override int CoordinateBits
Number of bits used to encode the y-coordinate.
Definition: Edwards25519.cs:64
Base class of Twisted Edwards curves (-x²+y²=1+dx²y²) over a prime field.
byte[] AdditionalInfo
Curve-specific additional information
virtual byte[] PublicKey
Encoded public key
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