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