-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
142 lines (112 loc) · 4.06 KB
/
common.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
package experimental
import (
"crypto/sha256"
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/frontend/cs/r1cs"
"github.com/consensys/gnark/std/algebra/native/sw_bls24315"
"github.com/consensys/gnark/std/math/uints"
"github.com/consensys/gnark/std/recursion/groth16"
"math/big"
)
// type ScalarField = sw_bls24315.ScalarField
// type G1Affine = sw_bls24315.G1Affine
// type G2Affine = sw_bls24315.G2Affine
// type GTEl = sw_bls24315.GT
//type ScalarField = sw_bls12381.ScalarField
//type G1Affine = sw_bls12381.G1Affine
//type G2Affine = sw_bls12381.G2Affine
//type GTEl = sw_bls12381.GTEl
// type ScalarField = sw_bn254.ScalarField
// type G1Affine = sw_bn254.G1Affine
// type G2Affine = sw_bn254.G2Affine
// type GTEl = sw_bn254.GTEl
type ScalarField = sw_bls24315.ScalarField
type G1Affine = sw_bls24315.G1Affine
type G2Affine = sw_bls24315.G2Affine
type GTEl = sw_bls24315.GT
func SetupBaseCase(innerField *big.Int) (constraint.ConstraintSystem, native_groth16.ProvingKey, native_groth16.VerifyingKey, error) {
baseCcs, err := frontend.Compile(innerField, r1cs.NewBuilder,
&SigCircuitBaseCase[ScalarField, G1Affine, G2Affine, GTEl]{})
if err != nil {
return nil, nil, nil, err
}
//srs, srsLagrange, err := unsafekzg.NewSRS(baseCcs)
if err != nil {
return nil, nil, nil, err
}
innerPK, innerVK, err := native_groth16.Setup(baseCcs)
if err != nil {
return nil, nil, nil, err
}
return baseCcs, innerPK, innerVK, nil
}
func SetupNormalCase(
outerField *big.Int,
parentCcs constraint.ConstraintSystem,
parentVk groth16.VerifyingKey[G1Affine, G2Affine, GTEl]) (constraint.ConstraintSystem, native_groth16.ProvingKey, native_groth16.VerifyingKey, error) {
innerCcs, err := frontend.Compile(outerField, r1cs.NewBuilder,
&SigCircuit[ScalarField, G1Affine, G2Affine, GTEl]{
PreviousProof: groth16.PlaceholderProof[G1Affine, G2Affine](parentCcs),
PreviousVk: parentVk,
PreviousWitness: groth16.PlaceholderWitness[ScalarField](parentCcs),
})
if err != nil {
return nil, nil, nil, err
}
//srs, srsLagrange, err := unsafekzg.NewSRS(innerCcs)
if err != nil {
return nil, nil, nil, err
}
innerPK, innerVK, err := native_groth16.Setup(innerCcs)
if err != nil {
return nil, nil, nil, err
}
return innerCcs, innerPK, innerVK, nil
}
func CreateBaseCaseProof(outerField, innerField *big.Int, fullTxBytes []byte, txnIdBytes [32]byte, innerCcs constraint.ConstraintSystem, provingKey native_groth16.ProvingKey) (
witness.Witness,
native_groth16.Proof,
error,
) {
genesisWitness, err := CreateBaseCaseWitness(fullTxBytes, innerField)
if err != nil {
return nil, nil, err
}
proof, err := native_groth16.Prove(innerCcs, provingKey, genesisWitness, groth16.GetNativeProverOptions(outerField, innerField))
return genesisWitness, proof, err
}
func CreateBaseCaseWitness(
fullTxBytes []byte,
innerField *big.Int,
) (witness.Witness, error) {
innerAssignment := SigCircuitBaseCase[ScalarField, G1Affine, G2Affine, GTEl]{}
firstHash := sha256.Sum256(fullTxBytes)
//assign the current Txn data
copy(innerAssignment.TxPreImage[:], uints.NewU8Array(fullTxBytes))
copy(innerAssignment.ImageHash[:], uints.NewU8Array(firstHash[:]))
innerWitness, err := frontend.NewWitness(&innerAssignment, innerField)
if err != nil {
return nil, err
}
return innerWitness, nil
}
/*
func CreateOuterAssignment(
circuitWitness plonk.Witness[sw_bls12377.ScalarField],
circuitProof groth16_bls24315.Proof,
verifyingKey groth16_bls24315.VerifyingKey,
fullTxBytes []byte, tokenId [32]byte) SigCircuit[ScalarField, G1Affine, G2Affine, GTEl] {
outerAssignment := SigCircuit[ScalarField, G1Affine, G2Affine, GTEl]{
PreviousProof: circuitProof,
PreviousVk: verifyingKey,
}
firstHash := sha256.Sum256(fullTxBytes)
//currTxId := sha256.Sum256(firstHash[:])
copy(outerAssignment.TxPreImage[:], uints.NewU8Array(fullTxBytes))
copy(outerAssignment.ImageHash[:], uints.NewU8Array(firstHash[:]))
return outerAssignment
}
*/