gnark-plonky2-verifier/poseidon/bn254_test.go
poseidon/bn254_test.goRaw
1package poseidon
2
3import (
4 "testing"
5
6 "github.com/consensys/gnark-crypto/ecc"
7 "github.com/consensys/gnark/frontend"
8 "github.com/consensys/gnark/test"
9 gl "github.com/succinctlabs/gnark-plonky2-verifier/goldilocks"
10)
11
12type TestPoseidonBN254Circuit struct {
13 In [BN254_SPONGE_WIDTH]frontend.Variable
14 Out [BN254_SPONGE_WIDTH]frontend.Variable
15}
16
17func (circuit *TestPoseidonBN254Circuit) Define(api frontend.API) error {
18 poseidonChip := NewBN254Chip(api)
19 output := poseidonChip.Poseidon(circuit.In)
20
21 for i := 0; i < BN254_SPONGE_WIDTH; i++ {
22 api.AssertIsEqual(
23 output[i],
24 circuit.Out[i],
25 )
26 }
27
28 return nil
29}
30
31func TestPoseidonBN254(t *testing.T) {
32 assert := test.NewAssert(t)
33
34 testCaseFn := func(in [BN254_SPONGE_WIDTH]frontend.Variable, out [BN254_SPONGE_WIDTH]frontend.Variable) {
35 circuit := TestPoseidonBN254Circuit{In: in, Out: out}
36 witness := TestPoseidonBN254Circuit{In: in, Out: out}
37 err := test.IsSolved(&circuit, &witness, ecc.BN254.ScalarField())
38 assert.NoError(err)
39 }
40
41 testCases := [][2][]string{
42 {
43 {"0", "0", "0", "0"},
44 {
45 "5317387130258456662214331362918410991734007599705406860481038345552731150762",
46 "17768273200467269691696191901389126520069745877826494955630904743826040320364",
47 "19413739268543925182080121099097652227979760828059217876810647045303340666757",
48 "3717738800218482999400886888123026296874264026760636028937972004600663725187",
49 },
50 },
51 {
52 {"0", "1", "2", "3"},
53 {
54 "6542985608222806190361240322586112750744169038454362455181422643027100751666",
55 "3478427836468552423396868478117894008061261013954248157992395910462939736589",
56 "1904980799580062506738911865015687096398867595589699208837816975692422464009",
57 "11971464497515232077059236682405357499403220967704831154657374522418385384151",
58 },
59 },
60 {
61 {
62 "21888242871839275222246405745257275088548364400416034343698204186575808495616",
63 "21888242871839275222246405745257275088548364400416034343698204186575808495616",
64 "21888242871839275222246405745257275088548364400416034343698204186575808495616",
65 "21888242871839275222246405745257275088548364400416034343698204186575808495616",
66 },
67 {
68 "13055670547682322550638362580666986963569035646873545133474324633020685301274",
69 "19087936485076376314486368416882351797015004625427655501762827988254486144933",
70 "10391468779200270580383536396630001155994223659670674913170907401637624483385",
71 "17202557688472898583549180366140168198092766974201433936205272956998081177816",
72 },
73 },
74 {
75 {
76 "6542985608222806190361240322586112750744169038454362455181422643027100751666",
77 "3478427836468552423396868478117894008061261013954248157992395910462939736589",
78 "1904980799580062506738911865015687096398867595589699208837816975692422464009",
79 "11971464497515232077059236682405357499403220967704831154657374522418385384151",
80 },
81 {
82 "21792249080447013894140672594027696524030291802493510986509431008224624594361",
83 "3536096706123550619294332177231935214243656967137545251021848527424156573335",
84 "14869351042206255711434675256184369368509719143073814271302931417334356905217",
85 "5027523131326906886284185656868809493297314443444919363729302983434650240523",
86 },
87 },
88 }
89
90 for _, testCase := range testCases {
91 var in [BN254_SPONGE_WIDTH]frontend.Variable
92 var out [BN254_SPONGE_WIDTH]frontend.Variable
93 copy(in[:], gl.StrArrayToFrontendVariableArray(testCase[0]))
94 copy(out[:], gl.StrArrayToFrontendVariableArray(testCase[1]))
95 testCaseFn(in, out)
96 }
97}