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 return nil
126 //panic(fmt.Sprintf("Unknown gate ID %s", gateId))
127}
128
129func getRegExMatches(r *regexp.Regexp, gateId string) map[string]int {
130 matches := r.FindStringSubmatch(gateId)
131 result := make(map[string]int)
132 for i, name := range r.SubexpNames() {
133 if i != 0 && name != "" {
134 value, err := strconv.Atoi(matches[i])
135 if err != nil {
136 panic("Invalid field value for \"name\": " + err.Error())
137 }
138 result[name] = value
139 }
140 }
141
142 return result
143}
144
145func (p *PlonkChip) computeFilter(
146 row uint64,
147 groupRange Range,
148 s QuadraticExtension,
149 manySelector bool,
150) QuadraticExtension {
151 product := p.qeAPI.ONE_QE
152 for i := groupRange.start; i < groupRange.end; i++ {
153 if i == uint64(row) {
154 continue
155 }
156
157 product = p.qeAPI.MulExtension(product, p.qeAPI.SubExtension(p.qeAPI.FieldToQE(NewFieldElement(i)), s))
158 }
159
160 if manySelector {
161 product = p.qeAPI.MulExtension(product, p.qeAPI.SubExtension(p.qeAPI.FieldToQE(NewFieldElement(UNUSED_SELECTOR)), s))
162 }
163
164 return product
165}
166
167func (p *PlonkChip) evalFiltered(
168 g gate,
169 vars EvaluationVars,
170 row uint64,
171 selectorIndex uint64,
172 groupRange Range,
173 numSelectors uint64,
174) []QuadraticExtension {
175 filter := p.computeFilter(row, groupRange, vars.localConstants[selectorIndex], numSelectors > 1)
176
177 vars.RemovePrefix(numSelectors)
178
179 unfiltered := g.EvalUnfiltered(p, vars)
180 for i := range unfiltered {
181 unfiltered[i] = p.qeAPI.MulExtension(unfiltered[i], filter)
182 }
183 return unfiltered
184}