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), A0, n0, 8)
28 {
29 }
30
36 public Curve25519(byte[] Secret)
37 : base(p0, new PointOnCurve(BasePointU, BasePointV), A0, n0, 8, Secret)
38 {
39 }
40
44 public override string CurveName => "Curve25519";
45
52 public override PointOnCurve ToXY(PointOnCurve UV)
53 {
54 BigInteger X = this.modP.Multiply(SqrtMinus486664, this.modP.Divide(UV.X, UV.Y));
55 BigInteger Y = this.modP.Divide(UV.X - BigInteger.One, UV.X + BigInteger.One);
56
57 if (X.Sign < 0)
58 X += this.p;
59
60 if (Y.Sign < 0)
61 Y += this.p;
62
63 return new PointOnCurve(X, Y);
64 }
65
72 public override PointOnCurve ToUV(PointOnCurve XY)
73 {
74 BigInteger U = this.modP.Divide(XY.Y + BigInteger.One, BigInteger.One - XY.Y);
75 BigInteger V = this.modP.Multiply(SqrtMinus486664, this.modP.Divide(U, XY.X));
76
77 if (U.Sign < 0)
78 U += this.p;
79
80 if (V.Sign < 0)
81 V += this.p;
82
83 return new PointOnCurve(U, V);
84 }
85
92 public override BigInteger ScalarMultiplication(byte[] N, BigInteger U)
93 {
94 return XFunction(N, U, A24, this.p, 255);
95 }
96
102 public override Tuple<byte[], byte[]> CalculatePrivateKey(byte[] Secret)
103 {
104 byte[] Bin = Secret;
105
106 if (Bin.Length != 32)
107 Bin = Hashes.ComputeSHA256Hash(Secret);
108
109 Bin[0] &= 0xf8;
110 Bin[31] &= 0x3f;
111 Bin[31] |= 0x40;
112
113 return new Tuple<byte[], byte[]>(Bin, null);
114 }
115
120 public override EdwardsCurveBase CreatePair()
121 {
122 PointOnCurve PublicKeyUV = this.PublicKeyPoint;
123 PointOnCurve PublicKeyXY = this.ToXY(PublicKeyUV);
124
125 Edwards25519 Candidate = new Edwards25519(this.PrivateKey, false);
126 PointOnCurve PublicKeyXY2 = Candidate.PublicKeyPoint;
127
128 if (!PublicKeyXY.Y.Equals(PublicKeyXY2.Y))
129 throw new InvalidOperationException("Unable to create pair curve.");
130
131 return Candidate;
132 }
133
140 public override byte[] Sign(byte[] Data, bool BigEndian)
141 {
142 throw new NotSupportedException("Signatures not supported.");
143 // return XEdDSA.Sign(Data, this.PrivateKey, Hashes.ComputeSHA512Hash, this);
144 }
145
152 public override byte[] Sign(Stream Data, bool BigEndian)
153 {
154 throw new NotSupportedException("Signatures not supported.");
155 // return XEdDSA.Sign(Data, this.PrivateKey, Hashes.ComputeSHA512Hash, this);
156 }
157
158 /*/// <summary>
164 public byte[] Sign(byte[] Data, GetRandomBytesHandler GetRandomBytes)
165 {
166 return XEdDSA.Sign(Data, this.PrivateKey, Hashes.ComputeSHA512Hash, this,
167 GetRandomBytes);
168 }*/
169
178 public override bool Verify(byte[] Data, byte[] PublicKey, bool BigEndian, byte[] Signature)
179 {
180 throw new NotSupportedException("Signatures not supported.");
181 //return XEdDSA.Verify(Data, PublicKey, Hashes.ComputeSHA512Hash, this,
182 // Signature, 255, 253);
183 }
184
193 public override bool Verify(Stream Data, byte[] PublicKey, bool BigEndian, byte[] Signature)
194 {
195 throw new NotSupportedException("Signatures not supported.");
196 //return XEdDSA.Verify(Data, PublicKey, Hashes.ComputeSHA512Hash, this,
197 // Signature, 255, 253);
198 }
199
200 }
201}
Curve25519, as defined in RFC 7748: https://tools.ietf.org/html/rfc7748
Definition: Curve25519.cs:13
override bool Verify(byte[] Data, byte[] PublicKey, bool BigEndian, byte[] Signature)
Verifies a signature of Data made by the EdDSA algorithm.
Definition: Curve25519.cs:178
override byte[] Sign(Stream Data, bool BigEndian)
Creates a signature of Data using the XEdDSA algorithm.
Definition: Curve25519.cs:152
override bool Verify(Stream Data, byte[] PublicKey, bool BigEndian, byte[] Signature)
Verifies a signature of Data made by the EdDSA algorithm.
Definition: Curve25519.cs:193
override byte[] Sign(byte[] Data, bool BigEndian)
Creates a signature of Data using the XEdDSA algorithm.
Definition: Curve25519.cs:140
override Tuple< byte[], byte[]> CalculatePrivateKey(byte[] Secret)
Calculates a private key from a secret.
Definition: Curve25519.cs:102
override EdwardsCurveBase CreatePair()
Creates the Edwards Curve pair.
Definition: Curve25519.cs:120
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:52
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 BigInteger ScalarMultiplication(byte[] N, BigInteger U)
Performs the scalar multiplication of N *U .
Definition: Curve25519.cs:92
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:72
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 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
Contains methods for simple hash calculations.
Definition: Hashes.cs:57
static byte[] ComputeSHA256Hash(byte[] Data)
Computes the SHA-256 hash of a block of binary data.
Definition: Hashes.cs:469
Represents a point on a curve.
Definition: PointOnCurve.cs:10