gnark-plonky2-verifier/challenger/challenger.go
challenger/challenger.goRaw
1package challenger
2
3import (
4 "fmt"
5
6 "github.com/consensys/gnark/frontend"
7 "github.com/succinctlabs/gnark-plonky2-verifier/fri"
8 gl "github.com/succinctlabs/gnark-plonky2-verifier/goldilocks"
9 "github.com/succinctlabs/gnark-plonky2-verifier/poseidon"
10 "github.com/succinctlabs/gnark-plonky2-verifier/types"
11 "github.com/succinctlabs/gnark-plonky2-verifier/variables"
12)
13
14type Chip struct {
15 api frontend.API `gnark:"-"`
16 poseidonChip *poseidon.GoldilocksChip
17 poseidonBN254Chip *poseidon.BN254Chip
18 spongeState poseidon.GoldilocksState
19 inputBuffer []gl.Variable
20 outputBuffer []gl.Variable
21}
22
23func NewChip(api frontend.API) *Chip {
24 var spongeState poseidon.GoldilocksState
25 var inputBuffer []gl.Variable
26 var outputBuffer []gl.Variable
27 for i := 0; i < poseidon.SPONGE_WIDTH; i++ {
28 spongeState[i] = gl.Zero()
29 }
30 poseidonChip := poseidon.NewGoldilocksChip(api)
31 poseidonBN254Chip := poseidon.NewBN254Chip(api)
32 return &Chip{
33 api: api,
34 poseidonChip: poseidonChip,
35 poseidonBN254Chip: poseidonBN254Chip,
36 spongeState: spongeState,
37 inputBuffer: inputBuffer,
38 outputBuffer: outputBuffer,
39 }
40}
41
42func (c *Chip) ObserveElement(element gl.Variable) {
43 // Clear the output buffer
44 c.outputBuffer = make([]gl.Variable, 0)
45 c.inputBuffer = append(c.inputBuffer, element)
46 if len(c.inputBuffer) == poseidon.SPONGE_RATE {
47 c.duplexing()
48 }
49}
50
51func (c *Chip) ObserveElements(elements []gl.Variable) {
52 for i := 0; i < len(elements); i++ {
53 c.ObserveElement(elements[i])
54 }
55}
56
57func (c *Chip) ObserveHash(hash poseidon.GoldilocksHashOut) {
58 elements := c.poseidonChip.ToVec(hash)
59 c.ObserveElements(elements)
60}
61
62func (c *Chip) ObserveBN254Hash(hash poseidon.BN254HashOut) {
63 elements := c.poseidonBN254Chip.ToVec(hash)
64 c.ObserveElements(elements)
65}
66
67func (c *Chip) ObserveCap(cap []poseidon.BN254HashOut) {
68 for i := 0; i < len(cap); i++ {
69 c.ObserveBN254Hash(cap[i])
70 }
71}
72
73func (c *Chip) ObserveExtensionElement(element gl.QuadraticExtensionVariable) {
74 c.ObserveElements(element[:])
75}
76
77func (c *Chip) ObserveExtensionElements(elements []gl.QuadraticExtensionVariable) {
78 for i := 0; i < len(elements); i++ {
79 c.ObserveExtensionElement(elements[i])
80 }
81}
82
83func (c *Chip) ObserveOpenings(openings fri.Openings) {
84 for i := 0; i < len(openings.Batches); i++ {
85 c.ObserveExtensionElements(openings.Batches[i].Values)
86 }
87}
88
89func (c *Chip) GetChallenge() gl.Variable {
90 if len(c.inputBuffer) != 0 || len(c.outputBuffer) == 0 {
91 c.duplexing()
92 }
93
94 challenge := c.outputBuffer[len(c.outputBuffer)-1]
95 c.outputBuffer = c.outputBuffer[:len(c.outputBuffer)-1]
96
97 return challenge
98}
99
100func (c *Chip) GetNChallenges(n uint64) []gl.Variable {
101 challenges := make([]gl.Variable, n)
102 for i := uint64(0); i < n; i++ {
103 challenges[i] = c.GetChallenge()
104 }
105 return challenges
106}
107
108func (c *Chip) GetExtensionChallenge() gl.QuadraticExtensionVariable {
109 values := c.GetNChallenges(2)
110 return gl.QuadraticExtensionVariable{values[0], values[1]}
111}
112
113func (c *Chip) GetHash() poseidon.GoldilocksHashOut {
114 return [poseidon.POSEIDON_GL_HASH_SIZE]gl.Variable{c.GetChallenge(), c.GetChallenge(), c.GetChallenge(), c.GetChallenge()}
115}
116
117func (c *Chip) GetFriChallenges(
118 commitPhaseMerkleCaps []variables.FriMerkleCap,
119 finalPoly variables.PolynomialCoeffs,
120 powWitness gl.Variable,
121 config types.FriConfig,
122) variables.FriChallenges {
123 numFriQueries := config.NumQueryRounds
124 friAlpha := c.GetExtensionChallenge()
125
126 var friBetas []gl.QuadraticExtensionVariable
127 for i := 0; i < len(commitPhaseMerkleCaps); i++ {
128 c.ObserveCap(commitPhaseMerkleCaps[i])
129 friBetas = append(friBetas, c.GetExtensionChallenge())
130 }
131
132 c.ObserveExtensionElements(finalPoly.Coeffs)
133 c.ObserveElement(powWitness)
134
135 friPowResponse := c.GetChallenge()
136 friQueryIndices := c.GetNChallenges(numFriQueries)
137
138 return variables.FriChallenges{
139 FriAlpha: friAlpha,
140 FriBetas: friBetas,
141 FriPowResponse: friPowResponse,
142 FriQueryIndices: friQueryIndices,
143 }
144}
145
146func (c *Chip) duplexing() {
147 if len(c.inputBuffer) > poseidon.SPONGE_RATE {
148 fmt.Println(len(c.inputBuffer))
149 panic("something went wrong")
150 }
151
152 glApi := gl.New(c.api)
153
154 for i := 0; i < len(c.inputBuffer); i++ {
155 c.spongeState[i] = glApi.Reduce(c.inputBuffer[i])
156 }
157 // Clear the input buffer
158 c.inputBuffer = make([]gl.Variable, 0)
159 c.spongeState = c.poseidonChip.Poseidon(c.spongeState)
160
161 // Clear the output buffer
162 c.outputBuffer = make([]gl.Variable, 0)
163 for i := 0; i < poseidon.SPONGE_RATE; i++ {
164 c.outputBuffer = append(c.outputBuffer, c.spongeState[i])
165 }
166}