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-plonky2-verifier/field"
  5	. "gnark-plonky2-verifier/field"
  6	. "gnark-plonky2-verifier/poseidon"
  7	"gnark-plonky2-verifier/utils"
  8	"testing"
  9
 10	"github.com/consensys/gnark/frontend"
 11	"github.com/consensys/gnark/test"
 12)
 13
 14type TestChallengerCircuit struct {
 15	PublicInputs              [3]frontend.Variable
 16	CircuitDigest             [4]frontend.Variable
 17	WiresCap                  [16][4]frontend.Variable
 18	PlonkZsPartialProductsCap [16][4]frontend.Variable
 19	QuotientPolysCap          [16][4]frontend.Variable
 20}
 21
 22func (circuit *TestChallengerCircuit) Define(api frontend.API) error {
 23	fieldAPI := field.NewFieldAPI(api)
 24	degreeBits := 3
 25	qeAPI := NewQuadraticExtensionAPI(fieldAPI, uint64(degreeBits))
 26	poseidonChip := NewPoseidonChip(api, fieldAPI, qeAPI)
 27	challengerChip := NewChallengerChip(api, fieldAPI, poseidonChip)
 28
 29	var circuitDigest [4]F
 30	for i := 0; i < len(circuitDigest); i++ {
 31		circuitDigest[i] = fieldAPI.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] = fieldAPI.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] = fieldAPI.FromBinary(api.ToBinary(circuit.WiresCap[i][j], 64)).(F)
 43		}
 44	}
 45
 46	var plonkZsPartialProductsCap [16][4]F
 47	for i := 0; i < len(plonkZsPartialProductsCap); i++ {
 48		for j := 0; j < len(plonkZsPartialProductsCap[0]); j++ {
 49			plonkZsPartialProductsCap[i][j] = fieldAPI.FromBinary(api.ToBinary(circuit.PlonkZsPartialProductsCap[i][j], 64)).(F)
 50		}
 51	}
 52
 53	var quotientPolysCap [16][4]F
 54	for i := 0; i < len(quotientPolysCap); i++ {
 55		for j := 0; j < len(quotientPolysCap[0]); j++ {
 56			quotientPolysCap[i][j] = fieldAPI.FromBinary(api.ToBinary(circuit.QuotientPolysCap[i][j], 64)).(F)
 57		}
 58	}
 59
 60	publicInputHash := poseidonChip.HashNoPad(publicInputs[:])
 61	challengerChip.ObserveHash(circuitDigest)
 62	challengerChip.ObserveHash(publicInputHash)
 63	challengerChip.ObserveCap(wiresCap[:])
 64
 65	numChallenges := uint64(2)
 66	plonkBetas := challengerChip.GetNChallenges(numChallenges)
 67	plonkGammas := challengerChip.GetNChallenges(numChallenges)
 68
 69	expectedPublicInputHash := [4]F{
 70		NewFieldElementFromString("8416658900775745054"),
 71		NewFieldElementFromString("12574228347150446423"),
 72		NewFieldElementFromString("9629056739760131473"),
 73		NewFieldElementFromString("3119289788404190010"),
 74	}
 75
 76	for i := 0; i < 4; i++ {
 77		fieldAPI.AssertIsEqual(publicInputHash[i], expectedPublicInputHash[i])
 78	}
 79
 80	expectedPlonkBetas := [2]F{
 81		NewFieldElementFromString("4678728155650926271"),
 82		NewFieldElementFromString("13611962404289024887"),
 83	}
 84
 85	expectedPlonkGammas := [2]frontend.Variable{
 86		NewFieldElementFromString("13237663823305715949"),
 87		NewFieldElementFromString("15389314098328235145"),
 88	}
 89
 90	for i := 0; i < 2; i++ {
 91		fieldAPI.AssertIsEqual(plonkBetas[i], expectedPlonkBetas[i])
 92		fieldAPI.AssertIsEqual(plonkGammas[i], expectedPlonkGammas[i])
 93	}
 94
 95	challengerChip.ObserveCap(plonkZsPartialProductsCap[:])
 96	plonkAlphas := challengerChip.GetNChallenges(numChallenges)
 97
 98	expectedPlonkAlphas := [2]F{
 99		NewFieldElementFromString("14505919539124304197"),
100		NewFieldElementFromString("1695455639263736117"),
101	}
102
103	for i := 0; i < 2; i++ {
104		fieldAPI.AssertIsEqual(plonkAlphas[i], expectedPlonkAlphas[i])
105	}
106
107	challengerChip.ObserveCap(quotientPolysCap[:])
108	plonkZeta := challengerChip.GetExtensionChallenge()
109
110	expectedPlonkZeta := QuadraticExtension{
111		NewFieldElementFromString("14887793628029982930"),
112		NewFieldElementFromString("1136137158284059037"),
113	}
114
115	for i := 0; i < 2; i++ {
116		fieldAPI.AssertIsEqual(plonkZeta[i], expectedPlonkZeta[i])
117	}
118
119	return nil
120}
121
122func TestChallengerWitness(t *testing.T) {
123	assert := test.NewAssert(t)
124
125	testCase := func(
126		publicInputs [3]frontend.Variable,
127		circuitDigest [4]frontend.Variable,
128		wiresCap [16][4]frontend.Variable,
129		plonkZsPartialProductsCap [16][4]frontend.Variable,
130		quotientPolysCap [16][4]frontend.Variable,
131	) {
132		circuit := TestChallengerCircuit{
133			PublicInputs:              publicInputs,
134			CircuitDigest:             circuitDigest,
135			WiresCap:                  wiresCap,
136			PlonkZsPartialProductsCap: plonkZsPartialProductsCap,
137			QuotientPolysCap:          quotientPolysCap,
138		}
139		witness := TestChallengerCircuit{
140			PublicInputs:              publicInputs,
141			CircuitDigest:             circuitDigest,
142			WiresCap:                  wiresCap,
143			PlonkZsPartialProductsCap: plonkZsPartialProductsCap,
144			QuotientPolysCap:          quotientPolysCap,
145		}
146		err := test.IsSolved(&circuit, &witness, TEST_CURVE.ScalarField())
147		assert.NoError(err)
148	}
149
150	publicInputsStr := []string{"0", "1", "3736710860384812976"}
151	circuitDigestStr := []string{"7754113318730736048", "18436136620016916513", "18054530212389526288", "5893739326632906028"}
152	wiresCapStr := [][]string{
153		{"13884351014873073118", "5174249846243191862", "2208632528791973868", "1071582828677910652"},
154		{"11475361245556894879", "14867351574926692044", "17013374066934071379", "1027671036932569748"},
155		{"5604634992452399010", "3684464596850094189", "5565599237356852406", "4136295609943151014"},
156		{"8463721840990025805", "5922588965472526198", "8096699027533803435", "2210089353004111478"},
157		{"17531628199677307555", "11513452064460680964", "1482441508929181375", "5139566233781982440"},
158		{"13271417993289093233", "17257193898955790413", "16883807866578566670", "7423179920948669117"},
159		{"13462567520785358202", "15555103598281658890", "5859961276885232601", "4464568704709749394"},
160		{"153012620162729043", "14072764618167122665", "3025694603779494447", "15948104906680148838"},
161		{"18050235253694287284", "11467396424826912141", "11302553396166323353", "10976271719722841224"},
162		{"15208241660644051470", "8520722208187871063", "10775022596056682771", "16048513824198271730"},
163		{"6929477084755896240", "11382029470138215117", "13205948643259905511", "9421863267852221772"},
164		{"15449187573546292268", "10216729601353604194", "9493934392442974211", "9848643714440191835"},
165		{"2172475758127444753", "16681095938683502188", "9983383760611275566", "2603547977557388755"},
166		{"17440301588003279095", "11799356585691460705", "1386003375936412946", "11059100806278290279"},
167		{"10758265002546797581", "1374136260999724547", "7200401521491969338", "219493657547391496"},
168		{"5995963332181008902", "4442996285152250372", "2005936434281221193", "6869325719052666642"},
169	}
170	plonkZsPartialProductsCapStr := [][]string{
171		{"1209867952068639569", "4958824272276746373", "8278739766347565702", "1966940898171663504"},
172		{"12599305286358028697", "8932511136775685440", "5376267558248004641", "6313904687311555884"},
173		{"11190791343943249124", "4016631697385248176", "10356629842603047568", "10968099068686195317"},
174		{"1963983823153667719", "6333891613271539690", "12318891063769180636", "10443318253972130654"},
175		{"7799898099378084347", "2751829638242157622", "8351904444410446701", "5284662773710644867"},
176		{"1588568181448440843", "10836321455257423751", "5543952383542989142", "12946954522116753258"},
177		{"15710202198621978057", "13746115173212319217", "6103259182317700987", "17589471289629134988"},
178		{"12877950969971815168", "4963889190939310439", "8868772654550990048", "11774978531783219015"},
179		{"16832740767463005599", "15040340114131672027", "7469306538360789573", "3154855824233652432"},
180		{"9383568437827143152", "1741060064145647394", "17668587021570420286", "5241789470902809114"},
181		{"2087729156816989530", "8248918881937854542", "8673194597758568216", "10710697836634846115"},
182		{"11253371860840267365", "16818881664594712299", "11933553751682199585", "1936353232880935379"},
183		{"12163553231829171860", "17244267969759347515", "2003902333564157189", "6934019871173840760"},
184		{"2082141893879862527", "18267460725569427782", "1129651898415533808", "14011240934155569890"},
185		{"2526273401266876282", "6955959191669943337", "5926536548217021446", "17949337312612691782"},
186		{"8858882459906353593", "5813258279939597857", "6320047506247573502", "15969724232572328561"},
187	}
188	quotientPolysCapStr := [][]string{
189		{"9435614145733021495", "1742717829476348934", "11178548223985487003", "14531951007568589725"},
190		{"11747844681527676730", "3089691012847802165", "5887135310661642077", "13943570416123664971"},
191		{"11150071448774479229", "4486829025930200476", "9369448886033958276", "15757606153229850783"},
192		{"14603194410536469617", "11776185929725558373", "3122936423686490326", "10128277488128872810"},
193		{"4990578700975083076", "4997575606014863069", "14499603187047727337", "14028694557236527137"},
194		{"2279147899956815983", "16034899207717647338", "14763350037932939672", "10075834812570828076"},
195		{"1102006741007271956", "15242779529961262072", "6900547375301951311", "8631780317175902419"},
196		{"6299112770394539219", "6297397453582105768", "14148031335065995704", "3794733067587629405"},
197		{"7891039548997763820", "4260484126440019022", "6493066317319943586", "14775252570136307979"},
198		{"10790514248728420789", "14444029601980227412", "17514190309172155536", "12973059492411164965"},
199		{"8940755416742726696", "8469566845539112244", "7642612722784522739", "15276772682665052607"},
200		{"18306931819862706026", "14374659904694625207", "8609543532143656606", "17350044275494282679"},
201		{"9062024023737444614", "13780128979028684176", "6115495431779737008", "7170446003855284754"},
202		{"6191400598853400595", "7806485717076924017", "3225145303141729264", "3644550749005104128"},
203		{"15759718266801608721", "2406060174022670585", "15679263832775538866", "18066847192985300443"},
204		{"9184823221361582966", "4767786405185004644", "9827047623720647370", "993615002460432327"},
205	}
206
207	var publicInputs [3]frontend.Variable
208	var circuitDigest [4]frontend.Variable
209	var wiresCap [16][4]frontend.Variable
210	var plonkZsPartialProductsCap [16][4]frontend.Variable
211	var quotientPolysCap [16][4]frontend.Variable
212
213	copy(publicInputs[:], utils.StrArrayToFrontendVariableArray(publicInputsStr))
214	copy(circuitDigest[:], utils.StrArrayToFrontendVariableArray(circuitDigestStr))
215	for i := 0; i < len(wiresCapStr); i++ {
216		copy(wiresCap[i][:], utils.StrArrayToFrontendVariableArray(wiresCapStr[i]))
217	}
218	for i := 0; i < len(plonkZsPartialProductsCapStr); i++ {
219		copy(plonkZsPartialProductsCap[i][:], utils.StrArrayToFrontendVariableArray(plonkZsPartialProductsCapStr[i]))
220	}
221	for i := 0; i < len(quotientPolysCapStr); i++ {
222		copy(quotientPolysCap[i][:], utils.StrArrayToFrontendVariableArray(quotientPolysCapStr[i]))
223	}
224
225	testCase(publicInputs, circuitDigest, wiresCap, plonkZsPartialProductsCap, quotientPolysCap)
226}