@ -1,79 +1,97 @@ |
|||||
package plonky2_verifier |
package plonky2_verifier |
||||
|
|
||||
import ( |
import ( |
||||
|
"fmt" |
||||
|
. "gnark-ed25519/goldilocks" |
||||
|
"gnark-ed25519/poseidon" |
||||
|
. "gnark-ed25519/poseidon" |
||||
|
|
||||
"github.com/consensys/gnark/frontend" |
"github.com/consensys/gnark/frontend" |
||||
) |
) |
||||
|
|
||||
type V = frontend.Variable |
|
||||
|
|
||||
type Challenger struct { |
|
||||
sponge_state: [SPONGE_WIDTH]V |
|
||||
input_buffer: []V |
|
||||
output_buffer: []V |
|
||||
|
type ChallengerChip struct { |
||||
|
api frontend.API |
||||
|
field frontend.API |
||||
|
poseidonChip PoseidonChip |
||||
|
spongeState [SPONGE_WIDTH]GoldilocksElement |
||||
|
inputBuffer []GoldilocksElement |
||||
|
outputBuffer []GoldilocksElement |
||||
} |
} |
||||
|
|
||||
func NewChallenger() Challenger { |
|
||||
var sponge_state [SPONGE_WIDTH]V |
|
||||
for i := 0; i < SPONGE_WIDTH; i++ { |
|
||||
sponge_state[i] = 0 |
|
||||
} |
|
||||
return Challenger { |
|
||||
sponge_state: sponge_state, |
|
||||
input_buffer: []V{}, |
|
||||
output_buffer: []V{}, |
|
||||
|
func NewChallengerChip(api frontend.API, field frontend.API, poseidonChip PoseidonChip) *ChallengerChip { |
||||
|
var spongeState [SPONGE_WIDTH]GoldilocksElement |
||||
|
var inputBuffer []GoldilocksElement |
||||
|
var outputBuffer []GoldilocksElement |
||||
|
return &ChallengerChip{ |
||||
|
api: api, |
||||
|
field: field, |
||||
|
poseidonChip: poseidonChip, |
||||
|
spongeState: spongeState, |
||||
|
inputBuffer: inputBuffer, |
||||
|
outputBuffer: outputBuffer, |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
func (c *Challenger) observe_elements(elements []V) { |
|
||||
for _, element := range elements { |
|
||||
c.observe_element(element) |
|
||||
|
func (c *ChallengerChip) ObserveElement(element GoldilocksElement) { |
||||
|
c.outputBuffer = clearBuffer(c.outputBuffer) |
||||
|
c.inputBuffer = append(c.inputBuffer, element) |
||||
|
if len(c.inputBuffer) == SPONGE_RATE { |
||||
|
c.duplexing() |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
func (c *Challenger) observe_hash(hash []V) { |
|
||||
c.observe_elements(hash) |
|
||||
|
func (c *ChallengerChip) ObserveElements(elements []GoldilocksElement) { |
||||
|
for i := 0; i < len(elements); i++ { |
||||
|
c.ObserveElement(elements[i]) |
||||
|
} |
||||
} |
} |
||||
|
|
||||
func (c *Challenger) observe_element(element V) { |
|
||||
c.output_buffer = V[]{} |
|
||||
c.input_buffer = append(c.input_buffer, element) |
|
||||
if len(c.input_buffer) == SPONGE_RATE { |
|
||||
c.duplexing() |
|
||||
} |
|
||||
|
func (c *ChallengerChip) ObserveHash(hash HashOutput) { |
||||
|
c.ObserveElements(hash[:]) |
||||
} |
} |
||||
|
|
||||
func (c *Challenger) observe_cap(cap [][]V) { |
|
||||
for _, hash := range cap { |
|
||||
c.observe_hash(hash) |
|
||||
|
func (c *ChallengerChip) ObserveCap(cap []HashOutput) { |
||||
|
for i := 0; i < len(cap); i++ { |
||||
|
c.ObserveHash(cap[i]) |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
func (c *Challenger) duplexing() { |
|
||||
if len(c.input_buffer) > SPONGE_RATE { panic("buffer too large") } |
|
||||
|
|
||||
for i, input := range c.input_buffer { |
|
||||
c.sponge_state[i] = input |
|
||||
|
func (c *ChallengerChip) GetChallenge() GoldilocksElement { |
||||
|
if len(c.inputBuffer) != 0 || len(c.outputBuffer) == 0 { |
||||
|
c.duplexing() |
||||
} |
} |
||||
c.input_buffer = V[]{} |
|
||||
|
|
||||
c.sponge_state = poseidon(c.sponge_state) |
|
||||
|
challenge := c.outputBuffer[len(c.outputBuffer)-1] |
||||
|
c.outputBuffer = c.outputBuffer[:len(c.outputBuffer)-1] |
||||
|
|
||||
c.output_buffer = c.sponge_state[:SPONGE_RATE] |
|
||||
|
return challenge |
||||
} |
} |
||||
|
|
||||
func (c *Challenger) get_challenge() V { |
|
||||
if len(c.input_buffer) > 0 || len(c.output_buffer) == 0 { |
|
||||
c.duplexing() |
|
||||
|
func (c *ChallengerChip) GetNChallenges(n int) []GoldilocksElement { |
||||
|
challenges := make([]GoldilocksElement, n) |
||||
|
for i := 0; i < n; i++ { |
||||
|
challenges[i] = c.GetChallenge() |
||||
} |
} |
||||
result := c.output_buffer[len(c.output_buffer) - 1] |
|
||||
c.output_buffer = c.output_buffer[:len(c.output_buffer) - 1] |
|
||||
return result |
|
||||
|
return challenges |
||||
} |
} |
||||
|
|
||||
func (c *Challenger) get_n_challenges(n int) []V { |
|
||||
result := make([]V, n) |
|
||||
for i := 0; i < n; i++ { |
|
||||
result[i] = c.get_challenge() |
|
||||
|
func clearBuffer(buffer []GoldilocksElement) []GoldilocksElement { |
||||
|
return make([]GoldilocksElement, 0) |
||||
|
} |
||||
|
|
||||
|
func (c *ChallengerChip) duplexing() { |
||||
|
if len(c.inputBuffer) > SPONGE_RATE { |
||||
|
fmt.Println(len(c.inputBuffer)) |
||||
|
panic("something went wrong") |
||||
|
} |
||||
|
for i := 0; i < len(c.inputBuffer); i++ { |
||||
|
c.spongeState[i] = c.inputBuffer[i] |
||||
|
} |
||||
|
c.inputBuffer = clearBuffer(c.inputBuffer) |
||||
|
c.spongeState = c.poseidonChip.Poseidon(c.spongeState) |
||||
|
clearBuffer(c.outputBuffer) |
||||
|
for i := 0; i < poseidon.SPONGE_RATE; i++ { |
||||
|
c.outputBuffer = append(c.outputBuffer, c.spongeState[i]) |
||||
|
// c.outputBuffer[i] = c.spongeState[i]
|
||||
} |
} |
||||
return result |
|
||||
} |
} |
@ -0,0 +1,117 @@ |
|||||
|
package plonky2_verifier |
||||
|
|
||||
|
import ( |
||||
|
. "gnark-ed25519/goldilocks" |
||||
|
. "gnark-ed25519/poseidon" |
||||
|
"gnark-ed25519/utils" |
||||
|
"testing" |
||||
|
|
||||
|
"github.com/consensys/gnark-crypto/ecc" |
||||
|
"github.com/consensys/gnark/frontend" |
||||
|
"github.com/consensys/gnark/test" |
||||
|
) |
||||
|
|
||||
|
var testCurve = ecc.BN254 |
||||
|
|
||||
|
type TestChallengerCircuit struct { |
||||
|
PublicInputs [3]frontend.Variable |
||||
|
CircuitDigest [4]frontend.Variable |
||||
|
WiresCap [16][4]frontend.Variable |
||||
|
} |
||||
|
|
||||
|
func (circuit *TestChallengerCircuit) Define(api frontend.API) error { |
||||
|
goldilocksApi := NewGoldilocksAPI(api) |
||||
|
poseidonChip := NewPoseidonChip(api, goldilocksApi) |
||||
|
challengerChip := NewChallengerChip(api, goldilocksApi, *poseidonChip) |
||||
|
|
||||
|
var circuitDigestGoldilocks [4]GoldilocksElement |
||||
|
for i := 0; i < 4; i++ { |
||||
|
circuitDigestGoldilocks[i] = goldilocksApi.FromBinary(api.ToBinary(circuit.CircuitDigest[i], 64)).(GoldilocksElement) |
||||
|
} |
||||
|
|
||||
|
var publicInputsGoldilocks [3]GoldilocksElement |
||||
|
for i := 0; i < 3; i++ { |
||||
|
publicInputsGoldilocks[i] = goldilocksApi.FromBinary(api.ToBinary(circuit.PublicInputs[i], 64)).(GoldilocksElement) |
||||
|
} |
||||
|
|
||||
|
var wiresCapGoldilocks [16][4]GoldilocksElement |
||||
|
for i := 0; i < 16; i++ { |
||||
|
for j := 0; j < 4; j++ { |
||||
|
wiresCapGoldilocks[i][j] = goldilocksApi.FromBinary(api.ToBinary(circuit.WiresCap[i][j], 64)).(GoldilocksElement) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
publicInputHash := poseidonChip.HashNoPad(publicInputsGoldilocks[:]) |
||||
|
challengerChip.ObserveHash(circuitDigestGoldilocks) |
||||
|
challengerChip.ObserveHash(publicInputHash) |
||||
|
challengerChip.ObserveCap(wiresCapGoldilocks[:]) |
||||
|
|
||||
|
nbChallenges := 2 |
||||
|
plonkBetas := challengerChip.GetNChallenges(nbChallenges) |
||||
|
plonkGammas := challengerChip.GetNChallenges(nbChallenges) |
||||
|
|
||||
|
var expectedPlonkBetas [2]frontend.Variable |
||||
|
expectedPlonkBetas[0] = frontend.Variable("4678728155650926271") |
||||
|
expectedPlonkBetas[1] = frontend.Variable("13611962404289024887") |
||||
|
|
||||
|
var expectedPlonkGammas [2]frontend.Variable |
||||
|
expectedPlonkGammas[0] = frontend.Variable("13237663823305715949") |
||||
|
expectedPlonkGammas[1] = frontend.Variable("15389314098328235145") |
||||
|
|
||||
|
for i := 0; i < 2; i++ { |
||||
|
goldilocksApi.AssertIsEqual( |
||||
|
plonkBetas[i], |
||||
|
goldilocksApi.FromBinary(api.ToBinary(expectedPlonkBetas[i])).(GoldilocksElement), |
||||
|
) |
||||
|
goldilocksApi.AssertIsEqual( |
||||
|
plonkGammas[i], |
||||
|
goldilocksApi.FromBinary(api.ToBinary(expectedPlonkGammas[i])).(GoldilocksElement), |
||||
|
) |
||||
|
} |
||||
|
|
||||
|
return nil |
||||
|
} |
||||
|
|
||||
|
func TestChallengerWitness(t *testing.T) { |
||||
|
assert := test.NewAssert(t) |
||||
|
|
||||
|
testCase := func(publicInputs [3]frontend.Variable, circuitDigest [4]frontend.Variable, wiresCap [16][4]frontend.Variable) { |
||||
|
circuit := TestChallengerCircuit{PublicInputs: publicInputs, CircuitDigest: circuitDigest, WiresCap: wiresCap} |
||||
|
witness := TestChallengerCircuit{PublicInputs: publicInputs, CircuitDigest: circuitDigest, WiresCap: wiresCap} |
||||
|
err := test.IsSolved(&circuit, &witness, testCurve.ScalarField()) |
||||
|
assert.NoError(err) |
||||
|
} |
||||
|
|
||||
|
publicInputsStr := []string{"0", "1", "3736710860384812976"} |
||||
|
circuitDigestStr := []string{"7754113318730736048", "18436136620016916513", "18054530212389526288", "5893739326632906028"} |
||||
|
wiresCapStr := [][]string{ |
||||
|
{"13884351014873073118", "5174249846243191862", "2208632528791973868", "1071582828677910652"}, |
||||
|
{"11475361245556894879", "14867351574926692044", "17013374066934071379", "1027671036932569748"}, |
||||
|
{"5604634992452399010", "3684464596850094189", "5565599237356852406", "4136295609943151014"}, |
||||
|
{"8463721840990025805", "5922588965472526198", "8096699027533803435", "2210089353004111478"}, |
||||
|
{"17531628199677307555", "11513452064460680964", "1482441508929181375", "5139566233781982440"}, |
||||
|
{"13271417993289093233", "17257193898955790413", "16883807866578566670", "7423179920948669117"}, |
||||
|
{"13462567520785358202", "15555103598281658890", "5859961276885232601", "4464568704709749394"}, |
||||
|
{"153012620162729043", "14072764618167122665", "3025694603779494447", "15948104906680148838"}, |
||||
|
{"18050235253694287284", "11467396424826912141", "11302553396166323353", "10976271719722841224"}, |
||||
|
{"15208241660644051470", "8520722208187871063", "10775022596056682771", "16048513824198271730"}, |
||||
|
{"6929477084755896240", "11382029470138215117", "13205948643259905511", "9421863267852221772"}, |
||||
|
{"15449187573546292268", "10216729601353604194", "9493934392442974211", "9848643714440191835"}, |
||||
|
{"2172475758127444753", "16681095938683502188", "9983383760611275566", "2603547977557388755"}, |
||||
|
{"17440301588003279095", "11799356585691460705", "1386003375936412946", "11059100806278290279"}, |
||||
|
{"10758265002546797581", "1374136260999724547", "7200401521491969338", "219493657547391496"}, |
||||
|
{"5995963332181008902", "4442996285152250372", "2005936434281221193", "6869325719052666642"}, |
||||
|
} |
||||
|
|
||||
|
var publicInputs [3]frontend.Variable |
||||
|
var circuitDigest [4]frontend.Variable |
||||
|
var wiresCap [16][4]frontend.Variable |
||||
|
|
||||
|
copy(publicInputs[:], utils.StrArrayToFrontendVariableArray(publicInputsStr)) |
||||
|
copy(circuitDigest[:], utils.StrArrayToFrontendVariableArray(circuitDigestStr)) |
||||
|
for i := 0; i < len(wiresCapStr); i++ { |
||||
|
copy(wiresCap[i][:], utils.StrArrayToFrontendVariableArray(wiresCapStr[i])) |
||||
|
} |
||||
|
|
||||
|
testCase(publicInputs, circuitDigest, wiresCap) |
||||
|
} |
@ -0,0 +1,60 @@ |
|||||
|
package poseidon |
||||
|
|
||||
|
import ( |
||||
|
. "gnark-ed25519/goldilocks" |
||||
|
"gnark-ed25519/utils" |
||||
|
"testing" |
||||
|
|
||||
|
"github.com/consensys/gnark-crypto/ecc" |
||||
|
"github.com/consensys/gnark/frontend" |
||||
|
"github.com/consensys/gnark/test" |
||||
|
) |
||||
|
|
||||
|
var testCurve = ecc.BN254 |
||||
|
|
||||
|
type TestPublicInputsHashCircuit struct { |
||||
|
In [3]frontend.Variable |
||||
|
Out [4]frontend.Variable |
||||
|
} |
||||
|
|
||||
|
func (circuit *TestPublicInputsHashCircuit) Define(api frontend.API) error { |
||||
|
goldilocksApi := NewGoldilocksAPI(api) |
||||
|
|
||||
|
// BN254 -> Binary(64) -> GoldilocksElement
|
||||
|
var input [3]GoldilocksElement |
||||
|
for i := 0; i < 3; i++ { |
||||
|
input[i] = goldilocksApi.FromBinary(api.ToBinary(circuit.In[i], 64)).(GoldilocksElement) |
||||
|
} |
||||
|
|
||||
|
poseidonChip := &PoseidonChip{api: api, field: goldilocksApi} |
||||
|
output := poseidonChip.HashNoPad(input[:]) |
||||
|
|
||||
|
// Check that output is correct
|
||||
|
for i := 0; i < 4; i++ { |
||||
|
goldilocksApi.AssertIsEqual( |
||||
|
output[i], |
||||
|
goldilocksApi.FromBinary(api.ToBinary(circuit.Out[i])).(GoldilocksElement), |
||||
|
) |
||||
|
} |
||||
|
|
||||
|
return nil |
||||
|
} |
||||
|
|
||||
|
func TestPublicInputsHashWitness(t *testing.T) { |
||||
|
assert := test.NewAssert(t) |
||||
|
|
||||
|
testCase := func(in [3]frontend.Variable, out [4]frontend.Variable) { |
||||
|
circuit := TestPublicInputsHashCircuit{In: in, Out: out} |
||||
|
witness := TestPublicInputsHashCircuit{In: in, Out: out} |
||||
|
err := test.IsSolved(&circuit, &witness, testCurve.ScalarField()) |
||||
|
assert.NoError(err) |
||||
|
} |
||||
|
|
||||
|
inStr := []string{"0", "1", "3736710860384812976"} |
||||
|
outStr := []string{"8416658900775745054", "12574228347150446423", "9629056739760131473", "3119289788404190010"} |
||||
|
var in [3]frontend.Variable |
||||
|
var out [4]frontend.Variable |
||||
|
copy(in[:], utils.StrArrayToFrontendVariableArray(inStr)) |
||||
|
copy(out[:], utils.StrArrayToFrontendVariableArray(outStr)) |
||||
|
testCase(in, out) |
||||
|
} |
@ -0,0 +1,25 @@ |
|||||
|
package utils |
||||
|
|
||||
|
import ( |
||||
|
"math/big" |
||||
|
|
||||
|
"github.com/consensys/gnark/frontend" |
||||
|
) |
||||
|
|
||||
|
func StrArrayToBigIntArray(input []string) []big.Int { |
||||
|
var output []big.Int |
||||
|
for i := 0; i < len(input); i++ { |
||||
|
a := new(big.Int) |
||||
|
a, _ = a.SetString(input[i], 10) |
||||
|
output = append(output, *a) |
||||
|
} |
||||
|
return output |
||||
|
} |
||||
|
|
||||
|
func StrArrayToFrontendVariableArray(input []string) []frontend.Variable { |
||||
|
var output []frontend.Variable |
||||
|
for i := 0; i < len(input); i++ { |
||||
|
output = append(output, frontend.Variable(input[i])) |
||||
|
} |
||||
|
return output |
||||
|
} |