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