-
Notifications
You must be signed in to change notification settings - Fork 55
/
binary_test.go
307 lines (274 loc) · 8.27 KB
/
binary_test.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
// uint256: Fixed size 256-bit math library
// Copyright 2020 uint256 Authors
// SPDX-License-Identifier: BSD-3-Clause
package uint256
import (
"fmt"
"math/big"
"testing"
)
type opDualArgFunc func(*Int, *Int, *Int) *Int
type bigDualArgFunc func(*big.Int, *big.Int, *big.Int) *big.Int
type opCmpArgFunc func(*Int, *Int) bool
type bigCmpArgFunc func(*big.Int, *big.Int) bool
type binaryOpEntry struct {
name string
u256Fn opDualArgFunc
bigFn bigDualArgFunc
}
func lookupBinary(name string) binaryOpEntry {
for _, tc := range binaryOpFuncs {
if tc.name == name {
return tc
}
}
panic(fmt.Sprintf("%v not found", name))
}
var binaryOpFuncs = []binaryOpEntry{
{"Add", (*Int).Add, (*big.Int).Add},
{"Sub", (*Int).Sub, (*big.Int).Sub},
{"Mul", (*Int).Mul, (*big.Int).Mul},
{"Div", (*Int).Div, bigDiv},
{"Mod", (*Int).Mod, bigMod},
{"SDiv", (*Int).SDiv, bigSDiv},
{"SMod", (*Int).SMod, bigSMod},
{"And", (*Int).And, (*big.Int).And},
{"Or", (*Int).Or, (*big.Int).Or},
{"Xor", (*Int).Xor, (*big.Int).Xor},
{"Exp", (*Int).Exp, func(b1, b2, b3 *big.Int) *big.Int { return b1.Exp(b2, b3, bigtt256) }},
{"Lsh", u256Lsh, bigLsh},
{"Rsh", u256Rsh, bigRsh},
{"SRsh", u256SRsh, bigSRsh},
{"DivModDiv", divModDiv, bigDiv},
{"DivModMod", divModMod, bigMod},
{"udivremDiv", udivremDiv, bigDiv},
{"udivremMod", udivremMod, bigMod},
{"ExtendSign", (*Int).ExtendSign, bigExtendSign},
}
var cmpOpFuncs = []struct {
name string
u256Fn opCmpArgFunc
bigFn bigCmpArgFunc
}{
{"Eq", (*Int).Eq, func(a, b *big.Int) bool { return a.Cmp(b) == 0 }},
{"Lt", (*Int).Lt, func(a, b *big.Int) bool { return a.Cmp(b) < 0 }},
{"Gt", (*Int).Gt, func(a, b *big.Int) bool { return a.Cmp(b) > 0 }},
{"Slt", (*Int).Slt, func(a, b *big.Int) bool { return bigS256(a).Cmp(bigS256(b)) < 0 }},
{"Sgt", (*Int).Sgt, func(a, b *big.Int) bool { return bigS256(a).Cmp(bigS256(b)) > 0 }},
{"CmpEq", func(a, b *Int) bool { return a.Cmp(b) == 0 }, func(a, b *big.Int) bool { return a.Cmp(b) == 0 }},
{"CmpLt", func(a, b *Int) bool { return a.Cmp(b) < 0 }, func(a, b *big.Int) bool { return a.Cmp(b) < 0 }},
{"CmpGt", func(a, b *Int) bool { return a.Cmp(b) > 0 }, func(a, b *big.Int) bool { return a.Cmp(b) > 0 }},
{"LtUint64", func(a, b *Int) bool { return a.LtUint64(b.Uint64()) }, func(a, b *big.Int) bool { return a.Cmp(new(big.Int).SetUint64(b.Uint64())) < 0 }},
{"GtUint64", func(a, b *Int) bool { return a.GtUint64(b.Uint64()) }, func(a, b *big.Int) bool { return a.Cmp(new(big.Int).SetUint64(b.Uint64())) > 0 }},
}
func checkBinaryOperation(t *testing.T, opName string, op opDualArgFunc, bigOp bigDualArgFunc, x, y Int) {
var (
b1 = x.ToBig()
b2 = y.ToBig()
f1 = x.Clone()
f2 = y.Clone()
operation = fmt.Sprintf("op: %v ( %v, %v ) ", opName, x.Hex(), y.Hex())
want, _ = FromBig(bigOp(new(big.Int), b1, b2))
have = op(new(Int), f1, f2)
)
// Compare result with big.Int.
if !have.Eq(want) {
t.Fatalf("%v\nwant : %#x\nhave : %#x\n", operation, want, have)
}
// Check if arguments are unmodified.
if !f1.Eq(x.Clone()) {
t.Fatalf("%v\nfirst argument had been modified: %x", operation, f1)
}
if !f2.Eq(y.Clone()) {
t.Fatalf("%v\nsecond argument had been modified: %x", operation, f2)
}
// Check if reusing args as result works correctly.
have = op(f1, f1, y.Clone())
if have != f1 {
t.Fatalf("%v\nunexpected pointer returned: %p, expected: %p\n", operation, have, f1)
}
if !have.Eq(want) {
t.Fatalf("%v\non argument reuse x.op(x,y)\nwant : %#x\nhave : %#x\n", operation, want, have)
}
have = op(f2, x.Clone(), f2)
if have != f2 {
t.Fatalf("%v\nunexpected pointer returned: %p, expected: %p\n", operation, have, f2)
}
if !have.Eq(want) {
t.Fatalf("%v\n on argument reuse x.op(y,x)\nwant : %#x\nhave : %#x\n", operation, want, have)
}
}
func TestBinaryOperations(t *testing.T) {
for _, tc := range binaryOpFuncs {
for _, inputs := range binTestCases {
f1 := MustFromHex(inputs[0])
f2 := MustFromHex(inputs[1])
checkBinaryOperation(t, tc.name, tc.u256Fn, tc.bigFn, *f1, *f2)
}
}
}
func Test10KRandomBinaryOperations(t *testing.T) {
for _, tc := range binaryOpFuncs {
for i := 0; i < 10000; i++ {
f1 := randNum()
f2 := randNum()
checkBinaryOperation(t, tc.name, tc.u256Fn, tc.bigFn, *f1, *f2)
}
}
}
func FuzzBinaryOperations(f *testing.F) {
f.Fuzz(func(t *testing.T, x0, x1, x2, x3, y0, y1, y2, y3 uint64) {
x := Int{x0, x1, x2, x3}
y := Int{y0, y1, y2, y3}
for _, tc := range binaryOpFuncs {
checkBinaryOperation(t, tc.name, tc.u256Fn, tc.bigFn, x, y)
}
})
}
func u256Rsh(z, x, y *Int) *Int {
return z.Rsh(x, uint(y.Uint64()&0x1FF))
}
func bigRsh(z, x, y *big.Int) *big.Int {
return z.Rsh(x, uint(y.Uint64()&0x1FF))
}
func u256Lsh(z, x, y *Int) *Int {
return z.Lsh(x, uint(y.Uint64()&0x1FF))
}
func u256SRsh(z, x, y *Int) *Int {
return z.SRsh(x, uint(y.Uint64()&0x1FF))
}
func bigLsh(z, x, y *big.Int) *big.Int {
return z.Lsh(x, uint(y.Uint64()&0x1FF))
}
func bigSRsh(z, x, y *big.Int) *big.Int {
return z.Rsh(bigS256(x), uint(y.Uint64()&0x1FF))
}
func bigExtendSign(result, num, byteNum *big.Int) *big.Int {
if byteNum.Cmp(big.NewInt(31)) >= 0 {
return result.Set(num)
}
bit := uint(byteNum.Uint64()*8 + 7)
mask := byteNum.Lsh(big.NewInt(1), bit)
mask.Sub(mask, big.NewInt(1))
if num.Bit(int(bit)) > 0 {
result.Or(num, mask.Not(mask))
} else {
result.And(num, mask)
}
return result
}
// bigDiv implements uint256/EVM compatible division for big.Int: returns 0 when dividing by 0
func bigDiv(z, x, y *big.Int) *big.Int {
if y.Sign() == 0 {
return z.SetUint64(0)
}
return z.Div(x, y)
}
// bigMod implements uint256/EVM compatible mod for big.Int: returns 0 when dividing by 0
func bigMod(z, x, y *big.Int) *big.Int {
if y.Sign() == 0 {
return z.SetUint64(0)
}
return z.Mod(x, y)
}
// bigSDiv implements EVM-compatible SDIV operation on big.Int
func bigSDiv(result, x, y *big.Int) *big.Int {
if y.Sign() == 0 {
return result.SetUint64(0)
}
sx := bigS256(x)
sy := bigS256(y)
n := new(big.Int)
if sx.Sign() == sy.Sign() {
n.SetInt64(1)
} else {
n.SetInt64(-1)
}
result.Div(sx.Abs(sx), sy.Abs(sy))
result.Mul(result, n)
return result
}
// bigSMod implements EVM-compatible SMOD operation on big.Int
func bigSMod(result, x, y *big.Int) *big.Int {
if y.Sign() == 0 {
return result.SetUint64(0)
}
sx := bigS256(x)
sy := bigS256(y)
neg := sx.Sign() < 0
result.Mod(sx.Abs(sx), sy.Abs(sy))
if neg {
result.Neg(result)
}
return bigU256(result)
}
// divModDiv wraps DivMod and returns quotient only
func divModDiv(z, x, y *Int) *Int {
var m Int
z.DivMod(x, y, &m)
return z
}
// divModMod wraps DivMod and returns modulus only
func divModMod(z, x, y *Int) *Int {
new(Int).DivMod(x, y, z)
return z
}
// udivremDiv wraps udivrem and returns quotient
func udivremDiv(z, x, y *Int) *Int {
var quot Int
if !y.IsZero() {
udivrem(quot[:], x[:], y, nil)
}
return z.Set(")
}
// udivremMod wraps udivrem and returns remainder
func udivremMod(z, x, y *Int) *Int {
if y.IsZero() {
return z.Clear()
}
var quot, rem Int
udivrem(quot[:], x[:], y, &rem)
return z.Set(&rem)
}
func checkCompareOperation(t *testing.T, opName string, op opCmpArgFunc, bigOp bigCmpArgFunc, x, y Int) {
var (
f1orig = x.Clone()
f2orig = y.Clone()
b1 = x.ToBig()
b2 = y.ToBig()
f1 = new(Int).Set(f1orig)
f2 = new(Int).Set(f2orig)
operation = fmt.Sprintf("op: %v ( %v, %v ) ", opName, x.Hex(), y.Hex())
want = bigOp(b1, b2)
have = op(f1, f2)
)
// Compare result with big.Int.
if have != want {
t.Fatalf("%v\nwant : %v\nhave : %v\n", operation, want, have)
}
// Check if arguments are unmodified.
if !f1.Eq(f1orig) {
t.Fatalf("%v\nfirst argument had been modified: %x", operation, f1)
}
if !f2.Eq(f2orig) {
t.Fatalf("%v\nsecond argument had been modified: %x", operation, f2)
}
}
func TestCompareOperations(t *testing.T) {
for _, tc := range cmpOpFuncs {
for _, inputs := range binTestCases {
f1 := MustFromHex(inputs[0])
f2 := MustFromHex(inputs[1])
checkCompareOperation(t, tc.name, tc.u256Fn, tc.bigFn, *f1, *f2)
}
}
}
func FuzzCompareOperations(f *testing.F) {
f.Fuzz(func(t *testing.T, x0, x1, x2, x3, y0, y1, y2, y3 uint64) {
x := Int{x0, x1, x2, x3}
y := Int{y0, y1, y2, y3}
for _, tc := range cmpOpFuncs {
checkCompareOperation(t, tc.name, tc.u256Fn, tc.bigFn, x, y)
}
})
}