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