-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathmain.go
More file actions
134 lines (116 loc) · 3.91 KB
/
main.go
File metadata and controls
134 lines (116 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"fmt"
"github.com/consensys/gnark"
"log"
"os"
"strings"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend/plonk"
cs "github.com/consensys/gnark/constraint/bn254"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/test/unsafekzg"
"github.com/consensys/gnark/frontend/cs/scs"
)
// CubicCircuit defines a simple circuit
// x**3 + x + 5 == y
type CubicCircuit struct {
// struct tags on a variable is optional
// default uses variable name and secret visibility.
X frontend.Variable `gnark:"x"`
Y frontend.Variable `gnark:",public"`
}
// Define declares the circuit constraints
// x**3 + x + 5 == y
func (circuit *CubicCircuit) Define(api frontend.API) error {
x3 := api.Mul(circuit.X, circuit.X, circuit.X)
api.AssertIsEqual(circuit.Y, api.Add(x3, circuit.X, 5))
return nil
}
func main() {
outputDir := "scripts/test_files/gnark_plonk_bn254_script/"
var gnarkVersion = strings.ReplaceAll(gnark.Version.String(), ".", "_")
var circuit CubicCircuit
// use scs.NewBuilder instead of r1cs.NewBuilder (groth16)
ccs, err := frontend.Compile(ecc.BN254.ScalarField(), scs.NewBuilder, &circuit)
if err != nil {
panic("circuit compilation error")
}
// use unsafekzg.NewSRS to generate the SRS and the Lagrange interpolation of the SRS
// Setup prepares the public data associated to a circuit + public inputs.
// The kzg SRS must be provided in canonical and lagrange form.
// For test purposes, see test/unsafekzg package. With an existing SRS generated through MPC in canonical form,
r1cs := ccs.(*cs.SparseR1CS)
srs, srsLagrangeInterpolation, err := unsafekzg.NewSRS(r1cs)
// srs, err := test.NewKZGSRS(r1cs)
if err != nil {
panic("KZG setup error")
}
// add srsLagrangeInterpolation to the Setup function
pk, vk, _ := plonk.Setup(ccs, srs, srsLagrangeInterpolation)
assignment := CubicCircuit{X: 3, Y: 35}
fullWitness, err := frontend.NewWitness(&assignment, ecc.BN254.ScalarField())
if err != nil {
log.Fatal(err)
}
publicWitness, err := frontend.NewWitness(&assignment, ecc.BN254.ScalarField(), frontend.PublicOnly())
if err != nil {
log.Fatal(err)
}
// This proof should be serialized for testing in the operator
proof, err := plonk.Prove(ccs, pk, fullWitness)
if err != nil {
panic("PLONK proof generation error")
}
// The proof is verified before writing it into a file to make sure it is valid.
err = plonk.Verify(proof, vk, publicWitness)
if err != nil {
panic("PLONK proof not verified")
}
// Open files for writing the proof, the verification key and the public witness
proofFile, err := os.Create(outputDir + "plonk_" + gnarkVersion + ".proof")
if err != nil {
panic(err)
}
vkFile, err := os.Create(outputDir + "plonk_" + gnarkVersion + ".vk")
if err != nil {
panic(err)
}
witnessFile, err := os.Create(outputDir + "plonk_pub_input_" + gnarkVersion + ".pub")
if err != nil {
panic(err)
}
defer func(proofFile *os.File) {
err := proofFile.Close()
if err != nil {
log.Fatal("could not close proof file:", err)
}
}(proofFile)
defer func(vkFile *os.File) {
err := vkFile.Close()
if err != nil {
log.Fatal("could not close verification key file:", err)
}
}(vkFile)
defer func(witnessFile *os.File) {
err := witnessFile.Close()
if err != nil {
log.Fatal("could not close witness file:", err)
}
}(witnessFile)
_, err = proof.WriteTo(proofFile)
if err != nil {
panic("could not serialize proof into file")
}
_, err = vk.WriteTo(vkFile)
if err != nil {
panic("could not serialize verification key into file")
}
_, err = publicWitness.WriteTo(witnessFile)
if err != nil {
panic("could not serialize proof into file")
}
fmt.Println("Proof written into " + outputDir + "plonk_" + gnarkVersion + ".proof")
fmt.Println("Verification key written into " + outputDir + "plonk_" + gnarkVersion + ".vk")
fmt.Println("Public witness written into " + outputDir + "plonk_pub_input_" + gnarkVersion + ".pub")
}