Skip to content

Commit f63fb1f

Browse files
testing(poseidon): adds tests for the poseidon2 wrapper (#300)
Signed-off-by: AlexandreBelling <alexandrebelling8@gmail.com>
1 parent 8a4e741 commit f63fb1f

File tree

5 files changed

+43
-26
lines changed

5 files changed

+43
-26
lines changed

gnark/gnark-jni/gnark-jni.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func computeMimcBls12377(input *C.char, inputLength C.int, output *C.char) C.int
6060
return C.int(len(hash))
6161
}
6262

63-
// export computePoseidon2Koalabear
63+
//export computePoseidon2Koalabear
6464
func computePoseidon2Koalabear(input *C.char, inputLength C.int, output *C.char) C.int {
6565
inputSlice := C.GoBytes(unsafe.Pointer(input), inputLength)
6666
outputSlice := (*[32]byte)(unsafe.Pointer(output))[:]

gnark/gnark-jni/go.mod

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@ go 1.25
44

55
toolchain go1.25.4
66

7-
require github.com/consensys/gnark-crypto v0.17.0
7+
require github.com/consensys/gnark-crypto v0.19.1-0.20250919185810-d7ecdb060877
88

99
require (
10-
github.com/bits-and-blooms/bitset v1.20.0 // indirect
11-
github.com/consensys/bavard v0.1.29 // indirect
12-
github.com/mmcloughlin/addchain v0.4.0 // indirect
13-
golang.org/x/crypto v0.33.0 // indirect
14-
golang.org/x/sys v0.30.0 // indirect
15-
rsc.io/tmplfunc v0.0.3 // indirect
10+
github.com/bits-and-blooms/bitset v1.24.0 // indirect
11+
golang.org/x/crypto v0.43.0 // indirect
12+
golang.org/x/sys v0.37.0 // indirect
1613
)

gnark/gnark-jni/go.sum

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
1-
github.com/bits-and-blooms/bitset v1.20.0 h1:2F+rfL86jE2d/bmw7OhqUg2Sj/1rURkBn3MdfoPyRVU=
2-
github.com/bits-and-blooms/bitset v1.20.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
3-
github.com/consensys/bavard v0.1.29 h1:fobxIYksIQ+ZSrTJUuQgu+HIJwclrAPcdXqd7H2hh1k=
4-
github.com/consensys/bavard v0.1.29/go.mod h1:k/zVjHHC4B+PQy1Pg7fgvG3ALicQw540Crag8qx+dZs=
5-
github.com/consensys/gnark-crypto v0.17.0 h1:vKDhZMOrySbpZDCvGMOELrHFv/A9mJ7+9I8HEfRZSkI=
6-
github.com/consensys/gnark-crypto v0.17.0/go.mod h1:A2URlMHUT81ifJ0UlLzSlm7TmnE3t7VxEThApdMukJw=
1+
github.com/bits-and-blooms/bitset v1.24.0 h1:H4x4TuulnokZKvHLfzVRTHJfFfnHEeSYJizujEZvmAM=
2+
github.com/bits-and-blooms/bitset v1.24.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
3+
github.com/consensys/gnark-crypto v0.19.1-0.20250919185810-d7ecdb060877 h1:4xq7C8vx7G5T1FFA+PwCcNrVDLMCAT5qFAsAL9GBs7Y=
4+
github.com/consensys/gnark-crypto v0.19.1-0.20250919185810-d7ecdb060877/go.mod h1:OgCH7cSoJ46c+nOzvQuwOrIE9fawpXMYOQFzj22Vy3E=
75
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
86
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
9-
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
107
github.com/leanovate/gopter v0.2.11 h1:vRjThO1EKPb/1NsDXuDrzldR28RLkBflWYcU9CvzWu4=
118
github.com/leanovate/gopter v0.2.11/go.mod h1:aK3tzZP/C+p1m3SPRE4SYZFGP7jjkuSI4f7Xvpt0S9c=
12-
github.com/mmcloughlin/addchain v0.4.0 h1:SobOdjm2xLj1KkXN5/n0xTIWyZA2+s99UCY1iPfkHRY=
13-
github.com/mmcloughlin/addchain v0.4.0/go.mod h1:A86O+tHqZLMNO4w6ZZ4FlVQEadcoqkyU72HC5wJ4RlU=
14-
github.com/mmcloughlin/profile v0.1.1/go.mod h1:IhHD7q1ooxgwTgjxQYkACGA77oFTDdFVejUS1/tS/qU=
159
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1610
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
17-
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
18-
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
19-
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
20-
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
21-
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
22-
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
11+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
12+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
13+
golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04=
14+
golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0=
15+
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
16+
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
2317
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
2418
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
2519
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
2620
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
27-
rsc.io/tmplfunc v0.0.3 h1:53XFQh69AfOa8Tw0Jm7t+GV7KZhOi6jzsCzTtKbMvzU=
28-
rsc.io/tmplfunc v0.0.3/go.mod h1:AG3sTPzElb1Io3Yg4voV9AGZJuleGAwaVRxL9M49PhA=

gnark/src/main/java/org/hyperledger/besu/nativelib/gnark/LibGnark.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@ public static native int computeMimcBn254(
4343
public static native int computeMimcBls12377(
4444
byte[] i, int i_len, byte[] o);
4545

46+
public static native int computePoseidon2Koalabear(
47+
byte[] i, int i_len, byte[] o);
48+
4649
}

gnark/src/test/java/org/hyperledger/besu/nativelib/gnark/LibGnarkTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,30 @@ public void testLongStringBls12377() {
7474
assertThat(Bytes.wrap(output)).isEqualTo(Bytes.fromHexString("0x12900ae41a010e54e3b1ed95efa39071d357ff642aeedd30a2c4e13250409662"));
7575
}
7676

77+
@Test
78+
public void testHashZeroKoalabear() {
79+
byte[] output = new byte[Bytes32.SIZE];
80+
LibGnark.computePoseidon2Koalabear(Bytes32.ZERO.toArrayUnsafe(), Bytes32.SIZE, output);
81+
assertThat(Bytes.wrap(output)).isEqualTo(Bytes.fromHexString("0x0656ab853b3f52840362a8177e217b630c3f876b11e848365145aa24220647fc"));
82+
}
83+
84+
@Test
85+
public void testHashOneKoalabear() {
86+
byte[] output = new byte[Bytes32.SIZE];
87+
LibGnark.computePoseidon2Koalabear(Bytes32.leftPad(Bytes.of(1)).toArrayUnsafe(), Bytes32.SIZE, output);
88+
assertThat(Bytes.wrap(output)).isEqualTo(Bytes.fromHexString("0x532d760a239087f458d23ee549db4a5771815a387616ec5f31be90fd690886a5"));
89+
}
90+
91+
@Test
92+
public void testLongStringKoalabear() {
93+
MutableBytes input = MutableBytes.of(new byte[Bytes32.SIZE*16]);
94+
for (int i = 0; i < 16; i++) {
95+
input.set(Bytes32.SIZE*(i+1)-1,(byte) i);
96+
}
97+
byte[] output = new byte[Bytes32.SIZE];
98+
LibGnark.computePoseidon2Koalabear(input.toArrayUnsafe(), input.size(), output);
99+
assertThat(Bytes.wrap(output)).isEqualTo(Bytes.fromHexString("0x254c857251520cbd40981dd74c2b3ee345acf16978e701324181926236278aaa"));
100+
}
101+
77102

78103
}

0 commit comments

Comments
 (0)