-
Notifications
You must be signed in to change notification settings - Fork 40
/
bls12381.go
234 lines (213 loc) · 5.65 KB
/
bls12381.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
// Copyright 2020 Martin Holst Swende, Marius van der Wijden
// This file is part of the goevmlab library.
//
// The library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the goevmlab library. If not, see <http://www.gnu.org/licenses/>.
package fuzzing
import (
crand "crypto/rand"
"math/big"
"math/rand"
bls12381 "github.com/consensys/gnark-crypto/ecc/bls12-381"
"github.com/ethereum/go-ethereum/common"
"github.com/holiman/goevmlab/ops"
"github.com/holiman/goevmlab/program"
)
var modulo, _ = big.NewInt(0).SetString("0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab", 0)
type blsPrec struct {
addr int
newData func() []byte
outsize int
}
var precompilesBLS = []blsPrec{
{0xb, newG1Add, 128}, // G1Add
{0xc, newG1Mul, 128}, // G1Mul
{0xd, newG1Exp, 128}, // G1MultiExp
{0xe, newG2Add, 256}, // G2Add
{0xf, newG2Mul, 256}, // G2Mul
{0x10, newG2Exp, 256}, // G2MultiExp
{0x11, newPairing, 32}, // Pairing
{0x12, newFPtoG1, 128}, // FP to G1
{0x13, newFP2toG2, 256}, // FP2 to G2
}
func fillBls(gst *GstMaker, fork string) {
// Add a contract which calls BLS
dest := common.HexToAddress("0x00ca110b15012381")
code := RandCallBLS()
gst.AddAccount(dest, GenesisAccount{
Code: code,
Balance: new(big.Int),
Storage: make(map[common.Hash]common.Hash),
})
// The transaction
gst.SetTx(&StTransaction{
// 8M gaslimit
GasLimit: []uint64{8000000},
Nonce: 0,
Value: []string{randHex(4)},
Data: []string{randHex(100)},
GasPrice: big.NewInt(0x10),
To: dest.Hex(),
Sender: sender,
PrivateKey: pKey,
})
}
func RandCallBLS() []byte {
p := program.NewProgram()
offset := 0
for _, precompile := range precompilesBLS {
data := precompile.newData()
p.Mstore(data, 0)
memInFn := func() (offset, size interface{}) {
offset, size = 0, len(data)
return
}
sizeOut := precompile.outsize
memOutFn := func() (offset, size interface{}) {
offset, size = 0, sizeOut
return
}
addrGen := func() interface{} {
return precompile.addr
}
p2 := RandCall(GasRandomizer(), addrGen, ValueRandomizer(), memInFn, memOutFn)
p.AddAll(p2)
// pop the ret value
p.Op(ops.POP)
// Store the output in some slot, to make sure the stateroot changes
p.MemToStorage(0, sizeOut, offset)
offset += sizeOut
}
return p.Bytecode()
}
func newG1Add() []byte {
a := newG1Point()
b := newG1Point()
return append(a, b...)
}
func newG1Mul() []byte {
a := newG1Point()
mul := make([]byte, 32)
_, _ = crand.Read(mul)
return append(a, mul...)
}
func newG1Exp() []byte {
i := randInt64()
var res []byte
for k := 0; k < int(i); k++ {
input := newG1Mul()
res = append(res, input...)
}
return res
}
func newG2Add() []byte {
a := newG2Point()
b := newG2Point()
return append(a, b...)
}
func newG2Mul() []byte {
a := newG2Point()
mul := make([]byte, 32)
_, _ = crand.Read(mul)
return append(a, mul...)
}
func newG2Exp() []byte {
i := randInt64()
var res []byte
for k := 0; k < int(i); k++ {
input := newG2Mul()
res = append(res, input...)
}
return res
}
func newFPtoG1() []byte {
return newFieldElement()
}
func newFP2toG2() []byte {
a := newFieldElement()
b := newFieldElement()
return append(a, b...)
}
// randInt64 returns a new random int64
// With 3% probability it outputs 0
// With 92% probability it outputs a number [0..30)
// With 5% probability it outputs a number [0..150)
func randInt64() int64 {
b := rand.Int31n(100)
// Zero or not?
if b < 3 {
return 0
}
if b < 95 {
return rand.Int63n(30)
}
return rand.Int63n(150)
}
// newPairing creates a new valid pairing.
// We create the following pairing:
// e(aMul1 * G1, bMul1 * G2) * e(aMul2 * G1, bMul2 * G2) * ... * e(aMuln * G1, bMuln * G2) == e(G1, G2) ^ s
// with s = sum(x: 1 -> n: (aMulx * bMulx))
func newPairing() []byte {
_, _, _, genG2 := bls12381.Generators()
pairs := randInt64()
var res []byte
target := new(big.Int)
// LHS: sum(x: 1->n: e(aMulx * G1, bMulx * G2))
for k := 0; k < int(pairs); k++ {
aMul := new(big.Int).SetBytes(newFieldElement())
bMul := new(big.Int).SetBytes(newFieldElement())
g1 := new(bls12381.G1Affine).ScalarMultiplicationBase(aMul)
g2 := new(bls12381.G2Affine).ScalarMultiplication(&genG2, bMul)
res = append(res, g1.Marshal()...)
res = append(res, g2.Marshal()...)
// Add to s
target = target.Add(target, aMul.Mul(aMul, bMul))
}
// RHS: e(G1, G2) ^ s
ta := new(bls12381.G1Affine).ScalarMultiplicationBase(target)
ta.Neg(ta)
res = append(res, ta.Marshal()...)
res = append(res, genG2.Marshal()...)
return res
}
func newFieldElement() []byte {
ret, err := crand.Int(crand.Reader, modulo)
if err != nil {
panic(err)
}
bytes := ret.Bytes()
buf := make([]byte, 48)
copy(buf[48-len(bytes):], bytes)
return buf
}
func newG1Point() []byte {
a := newFieldElement()
g1 := new(bls12381.G1Affine)
_, err := g1.SetBytes(a)
if err != nil {
panic(err)
}
return g1.Marshal()
}
func newG2Point() []byte {
a := newFieldElement()
b := newFieldElement()
x := append(a, b...)
// Compute mapping
g2 := new(bls12381.G2Affine)
_, err := g2.SetBytes(x)
if err != nil {
panic(err)
}
return g2.Marshal()
}