mirror of
https://github.com/arnaucube/gnark-plonky2-verifier.git
synced 2026-01-12 00:51:33 +01:00
added public inputs hash test, challenger test
This commit is contained in:
@@ -18,14 +18,14 @@ const SPONGE_WIDTH = 12
|
||||
const SPONGE_RATE = 8
|
||||
|
||||
type PoseidonState = [WIDTH]GoldilocksElement
|
||||
type HashOutput = [4]GoldilocksElement
|
||||
type PoseidonChip struct {
|
||||
api frontend.API
|
||||
field frontend.API
|
||||
}
|
||||
|
||||
func Poseidon(api frontend.API, field frontend.API, input PoseidonState) PoseidonState {
|
||||
chip := &PoseidonChip{api: api, field: field}
|
||||
return chip.Poseidon(input)
|
||||
func NewPoseidonChip(api frontend.API, field frontend.API) *PoseidonChip {
|
||||
return &PoseidonChip{api: api, field: field}
|
||||
}
|
||||
|
||||
func (c *PoseidonChip) Poseidon(input PoseidonState) PoseidonState {
|
||||
@@ -37,6 +37,37 @@ func (c *PoseidonChip) Poseidon(input PoseidonState) PoseidonState {
|
||||
return state
|
||||
}
|
||||
|
||||
func (c *PoseidonChip) HashNToMNoPad(input []GoldilocksElement, nbOutputs int) []GoldilocksElement {
|
||||
var state PoseidonState
|
||||
|
||||
for i := 0; i < len(input); i += SPONGE_RATE {
|
||||
for j := 0; j < SPONGE_RATE; j++ {
|
||||
if i+j < len(input) {
|
||||
state[j] = input[i+j]
|
||||
}
|
||||
}
|
||||
state = c.Poseidon(state)
|
||||
}
|
||||
|
||||
var outputs []GoldilocksElement
|
||||
|
||||
for {
|
||||
for i := 0; i < SPONGE_RATE; i++ {
|
||||
outputs = append(outputs, state[i])
|
||||
if len(outputs) == nbOutputs {
|
||||
return outputs
|
||||
}
|
||||
}
|
||||
state = c.Poseidon(state)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *PoseidonChip) HashNoPad(input []GoldilocksElement) HashOutput {
|
||||
var hash [4]GoldilocksElement
|
||||
copy(hash[:], c.HashNToMNoPad(input, 4))
|
||||
return hash
|
||||
}
|
||||
|
||||
func (c *PoseidonChip) fullRounds(state PoseidonState, roundCounter *int) PoseidonState {
|
||||
for i := 0; i < HALF_N_FULL_ROUNDS; i++ {
|
||||
state = c.constantLayer(state, roundCounter)
|
||||
|
||||
@@ -2,18 +2,15 @@ package poseidon
|
||||
|
||||
import (
|
||||
. "gnark-ed25519/goldilocks"
|
||||
"math/big"
|
||||
"gnark-ed25519/utils"
|
||||
"testing"
|
||||
|
||||
"github.com/consensys/gnark-crypto/ecc"
|
||||
"github.com/consensys/gnark/backend/groth16"
|
||||
"github.com/consensys/gnark/frontend"
|
||||
"github.com/consensys/gnark/frontend/cs/r1cs"
|
||||
"github.com/consensys/gnark/test"
|
||||
)
|
||||
|
||||
var testCurve = ecc.BN254
|
||||
|
||||
type TestPoseidonCircuit struct {
|
||||
In [12]frontend.Variable
|
||||
Out [12]frontend.Variable
|
||||
@@ -28,7 +25,8 @@ func (circuit *TestPoseidonCircuit) Define(api frontend.API) error {
|
||||
input[i] = goldilocksApi.FromBinary(api.ToBinary(circuit.In[i], 64)).(GoldilocksElement)
|
||||
}
|
||||
|
||||
output := Poseidon(api, goldilocksApi, input)
|
||||
chip := NewPoseidonChip(api, goldilocksApi)
|
||||
output := chip.Poseidon(input)
|
||||
|
||||
// Check that output is correct
|
||||
for i := 0; i < 12; i++ {
|
||||
@@ -44,66 +42,39 @@ func (circuit *TestPoseidonCircuit) Define(api frontend.API) error {
|
||||
func TestPoseidonWitness(t *testing.T) {
|
||||
assert := test.NewAssert(t)
|
||||
|
||||
testCase := func(inBigInt [12]big.Int, outBigInt [12]big.Int) {
|
||||
var in [12]frontend.Variable
|
||||
var out [12]frontend.Variable
|
||||
|
||||
for i := 0; i < 12; i++ {
|
||||
in[i] = inBigInt[i]
|
||||
out[i] = outBigInt[i]
|
||||
}
|
||||
|
||||
testCase := func(in [12]frontend.Variable, out [12]frontend.Variable) {
|
||||
circuit := TestPoseidonCircuit{In: in, Out: out}
|
||||
witness := TestPoseidonCircuit{In: in, Out: out}
|
||||
err := test.IsSolved(&circuit, &witness, testCurve.ScalarField())
|
||||
assert.NoError(err)
|
||||
}
|
||||
|
||||
inStr := [12]string{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"}
|
||||
outStr := [12]string{
|
||||
inStr := []string{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"}
|
||||
outStr := []string{
|
||||
"4330397376401421145", "14124799381142128323", "8742572140681234676",
|
||||
"14345658006221440202", "15524073338516903644", "5091405722150716653",
|
||||
"15002163819607624508", "2047012902665707362", "16106391063450633726",
|
||||
"4680844749859802542", "15019775476387350140", "1698615465718385111",
|
||||
}
|
||||
|
||||
var inBigInt [12]big.Int
|
||||
var outBigInt [12]big.Int
|
||||
|
||||
for i := 0; i < 12; i++ {
|
||||
inTmp := new(big.Int)
|
||||
inTmp, _ = inTmp.SetString(inStr[i], 10)
|
||||
inBigInt[i] = *inTmp
|
||||
|
||||
outTmp := new(big.Int)
|
||||
outTmp, _ = outTmp.SetString(outStr[i], 10)
|
||||
outBigInt[i] = *outTmp
|
||||
}
|
||||
|
||||
testCase(inBigInt, outBigInt)
|
||||
var in [12]frontend.Variable
|
||||
var out [12]frontend.Variable
|
||||
copy(in[:], utils.StrArrayToFrontendVariableArray(inStr))
|
||||
copy(out[:], utils.StrArrayToFrontendVariableArray(outStr))
|
||||
testCase(in, out)
|
||||
}
|
||||
|
||||
func TestPoseidonProof(t *testing.T) {
|
||||
inStr := [12]string{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"}
|
||||
outStr := [12]string{
|
||||
inStr := []string{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"}
|
||||
outStr := []string{
|
||||
"4330397376401421145", "14124799381142128323", "8742572140681234676",
|
||||
"14345658006221440202", "15524073338516903644", "5091405722150716653",
|
||||
"15002163819607624508", "2047012902665707362", "16106391063450633726",
|
||||
"4680844749859802542", "15019775476387350140", "1698615465718385111",
|
||||
}
|
||||
|
||||
var in [12]frontend.Variable
|
||||
var out [12]frontend.Variable
|
||||
|
||||
for i := 0; i < 12; i++ {
|
||||
inTmp := new(big.Int)
|
||||
inTmp, _ = inTmp.SetString(inStr[i], 10)
|
||||
in[i] = *inTmp
|
||||
|
||||
outTmp := new(big.Int)
|
||||
outTmp, _ = outTmp.SetString(outStr[i], 10)
|
||||
out[i] = *outTmp
|
||||
}
|
||||
copy(in[:], utils.StrArrayToFrontendVariableArray(inStr))
|
||||
copy(out[:], utils.StrArrayToFrontendVariableArray(outStr))
|
||||
|
||||
circuit := TestPoseidonCircuit{In: in, Out: out}
|
||||
assignment := TestPoseidonCircuit{In: in, Out: out}
|
||||
|
||||
60
poseidon/public_inputs_hash_test.go
Normal file
60
poseidon/public_inputs_hash_test.go
Normal file
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user