-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
143 lines (121 loc) · 4.22 KB
/
main.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
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/constraint"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/r1cs"
"github.com/hyle-org/hyle/x/zktx/keeper/gnark"
)
func Setup[T frontend.Circuit](circuit T) (constraint.ConstraintSystem, groth16.ProvingKey, groth16.VerifyingKey) {
compiledCircuit, _ := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, circuit)
// groth16 zkSNARK: Setup
provingKey, verifyingKey, _ := groth16.Setup(compiledCircuit)
return compiledCircuit, provingKey, verifyingKey
}
func GenerateProof(ccs constraint.ConstraintSystem, witness witness.Witness, pk groth16.ProvingKey, vk groth16.VerifyingKey) (gnark.Groth16Proof, error) {
// First generate the actual groth16 proof object
proof, err := groth16.Prove(ccs, pk, witness)
if err != nil {
return gnark.Groth16Proof{}, err
}
// Then serialize all that in the format Hylé expects
var proofBuf bytes.Buffer
_, err = proof.WriteTo(&proofBuf)
if err != nil {
return gnark.Groth16Proof{}, err
}
var vkBuf bytes.Buffer
_, err = vk.WriteTo(&vkBuf)
if err != nil {
return gnark.Groth16Proof{}, err
}
publicWitness, err := witness.Public()
if err != nil {
return gnark.Groth16Proof{}, err
}
var publicWitnessBuf bytes.Buffer
_, err = publicWitness.WriteTo(&publicWitnessBuf)
if err != nil {
return gnark.Groth16Proof{}, err
}
return gnark.Groth16Proof{
Proof: proofBuf.Bytes(),
VerifyingKey: vkBuf.Bytes(),
PublicWitness: publicWitnessBuf.Bytes(),
}, nil
}
// This function exists to preserve private keys across runs
func loadCircuit(path string, circuit frontend.Circuit) (constraint.ConstraintSystem, groth16.ProvingKey, groth16.VerifyingKey, error) {
// Read from file
f, err := os.Open(path)
if err != nil {
// Create a new circuit
css, pk, vk := Setup(circuit)
// Serialize it as an example and so the pk/vk don't change
var buf bytes.Buffer
pk.WriteTo(&buf)
vk.WriteTo(&buf)
// There appears to be a bug in gnark 0.9 where readFrom reads too many bytes here, likely fixed in >= 0.11
// So I'll put this last
css.WriteTo(&buf)
f, _ := os.Create(path)
f.Write(buf.Bytes())
return css, pk, vk, nil
} else {
pk := groth16.NewProvingKey(ecc.BN254)
pk.ReadFrom(f)
vk := groth16.NewVerifyingKey(ecc.BN254)
vk.ReadFrom(f)
css := groth16.NewCS(ecc.BN254)
css.ReadFrom(f)
return css, pk, vk, nil
}
}
func simpleMain() {
css, pk, vk, err := loadCircuit("simple_circuit.bin", &SimpleCircuit{})
if err != nil {
panic(err)
}
witness, _ := (&SimpleCircuit{}).CreateWitness(4)
hyle_proof, _ := GenerateProof(css, witness, pk, vk)
hyle_proof_marshalled, _ := json.Marshal(hyle_proof)
f, _ := os.Create("simple_proof.json")
f.Write(hyle_proof_marshalled)
fmt.Println("Proof generated and saved to simple_proof.json")
fmt.Println("Verifying key:", base64.StdEncoding.EncodeToString(hyle_proof.VerifyingKey))
fmt.Println("Verify with hyled tx zktx verify")
}
func collatzMain() {
css, pk, vk, err := loadCircuit("collatz_circuit.bin", &CollatzCircuit{
HyleCircuit: gnark.HyleCircuit{
// We need to specify the lengths of the arrays
Input: []frontend.Variable{1},
Output: []frontend.Variable{1},
},
})
if err != nil {
panic(err)
}
witness, _ := (&CollatzCircuit{}).CreateResetWitness(4, "toto.collatz")
hyle_proof, _ := GenerateProof(css, witness, pk, vk)
hyle_proof_marshalled, _ := json.Marshal(hyle_proof)
f, _ := os.Create("collatz_proof.json")
f.Write(hyle_proof_marshalled)
parsedData, _ := hyle_proof.ExtractData(witness)
fmt.Println("Proof generated and saved to collatz_proof.json")
fmt.Println("Initial state:", base64.StdEncoding.EncodeToString(parsedData.InitialState))
fmt.Println("Verifying key:", base64.StdEncoding.EncodeToString(hyle_proof.VerifyingKey))
fmt.Println("Register the contract with `hyled tx zktx register [your address] gnark-groth16-te-BN254 [verifying key] collatz [initial state]`")
fmt.Println("Execute a state transition with `hyled tx zktx execute collatz [path to collatz_proof.json]`")
}
func main() {
simpleMain()
collatzMain()
}