github.com/ethxdao/go-ethereum@v0.0.0-20221218102228-5ae34a9cc189/common/math/big_test.go (about) 1 // Copyright 2017 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package math 18 19 import ( 20 "bytes" 21 "encoding/hex" 22 "math/big" 23 "testing" 24 25 "github.com/ethxdao/go-ethereum/common" 26 ) 27 28 func TestHexOrDecimal256(t *testing.T) { 29 tests := []struct { 30 input string 31 num *big.Int 32 ok bool 33 }{ 34 {"", big.NewInt(0), true}, 35 {"0", big.NewInt(0), true}, 36 {"0x0", big.NewInt(0), true}, 37 {"12345678", big.NewInt(12345678), true}, 38 {"0x12345678", big.NewInt(0x12345678), true}, 39 {"0X12345678", big.NewInt(0x12345678), true}, 40 // Tests for leading zero behaviour: 41 {"0123456789", big.NewInt(123456789), true}, // note: not octal 42 {"00", big.NewInt(0), true}, 43 {"0x00", big.NewInt(0), true}, 44 {"0x012345678abc", big.NewInt(0x12345678abc), true}, 45 // Invalid syntax: 46 {"abcdef", nil, false}, 47 {"0xgg", nil, false}, 48 // Larger than 256 bits: 49 {"115792089237316195423570985008687907853269984665640564039457584007913129639936", nil, false}, 50 } 51 for _, test := range tests { 52 var num HexOrDecimal256 53 err := num.UnmarshalText([]byte(test.input)) 54 if (err == nil) != test.ok { 55 t.Errorf("ParseBig(%q) -> (err == nil) == %t, want %t", test.input, err == nil, test.ok) 56 continue 57 } 58 if test.num != nil && (*big.Int)(&num).Cmp(test.num) != 0 { 59 t.Errorf("ParseBig(%q) -> %d, want %d", test.input, (*big.Int)(&num), test.num) 60 } 61 } 62 } 63 64 func TestMustParseBig256(t *testing.T) { 65 defer func() { 66 if recover() == nil { 67 t.Error("MustParseBig should've panicked") 68 } 69 }() 70 MustParseBig256("ggg") 71 } 72 73 func TestBigMax(t *testing.T) { 74 a := big.NewInt(10) 75 b := big.NewInt(5) 76 77 max1 := BigMax(a, b) 78 if max1 != a { 79 t.Errorf("Expected %d got %d", a, max1) 80 } 81 82 max2 := BigMax(b, a) 83 if max2 != a { 84 t.Errorf("Expected %d got %d", a, max2) 85 } 86 } 87 88 func TestBigMin(t *testing.T) { 89 a := big.NewInt(10) 90 b := big.NewInt(5) 91 92 min1 := BigMin(a, b) 93 if min1 != b { 94 t.Errorf("Expected %d got %d", b, min1) 95 } 96 97 min2 := BigMin(b, a) 98 if min2 != b { 99 t.Errorf("Expected %d got %d", b, min2) 100 } 101 } 102 103 func TestFirstBigSet(t *testing.T) { 104 tests := []struct { 105 num *big.Int 106 ix int 107 }{ 108 {big.NewInt(0), 0}, 109 {big.NewInt(1), 0}, 110 {big.NewInt(2), 1}, 111 {big.NewInt(0x100), 8}, 112 } 113 for _, test := range tests { 114 if ix := FirstBitSet(test.num); ix != test.ix { 115 t.Errorf("FirstBitSet(b%b) = %d, want %d", test.num, ix, test.ix) 116 } 117 } 118 } 119 120 func TestPaddedBigBytes(t *testing.T) { 121 tests := []struct { 122 num *big.Int 123 n int 124 result []byte 125 }{ 126 {num: big.NewInt(0), n: 4, result: []byte{0, 0, 0, 0}}, 127 {num: big.NewInt(1), n: 4, result: []byte{0, 0, 0, 1}}, 128 {num: big.NewInt(512), n: 4, result: []byte{0, 0, 2, 0}}, 129 {num: BigPow(2, 32), n: 4, result: []byte{1, 0, 0, 0, 0}}, 130 } 131 for _, test := range tests { 132 if result := PaddedBigBytes(test.num, test.n); !bytes.Equal(result, test.result) { 133 t.Errorf("PaddedBigBytes(%d, %d) = %v, want %v", test.num, test.n, result, test.result) 134 } 135 } 136 } 137 138 func BenchmarkPaddedBigBytesLargePadding(b *testing.B) { 139 bigint := MustParseBig256("123456789123456789123456789123456789") 140 for i := 0; i < b.N; i++ { 141 PaddedBigBytes(bigint, 200) 142 } 143 } 144 145 func BenchmarkPaddedBigBytesSmallPadding(b *testing.B) { 146 bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC") 147 for i := 0; i < b.N; i++ { 148 PaddedBigBytes(bigint, 5) 149 } 150 } 151 152 func BenchmarkPaddedBigBytesSmallOnePadding(b *testing.B) { 153 bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC") 154 for i := 0; i < b.N; i++ { 155 PaddedBigBytes(bigint, 32) 156 } 157 } 158 159 func BenchmarkByteAtBrandNew(b *testing.B) { 160 bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC") 161 for i := 0; i < b.N; i++ { 162 bigEndianByteAt(bigint, 15) 163 } 164 } 165 166 func BenchmarkByteAt(b *testing.B) { 167 bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC") 168 for i := 0; i < b.N; i++ { 169 bigEndianByteAt(bigint, 15) 170 } 171 } 172 173 func BenchmarkByteAtOld(b *testing.B) { 174 bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC") 175 for i := 0; i < b.N; i++ { 176 PaddedBigBytes(bigint, 32) 177 } 178 } 179 180 func TestReadBits(t *testing.T) { 181 check := func(input string) { 182 want, _ := hex.DecodeString(input) 183 int, _ := new(big.Int).SetString(input, 16) 184 buf := make([]byte, len(want)) 185 ReadBits(int, buf) 186 if !bytes.Equal(buf, want) { 187 t.Errorf("have: %x\nwant: %x", buf, want) 188 } 189 } 190 check("000000000000000000000000000000000000000000000000000000FEFCF3F8F0") 191 check("0000000000012345000000000000000000000000000000000000FEFCF3F8F0") 192 check("18F8F8F1000111000110011100222004330052300000000000000000FEFCF3F8F0") 193 } 194 195 func TestU256(t *testing.T) { 196 tests := []struct{ x, y *big.Int }{ 197 {x: big.NewInt(0), y: big.NewInt(0)}, 198 {x: big.NewInt(1), y: big.NewInt(1)}, 199 {x: BigPow(2, 255), y: BigPow(2, 255)}, 200 {x: BigPow(2, 256), y: big.NewInt(0)}, 201 {x: new(big.Int).Add(BigPow(2, 256), big.NewInt(1)), y: big.NewInt(1)}, 202 // negative values 203 {x: big.NewInt(-1), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1))}, 204 {x: big.NewInt(-2), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2))}, 205 {x: BigPow(2, -255), y: big.NewInt(1)}, 206 } 207 for _, test := range tests { 208 if y := U256(new(big.Int).Set(test.x)); y.Cmp(test.y) != 0 { 209 t.Errorf("U256(%x) = %x, want %x", test.x, y, test.y) 210 } 211 } 212 } 213 214 func TestU256Bytes(t *testing.T) { 215 ubytes := make([]byte, 32) 216 ubytes[31] = 1 217 218 unsigned := U256Bytes(big.NewInt(1)) 219 if !bytes.Equal(unsigned, ubytes) { 220 t.Errorf("expected %x got %x", ubytes, unsigned) 221 } 222 } 223 224 func TestBigEndianByteAt(t *testing.T) { 225 tests := []struct { 226 x string 227 y int 228 exp byte 229 }{ 230 {"00", 0, 0x00}, 231 {"01", 1, 0x00}, 232 {"00", 1, 0x00}, 233 {"01", 0, 0x01}, 234 {"0000000000000000000000000000000000000000000000000000000000102030", 0, 0x30}, 235 {"0000000000000000000000000000000000000000000000000000000000102030", 1, 0x20}, 236 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 31, 0xAB}, 237 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 32, 0x00}, 238 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 500, 0x00}, 239 } 240 for _, test := range tests { 241 v := new(big.Int).SetBytes(common.Hex2Bytes(test.x)) 242 actual := bigEndianByteAt(v, test.y) 243 if actual != test.exp { 244 t.Fatalf("Expected [%v] %v:th byte to be %v, was %v.", test.x, test.y, test.exp, actual) 245 } 246 } 247 } 248 func TestLittleEndianByteAt(t *testing.T) { 249 tests := []struct { 250 x string 251 y int 252 exp byte 253 }{ 254 {"00", 0, 0x00}, 255 {"01", 1, 0x00}, 256 {"00", 1, 0x00}, 257 {"01", 0, 0x00}, 258 {"0000000000000000000000000000000000000000000000000000000000102030", 0, 0x00}, 259 {"0000000000000000000000000000000000000000000000000000000000102030", 1, 0x00}, 260 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 31, 0x00}, 261 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 32, 0x00}, 262 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 0, 0xAB}, 263 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 1, 0xCD}, 264 {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 0, 0x00}, 265 {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 1, 0xCD}, 266 {"0000000000000000000000000000000000000000000000000000000000102030", 31, 0x30}, 267 {"0000000000000000000000000000000000000000000000000000000000102030", 30, 0x20}, 268 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 32, 0x0}, 269 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 31, 0xFF}, 270 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0xFFFF, 0x0}, 271 } 272 for _, test := range tests { 273 v := new(big.Int).SetBytes(common.Hex2Bytes(test.x)) 274 actual := Byte(v, 32, test.y) 275 if actual != test.exp { 276 t.Fatalf("Expected [%v] %v:th byte to be %v, was %v.", test.x, test.y, test.exp, actual) 277 } 278 } 279 } 280 281 func TestS256(t *testing.T) { 282 tests := []struct{ x, y *big.Int }{ 283 {x: big.NewInt(0), y: big.NewInt(0)}, 284 {x: big.NewInt(1), y: big.NewInt(1)}, 285 {x: big.NewInt(2), y: big.NewInt(2)}, 286 { 287 x: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)), 288 y: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)), 289 }, 290 { 291 x: BigPow(2, 255), 292 y: new(big.Int).Neg(BigPow(2, 255)), 293 }, 294 { 295 x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1)), 296 y: big.NewInt(-1), 297 }, 298 { 299 x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2)), 300 y: big.NewInt(-2), 301 }, 302 } 303 for _, test := range tests { 304 if y := S256(test.x); y.Cmp(test.y) != 0 { 305 t.Errorf("S256(%x) = %x, want %x", test.x, y, test.y) 306 } 307 } 308 } 309 310 func TestExp(t *testing.T) { 311 tests := []struct{ base, exponent, result *big.Int }{ 312 {base: big.NewInt(0), exponent: big.NewInt(0), result: big.NewInt(1)}, 313 {base: big.NewInt(1), exponent: big.NewInt(0), result: big.NewInt(1)}, 314 {base: big.NewInt(1), exponent: big.NewInt(1), result: big.NewInt(1)}, 315 {base: big.NewInt(1), exponent: big.NewInt(2), result: big.NewInt(1)}, 316 {base: big.NewInt(3), exponent: big.NewInt(144), result: MustParseBig256("507528786056415600719754159741696356908742250191663887263627442114881")}, 317 {base: big.NewInt(2), exponent: big.NewInt(255), result: MustParseBig256("57896044618658097711785492504343953926634992332820282019728792003956564819968")}, 318 } 319 for _, test := range tests { 320 if result := Exp(test.base, test.exponent); result.Cmp(test.result) != 0 { 321 t.Errorf("Exp(%d, %d) = %d, want %d", test.base, test.exponent, result, test.result) 322 } 323 } 324 }