mirror of
https://github.com/arnaucube/gnark-plonky2-verifier.git
synced 2026-01-12 09:01:32 +01:00
added plonky2_verifier_benchmark
This commit is contained in:
@@ -9,8 +9,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ChallengerChip struct {
|
type ChallengerChip struct {
|
||||||
api frontend.API
|
api frontend.API `gnark:"-"`
|
||||||
field frontend.API
|
field frontend.API `gnark:"-"`
|
||||||
poseidonChip *poseidon.PoseidonChip
|
poseidonChip *poseidon.PoseidonChip
|
||||||
spongeState [poseidon.SPONGE_WIDTH]F
|
spongeState [poseidon.SPONGE_WIDTH]F
|
||||||
inputBuffer []F
|
inputBuffer []F
|
||||||
@@ -21,6 +21,11 @@ func NewChallengerChip(api frontend.API, field frontend.API, poseidonChip *posei
|
|||||||
var spongeState [poseidon.SPONGE_WIDTH]F
|
var spongeState [poseidon.SPONGE_WIDTH]F
|
||||||
var inputBuffer []F
|
var inputBuffer []F
|
||||||
var outputBuffer []F
|
var outputBuffer []F
|
||||||
|
|
||||||
|
for i := 0; i < poseidon.SPONGE_WIDTH; i++ {
|
||||||
|
spongeState[i] = ZERO_F
|
||||||
|
}
|
||||||
|
|
||||||
return &ChallengerChip{
|
return &ChallengerChip{
|
||||||
api: api,
|
api: api,
|
||||||
field: field,
|
field: field,
|
||||||
|
|||||||
@@ -14,15 +14,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type FriChip struct {
|
type FriChip struct {
|
||||||
api frontend.API
|
api frontend.API `gnark:"-"`
|
||||||
fieldAPI frontend.API
|
fieldAPI frontend.API `gnark:"-"`
|
||||||
qeAPI *QuadraticExtensionAPI
|
qeAPI *QuadraticExtensionAPI `gnark:"-"`
|
||||||
hashAPI *HashAPI
|
hashAPI *HashAPI `gnark:"-"`
|
||||||
|
|
||||||
poseidonChip *poseidon.PoseidonChip
|
poseidonChip *poseidon.PoseidonChip
|
||||||
|
|
||||||
friParams *FriParams
|
friParams *FriParams `gnark:"-"`
|
||||||
verifierOnlyCircuitData *VerifierOnlyCircuitData
|
verifierOnlyCircuitData *VerifierOnlyCircuitData `gnark:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFriChip(
|
func NewFriChip(
|
||||||
|
|||||||
@@ -32,14 +32,14 @@ var QUOTIENT = PlonkOracle{
|
|||||||
}
|
}
|
||||||
|
|
||||||
type PlonkChip struct {
|
type PlonkChip struct {
|
||||||
api frontend.API
|
api frontend.API `gnark:"-"`
|
||||||
qeAPI *QuadraticExtensionAPI
|
qeAPI *QuadraticExtensionAPI `gnark:"-"`
|
||||||
|
|
||||||
commonData CommonCircuitData
|
commonData CommonCircuitData `gnark:"-"`
|
||||||
|
|
||||||
DEGREE F
|
DEGREE F `gnark:"-"`
|
||||||
DEGREE_BITS_F F
|
DEGREE_BITS_F F `gnark:"-"`
|
||||||
DEGREE_QE QuadraticExtension
|
DEGREE_QE QuadraticExtension `gnark:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPlonkChip(api frontend.API, qeAPI *QuadraticExtensionAPI, commonData CommonCircuitData) *PlonkChip {
|
func NewPlonkChip(api frontend.API, qeAPI *QuadraticExtensionAPI, commonData CommonCircuitData) *PlonkChip {
|
||||||
|
|||||||
@@ -8,9 +8,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type VerifierChip struct {
|
type VerifierChip struct {
|
||||||
api frontend.API
|
api frontend.API `gnark:"-"`
|
||||||
fieldAPI frontend.API
|
fieldAPI frontend.API `gnark:"-"`
|
||||||
qeAPI *QuadraticExtensionAPI
|
qeAPI *QuadraticExtensionAPI `gnark:"-"`
|
||||||
poseidonChip *poseidon.PoseidonChip
|
poseidonChip *poseidon.PoseidonChip
|
||||||
plonkChip *PlonkChip
|
plonkChip *PlonkChip
|
||||||
friChip *FriChip
|
friChip *FriChip
|
||||||
|
|||||||
94
plonky2_verifier_benchmark.go
Normal file
94
plonky2_verifier_benchmark.go
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
. "gnark-ed25519/field"
|
||||||
|
. "gnark-ed25519/plonky2_verifier"
|
||||||
|
"gnark-ed25519/poseidon"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/consensys/gnark-crypto/ecc"
|
||||||
|
"github.com/consensys/gnark/backend/groth16"
|
||||||
|
"github.com/consensys/gnark/frontend"
|
||||||
|
"github.com/consensys/gnark/frontend/cs/r1cs"
|
||||||
|
)
|
||||||
|
|
||||||
|
type BenchmarkPlonky2VerifierCircuit struct {
|
||||||
|
proofWithPis ProofWithPublicInputs
|
||||||
|
|
||||||
|
verifierChip *VerifierChip
|
||||||
|
}
|
||||||
|
|
||||||
|
func (circuit *BenchmarkPlonky2VerifierCircuit) Define(api frontend.API) error {
|
||||||
|
proofWithPis := DeserializeProofWithPublicInputs("./plonky2_verifier/data/dummy_2^14_gates/proof_with_public_inputs.json")
|
||||||
|
commonCircuitData := DeserializeCommonCircuitData("./plonky2_verifier/data/dummy_2^14_gates/common_circuit_data.json")
|
||||||
|
verifierOnlyCircuitData := DeserializeVerifierOnlyCircuitData("./plonky2_verifier/data/dummy_2^14_gates/verifier_only_circuit_data.json")
|
||||||
|
|
||||||
|
fieldAPI := NewFieldAPI(api)
|
||||||
|
qeAPI := NewQuadraticExtensionAPI(fieldAPI, commonCircuitData.DegreeBits)
|
||||||
|
hashAPI := NewHashAPI(fieldAPI)
|
||||||
|
poseidonChip := poseidon.NewPoseidonChip(api, fieldAPI)
|
||||||
|
friChip := NewFriChip(api, fieldAPI, qeAPI, hashAPI, poseidonChip, &commonCircuitData.FriParams)
|
||||||
|
plonkChip := NewPlonkChip(api, qeAPI, commonCircuitData)
|
||||||
|
circuit.verifierChip = NewVerifierChip(api, fieldAPI, qeAPI, poseidonChip, plonkChip, friChip)
|
||||||
|
|
||||||
|
circuit.verifierChip.Verify(proofWithPis, verifierOnlyCircuitData, commonCircuitData)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func compileCircuit() frontend.CompiledConstraintSystem {
|
||||||
|
circuit := BenchmarkPlonky2VerifierCircuit{}
|
||||||
|
proofWithPis := DeserializeProofWithPublicInputs("./plonky2_verifier/data/dummy_2^14_gates/proof_with_public_inputs.json")
|
||||||
|
circuit.proofWithPis = proofWithPis
|
||||||
|
|
||||||
|
r1cs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("error in building circuit", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r1cs
|
||||||
|
}
|
||||||
|
|
||||||
|
func createProof(r1cs frontend.CompiledConstraintSystem) groth16.Proof {
|
||||||
|
proofWithPis := DeserializeProofWithPublicInputs("./plonky2_verifier/data/dummy_2^14_gates/proof_with_public_inputs.json")
|
||||||
|
|
||||||
|
// Witness
|
||||||
|
assignment := &BenchmarkPlonky2VerifierCircuit{
|
||||||
|
proofWithPis: proofWithPis,
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Generating witness", time.Now())
|
||||||
|
witness, _ := frontend.NewWitness(assignment, ecc.BN254.ScalarField())
|
||||||
|
publicWitness, _ := witness.Public()
|
||||||
|
|
||||||
|
fmt.Println("Running circuit setup", time.Now())
|
||||||
|
pk, vk, err := groth16.Setup(r1cs)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Creating proof", time.Now())
|
||||||
|
proof, err := groth16.Prove(r1cs, pk, witness)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
fmt.Println("Verifying proof", time.Now())
|
||||||
|
err = groth16.Verify(proof, vk, publicWitness)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return proof
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r1cs := compileCircuit()
|
||||||
|
proof := createProof(r1cs)
|
||||||
|
fmt.Println(proof.CurveID(), time.Now())
|
||||||
|
}
|
||||||
@@ -17,8 +17,8 @@ const SPONGE_RATE = 8
|
|||||||
|
|
||||||
type PoseidonState = [WIDTH]F
|
type PoseidonState = [WIDTH]F
|
||||||
type PoseidonChip struct {
|
type PoseidonChip struct {
|
||||||
api frontend.API
|
api frontend.API `gnark:"-"`
|
||||||
field frontend.API
|
field frontend.API `gnark:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPoseidonChip(api frontend.API, field frontend.API) *PoseidonChip {
|
func NewPoseidonChip(api frontend.API, field frontend.API) *PoseidonChip {
|
||||||
|
|||||||
Reference in New Issue
Block a user