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