github.com/core-coin/go-core/v2@v2.1.9/common/math/big_test.go (about) 1 // Copyright 2017 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core 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-core 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-core 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/core-coin/go-core/v2/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 175 bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC") 176 for i := 0; i < b.N; i++ { 177 PaddedBigBytes(bigint, 32) 178 } 179 } 180 181 func TestReadBits(t *testing.T) { 182 check := func(input string) { 183 want, _ := hex.DecodeString(input) 184 int, _ := new(big.Int).SetString(input, 16) 185 buf := make([]byte, len(want)) 186 ReadBits(int, buf) 187 if !bytes.Equal(buf, want) { 188 t.Errorf("have: %x\nwant: %x", buf, want) 189 } 190 } 191 check("000000000000000000000000000000000000000000000000000000FEFCF3F8F0") 192 check("0000000000012345000000000000000000000000000000000000FEFCF3F8F0") 193 check("18F8F8F1000111000110011100222004330052300000000000000000FEFCF3F8F0") 194 } 195 196 func TestU256(t *testing.T) { 197 tests := []struct{ x, y *big.Int }{ 198 {x: big.NewInt(0), y: big.NewInt(0)}, 199 {x: big.NewInt(1), y: big.NewInt(1)}, 200 {x: BigPow(2, 255), y: BigPow(2, 255)}, 201 {x: BigPow(2, 256), y: big.NewInt(0)}, 202 {x: new(big.Int).Add(BigPow(2, 256), big.NewInt(1)), y: big.NewInt(1)}, 203 // negative values 204 {x: big.NewInt(-1), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1))}, 205 {x: big.NewInt(-2), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2))}, 206 {x: BigPow(2, -255), y: big.NewInt(1)}, 207 } 208 for _, test := range tests { 209 if y := U256(new(big.Int).Set(test.x)); y.Cmp(test.y) != 0 { 210 t.Errorf("U256(%x) = %x, want %x", test.x, y, test.y) 211 } 212 } 213 } 214 215 func TestU256Bytes(t *testing.T) { 216 ubytes := make([]byte, 32) 217 ubytes[31] = 1 218 219 unsigned := U256Bytes(big.NewInt(1)) 220 if !bytes.Equal(unsigned, ubytes) { 221 t.Errorf("expected %x got %x", ubytes, unsigned) 222 } 223 } 224 225 func TestBigEndianByteAt(t *testing.T) { 226 tests := []struct { 227 x string 228 y int 229 exp byte 230 }{ 231 {"00", 0, 0x00}, 232 {"01", 1, 0x00}, 233 {"00", 1, 0x00}, 234 {"01", 0, 0x01}, 235 {"0000000000000000000000000000000000000000000000000000000000102030", 0, 0x30}, 236 {"0000000000000000000000000000000000000000000000000000000000102030", 1, 0x20}, 237 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 31, 0xAB}, 238 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 32, 0x00}, 239 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 500, 0x00}, 240 } 241 for _, test := range tests { 242 v := new(big.Int).SetBytes(common.Hex2Bytes(test.x)) 243 actual := bigEndianByteAt(v, test.y) 244 if actual != test.exp { 245 t.Fatalf("Expected [%v] %v:th byte to be %v, was %v.", test.x, test.y, test.exp, actual) 246 } 247 248 } 249 } 250 func TestLittleEndianByteAt(t *testing.T) { 251 tests := []struct { 252 x string 253 y int 254 exp byte 255 }{ 256 {"00", 0, 0x00}, 257 {"01", 1, 0x00}, 258 {"00", 1, 0x00}, 259 {"01", 0, 0x00}, 260 {"0000000000000000000000000000000000000000000000000000000000102030", 0, 0x00}, 261 {"0000000000000000000000000000000000000000000000000000000000102030", 1, 0x00}, 262 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 31, 0x00}, 263 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 32, 0x00}, 264 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 0, 0xAB}, 265 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 1, 0xCD}, 266 {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 0, 0x00}, 267 {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 1, 0xCD}, 268 {"0000000000000000000000000000000000000000000000000000000000102030", 31, 0x30}, 269 {"0000000000000000000000000000000000000000000000000000000000102030", 30, 0x20}, 270 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 32, 0x0}, 271 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 31, 0xFF}, 272 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0xFFFF, 0x0}, 273 } 274 for _, test := range tests { 275 v := new(big.Int).SetBytes(common.Hex2Bytes(test.x)) 276 actual := Byte(v, 32, test.y) 277 if actual != test.exp { 278 t.Fatalf("Expected [%v] %v:th byte to be %v, was %v.", test.x, test.y, test.exp, actual) 279 } 280 281 } 282 } 283 284 func TestS256(t *testing.T) { 285 tests := []struct{ x, y *big.Int }{ 286 {x: big.NewInt(0), y: big.NewInt(0)}, 287 {x: big.NewInt(1), y: big.NewInt(1)}, 288 {x: big.NewInt(2), y: big.NewInt(2)}, 289 { 290 x: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)), 291 y: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)), 292 }, 293 { 294 x: BigPow(2, 255), 295 y: new(big.Int).Neg(BigPow(2, 255)), 296 }, 297 { 298 x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1)), 299 y: big.NewInt(-1), 300 }, 301 { 302 x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2)), 303 y: big.NewInt(-2), 304 }, 305 } 306 for _, test := range tests { 307 if y := S256(test.x); y.Cmp(test.y) != 0 { 308 t.Errorf("S256(%x) = %x, want %x", test.x, y, test.y) 309 } 310 } 311 } 312 313 func TestExp(t *testing.T) { 314 tests := []struct{ base, exponent, result *big.Int }{ 315 {base: big.NewInt(0), exponent: big.NewInt(0), result: big.NewInt(1)}, 316 {base: big.NewInt(1), exponent: big.NewInt(0), result: big.NewInt(1)}, 317 {base: big.NewInt(1), exponent: big.NewInt(1), result: big.NewInt(1)}, 318 {base: big.NewInt(1), exponent: big.NewInt(2), result: big.NewInt(1)}, 319 {base: big.NewInt(3), exponent: big.NewInt(144), result: MustParseBig256("507528786056415600719754159741696356908742250191663887263627442114881")}, 320 {base: big.NewInt(2), exponent: big.NewInt(255), result: MustParseBig256("57896044618658097711785492504343953926634992332820282019728792003956564819968")}, 321 } 322 for _, test := range tests { 323 if result := Exp(test.base, test.exponent); result.Cmp(test.result) != 0 { 324 t.Errorf("Exp(%d, %d) = %d, want %d", test.base, test.exponent, result, test.result) 325 } 326 } 327 }