Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Curve25519.cs
1using System;
2using System.Globalization;
3using System.IO;
4using System.Numerics;
5
7{
13 {
14 private static readonly BigInteger p0 = BigInteger.Pow(2, 255) - 19;
15 private static readonly BigInteger A0 = 486662;
16 private static readonly BigInteger A24 = (A0 - 2) / 4;
17 private static readonly BigInteger n0 = BigInteger.Pow(2, 252) + BigInteger.Parse("14def9dea2f79cd65812631a5cf5d3ed", NumberStyles.HexNumber);
18 private static readonly BigInteger BasePointU = 9;
19 private static readonly BigInteger BasePointV = BigInteger.Parse("14781619447589544791020593568409986887264606134616475288964881837755586237401");
20 private static readonly BigInteger SqrtMinus486664 = ModulusP.SqrtModP(-486664, p0);
21
26 public Curve25519()
27 : base(p0, new PointOnCurve(BasePointU, BasePointV), n0, 8)
28 {
29 }
30
36 public Curve25519(byte[] Secret)
37 : base(p0, new PointOnCurve(BasePointU, BasePointV), n0, 8, Secret)
38 {
39 }
40
44 public override string CurveName => "Curve25519";
45
49 protected override BigInteger A => A0;
50
57 public override PointOnCurve ToXY(PointOnCurve UV)
58 {
59 BigInteger X = this.modP.Multiply(SqrtMinus486664, this.modP.Divide(UV.X, UV.Y));
60 BigInteger Y = this.modP.Divide(UV.X - BigInteger.One, UV.X + BigInteger.One);
61
62 if (X.Sign < 0)
63 X += this.p;
64
65 if (Y.Sign < 0)
66 Y += this.p;
67
68 return new PointOnCurve(X, Y);
69 }
70
77 public override PointOnCurve ToUV(PointOnCurve XY)
78 {
79 BigInteger U = this.modP.Divide(XY.Y + BigInteger.One, BigInteger.One - XY.Y);
80 BigInteger V = this.modP.Multiply(SqrtMinus486664, this.modP.Divide(U, XY.X));
81
82 if (U.Sign < 0)
83 U += this.p;
84
85 if (V.Sign < 0)
86 V += this.p;
87
88 return new PointOnCurve(U, V);
89 }
90
97 public override BigInteger ScalarMultiplication(byte[] N, BigInteger U)
98 {
99 return XFunction(N, U, A24, this.p, 255);
100 }
101
107 public override Tuple<byte[], byte[]> CalculatePrivateKey(byte[] Secret)
108 {
109 byte[] Bin = Secret;
110
111 if (Bin.Length != 32)
112 Bin = Hashes.ComputeSHA256Hash(Secret);
113
114 Bin[0] &= 0xf8;
115 Bin[31] &= 0x3f;
116 Bin[31] |= 0x40;
117
118 return new Tuple<byte[], byte[]>(Bin, null);
119 }
120
125 public override EdwardsCurveBase CreatePair()
126 {
127 PointOnCurve PublicKeyUV = this.PublicKeyPoint;
128 PointOnCurve PublicKeyXY = this.ToXY(PublicKeyUV);
129
130 Edwards25519 Candidate = new Edwards25519(this.PrivateKey, false);
131 PointOnCurve PublicKeyXY2 = Candidate.PublicKeyPoint;
132
133 if (!PublicKeyXY.Y.Equals(PublicKeyXY2.Y))
134 throw new InvalidOperationException("Unable to create pair curve.");
135
136 return Candidate;
137 }
138
144 public override byte[] Sign(byte[] Data)
145 {
146 throw new NotSupportedException("Signatures not supported.");
147 // return XEdDSA.Sign(Data, this.PrivateKey, Hashes.ComputeSHA512Hash, this);
148 }
149
155 public override byte[] Sign(Stream Data)
156 {
157 throw new NotSupportedException("Signatures not supported.");
158 // return XEdDSA.Sign(Data, this.PrivateKey, Hashes.ComputeSHA512Hash, this);
159 }
160
161 /*/// <summary>
167 public byte[] Sign(byte[] Data, GetRandomBytesHandler GetRandomBytes)
168 {
169 return XEdDSA.Sign(Data, this.PrivateKey, Hashes.ComputeSHA512Hash, this,
170 GetRandomBytes);
171 }*/
172
180 public override bool Verify(byte[] Data, byte[] PublicKey, byte[] Signature)
181 {
182 throw new NotSupportedException("Signatures not supported.");
183 //return XEdDSA.Verify(Data, PublicKey, Hashes.ComputeSHA512Hash, this,
184 // Signature, 255, 253);
185 }
186
194 public override bool Verify(Stream Data, byte[] PublicKey, byte[] Signature)
195 {
196 throw new NotSupportedException("Signatures not supported.");
197 //return XEdDSA.Verify(Data, PublicKey, Hashes.ComputeSHA512Hash, this,
198 // Signature, 255, 253);
199 }
200
201 }
202}
Curve25519, as defined in RFC 7748: https://tools.ietf.org/html/rfc7748
Definition: Curve25519.cs:13
override BigInteger A
a Coefficient in the definition of the curve E: v²=u³+A*u²+u
Definition: Curve25519.cs:49
override byte[] Sign(byte[] Data)
Creates a signature of Data using the XEdDSA algorithm.
Definition: Curve25519.cs:144
override Tuple< byte[], byte[]> CalculatePrivateKey(byte[] Secret)
Calculates a private key from a secret.
Definition: Curve25519.cs:107
override EdwardsCurveBase CreatePair()
Creates the Edwards Curve pair.
Definition: Curve25519.cs:125
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: Curve25519.cs:57
Curve25519(byte[] Secret)
Curve25519, as defined in RFC 7748: https://tools.ietf.org/html/rfc7748
Definition: Curve25519.cs:36
override string CurveName
Name of curve.
Definition: Curve25519.cs:44
override byte[] Sign(Stream Data)
Creates a signature of Data using the XEdDSA algorithm.
Definition: Curve25519.cs:155
override bool Verify(Stream Data, byte[] PublicKey, byte[] Signature)
Verifies a signature of Data made by the EdDSA algorithm.
Definition: Curve25519.cs:194
override BigInteger ScalarMultiplication(byte[] N, BigInteger U)
Performs the scalar multiplication of N *U .
Definition: Curve25519.cs:97
Curve25519()
Curve25519, as defined in RFC 7748: https://tools.ietf.org/html/rfc7748
Definition: Curve25519.cs:26
override PointOnCurve ToUV(PointOnCurve XY)
Converts a pair of (X,Y) coordinates for the birational Edwards curve to a pair of (U,...
Definition: Curve25519.cs:77
override bool Verify(byte[] Data, byte[] PublicKey, byte[] Signature)
Verifies a signature of Data made by the EdDSA algorithm.
Definition: Curve25519.cs:180
Edwards25519 Elliptic Curve, as defined in RFC7748 and RFC8032: https://tools.ietf....
Definition: Edwards25519.cs:14
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.
Integer arithmetic, modulus a prime.
Definition: ModulusP.cs:10
BigInteger Multiply(BigInteger a, BigInteger b)
Multiplies two numbers, modulus p
Definition: ModulusP.cs:80
static BigInteger SqrtModP(BigInteger N, BigInteger p)
Computes sqrt(N) mod p.
Definition: ModulusP.cs:166
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
Contains methods for simple hash calculations.
Definition: Hashes.cs:59
static byte[] ComputeSHA256Hash(byte[] Data)
Computes the SHA-256 hash of a block of binary data.
Definition: Hashes.cs:348
Represents a point on a curve.
Definition: PointOnCurve.cs:11