-
Notifications
You must be signed in to change notification settings - Fork 4
/
verify.go
77 lines (66 loc) · 2.33 KB
/
verify.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
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"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/sindri-labs/forge-sample-data/gnark/compress/circuit"
)
type ProofDetailResponse struct {
Proof Proof `json:"proof"`
VerificationKey VerificationKey `json:"verification_key"`
PublicInputJson json.RawMessage `json:"public"`
}
type Proof struct {
EncodedProof string `json:"proof"`
}
type VerificationKey struct {
EncodedVerifyingKey string `json:"verifying_key"`
}
func exitOnError(err error, action string) {
if err != nil {
fmt.Printf("Error %s: %v\n", action, err)
os.Exit(1)
}
}
func main() {
// Parse the necessary data from proof detail response JSON.
var proofDetailResponse ProofDetailResponse
if len(os.Args) < 2 {
fmt.Println("Please provide the path to the proof detail JSON file as an argument.")
return
}
filename := os.Args[1]
data, err := os.ReadFile(filename)
exitOnError(err, "reading file")
err = json.Unmarshal(data, &proofDetailResponse)
exitOnError(err, "unmarshalling JSON")
// Load in the proof.
decodedProof, err := base64.StdEncoding.DecodeString(proofDetailResponse.Proof.EncodedProof)
exitOnError(err, "decoding proof")
proof := groth16.NewProof(ecc.BN254)
_, err = proof.ReadFrom(bytes.NewReader(decodedProof))
exitOnError(err, "reading proof")
// Load in the verifying key.
decodedVerifyingKey, err := base64.StdEncoding.DecodeString(proofDetailResponse.VerificationKey.EncodedVerifyingKey)
exitOnError(err, "decoding verifying key")
verifyingKey := groth16.NewVerifyingKey(ecc.BN254)
_, err = verifyingKey.ReadFrom(bytes.NewReader(decodedVerifyingKey))
exitOnError(err, "reading verifying key")
// Construct the witness based on the public inputs.
schema, err := frontend.NewSchema(&compress.Circuit{})
exitOnError(err, "constructing schema")
publicWitness, err := witness.New(ecc.BN254.ScalarField())
exitOnError(err, "constructing witness")
err = publicWitness.FromJSON(schema, proofDetailResponse.PublicInputJson)
exitOnError(err, "parsing public inputs")
// Verify the proof.
err = groth16.Verify(proof, verifyingKey, publicWitness)
exitOnError(err, "verifying proof")
fmt.Println("Proof verified successfully.")
}