-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[neox-2.x] add interop services (#1597)
* add interop services: Neo.Cryptography.Keccak256 and Neo.Cryptography.Ecrecover * Add KeyRecover function Unit Test * add wrong key case * fix format * Add keccak256 Hash Function test * Add hexadecimal value check in keccak256 Add recovery id in generate signature * remove unnecessary blank * fix whitespace formatting * fix some problems * clean * refactor code * Remove keccak256 hash algorithm * Remove keccak256 UT * remove unnecessary reference * Remove unused usings * Add exception type Judgement * Add signature validate part * fix format * remove docheck Co-authored-by: 晨 黎 <lichen@neo.org> Co-authored-by: Luchuan <luchuan@neo.org>
- Loading branch information
1 parent
a8756d5
commit 1f7da51
Showing
6 changed files
with
151 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System.Security.Cryptography; | ||
using System.Numerics; | ||
using System; | ||
|
||
namespace Neo.Cryptography.ECC.Tests | ||
{ | ||
[TestClass()] | ||
public class UT_ECDsa | ||
{ | ||
public static byte[] generatekey(int privateKeyLength) | ||
{ | ||
byte[] privateKey = new byte[privateKeyLength]; | ||
using (RandomNumberGenerator rng = RandomNumberGenerator.Create()) | ||
{ | ||
rng.GetBytes(privateKey); | ||
} | ||
return privateKey; | ||
} | ||
|
||
[TestMethod()] | ||
public void KeyRecoverTest() | ||
{ | ||
KeyRecover(ECCurve.Secp256k1); | ||
KeyRecover(ECCurve.Secp256r1); | ||
} | ||
|
||
public static void KeyRecover(ECCurve Curve) | ||
{ | ||
byte[] privateKey = generatekey(32); | ||
ECPoint publicKey = Curve.G * privateKey; | ||
ECDsa ecdsa = new ECDsa(privateKey, Curve); | ||
byte[] message = System.Text.Encoding.Default.GetBytes("HelloWorld"); | ||
BigInteger[] signatures = ecdsa.GenerateSignature(message); | ||
BigInteger r = signatures[0]; | ||
BigInteger s = signatures[1]; | ||
bool v; | ||
if (signatures[2] == 0) | ||
{ | ||
v = true; | ||
} | ||
else | ||
{ | ||
v = false; | ||
} | ||
ECPoint recoverKey = ECDsa.KeyRecover(Curve, r, s, message, v); | ||
Assert.IsTrue(recoverKey.Equals(publicKey)); | ||
//wrong r part | ||
r = new BigInteger(generatekey(32)); | ||
s = new BigInteger(generatekey(32)); | ||
try | ||
{ | ||
recoverKey = ECDsa.KeyRecover(Curve, r, s, message, v); | ||
Assert.IsFalse(recoverKey.Equals(publicKey)); | ||
} | ||
catch (Exception e) | ||
{ | ||
Assert.IsTrue(e.GetType() == typeof(ArithmeticException)); | ||
} | ||
} | ||
} | ||
} |
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