arnaucube
arnaucube/gnark-plonky2-verifierPublic Self-hosted repository
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 [4]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	degreeBits uint64,
122	config types.FriConfig,
123) variables.FriChallenges {
124	numFriQueries := config.NumQueryRounds
125	friAlpha := c.GetExtensionChallenge()
126
127	var friBetas []gl.QuadraticExtensionVariable
128	for i := 0; i < len(commitPhaseMerkleCaps); i++ {
129		c.ObserveCap(commitPhaseMerkleCaps[i])
130		friBetas = append(friBetas, c.GetExtensionChallenge())
131	}
132
133	c.ObserveExtensionElements(finalPoly.Coeffs)
134	c.ObserveElement(powWitness)
135
136	friPowResponse := c.GetChallenge()
137	friQueryIndices := c.GetNChallenges(numFriQueries)
138
139	return variables.FriChallenges{
140		FriAlpha:        friAlpha,
141		FriBetas:        friBetas,
142		FriPowResponse:  friPowResponse,
143		FriQueryIndices: friQueryIndices,
144	}
145}
146
147func (c *Chip) duplexing() {
148	if len(c.inputBuffer) > poseidon.SPONGE_RATE {
149		fmt.Println(len(c.inputBuffer))
150		panic("something went wrong")
151	}
152
153	glApi := gl.New(c.api)
154
155	for i := 0; i < len(c.inputBuffer); i++ {
156		c.spongeState[i] = glApi.Reduce(c.inputBuffer[i])
157	}
158	// Clear the input buffer
159	c.inputBuffer = make([]gl.Variable, 0)
160	c.spongeState = c.poseidonChip.Poseidon(c.spongeState)
161
162	// Clear the output buffer
163	c.outputBuffer = make([]gl.Variable, 0)
164	for i := 0; i < poseidon.SPONGE_RATE; i++ {
165		c.outputBuffer = append(c.outputBuffer, c.spongeState[i])
166	}
167}