-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseproof_api.go
199 lines (158 loc) · 4.78 KB
/
baseproof_api.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
package zklib
import (
"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/backend/witness"
"github.com/consensys/gnark/constraint"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/std/recursion/groth16"
txivc "github.com/twostack/zklib/twostack/groth16"
"math/big"
"os"
)
type BaseProof struct {
CurveId ecc.ID
InnerField *big.Int
OuterField *big.Int
verifierOptions backend.VerifierOption
proverOptions backend.ProverOption
Ccs constraint.ConstraintSystem
VerifyingKey native_groth16.VerifyingKey
ProvingKey native_groth16.ProvingKey
}
func NewBaseProof(baseTxSize int) (*BaseProof, error) {
po := &BaseProof{}
po.InnerField = txivc.InnerCurve.ScalarField()
po.OuterField = txivc.OuterCurve.ScalarField()
//IMPORTANT: Base proof needs to read the inner field's curveId
po.CurveId = txivc.InnerCurve
po.verifierOptions = groth16.GetNativeVerifierOptions(po.OuterField, po.InnerField)
po.proverOptions = groth16.GetNativeProverOptions(po.OuterField, po.InnerField)
baseCcs, provingKey, verifyingKey, err := po.readSetupParams(baseTxSize, po.InnerField, po.CurveId)
//ccs, err := frontend.Compile(po.InnerField, r1cs.NewBuilder, baseTxCircuit)
if err != nil {
return nil, err
}
po.Ccs = baseCcs
po.ProvingKey = provingKey
po.VerifyingKey = verifyingKey
return po, nil
}
func (po *BaseProof) readSetupParams(txSize int, innerField *big.Int, curveId ecc.ID) (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 := po.readCircuitParams()
if err != nil {
return nil, nil, nil, err
}
verifyingKey, provingKey, err := readKeys("base_", curveId)
if err != nil {
return nil, nil, nil, err
}
return baseCcs, provingKey, verifyingKey, nil
}
}
func (po *BaseProof) readCircuitParams() (constraint.ConstraintSystem, error) {
baseCcs := native_groth16.NewCS(txivc.InnerCurve)
ccsFile, err := os.OpenFile("base_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 (po *BaseProof) SetupKeys() error {
//
// if po.Ccs == nil {
// return fmt.Errorf("No constraint system found. Please call New() first.")
// }
//
// innerPK, innerVK, err := native_groth16.Setup(*po.Ccs)
//
// if err != nil {
// return err
// }
//
// po.ProvingKey = &innerPK
// po.VerifyingKey = &innerVK
//
// return nil
//}
func (po *BaseProof) ComputeProof(fullWitness witness.Witness) (
native_groth16.Proof,
error,
) {
return native_groth16.Prove(po.Ccs, po.ProvingKey, fullWitness, po.proverOptions)
}
func (po *BaseProof) VerifyProof(witness witness.Witness, proof native_groth16.Proof) bool {
err := native_groth16.Verify(proof, po.VerifyingKey, witness, po.verifierOptions)
if err != nil {
fmt.Printf("Fail on proof verification! %s\n", err)
return false
}
return true
}
/*
FIXME: This is duplicated from groth16/common.go
*/
func (po *BaseProof) CreateBaseCaseWitness(
rawTxBytes []byte,
currTxId [32]byte,
) (witness.Witness, error) {
innerAssignment := txivc.Sha256CircuitBaseCase{
RawTx: make([]frontend.Variable, len(rawTxBytes)),
CurrTxId: make([]frontend.Variable, len(currTxId)),
}
//assign the current Txn data
for ndx := range rawTxBytes {
innerAssignment.RawTx[ndx] = rawTxBytes[ndx]
}
for ndx := range currTxId {
innerAssignment.CurrTxId[ndx] = currTxId[ndx]
}
innerWitness, err := frontend.NewWitness(&innerAssignment, ecc.BLS12_377.ScalarField())
if err != nil {
return nil, err
}
return innerWitness, nil
}
func (po *BaseProof) CreateLightWitness(genesisTxId []byte) (witness.Witness, error) {
return txivc.CreateBaseCaseLightWitness(genesisTxId, po.InnerField)
}
// generate innerVK, innerPK, compiled circuit and save to disk
func (po *BaseProof) WriteKeys() error {
err := writeKeys(po.VerifyingKey, po.ProvingKey, "base_")
if err != nil {
return err
}
return nil
}
func (po *BaseProof) ReadKeys() error {
vk, pk, err := readKeys("base_", po.CurveId)
if err != nil {
return err
}
po.ProvingKey = pk
po.VerifyingKey = vk
return nil
}