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