-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathverifier.go
More file actions
148 lines (122 loc) · 4.57 KB
/
verifier.go
File metadata and controls
148 lines (122 loc) · 4.57 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
/*
#include <stdlib.h>
#include <stdint.h>
typedef struct ListRef {
const uint8_t *ptr;
uintptr_t len;
} ListRef;
*/
import "C"
import (
"bytes"
"github.com/yetanotherco/go-circom-prover-verifier/parsers"
"github.com/yetanotherco/go-circom-prover-verifier/verifier"
"log"
"unsafe"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend/groth16"
"github.com/consensys/gnark/backend/plonk"
"github.com/consensys/gnark/backend/witness"
)
func listRefToBytes(listRef C.ListRef) []byte {
if listRef.len == 0 {
return []byte{}
}
return C.GoBytes(unsafe.Pointer(listRef.ptr), C.int(listRef.len))
}
func main() {}
//export VerifyPlonkProofBLS12_381
func VerifyPlonkProofBLS12_381(proofBytes C.ListRef, pubInputBytes C.ListRef, verificationKeyBytes C.ListRef) bool {
return verifyPlonkProof(proofBytes, pubInputBytes, verificationKeyBytes, ecc.BLS12_381)
}
//export VerifyPlonkProofBN254
func VerifyPlonkProofBN254(proofBytes C.ListRef, pubInputBytes C.ListRef, verificationKeyBytes C.ListRef) bool {
return verifyPlonkProof(proofBytes, pubInputBytes, verificationKeyBytes, ecc.BN254)
}
//export VerifyGroth16ProofBN254
func VerifyGroth16ProofBN254(proofBytes C.ListRef, pubInputBytes C.ListRef, verificationKeyBytes C.ListRef) bool {
return verifyGroth16Proof(proofBytes, pubInputBytes, verificationKeyBytes, ecc.BN254)
}
// verifyPlonkProof contains the common proof verification logic.
func verifyPlonkProof(proofBytesRef C.ListRef, pubInputBytesRef C.ListRef, verificationKeyBytesRef C.ListRef, curve ecc.ID) bool {
proofBytes := listRefToBytes(proofBytesRef)
pubInputBytes := listRefToBytes(pubInputBytesRef)
verificationKeyBytes := listRefToBytes(verificationKeyBytesRef)
proofReader := bytes.NewReader(proofBytes)
proof := plonk.NewProof(curve)
if _, err := proof.ReadFrom(proofReader); err != nil {
log.Printf("Could not deserialize proof: %v", err)
return false
}
pubInputReader := bytes.NewReader(pubInputBytes)
pubInput, err := witness.New(curve.ScalarField())
if err != nil {
log.Printf("Error instantiating witness: %v", err)
return false
}
if _, err = pubInput.ReadFrom(pubInputReader); err != nil {
log.Printf("Could not read PLONK public input: %v", err)
return false
}
verificationKeyReader := bytes.NewReader(verificationKeyBytes)
verificationKey := plonk.NewVerifyingKey(curve)
if _, err = verificationKey.ReadFrom(verificationKeyReader); err != nil {
log.Printf("Could not read PLONK verifying key from bytes: %v", err)
return false
}
err = plonk.Verify(proof, verificationKey, pubInput)
return err == nil
}
// verifyGroth16Proof contains the common proof verification logic.
func verifyGroth16Proof(proofBytesRef C.ListRef, pubInputBytesRef C.ListRef, verificationKeyBytesRef C.ListRef, curve ecc.ID) bool {
proofBytes := listRefToBytes(proofBytesRef)
pubInputBytes := listRefToBytes(pubInputBytesRef)
verificationKeyBytes := listRefToBytes(verificationKeyBytesRef)
proofReader := bytes.NewReader(proofBytes)
proof := groth16.NewProof(curve)
if _, err := proof.ReadFrom(proofReader); err != nil {
log.Printf("Could not deserialize proof: %v", err)
return false
}
pubInputReader := bytes.NewReader(pubInputBytes)
pubInput, err := witness.New(curve.ScalarField())
if err != nil {
log.Printf("Error instantiating witness: %v", err)
return false
}
if _, err = pubInput.ReadFrom(pubInputReader); err != nil {
log.Printf("Could not read Groth16 public input: %v", err)
return false
}
verificationKeyReader := bytes.NewReader(verificationKeyBytes)
verificationKey := groth16.NewVerifyingKey(curve)
if _, err = verificationKey.ReadFrom(verificationKeyReader); err != nil {
log.Printf("Could not read Groth16 verifying key from bytes: %v", err)
return false
}
err = groth16.Verify(proof, verificationKey, pubInput)
return err == nil
}
//export VerifyCircomGroth16ProofBN128
func VerifyCircomGroth16ProofBN128(proofBytesRef C.ListRef, pubInputBytesRef C.ListRef, verificationKeyBytesRef C.ListRef) bool {
proofBytes := listRefToBytes(proofBytesRef)
pubInputBytes := listRefToBytes(pubInputBytesRef)
verificationKeyBytes := listRefToBytes(verificationKeyBytesRef)
proof, err := parsers.ParseProof(proofBytes)
if err != nil {
log.Printf("Could not parse proof: %v", err)
return false
}
public, err := parsers.ParsePublicSignals(pubInputBytes)
if err != nil {
log.Printf("Could not parse public signals: %v", err)
return false
}
vk, err := parsers.ParseVk(verificationKeyBytes)
if err != nil {
log.Printf("Could not parse verification key: %v", err)
return false
}
return verifier.Verify(vk, proof, public)
}