-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
65 lines (62 loc) · 1.31 KB
/
utils_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
package crypto
import (
"github.com/facebookgo/ensure"
"math/big"
"testing"
)
func TestIsU256(t *testing.T) {
ensure.True(t, isU256(big.NewInt(0)))
ensure.True(t, isU256(big.NewInt(1)))
ensure.False(t, isU256(big.NewInt(-1)))
{
buf := make([]byte, 64)
n := new(big.Int).SetBytes(buf)
ensure.True(t, isU256(n))
}
{
buf := make([]byte, 64)
for i := 0; i < 64; i++ {
buf[i] = 255
}
n := new(big.Int).SetBytes(buf)
ensure.False(t, isU256(n))
}
{
buf := make([]byte, 32)
for i := 0; i < 32; i++ {
buf[i] = 255
}
n := new(big.Int).SetBytes(buf)
ensure.True(t, isU256(n))
}
}
func TestBigToBytes(t *testing.T) {
{
expected := make([]byte, 32)
actual := nonNegBigTo32Bytes(big.NewInt(0))
ensure.DeepEqual(t, actual, expected)
}
{
expected := make([]byte, 32)
expected[31] = 100
actual := nonNegBigTo32Bytes(big.NewInt(100))
ensure.DeepEqual(t, actual, expected)
}
{
expected := make([]byte, 32)
expected[31] = 4
expected[30] = 1
actual := nonNegBigTo32Bytes(big.NewInt(260))
ensure.DeepEqual(t, actual, expected)
}
{
expected := make([]byte, 32)
for i := 0; i < 32; i++ {
expected[i] = 255
}
one := big.NewInt(1)
u256Max := new(big.Int).Sub(new(big.Int).Lsh(one, 256), one)
actual := nonNegBigTo32Bytes(u256Max)
ensure.DeepEqual(t, actual, expected)
}
}