gnark-plonky2-verifier/plonky2_verifier/gate.go
plonky2_verifier/gate.goRaw
1package plonky2_verifier
2
3import (
4 . "gnark-plonky2-verifier/field"
5 "regexp"
6 "strconv"
7 "strings"
8)
9
10type gate interface {
11 Id() string
12 EvalUnfiltered(p *PlonkChip, vars EvaluationVars) []QuadraticExtension
13}
14
15func GateInstanceFromId(gateId string) gate {
16 if strings.HasPrefix(gateId, "ArithmeticGate") {
17 numOpsRaw := strings.Split(gateId, ":")[1]
18 numOpsRaw = strings.Split(numOpsRaw, "}")[0]
19 numOpsRaw = strings.TrimSpace(numOpsRaw)
20 numOps, err := strconv.Atoi(numOpsRaw)
21 if err != nil {
22 panic("Invalid gate ID for ArithmeticGate")
23 }
24 return NewArithmeticGate(uint64(numOps))
25 }
26
27 if strings.HasPrefix(gateId, "ConstantGate") {
28 numConstsRaw := strings.Split(gateId, ":")[1]
29 numConstsRaw = strings.Split(numConstsRaw, "}")[0]
30 numConstsRaw = strings.TrimSpace(numConstsRaw)
31 numConsts, err := strconv.Atoi(numConstsRaw)
32 if err != nil {
33 panic("Invalid gate ID")
34 }
35 return NewConstantGate(uint64(numConsts))
36 }
37
38 if gateId == "NoopGate" {
39 return NewNoopGate()
40 }
41
42 if gateId == "PublicInputGate" {
43 return NewPublicInputGate()
44 }
45
46 if strings.HasPrefix(gateId, "PoseidonGate") {
47 return NewPoseidonGate()
48 }
49
50 if strings.HasPrefix(gateId, "BaseSumGate") {
51 // Has the format "BaseSumGate { num_limbs: 32 } + Base: 2"
52
53 regEx := "BaseSumGate { num_limbs: (?P<numLimbs>[0-9]+) } \\+ Base: (?P<base>[0-9]+)"
54 r, err := regexp.Compile(regEx)
55 if err != nil {
56 panic("Invalid BaseSumGate regular expression")
57 }
58
59 matches := getRegExMatches(r, gateId)
60 numLimbs, hasNumLimbs := matches["numLimbs"]
61 base, hasBase := matches["base"]
62 if !hasNumLimbs || !hasBase {
63 panic("Invalid BaseSumGate ID")
64 }
65
66 return NewBaseSumGate(uint64(numLimbs), uint64(base))
67 }
68
69 if strings.HasPrefix(gateId, "RandomAccessGate") {
70 // Has the format "RandomAccessGate { bits: 2, num_copies: 13, num_extra_constants: 2, _phantom: PhantomData<plonky2_field::goldilocks_field::GoldilocksField> }<D=2>"
71
72 regEx := "RandomAccessGate { bits: (?P<bits>[0-9]+), num_copies: (?P<numCopies>[0-9]+), num_extra_constants: (?P<numExtraConstants>[0-9]+), _phantom: PhantomData<plonky2_field::goldilocks_field::GoldilocksField> }<D=(?P<base>[0-9]+)>"
73 r, err := regexp.Compile(regEx)
74 if err != nil {
75 panic("Invalid RandomAccessGate regular expression")
76 }
77
78 matches := getRegExMatches(r, gateId)
79 bits, hasBits := matches["bits"]
80 numCopies, hasNumCopies := matches["numCopies"]
81 numExtraConstants, hasNumExtraConstants := matches["numExtraConstants"]
82 if !hasBits || !hasNumCopies || !hasNumExtraConstants {
83 panic("Invalid RandomAccessGate ID")
84 }
85
86 return NewRandomAccessGate(uint64(bits), uint64(numCopies), uint64(numExtraConstants))
87 }
88
89 if strings.HasPrefix(gateId, "ArithmeticExtension") {
90 // Has the format "ArithmeticExtensionGate { num_ops: 10 }"
91
92 regEx := "ArithmeticExtensionGate { num_ops: (?P<numOps>[0-9]+) }"
93 r, err := regexp.Compile(regEx)
94 if err != nil {
95 panic("Invalid ArithmeticExtensionGate regular expression")
96 }
97
98 matches := getRegExMatches(r, gateId)
99 numOps, hasNumOps := matches["numOps"]
100 if !hasNumOps {
101 panic("Invalid ArithmeticExtensionGate ID")
102 }
103
104 return NewArithmeticExtensionGate(uint64(numOps))
105 }
106
107 if strings.HasPrefix(gateId, "MultiplicationExtension") {
108 // Has the format "ArithmeticExtensionGate { num_ops: 10 }"
109
110 regEx := "MultiplicationExtension { num_ops: (?P<numOps>[0-9]+) }"
111 r, err := regexp.Compile(regEx)
112 if err != nil {
113 panic("Invalid MultiplicationExtension regular expression")
114 }
115
116 matches := getRegExMatches(r, gateId)
117 numOps, hasNumOps := matches["numOps"]
118 if !hasNumOps {
119 panic("Invalid MultiplicationExtension ID")
120 }
121
122 return NewMultiplicationExtensionGate(uint64(numOps))
123 }
124
125 if strings.HasPrefix(gateId, "ReducingExtensionGate") {
126 // Has the format "ReducingExtensionGate { num_coeffs: 33 }"
127
128 regEx := "ReducingExtensionGate { num_coeffs: (?P<numCoeffs>[0-9]+) }"
129 r, err := regexp.Compile(regEx)
130 if err != nil {
131 panic("Invalid ReducingExtensionGate regular expression")
132 }
133
134 matches := getRegExMatches(r, gateId)
135 numCoeffs, hasNumCoeffs := matches["numCoeffs"]
136 if !hasNumCoeffs {
137 panic("Invalid ReducingExtensionGate ID")
138 }
139
140 return NewReducingExtensionGate(uint64(numCoeffs))
141 }
142
143 return nil
144 //panic(fmt.Sprintf("Unknown gate ID %s", gateId))
145}
146
147func getRegExMatches(r *regexp.Regexp, gateId string) map[string]int {
148 matches := r.FindStringSubmatch(gateId)
149 result := make(map[string]int)
150 for i, name := range r.SubexpNames() {
151 if i != 0 && name != "" {
152 value, err := strconv.Atoi(matches[i])
153 if err != nil {
154 panic("Invalid field value for \"name\": " + err.Error())
155 }
156 result[name] = value
157 }
158 }
159
160 return result
161}
162
163func (p *PlonkChip) computeFilter(
164 row uint64,
165 groupRange Range,
166 s QuadraticExtension,
167 manySelector bool,
168) QuadraticExtension {
169 product := p.qeAPI.ONE_QE
170 for i := groupRange.start; i < groupRange.end; i++ {
171 if i == uint64(row) {
172 continue
173 }
174
175 product = p.qeAPI.MulExtension(product, p.qeAPI.SubExtension(p.qeAPI.FieldToQE(NewFieldElement(i)), s))
176 }
177
178 if manySelector {
179 product = p.qeAPI.MulExtension(product, p.qeAPI.SubExtension(p.qeAPI.FieldToQE(NewFieldElement(UNUSED_SELECTOR)), s))
180 }
181
182 return product
183}
184
185func (p *PlonkChip) evalFiltered(
186 g gate,
187 vars EvaluationVars,
188 row uint64,
189 selectorIndex uint64,
190 groupRange Range,
191 numSelectors uint64,
192) []QuadraticExtension {
193 filter := p.computeFilter(row, groupRange, vars.localConstants[selectorIndex], numSelectors > 1)
194
195 vars.RemovePrefix(numSelectors)
196
197 unfiltered := g.EvalUnfiltered(p, vars)
198 for i := range unfiltered {
199 unfiltered[i] = p.qeAPI.MulExtension(unfiltered[i], filter)
200 }
201 return unfiltered
202}