-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
268 lines (226 loc) · 6.33 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
"crypto/rand"
"fmt"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark-crypto/ecc/twistededwards"
"github.com/consensys/gnark-crypto/hash"
"github.com/consensys/gnark-crypto/signature/eddsa"
"github.com/consensys/gnark/backend/groth16"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/r1cs"
mrand "math/rand"
"time"
)
func GetEddsaCircuit() *eddsaCircuit {
// instantiate hash function
hFunc := hash.MIMC_BN254.New()
// create a eddsa key pair
privateKey, err := eddsa.New(twistededwards.BN254, rand.Reader)
if err != nil {
fmt.Println("failed to create a key pair. error:", err)
return nil
}
publicKey := privateKey.Public()
msg := randomMsg()
fmt.Printf("%v message: %s\n", time.Now(), string(msg))
// sign the message
signature, err := privateKey.Sign(msg, hFunc)
if err != nil {
fmt.Println("failed to Sign. error:", err)
return nil
}
// declare the witness
var assignment eddsaCircuit
// assign message value
assignment.Message = msg
// public key bytes
_publicKey := publicKey.Bytes()
// assign public key values
assignment.PublicKey.Assign(twistededwards.BN254, _publicKey[:32])
// assign signature values
assignment.Signature.Assign(twistededwards.BN254, signature)
return &assignment
}
func GetEddsaCircuitBatch() *eddsaCircuitBatch {
var assignment eddsaCircuitBatch
for i := 0; i < BatchSize; i++ {
cr := GetEddsaCircuit()
assignment.PublicKey[i] = cr.PublicKey
assignment.Signature[i] = cr.Signature
assignment.Message[i] = cr.Message
}
return &assignment
}
func Prove() {
// compiles our circuit into a R1CS
var circuit eddsaCircuit
ccs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit)
if err != nil {
fmt.Println("failed to Compile. error:", err)
return
}
// groth16 zkSNARK: Setup
pk, vk, err := groth16.Setup(ccs)
if err != nil {
fmt.Println("failed to Setup. error:", err)
return
}
witness, err := frontend.NewWitness(GetEddsaCircuit(), ecc.BN254.ScalarField())
if err != nil {
fmt.Println("failed to NewWitness. error:", err)
return
}
publicWitness, err := witness.Public()
if err != nil {
fmt.Println("failed to witness.Public. error:", err)
return
}
// groth16: Prove & Verify
proof, err := groth16.Prove(ccs, pk, witness)
if err != nil {
fmt.Println("failed to Prove. error:", err)
return
}
err = groth16.Verify(proof, vk, publicWitness)
if err != nil {
fmt.Println("Prove get invalid signature")
} else {
fmt.Println("Prove get valid signature")
}
}
func ProveBatch() {
// compiles our circuit into a R1CS
var circuit eddsaCircuitBatch
ccs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit)
if err != nil {
fmt.Println("failed to Compile. error:", err)
return
}
// groth16 zkSNARK: Setup
pk, vk, err := groth16.Setup(ccs)
if err != nil {
fmt.Println("failed to Setup. error:", err)
return
}
witness, err := frontend.NewWitness(GetEddsaCircuitBatch(), ecc.BN254.ScalarField())
if err != nil {
fmt.Println("failed to NewWitness. error:", err)
return
}
publicWitness, err := witness.Public()
if err != nil {
fmt.Println("failed to witness.Public. error:", err)
return
}
// groth16: Prove & Verify
proof, err := groth16.Prove(ccs, pk, witness)
if err != nil {
fmt.Println("failed to Prove. error:", err)
return
}
err = groth16.Verify(proof, vk, publicWitness)
if err != nil {
fmt.Println("Prove get invalid signature")
} else {
fmt.Println("Prove get valid signature")
}
}
func main() {
//GenVerifyProve()
//Prove()
ProveBatch()
}
// Define a set of characters to choose from
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
// Generate a random string of length n
func randomMsg() []byte {
// Seed the random number generator
mrand.Seed(time.Now().UnixNano())
// Generate a random length between 1 and 31
n := mrand.Intn(31) + 1
b := make([]byte, n)
for i := range b {
b[i] = charset[mrand.Intn(len(charset))]
}
return b
}
func GenVerifyProve() {
// instantiate hash function
hFunc := hash.MIMC_BN254.New()
// create a eddsa key pair
privateKey, err := eddsa.New(twistededwards.BN254, rand.Reader)
if err != nil {
fmt.Println("failed to create a key pair. error:", err)
return
}
publicKey := privateKey.Public()
// note that the message is on 4 bytes
msg := []byte("privateKeyprivateKeyprivateKe")
//msg := []byte{0xde, 0xad, 0xf0, 0x0d, 0xde, 0xad, 0xf0, 0x0d, 0xde, 0xad, 0xf0, 0x0d, 0xde, 0xad, 0xf0, 0x0d, 0xf0, 0x0d, 0xad, 0xf0, 0x0d, 0xde, 0xad, 0xf0, 0x0d, 0xde, 0xad, 0xf0, 0x0d, 0xde, 0xad, 0xf0, 0x0d, 0xf0, 0x0d}
fmt.Printf("message: %s\n", string(msg))
// sign the message
signature, err := privateKey.Sign(msg, hFunc)
if err != nil {
fmt.Println("failed to Sign. error:", err)
return
}
// verifies signature
isValid, err := publicKey.Verify(signature, msg, hFunc)
if err != nil {
fmt.Println("failed to Verify. error:", err)
return
}
if !isValid {
fmt.Println("invalid signature")
return
} else {
fmt.Println("valid signature")
}
// compiles our circuit into a R1CS
var circuit eddsaCircuit
ccs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit)
if err != nil {
fmt.Println("failed to Compile. error:", err)
return
}
// groth16 zkSNARK: Setup
pk, vk, err := groth16.Setup(ccs)
if err != nil {
fmt.Println("failed to Setup. error:", err)
return
}
// witness definition
// declare the witness
var assignment eddsaCircuit
// assign message value
assignment.Message = msg
// public key bytes
_publicKey := publicKey.Bytes()
// assign public key values
assignment.PublicKey.Assign(twistededwards.BN254, _publicKey[:32])
// assign signature values
assignment.Signature.Assign(twistededwards.BN254, signature)
witness, err := frontend.NewWitness(&assignment, ecc.BN254.ScalarField())
if err != nil {
fmt.Println("failed to NewWitness. error:", err)
return
}
publicWitness, err := witness.Public()
if err != nil {
fmt.Println("failed to witness.Public. error:", err)
return
}
// groth16: Prove & Verify
proof, err := groth16.Prove(ccs, pk, witness)
if err != nil {
fmt.Println("failed to Prove. error:", err)
return
}
err = groth16.Verify(proof, vk, publicWitness)
if err != nil {
fmt.Println("Prove get invalid signature")
} else {
fmt.Println("Prove get valid signature")
}
}