-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit_app.go
130 lines (109 loc) · 3.19 KB
/
unit_app.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
package main
import (
"encoding/hex"
"fmt"
"os"
"strings"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark-crypto/kzg"
"github.com/consensys/gnark/backend/plonk"
cs "github.com/consensys/gnark/constraint/bn254"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/std/algebra/emulated/sw_bn254"
recursive_plonk "github.com/consensys/gnark/std/recursion/plonk"
"github.com/consensys/gnark/test/unsafekzg"
"github.com/lightec-xyz/chainark"
"github.com/lightec-xyz/chainark/example"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("usage: ./unit setup")
fmt.Println("usage: ./unit prove beginIdHex endIdHex")
return
}
ccs := example.NewUnitCcs()
if strings.EqualFold(os.Args[1], "setup") {
fmt.Println("setting up... ")
scs := ccs.(*cs.SparseR1CS)
var srs, srsLagrange kzg.SRS
// let's generate the files again
srs, srsLagrange, err := unsafekzg.NewSRS(scs, unsafekzg.WithFSCache())
if err != nil {
panic(err)
}
pk, vk, err := plonk.Setup(ccs, srs, srsLagrange)
//_, err := plonk.Setup(r1cs, kate, &publicWitness)
if err != nil {
panic(err)
}
pkFile, err := os.Create(example.UnitPkeyFile)
if err != nil {
panic(err)
}
pk.WriteTo(pkFile)
pkFile.Close()
vkFile, err := os.Create(example.UnitVkeyFile)
if err != nil {
panic(err)
}
vk.WriteTo(vkFile)
vkFile.Close()
fmt.Println("saved pkey and vkey")
return
}
idHexLen := example.IDLength * example.LinkageIDBitsPerElement * 2 / 8
if len(os.Args) < 4 || len(os.Args[2]) != idHexLen || len(os.Args[3]) != idHexLen {
fmt.Println("usage: ./unit prove beginIdHex endIdHex\nNote that the Id is some value of SHA256, thus 32 bytes.")
return
}
beginHex := os.Args[2]
beginBytes := make([]byte, len(beginHex)/2)
endHex := os.Args[3]
endBytes := make([]byte, len(endHex)/2)
hex.Decode(beginBytes, []byte(beginHex))
hex.Decode(endBytes, []byte(endHex))
bId := chainark.LinkageIDFromBytes(beginBytes, example.LinkageIDBitsPerElement)
eId := chainark.LinkageIDFromBytes(endBytes, example.LinkageIDBitsPerElement)
w := example.UnitCircuit[sw_bn254.ScalarField, sw_bn254.G1Affine, sw_bn254.G2Affine, sw_bn254.GTEl]{
BeginID: bId,
EndID: eId,
}
witness, err := frontend.NewWitness(&w, ecc.BN254.ScalarField(), frontend.PublicOnly())
if err != nil {
panic(err)
}
fmt.Println("loading keys ...")
pkey := plonk.NewProvingKey(ecc.BN254)
vkey := plonk.NewVerifyingKey(ecc.BN254)
pkFile, err := os.Open(example.UnitPkeyFile)
if err != nil {
panic(err)
}
pkey.ReadFrom(pkFile)
pkFile.Close()
vkFile, err := os.Open(example.UnitVkeyFile)
if err != nil {
panic(err)
}
vkey.ReadFrom(vkFile)
vkFile.Close()
fmt.Println("proving ...")
proof, err := plonk.Prove(ccs, pkey, witness,
recursive_plonk.GetNativeProverOptions(ecc.BN254.ScalarField(), ecc.BN254.ScalarField()))
if err != nil {
panic(err)
}
fmt.Println("verifying ...")
err = plonk.Verify(proof, vkey, witness,
recursive_plonk.GetNativeVerifierOptions(ecc.BN254.ScalarField(), ecc.BN254.ScalarField()))
if err != nil {
panic(err)
}
proofFile, err := os.Create(example.UnitProofFile)
if err != nil {
panic(err)
}
proof.WriteTo(proofFile)
proofFile.Close()
fmt.Println("saved proof")
}