Skip to content

Commit

Permalink
compile and save methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirhemo committed Aug 28, 2023
1 parent e0488a4 commit f465177
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 15 deletions.
10 changes: 6 additions & 4 deletions plonky2x-verifier/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ func main() {
circuitName := flag.String("circuit", "", "Circuit data directory")
proofFlag := flag.Bool("verify", false, "profile the circuit")
testFlag := flag.Bool("test", false, "test the circuit")
compileFlag := flag.Bool("compile", false, "Compile the universal verifier circuit")
serializeFlag := flag.Bool("serialize", false, "Serialize the universal verifier circuit")
compileFlag := flag.Bool("compile", false, "Compile and save the universal verifier circuit")
flag.Parse()

if *circuitName == "" {
Expand All @@ -34,9 +33,12 @@ func main() {
if *compileFlag {
fmt.Println("Checking for an existing verifier circuit")
fmt.Println("Compiling verifier circuit")
if *serializeFlag {
fmt.Println("Serializing verifier circuit")
r1cs, pk, vk, err := CompileVerifierCircuit("./data/dummy")
if err != nil {
fmt.Println("Failed to compile verifier circuit:", err)
os.Exit(1)
}
SaveVerifierCircuit("./build", r1cs, pk, vk)
}

if *proofFlag {
Expand Down
75 changes: 64 additions & 11 deletions plonky2x-verifier/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func VerifierCircuitTest(circuitPath string, dummyCircuitPath string) error {
return test.IsSolved(&circuit, &witness, ecc.BN254.ScalarField())
}

func Compile(dummyCircuitPath string) (constraint.ConstraintSystem, groth16.ProvingKey, groth16.VerifyingKey) {
func CompileVerifierCircuit(dummyCircuitPath string) (constraint.ConstraintSystem, groth16.ProvingKey, groth16.VerifyingKey, error) {
verifierOnlyCircuitData := verifier.DeserializeVerifierOnlyCircuitData(dummyCircuitPath + "/verifier_only_circuit_data.json")
proofWithPis := verifier.DeserializeProofWithPublicInputs(dummyCircuitPath + "/proof_with_public_inputs.json")
circuit := Plonky2xVerifierCircuit{
Expand All @@ -62,24 +62,77 @@ func Compile(dummyCircuitPath string) (constraint.ConstraintSystem, groth16.Prov
}
r1cs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit)
if err != nil {
fmt.Println("error in building circuit", err)
os.Exit(1)
return nil, nil, nil, err
}

fmt.Println("Running circuit setup", time.Now())
pk, vk, err := groth16.Setup(r1cs)
if err != nil {
fmt.Println(err)
return nil, nil, nil, err
}

return r1cs, pk, vk, nil
}

func SaveVerifierCircuit(path string, r1cs constraint.ConstraintSystem, pk groth16.ProvingKey, vk groth16.VerifyingKey) error {

fmt.Println("Saving circuit constraints to", path+"/r1cs.bin")
r1csFile, err := os.Create(path + "/r1cs.bin")
if err != nil {
fmt.Println("error in creating r1cs file", err)
os.Exit(1)
}
r1cs.WriteTo(r1csFile)
r1csFile.Close()
fmt.Println("Successfully saved circuit constraints")

fmt.Println("Saving proving key to", path+"/pk.bin")
pkFile, err := os.Create(path + "/pk.bin")
if err != nil {
return err
}
pk.WriteRawTo(pkFile)
pkFile.Close()
fmt.Println("Successfully saved proving key")

fmt.Println("Saving verifying key to", path+"/vk.bin")
vkFile, err := os.Create(path + "/vk.bin")
if err != nil {
return err
}
vk.WriteRawTo(vkFile)
vkFile.Close()
fmt.Println("Successfully saved verifying key")

return r1cs, pk, vk
return nil
}

func Save(path string, r1cs constraint.ConstraintSystem, pk groth16.ProvingKey, vk groth16.VerifyingKey) {
// fmt.Println("Saving proving key to", path+"/pk.bin")
// pk.Write(path + "/pk.bin")
// fmt.Println("Saving verifying key to", path+"/vk.bin")
// vk.Write(path + "/vk.bin")
//
func Prove(circuitPath string, r1cs constraint.ConstraintSystem, pk groth16.ProvingKey) (groth16.Proof, error) {
verifierOnlyCircuitData := verifier.DeserializeVerifierOnlyCircuitData(circuitPath + "/verifier_only_circuit_data.json")
proofWithPis := verifier.DeserializeProofWithPublicInputs(circuitPath + "/proof_with_public_inputs.json")

// Circuit assignment
assignment := &Plonky2xVerifierCircuit{
ProofWithPis: proofWithPis,
VerifierData: verifierOnlyCircuitData,
CircuitPath: circuitPath,
}

fmt.Println("Generating witness")
start := time.Now()
witness, err := frontend.NewWitness(assignment, ecc.BN254.ScalarField())
if err != nil {
return nil, err
}
fmt.Println("Successfully generated witness, time: ", time.Since(start))

fmt.Println("Creating proof")
start = time.Now()
proof, err := groth16.Prove(r1cs, pk, witness)
if err != nil {
return nil, err
}
fmt.Println("Successfully created proof, time: ", time.Since(start))

return proof, nil
}

0 comments on commit f465177

Please sign in to comment.