forked from neo-project/neo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add standard cryptography primitives (neo-project#1419)
- Loading branch information
Showing
16 changed files
with
283 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using System; | ||
using System.Numerics; | ||
using System.Security.Cryptography; | ||
|
||
namespace Neo.Cryptography.ECC | ||
{ | ||
public class ECDsa | ||
{ | ||
private readonly byte[] privateKey; | ||
private readonly ECPoint publicKey; | ||
private readonly ECCurve curve; | ||
|
||
public ECDsa(byte[] privateKey, ECCurve curve) | ||
: this(curve.G * privateKey) | ||
{ | ||
this.privateKey = privateKey; | ||
} | ||
|
||
public ECDsa(ECPoint publicKey) | ||
{ | ||
this.publicKey = publicKey; | ||
this.curve = publicKey.Curve; | ||
} | ||
|
||
private BigInteger CalculateE(BigInteger n, ReadOnlySpan<byte> message) | ||
{ | ||
int messageBitLength = message.Length * 8; | ||
BigInteger trunc = new BigInteger(message, isUnsigned: true, isBigEndian: true); | ||
if (n.GetBitLength() < messageBitLength) | ||
{ | ||
trunc >>= messageBitLength - n.GetBitLength(); | ||
} | ||
return trunc; | ||
} | ||
|
||
public BigInteger[] GenerateSignature(ReadOnlySpan<byte> message) | ||
{ | ||
if (privateKey == null) throw new InvalidOperationException(); | ||
BigInteger e = CalculateE(curve.N, message); | ||
BigInteger d = new BigInteger(privateKey, isUnsigned: true, isBigEndian: true); | ||
BigInteger r, s; | ||
using (RandomNumberGenerator rng = RandomNumberGenerator.Create()) | ||
{ | ||
do | ||
{ | ||
BigInteger k; | ||
do | ||
{ | ||
do | ||
{ | ||
k = rng.NextBigInteger(curve.N.GetBitLength()); | ||
} | ||
while (k.Sign == 0 || k.CompareTo(curve.N) >= 0); | ||
ECPoint p = ECPoint.Multiply(curve.G, k); | ||
BigInteger x = p.X.Value; | ||
r = x.Mod(curve.N); | ||
} | ||
while (r.Sign == 0); | ||
s = (k.ModInverse(curve.N) * (e + d * r)).Mod(curve.N); | ||
if (s > curve.N / 2) | ||
{ | ||
s = curve.N - s; | ||
} | ||
} | ||
while (s.Sign == 0); | ||
} | ||
return new BigInteger[] { r, s }; | ||
} | ||
|
||
private static ECPoint SumOfTwoMultiplies(ECPoint P, BigInteger k, ECPoint Q, BigInteger l) | ||
{ | ||
int m = Math.Max(k.GetBitLength(), l.GetBitLength()); | ||
ECPoint Z = P + Q; | ||
ECPoint R = P.Curve.Infinity; | ||
for (int i = m - 1; i >= 0; --i) | ||
{ | ||
R = R.Twice(); | ||
if (k.TestBit(i)) | ||
{ | ||
if (l.TestBit(i)) | ||
R = R + Z; | ||
else | ||
R = R + P; | ||
} | ||
else | ||
{ | ||
if (l.TestBit(i)) | ||
R = R + Q; | ||
} | ||
} | ||
return R; | ||
} | ||
|
||
public bool VerifySignature(ReadOnlySpan<byte> message, BigInteger r, BigInteger s) | ||
{ | ||
if (r.Sign < 1 || s.Sign < 1 || r.CompareTo(curve.N) >= 0 || s.CompareTo(curve.N) >= 0) | ||
return false; | ||
BigInteger e = CalculateE(curve.N, message); | ||
BigInteger c = s.ModInverse(curve.N); | ||
BigInteger u1 = (e * c).Mod(curve.N); | ||
BigInteger u2 = (r * c).Mod(curve.N); | ||
ECPoint point = SumOfTwoMultiplies(curve.G, u1, publicKey, u2); | ||
BigInteger v = point.X.Value.Mod(curve.N); | ||
return v.Equals(r); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.