-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibapi.go
385 lines (300 loc) · 10.1 KB
/
libapi.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package zklib
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend"
native_groth16 "github.com/consensys/gnark/backend/groth16"
"github.com/consensys/gnark/constraint"
"github.com/consensys/gnark/std/recursion/groth16"
txivc "github.com/twostack/zklib/twostack/groth16"
"log"
"math/big"
"os"
"time"
)
/*
PreviousProof object to encapsulate the behaviour of
doing setup just once, and then repeatedly
constructing and verifying proofs and
*/
type ProofSystem struct {
InnerField *big.Int
OuterField *big.Int
verifierOptions backend.VerifierOption
proverOptions backend.ProverOption
//normal params
normalCurveId ecc.ID
normalCcs constraint.ConstraintSystem
normalVerifyingKey native_groth16.VerifyingKey
normalProvingKey native_groth16.ProvingKey
///
baseCurveId ecc.ID
baseCcs constraint.ConstraintSystem
baseVerifyingKey native_groth16.VerifyingKey
baseProvingKey native_groth16.ProvingKey
}
func NewProofSystem(normalPrefixSize int, normalPostfixSize int) (*ProofSystem, error) {
ps := &ProofSystem{}
ps.InnerField = txivc.InnerCurve.ScalarField()
ps.OuterField = txivc.OuterCurve.ScalarField()
err := ps.setupBaseCase(BASE_RAW_TX_SIZE)
if err != nil {
return nil, err
}
err = ps.setupNormalCase(normalPrefixSize, normalPostfixSize)
if err != nil {
return nil, err
}
return ps, nil
}
func (ps *ProofSystem) setupNormalCase(prefixSize int, postfixSize int) error {
//IMPORTANT: Normal proof needs to read the OUTER field's curveId
ps.normalCurveId = txivc.OuterCurve
normalCcs, provingKey, verifyingKey, err := ps.readNormalSetupParams(prefixSize, postfixSize, ps.OuterField)
if err != nil {
return err
}
ps.normalCcs = normalCcs
ps.normalProvingKey = provingKey
ps.normalVerifyingKey = verifyingKey
return nil
}
func (ps *ProofSystem) readNormalSetupParams(prefixSize int, postfixSize int, outerField *big.Int) (constraint.ConstraintSystem, native_groth16.ProvingKey, native_groth16.VerifyingKey, error) {
if _, err := os.Stat("norm_ccs.cbor"); errors.Is(err, os.ErrNotExist) {
//setup normal case for base parent VK
normalCcs, provingKey, verifyingKey, err := txivc.SetupNormalCase(prefixSize, postfixSize, outerField, ps.baseCcs)
//FIXME:
//normalCcs, provingKey, verifyingKey, err := txivc.SetupNormalCase(outerField, *normalCcs)
normalCcsFile, err := os.Create("norm_ccs.cbor")
_, err = normalCcs.WriteTo(normalCcsFile)
if err != nil {
return nil, nil, nil, err
}
normalCcsFile.Close()
err = writeKeys(verifyingKey, provingKey, "norm_")
if err != nil {
return nil, nil, nil, err
}
return normalCcs, provingKey, verifyingKey, nil
} else {
//in this portion we don't run Setup() again, because that generates different keys
normalCcs, err := ps.readCircuitParams("norm_", ps.normalCurveId)
if err != nil {
return nil, nil, nil, err
}
verifyingKey, provingKey, err := readKeys("norm_", ps.normalCurveId)
if err != nil {
return nil, nil, nil, err
}
return normalCcs, provingKey, verifyingKey, nil
}
}
func (ps *ProofSystem) setupBaseCase(baseTxSize int) error {
//IMPORTANT: Base proof needs to read the inner field's curveId
ps.baseCurveId = txivc.InnerCurve
ps.verifierOptions = groth16.GetNativeVerifierOptions(ps.OuterField, ps.InnerField)
ps.proverOptions = groth16.GetNativeProverOptions(ps.OuterField, ps.InnerField)
baseCcs, baseProvingKey, baseVerifyingKey, err := ps.readBaseParams(baseTxSize, ps.InnerField)
//ccs, err := frontend.Compile(po.InnerField, r1cs.NewBuilder, baseTxCircuit)
if err != nil {
return err
}
ps.baseCcs = baseCcs
ps.baseProvingKey = baseProvingKey
ps.baseVerifyingKey = baseVerifyingKey
return nil
}
func (ps *ProofSystem) readBaseParams(txSize int, innerField *big.Int) (constraint.ConstraintSystem, native_groth16.ProvingKey, native_groth16.VerifyingKey, error) {
if _, err := os.Stat("base_ccs.cbor"); errors.Is(err, os.ErrNotExist) {
baseCcs, provingKey, verifyingKey, err := txivc.SetupBaseCase(txSize, innerField)
baseccsFile, err := os.Create("base_ccs.cbor")
_, err = baseCcs.WriteTo(baseccsFile)
if err != nil {
return nil, nil, nil, err
}
baseccsFile.Close()
err = writeKeys(verifyingKey, provingKey, "base_")
if err != nil {
return nil, nil, nil, err
}
return baseCcs, provingKey, verifyingKey, nil
} else {
//in this portion we don't run Setup() again, because that generates different keys
baseCcs, err := ps.readCircuitParams("base_", ps.baseCurveId)
if err != nil {
return nil, nil, nil, err
}
verifyingKey, provingKey, err := readKeys("base_", ps.baseCurveId)
if err != nil {
return nil, nil, nil, err
}
return baseCcs, provingKey, verifyingKey, nil
}
}
func (ps *ProofSystem) readCircuitParams(prefix string, curveId ecc.ID) (constraint.ConstraintSystem, error) {
baseCcs := native_groth16.NewCS(curveId)
ccsFile, err := os.OpenFile(prefix+"ccs.cbor", os.O_RDONLY, 0444) //read-only
if err != nil {
return nil, err
}
_, err = baseCcs.ReadFrom(ccsFile)
if err != nil {
return nil, err
}
ccsFile.Close()
return baseCcs, nil
}
func writeKeys(verifyingKey native_groth16.VerifyingKey, provingKey native_groth16.ProvingKey, prefix string) error {
start := time.Now()
innerVKFile, err := os.Create(prefix + "vk.cbor")
_, err = verifyingKey.WriteRawTo(innerVKFile)
if err != nil {
return fmt.Errorf("Failed to write Verifying Key - %s", err)
}
err = innerVKFile.Close()
if err != nil {
return fmt.Errorf("Failed to close verifying key file handle - %s", err)
}
end := time.Since(start)
fmt.Printf("Exporting Verifying Key took : %s\n", end)
start = time.Now()
innerPKFile, err := os.Create(prefix + "pk.cbor")
_, err = provingKey.WriteRawTo(innerPKFile)
if err != nil {
return fmt.Errorf("Failed to write Proving Key - %s", err)
}
err = innerPKFile.Close()
if err != nil {
return fmt.Errorf("Failed to properly close Proving Key File handle - %s", err)
}
end = time.Since(start)
fmt.Printf("Exporting Proving Key took : %s\n", end)
return nil
}
func readKeys(prefix string, curveId ecc.ID) (native_groth16.VerifyingKey, native_groth16.ProvingKey, error) {
start := time.Now()
innerVKFile, err := os.OpenFile(prefix+"vk.cbor", os.O_RDONLY, 0444) //read-only
if err != nil {
log.Fatal(err)
return nil, nil, err
}
innerVK := native_groth16.NewVerifyingKey(curveId) //curve for inner circuit
_, err = innerVK.ReadFrom(innerVKFile)
if err != nil {
log.Fatal(err)
return nil, nil, err
}
innerVKFile.Close()
end := time.Since(start)
fmt.Printf("Importing Verifying Key took : %s\n", end)
start = time.Now()
innerPKFile, err := os.OpenFile(prefix+"pk.cbor", os.O_RDONLY, 0444)
if err != nil {
log.Fatal(err)
return nil, nil, err
}
innerPK := native_groth16.NewProvingKey(curveId) //curve for inner circuit
_, err = innerPK.ReadFrom(innerPKFile)
if err != nil {
log.Fatal(err)
return nil, nil, err
}
innerPKFile.Close()
end = time.Since(start)
fmt.Printf("Importing Proving Key took : %s\n", end)
return innerVK, innerPK, nil
}
//func (ps *ProofSystem) CreateNormalCaseProof(normalInfo *txivc.NormalProofInfo) (string, error) {
// //outerAssignment := normalProof.CreateOuterAssignment()
// return txivc.CreateNormalCaseProof(ps.baseCcs, ps.baseVerifyingKey, ps.baseCurveId, ps.InnerField, ps.OuterField, ps.normalProvingKey, normalInfo)
//}
func (ps *ProofSystem) CreateBaseCaseProof(pInfo txivc.BaseProofInfo) (string, error) {
fullTxBytes, _ := hex.DecodeString(pInfo.RawTx)
firstHash := sha256.Sum256(fullTxBytes)
genesisTxId := sha256.Sum256(firstHash[:])
genesisWitness, err := txivc.CreateBaseCaseFullWitness(fullTxBytes, genesisTxId[:])
if err != nil {
return "", err
}
genesisProof, err := txivc.ComputeProof(ps.baseCcs, ps.baseProvingKey, genesisWitness)
jsonBytes, err := json.Marshal(genesisProof)
if err != nil {
return "", err
}
return string(jsonBytes), nil
}
func (ps *ProofSystem) VerifyBaseProof(txId string, jsonProof string) bool {
txProof := native_groth16.NewProof(txivc.InnerCurve)
err := json.Unmarshal([]byte(jsonProof), &txProof)
if err != nil {
fmt.Printf("%s", err)
return false
}
genesisTxId, err := hex.DecodeString(txId)
if err != nil {
fmt.Printf("%s", err)
return false
}
publicWitness, err := txivc.CreateBaseCaseLightWitness(genesisTxId[:], ps.InnerField)
if err != nil {
fmt.Printf("%s", err)
return false
}
//isVerified := ps.VerifyProof(publicWitness, &txProof)
//func (po *BaseProof) VerifyProof(witness *witness.Witness, proof *native_groth16.Proof) bool {
err = native_groth16.Verify(txProof, ps.baseVerifyingKey, publicWitness, ps.verifierOptions)
if err != nil {
fmt.Printf("Fail on proof verification! %s\n", err)
return false
}
return true
}
var BASE_RAW_TX_SIZE = 191
func BootBaseProof(baseTxSize int) (*BaseProof, error) {
baseProof, err := NewBaseProof(baseTxSize)
if err != nil {
return nil, err
}
return baseProof, nil
}
func bootNormalProof(prefixSize int, postfixSize int, baseProof *BaseProof) (*NormalProof, error) {
normalProof, err := NewNormalProof(prefixSize, postfixSize, baseProof)
if err != nil {
return nil, err
}
return normalProof, nil
}
//func BootProofSystem() (*BaseProof, *NormalProof, error) {
//
// fmt.Println("Booting base case proof system. This will take around 1 minute")
// bp, err := BootBaseProof(BASE_RAW_TX_SIZE)
// if err != nil {
// fmt.Printf("Failed to bootstrap basecase proof system.Aborting: %s\n", err)
// return nil, nil, err
// }
//
// //bootstrap the normal case proof system
// fmt.Println("Booting normal case proof system. This will take around 1 minute")
// np, err := bootNormalProof(bp)
// if err != nil {
// fmt.Printf("Failed to bootstrap normalcase proof system.Aborting: %s\n", err)
// return nil, nil, err
// }
//
// return bp, np, nil
//}
/**
TX UTIL. Slicer
*/
/*
func CreateBaseCaseProof(proverOptions backend.ProverOption, innerCcs constraint.ConstraintSystem, genesisWitness witness.Witness, provingKey native_groth16.ProvingKey) (
native_groth16.Proof,
error,
) {
return native_groth16.Prove(innerCcs, provingKey, genesisWitness, proverOptions)
}
*/