-
Notifications
You must be signed in to change notification settings - Fork 0
/
verifier.go
204 lines (162 loc) · 5.24 KB
/
verifier.go
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package worker
import (
"fmt"
"os"
"path/filepath"
"github.com/cf/gnark-plonky2-verifier/types"
"github.com/cf/gnark-plonky2-verifier/variables"
"github.com/cf/gnark-plonky2-verifier/verifier"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend/groth16"
"github.com/consensys/gnark/backend/witness"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/r1cs"
)
func NewGroth16Verifier(Path string, curveId ecc.ID) (*Groth16Verifier, error) {
vk := groth16.NewVerifyingKey(curveId)
proof := groth16.NewProof(curveId)
publicWitness, err := witness.New(curveId.ScalarField())
if err != nil {
return nil, fmt.Errorf("error creating public witness: %w", err)
}
return &Groth16Verifier{Path: Path, Vk: vk, Proof: proof, PublicWitness: publicWitness}, nil
}
func (w *Groth16Verifier) CheckPath() error {
files := []string{
"common_circuit_data.json",
"proof_with_public_inputs.json",
"verifier_only_circuit_data.json",
}
for _, file := range files {
path := filepath.Join(w.Path, file)
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("path %s does not exist", path)
} else {
return fmt.Errorf("error checking path %s: %s", path, err)
}
}
}
return nil
}
func (w *Groth16Verifier) Setup() error {
if err := w.CheckPath(); err != nil {
return fmt.Errorf("failed to check path: %w", err)
}
commonCircuitData := types.ReadCommonCircuitData(filepath.Join(w.Path, "common_circuit_data.json"))
proofWithPis := variables.DeserializeProofWithPublicInputs(types.ReadProofWithPublicInputs(filepath.Join(w.Path, "proof_with_public_inputs.json")))
verifierOnlyCircuitData := variables.DeserializeVerifierOnlyCircuitData(types.ReadVerifierOnlyCircuitData(filepath.Join(w.Path, "verifier_only_circuit_data.json")))
circuit := verifier.ExampleVerifierCircuit{
Proof: proofWithPis.Proof,
PublicInputs: proofWithPis.PublicInputs,
VerifierOnlyCircuitData: verifierOnlyCircuitData,
CommonCircuitData: commonCircuitData,
}
builder := r1cs.NewBuilder
r1cs, err := frontend.Compile(ecc.BLS12_381.ScalarField(), builder, &circuit)
if err != nil {
return fmt.Errorf("failed to compile circuit: %w", err)
}
var errSetup error
_, w.Vk, errSetup = groth16.Setup(r1cs)
if errSetup != nil {
return fmt.Errorf("error in setting up circuit: %w", errSetup)
}
return nil
}
func (w *Groth16Verifier) Verify() error {
if w.Vk == nil || w.PublicWitness == nil || w.Proof == nil {
return fmt.Errorf("Verifier keys, public witness, or proof are not set")
}
if err := groth16.Verify(w.Proof, w.Vk, w.PublicWitness); err != nil {
return fmt.Errorf("error in verifying proof: %w", err)
}
return nil
}
func (w *Groth16Verifier) ReadVerifyingKey(keyPath string) error {
if w.Vk == nil {
return fmt.Errorf("verifying key is not initialized")
}
verifyingKeyFile, err := os.Open(keyPath)
if err != nil {
return fmt.Errorf("failed to open verifying key file: %w", err)
}
defer verifyingKeyFile.Close()
_, err = w.Vk.ReadFrom(verifyingKeyFile)
if err != nil {
return fmt.Errorf("failed to read verifying key: %w", err)
}
return nil
}
func (w *Groth16Verifier) WriteVerifyingKey(keyPath string) error {
if w.Vk == nil {
return fmt.Errorf("verifying key is not initialized")
}
fVK, err := os.Create(keyPath)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer fVK.Close()
if _, err := w.Vk.WriteTo(fVK); err != nil {
return fmt.Errorf("failed to write verifying key to file: %w", err)
}
return nil
}
func (w *Groth16Verifier) ReadProof(keyPath string) error {
if w.Proof == nil {
return fmt.Errorf("proof is not initialized")
}
proofFile, err := os.Open(keyPath)
if err != nil {
return fmt.Errorf("failed to open proof file: %w", err)
}
defer proofFile.Close()
_, err = w.Proof.ReadFrom(proofFile)
if err != nil {
return fmt.Errorf("failed to read proof: %w", err)
}
return nil
}
func (w *Groth16Verifier) WriteProof(keyPath string) error {
if w.Proof == nil {
return fmt.Errorf("verifier or proof is not initialized")
}
fVK, err := os.Create(keyPath)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer fVK.Close()
if _, err := w.Proof.WriteTo(fVK); err != nil {
return fmt.Errorf("failed to write proof to file: %w", err)
}
return nil
}
func (w *Groth16Verifier) ReadPublicInputs(inputPath string) error {
if w.PublicWitness == nil {
return fmt.Errorf("public inputs is not initialized")
}
publicinputsFile, err := os.Open(inputPath)
if err != nil {
return fmt.Errorf("failed to open public inputs file: %w", err)
}
defer publicinputsFile.Close()
_, err = w.PublicWitness.ReadFrom(publicinputsFile)
if err != nil {
return fmt.Errorf("failed to read public inputs: %w", err)
}
return nil
}
func (w *Groth16Verifier) WritePublicInputs(inputPath string) error {
if w.PublicWitness == nil {
return fmt.Errorf("public inputs is not initialized")
}
publicinputsFile, err := os.Create(inputPath)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer publicinputsFile.Close()
if _, err := w.PublicWitness.WriteTo(publicinputsFile); err != nil {
return fmt.Errorf("failed to write proof to file: %w", err)
}
return nil
}