Skip to content

Commit 7b1eb9b

Browse files
committed
Add unit test
1 parent a2e3579 commit 7b1eb9b

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using AElf.Cryptography.Bn254;
2+
using AElf.Types;
3+
using Bn254.Net;
4+
using Shouldly;
5+
using Xunit;
6+
using NetBn254 = Bn254.Net;
7+
8+
namespace AElf.Cryptography.Tests;
9+
10+
public class EdDsaHelperTest
11+
{
12+
public static byte[] ToBytes32(BigIntValue value)
13+
{
14+
var bytes = value.ToBigEndianBytes();
15+
var newArray = new byte[32];
16+
for (int i = 0; i < bytes.Length; i++)
17+
{
18+
newArray[31 - i] = bytes[bytes.Length - 1 - i];
19+
}
20+
21+
return newArray;
22+
}
23+
24+
[Fact]
25+
public void Bn254G1Mul_Test()
26+
{
27+
// Arrange
28+
byte[] x1 = ToBytes32(new BigIntValue(0));
29+
byte[] y1 = ToBytes32(new BigIntValue(0));
30+
byte[] scalar = ToBytes32(new BigIntValue(0));
31+
32+
// use raw api to compute result
33+
var (expectedXuInt256, expectedYuInt256) = NetBn254.Bn254.Mul(
34+
UInt256.FromBigEndianBytes(x1),
35+
UInt256.FromBigEndianBytes(y1),
36+
UInt256.FromBigEndianBytes(scalar)
37+
);
38+
var expectedX = expectedXuInt256.ToBigEndianBytes();
39+
var expectedY = expectedYuInt256.ToBigEndianBytes();
40+
41+
// Act
42+
var (xResult, yResult) = Bn254Helper.Bn254G1Mul(x1, y1, scalar);
43+
44+
// Assert
45+
xResult.ShouldBe(expectedX);
46+
yResult.ShouldBe(expectedY);
47+
}
48+
49+
[Fact]
50+
public void Bn254G1Add_Test()
51+
{
52+
// Arrange
53+
byte[] x1 = ToBytes32(new BigIntValue(0));
54+
byte[] y1 = ToBytes32(new BigIntValue(0));
55+
byte[] x2 = ToBytes32(new BigIntValue(0));
56+
byte[] y2 = ToBytes32(new BigIntValue(0));
57+
58+
// Use raw API to compute expected results
59+
var (expectedX3UInt256, expectedY3UInt256) = NetBn254.Bn254.Add(
60+
UInt256.FromBigEndianBytes(x1),
61+
UInt256.FromBigEndianBytes(y1),
62+
UInt256.FromBigEndianBytes(x2),
63+
UInt256.FromBigEndianBytes(y2)
64+
);
65+
var expectedX3 = expectedX3UInt256.ToBigEndianBytes();
66+
var expectedY3 = expectedY3UInt256.ToBigEndianBytes();
67+
68+
// Act
69+
var (x3Result, y3Result) = Bn254Helper.Bn254G1Add(x1, y1, x2, y2);
70+
71+
// Assert
72+
x3Result.ShouldBe(expectedX3);
73+
y3Result.ShouldBe(expectedY3);
74+
}
75+
76+
[Fact]
77+
public void Bn254Pairing_Test()
78+
{
79+
// Arrange
80+
var input = new (byte[], byte[], byte[], byte[], byte[], byte[])[]
81+
{
82+
(
83+
ToBytes32(new BigIntValue(0)),
84+
ToBytes32(new BigIntValue(0)),
85+
ToBytes32(new BigIntValue(0)),
86+
ToBytes32(new BigIntValue(0)),
87+
ToBytes32(new BigIntValue(0)),
88+
ToBytes32(new BigIntValue(0))
89+
)
90+
};
91+
92+
// Use raw API to compute expected results
93+
bool expected = NetBn254.Bn254.Pairing(new (UInt256, UInt256, UInt256, UInt256, UInt256, UInt256)[]
94+
{
95+
(
96+
UInt256.FromBigEndianBytes(input[0].Item1),
97+
UInt256.FromBigEndianBytes(input[0].Item2),
98+
UInt256.FromBigEndianBytes(input[0].Item3),
99+
UInt256.FromBigEndianBytes(input[0].Item4),
100+
UInt256.FromBigEndianBytes(input[0].Item5),
101+
UInt256.FromBigEndianBytes(input[0].Item6)
102+
)
103+
});
104+
105+
// Act
106+
bool result = Bn254Helper.Bn254Pairing(input);
107+
108+
// Assert
109+
result.ShouldBe(expected);
110+
}
111+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using AElf.Cryptography.EdDSA;
2+
using Shouldly;
3+
using Xunit;
4+
5+
namespace AElf.Cryptography.Tests;
6+
7+
public class Bn254HelperTest
8+
{
9+
[Fact]
10+
public void Ed25519Verify_Test()
11+
{
12+
var publicKey = "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a";
13+
var message = "";
14+
var signature =
15+
"e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b";
16+
17+
var publicKeyBytes = ByteArrayHelper.HexStringToByteArray(publicKey);
18+
var messageBytes = ByteArrayHelper.HexStringToByteArray(message);
19+
var signatureBytes = ByteArrayHelper.HexStringToByteArray(signature);
20+
21+
var ed25519VerifyResult = EdDsaHelper.Ed25519Verify(signatureBytes, messageBytes, publicKeyBytes);
22+
23+
ed25519VerifyResult.ShouldBe(true);
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Text;
2+
using AElf.Cryptography.Keccak;
3+
using Nethereum.Util;
4+
using Shouldly;
5+
using Xunit;
6+
7+
namespace AElf.Cryptography.Tests;
8+
9+
public class KeccakHelperTest
10+
{
11+
[Fact]
12+
public void Keccak256_Test()
13+
{
14+
byte[] message = Encoding.UTF8.GetBytes("Test message");
15+
16+
var expectedHash = Sha3Keccack.Current.CalculateHash(message);
17+
18+
var computedHash = KeccakHelper.Keccak256(message);
19+
20+
computedHash.ShouldBe(expectedHash);
21+
}
22+
}

0 commit comments

Comments
 (0)