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 return nil
108 //panic(fmt.Sprintf("Unknown gate ID %s", gateId))
109}
110
111func getRegExMatches(r *regexp.Regexp, gateId string) map[string]int {
112 matches := r.FindStringSubmatch(gateId)
113 result := make(map[string]int)
114 for i, name := range r.SubexpNames() {
115 if i != 0 && name != "" {
116 value, err := strconv.Atoi(matches[i])
117 if err != nil {
118 panic("Invalid field value for \"name\": " + err.Error())
119 }
120 result[name] = value
121 }
122 }
123
124 return result
125}
126
127func (p *PlonkChip) computeFilter(
128 row uint64,
129 groupRange Range,
130 s QuadraticExtension,
131 manySelector bool,
132) QuadraticExtension {
133 product := p.qeAPI.ONE_QE
134 for i := groupRange.start; i < groupRange.end; i++ {
135 if i == uint64(row) {
136 continue
137 }
138
139 product = p.qeAPI.MulExtension(product, p.qeAPI.SubExtension(p.qeAPI.FieldToQE(NewFieldElement(i)), s))
140 }
141
142 if manySelector {
143 product = p.qeAPI.MulExtension(product, p.qeAPI.SubExtension(p.qeAPI.FieldToQE(NewFieldElement(UNUSED_SELECTOR)), s))
144 }
145
146 return product
147}
148
149func (p *PlonkChip) evalFiltered(
150 g gate,
151 vars EvaluationVars,
152 row uint64,
153 selectorIndex uint64,
154 groupRange Range,
155 numSelectors uint64,
156) []QuadraticExtension {
157 filter := p.computeFilter(row, groupRange, vars.localConstants[selectorIndex], numSelectors > 1)
158
159 vars.RemovePrefix(numSelectors)
160
161 unfiltered := g.EvalUnfiltered(p, vars)
162 for i := range unfiltered {
163 unfiltered[i] = p.qeAPI.MulExtension(unfiltered[i], filter)
164 }
165 return unfiltered
166}