-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2e3579
commit 7b1eb9b
Showing
3 changed files
with
158 additions
and
0 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,111 @@ | ||
using AElf.Cryptography.Bn254; | ||
using AElf.Types; | ||
using Bn254.Net; | ||
using Shouldly; | ||
using Xunit; | ||
using NetBn254 = Bn254.Net; | ||
|
||
namespace AElf.Cryptography.Tests; | ||
|
||
public class EdDsaHelperTest | ||
{ | ||
public static byte[] ToBytes32(BigIntValue value) | ||
{ | ||
var bytes = value.ToBigEndianBytes(); | ||
var newArray = new byte[32]; | ||
for (int i = 0; i < bytes.Length; i++) | ||
{ | ||
newArray[31 - i] = bytes[bytes.Length - 1 - i]; | ||
} | ||
|
||
return newArray; | ||
} | ||
|
||
[Fact] | ||
public void Bn254G1Mul_Test() | ||
{ | ||
// Arrange | ||
byte[] x1 = ToBytes32(new BigIntValue(0)); | ||
byte[] y1 = ToBytes32(new BigIntValue(0)); | ||
byte[] scalar = ToBytes32(new BigIntValue(0)); | ||
|
||
// use raw api to compute result | ||
var (expectedXuInt256, expectedYuInt256) = NetBn254.Bn254.Mul( | ||
UInt256.FromBigEndianBytes(x1), | ||
UInt256.FromBigEndianBytes(y1), | ||
UInt256.FromBigEndianBytes(scalar) | ||
); | ||
var expectedX = expectedXuInt256.ToBigEndianBytes(); | ||
var expectedY = expectedYuInt256.ToBigEndianBytes(); | ||
|
||
// Act | ||
var (xResult, yResult) = Bn254Helper.Bn254G1Mul(x1, y1, scalar); | ||
|
||
// Assert | ||
xResult.ShouldBe(expectedX); | ||
yResult.ShouldBe(expectedY); | ||
} | ||
|
||
[Fact] | ||
public void Bn254G1Add_Test() | ||
{ | ||
// Arrange | ||
byte[] x1 = ToBytes32(new BigIntValue(0)); | ||
byte[] y1 = ToBytes32(new BigIntValue(0)); | ||
byte[] x2 = ToBytes32(new BigIntValue(0)); | ||
byte[] y2 = ToBytes32(new BigIntValue(0)); | ||
|
||
// Use raw API to compute expected results | ||
var (expectedX3UInt256, expectedY3UInt256) = NetBn254.Bn254.Add( | ||
UInt256.FromBigEndianBytes(x1), | ||
UInt256.FromBigEndianBytes(y1), | ||
UInt256.FromBigEndianBytes(x2), | ||
UInt256.FromBigEndianBytes(y2) | ||
); | ||
var expectedX3 = expectedX3UInt256.ToBigEndianBytes(); | ||
var expectedY3 = expectedY3UInt256.ToBigEndianBytes(); | ||
|
||
// Act | ||
var (x3Result, y3Result) = Bn254Helper.Bn254G1Add(x1, y1, x2, y2); | ||
|
||
// Assert | ||
x3Result.ShouldBe(expectedX3); | ||
y3Result.ShouldBe(expectedY3); | ||
} | ||
|
||
[Fact] | ||
public void Bn254Pairing_Test() | ||
{ | ||
// Arrange | ||
var input = new (byte[], byte[], byte[], byte[], byte[], byte[])[] | ||
{ | ||
( | ||
ToBytes32(new BigIntValue(0)), | ||
ToBytes32(new BigIntValue(0)), | ||
ToBytes32(new BigIntValue(0)), | ||
ToBytes32(new BigIntValue(0)), | ||
ToBytes32(new BigIntValue(0)), | ||
ToBytes32(new BigIntValue(0)) | ||
) | ||
}; | ||
|
||
// Use raw API to compute expected results | ||
bool expected = NetBn254.Bn254.Pairing(new (UInt256, UInt256, UInt256, UInt256, UInt256, UInt256)[] | ||
{ | ||
( | ||
UInt256.FromBigEndianBytes(input[0].Item1), | ||
UInt256.FromBigEndianBytes(input[0].Item2), | ||
UInt256.FromBigEndianBytes(input[0].Item3), | ||
UInt256.FromBigEndianBytes(input[0].Item4), | ||
UInt256.FromBigEndianBytes(input[0].Item5), | ||
UInt256.FromBigEndianBytes(input[0].Item6) | ||
) | ||
}); | ||
|
||
// Act | ||
bool result = Bn254Helper.Bn254Pairing(input); | ||
|
||
// Assert | ||
result.ShouldBe(expected); | ||
} | ||
} |
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,25 @@ | ||
using AElf.Cryptography.EdDSA; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace AElf.Cryptography.Tests; | ||
|
||
public class Bn254HelperTest | ||
{ | ||
[Fact] | ||
public void Ed25519Verify_Test() | ||
{ | ||
var publicKey = "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a"; | ||
var message = ""; | ||
var signature = | ||
"e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b"; | ||
|
||
var publicKeyBytes = ByteArrayHelper.HexStringToByteArray(publicKey); | ||
var messageBytes = ByteArrayHelper.HexStringToByteArray(message); | ||
var signatureBytes = ByteArrayHelper.HexStringToByteArray(signature); | ||
|
||
var ed25519VerifyResult = EdDsaHelper.Ed25519Verify(signatureBytes, messageBytes, publicKeyBytes); | ||
|
||
ed25519VerifyResult.ShouldBe(true); | ||
} | ||
} |
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,22 @@ | ||
using System.Text; | ||
using AElf.Cryptography.Keccak; | ||
using Nethereum.Util; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace AElf.Cryptography.Tests; | ||
|
||
public class KeccakHelperTest | ||
{ | ||
[Fact] | ||
public void Keccak256_Test() | ||
{ | ||
byte[] message = Encoding.UTF8.GetBytes("Test message"); | ||
|
||
var expectedHash = Sha3Keccack.Current.CalculateHash(message); | ||
|
||
var computedHash = KeccakHelper.Keccak256(message); | ||
|
||
computedHash.ShouldBe(expectedHash); | ||
} | ||
} |