arnaucube
arnaucube/gnark-plonky2-verifierPublic Self-hosted repository
gnark-plonky2-verifier/plonky2_verifier/challenger_test.go
plonky2_verifier/challenger_test.goRaw
  1package plonky2_verifier
  2
  3import (
  4	. "gnark-ed25519/goldilocks"
  5	. "gnark-ed25519/poseidon"
  6	"gnark-ed25519/utils"
  7	"testing"
  8
  9	"github.com/consensys/gnark-crypto/ecc"
 10	"github.com/consensys/gnark/frontend"
 11	"github.com/consensys/gnark/test"
 12)
 13
 14var testCurve = ecc.BN254
 15
 16type TestChallengerCircuit struct {
 17	PublicInputs  [3]frontend.Variable
 18	CircuitDigest [4]frontend.Variable
 19	WiresCap      [16][4]frontend.Variable
 20}
 21
 22func (circuit *TestChallengerCircuit) Define(api frontend.API) error {
 23	goldilocksApi := NewGoldilocksAPI(api)
 24	poseidonChip := NewPoseidonChip(api, goldilocksApi)
 25	challengerChip := NewChallengerChip(api, goldilocksApi, *poseidonChip)
 26
 27	var circuitDigestGoldilocks [4]GoldilocksElement
 28	for i := 0; i < 4; i++ {
 29		circuitDigestGoldilocks[i] = goldilocksApi.FromBinary(api.ToBinary(circuit.CircuitDigest[i], 64)).(GoldilocksElement)
 30	}
 31
 32	var publicInputsGoldilocks [3]GoldilocksElement
 33	for i := 0; i < 3; i++ {
 34		publicInputsGoldilocks[i] = goldilocksApi.FromBinary(api.ToBinary(circuit.PublicInputs[i], 64)).(GoldilocksElement)
 35	}
 36
 37	var wiresCapGoldilocks [16][4]GoldilocksElement
 38	for i := 0; i < 16; i++ {
 39		for j := 0; j < 4; j++ {
 40			wiresCapGoldilocks[i][j] = goldilocksApi.FromBinary(api.ToBinary(circuit.WiresCap[i][j], 64)).(GoldilocksElement)
 41		}
 42	}
 43
 44	publicInputHash := poseidonChip.HashNoPad(publicInputsGoldilocks[:])
 45	challengerChip.ObserveHash(circuitDigestGoldilocks)
 46	challengerChip.ObserveHash(publicInputHash)
 47	challengerChip.ObserveCap(wiresCapGoldilocks[:])
 48
 49	nbChallenges := 2
 50	plonkBetas := challengerChip.GetNChallenges(nbChallenges)
 51	plonkGammas := challengerChip.GetNChallenges(nbChallenges)
 52
 53	var expectedPlonkBetas [2]frontend.Variable
 54	expectedPlonkBetas[0] = frontend.Variable("4678728155650926271")
 55	expectedPlonkBetas[1] = frontend.Variable("13611962404289024887")
 56
 57	var expectedPlonkGammas [2]frontend.Variable
 58	expectedPlonkGammas[0] = frontend.Variable("13237663823305715949")
 59	expectedPlonkGammas[1] = frontend.Variable("15389314098328235145")
 60
 61	for i := 0; i < 2; i++ {
 62		goldilocksApi.AssertIsEqual(
 63			plonkBetas[i],
 64			goldilocksApi.FromBinary(api.ToBinary(expectedPlonkBetas[i])).(GoldilocksElement),
 65		)
 66		goldilocksApi.AssertIsEqual(
 67			plonkGammas[i],
 68			goldilocksApi.FromBinary(api.ToBinary(expectedPlonkGammas[i])).(GoldilocksElement),
 69		)
 70	}
 71
 72	return nil
 73}
 74
 75func TestChallengerWitness(t *testing.T) {
 76	assert := test.NewAssert(t)
 77
 78	testCase := func(publicInputs [3]frontend.Variable, circuitDigest [4]frontend.Variable, wiresCap [16][4]frontend.Variable) {
 79		circuit := TestChallengerCircuit{PublicInputs: publicInputs, CircuitDigest: circuitDigest, WiresCap: wiresCap}
 80		witness := TestChallengerCircuit{PublicInputs: publicInputs, CircuitDigest: circuitDigest, WiresCap: wiresCap}
 81		err := test.IsSolved(&circuit, &witness, testCurve.ScalarField())
 82		assert.NoError(err)
 83	}
 84
 85	publicInputsStr := []string{"0", "1", "3736710860384812976"}
 86	circuitDigestStr := []string{"7754113318730736048", "18436136620016916513", "18054530212389526288", "5893739326632906028"}
 87	wiresCapStr := [][]string{
 88		{"13884351014873073118", "5174249846243191862", "2208632528791973868", "1071582828677910652"},
 89		{"11475361245556894879", "14867351574926692044", "17013374066934071379", "1027671036932569748"},
 90		{"5604634992452399010", "3684464596850094189", "5565599237356852406", "4136295609943151014"},
 91		{"8463721840990025805", "5922588965472526198", "8096699027533803435", "2210089353004111478"},
 92		{"17531628199677307555", "11513452064460680964", "1482441508929181375", "5139566233781982440"},
 93		{"13271417993289093233", "17257193898955790413", "16883807866578566670", "7423179920948669117"},
 94		{"13462567520785358202", "15555103598281658890", "5859961276885232601", "4464568704709749394"},
 95		{"153012620162729043", "14072764618167122665", "3025694603779494447", "15948104906680148838"},
 96		{"18050235253694287284", "11467396424826912141", "11302553396166323353", "10976271719722841224"},
 97		{"15208241660644051470", "8520722208187871063", "10775022596056682771", "16048513824198271730"},
 98		{"6929477084755896240", "11382029470138215117", "13205948643259905511", "9421863267852221772"},
 99		{"15449187573546292268", "10216729601353604194", "9493934392442974211", "9848643714440191835"},
100		{"2172475758127444753", "16681095938683502188", "9983383760611275566", "2603547977557388755"},
101		{"17440301588003279095", "11799356585691460705", "1386003375936412946", "11059100806278290279"},
102		{"10758265002546797581", "1374136260999724547", "7200401521491969338", "219493657547391496"},
103		{"5995963332181008902", "4442996285152250372", "2005936434281221193", "6869325719052666642"},
104	}
105
106	var publicInputs [3]frontend.Variable
107	var circuitDigest [4]frontend.Variable
108	var wiresCap [16][4]frontend.Variable
109
110	copy(publicInputs[:], utils.StrArrayToFrontendVariableArray(publicInputsStr))
111	copy(circuitDigest[:], utils.StrArrayToFrontendVariableArray(circuitDigestStr))
112	for i := 0; i < len(wiresCapStr); i++ {
113		copy(wiresCap[i][:], utils.StrArrayToFrontendVariableArray(wiresCapStr[i]))
114	}
115
116	testCase(publicInputs, circuitDigest, wiresCap)
117}