This repository has been archived by the owner on Jan 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
eip1962_test.go
88 lines (78 loc) · 1.65 KB
/
eip1962_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
package eip
import (
"crypto/rand"
"encoding/hex"
"flag"
"fmt"
"math/big"
"testing"
)
var fuz int = 1
var targetNumberOfLimb int = -1
var from = 1
var to = 16
func TestMain(m *testing.M) {
_fuz := flag.Int("fuzz", 1, "# of iters")
nol := flag.Int("nol", 0, "backend bit size")
flag.Parse()
fuz = *_fuz
if *nol > 0 {
targetNumberOfLimb = *nol
if !(targetNumberOfLimb >= from && targetNumberOfLimb <= to) {
panic(fmt.Sprintf("limb size %d not supported", targetNumberOfLimb))
}
from = targetNumberOfLimb
to = targetNumberOfLimb
}
m.Run()
}
func randBytes(max *big.Int) []byte {
return padBytes(randBig(max).Bytes(), resolveLimbSize(max.BitLen())*8)
}
func randBig(max *big.Int) *big.Int {
bi, err := rand.Int(rand.Reader, max)
if err != nil {
panic(err)
}
return bi
}
func randScalar(max *big.Int) *big.Int {
return randBig(max)
}
func debugBytes(a ...[]byte) {
for _, b := range a {
for i := (len(b) / 8) - 1; i > -1; i-- {
fmt.Printf("0x%16.16x,\n", b[i*8:i*8+8])
}
fmt.Println()
}
}
func fromHex(size int, hexStrs ...string) []byte {
var out []byte
if size > 0 {
out = make([]byte, size*len(hexStrs))
}
for i := 0; i < len(hexStrs); i++ {
hexStr := hexStrs[i]
if hexStr[:2] == "0x" {
hexStr = hexStr[2:]
}
if len(hexStr)%2 == 1 {
hexStr = "0" + hexStr
}
bytes, err := hex.DecodeString(hexStr)
if err != nil {
panic(err)
}
if size <= 0 {
out = append(out, bytes...)
} else {
if len(bytes) > size {
panic(fmt.Sprintf("bad input string\ninput: %x\nsize: %d\nlenght: %d\n", bytes, size, len(bytes)))
}
offset := i*size + (size - len(bytes))
copy(out[offset:], bytes)
}
}
return out
}