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+ }
0 commit comments