big_test.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. package math
  17. import (
  18. "bytes"
  19. "encoding/hex"
  20. "math/big"
  21. "testing"
  22. "github.com/ethereum/go-ethereum/common"
  23. )
  24. func TestHexOrDecimal256(t *testing.T) {
  25. tests := []struct {
  26. input string
  27. num *big.Int
  28. ok bool
  29. }{
  30. {"", big.NewInt(0), true},
  31. {"0", big.NewInt(0), true},
  32. {"0x0", big.NewInt(0), true},
  33. {"12345678", big.NewInt(12345678), true},
  34. {"0x12345678", big.NewInt(0x12345678), true},
  35. {"0X12345678", big.NewInt(0x12345678), true},
  36. // Tests for leading zero behaviour:
  37. {"0123456789", big.NewInt(123456789), true}, // note: not octal
  38. {"00", big.NewInt(0), true},
  39. {"0x00", big.NewInt(0), true},
  40. {"0x012345678abc", big.NewInt(0x12345678abc), true},
  41. // Invalid syntax:
  42. {"abcdef", nil, false},
  43. {"0xgg", nil, false},
  44. // Larger than 256 bits:
  45. {"115792089237316195423570985008687907853269984665640564039457584007913129639936", nil, false},
  46. }
  47. for _, test := range tests {
  48. var num HexOrDecimal256
  49. err := num.UnmarshalText([]byte(test.input))
  50. if (err == nil) != test.ok {
  51. t.Errorf("ParseBig(%q) -> (err == nil) == %t, want %t", test.input, err == nil, test.ok)
  52. continue
  53. }
  54. if test.num != nil && (*big.Int)(&num).Cmp(test.num) != 0 {
  55. t.Errorf("ParseBig(%q) -> %d, want %d", test.input, (*big.Int)(&num), test.num)
  56. }
  57. }
  58. }
  59. func TestMustParseBig256(t *testing.T) {
  60. defer func() {
  61. if recover() == nil {
  62. t.Error("MustParseBig should've panicked")
  63. }
  64. }()
  65. MustParseBig256("ggg")
  66. }
  67. func TestBigMax(t *testing.T) {
  68. a := big.NewInt(10)
  69. b := big.NewInt(5)
  70. max1 := BigMax(a, b)
  71. if max1 != a {
  72. t.Errorf("Expected %d got %d", a, max1)
  73. }
  74. max2 := BigMax(b, a)
  75. if max2 != a {
  76. t.Errorf("Expected %d got %d", a, max2)
  77. }
  78. }
  79. func TestBigMin(t *testing.T) {
  80. a := big.NewInt(10)
  81. b := big.NewInt(5)
  82. min1 := BigMin(a, b)
  83. if min1 != b {
  84. t.Errorf("Expected %d got %d", b, min1)
  85. }
  86. min2 := BigMin(b, a)
  87. if min2 != b {
  88. t.Errorf("Expected %d got %d", b, min2)
  89. }
  90. }
  91. func TestFirstBigSet(t *testing.T) {
  92. tests := []struct {
  93. num *big.Int
  94. ix int
  95. }{
  96. {big.NewInt(0), 0},
  97. {big.NewInt(1), 0},
  98. {big.NewInt(2), 1},
  99. {big.NewInt(0x100), 8},
  100. }
  101. for _, test := range tests {
  102. if ix := FirstBitSet(test.num); ix != test.ix {
  103. t.Errorf("FirstBitSet(b%b) = %d, want %d", test.num, ix, test.ix)
  104. }
  105. }
  106. }
  107. func TestPaddedBigBytes(t *testing.T) {
  108. tests := []struct {
  109. num *big.Int
  110. n int
  111. result []byte
  112. }{
  113. {num: big.NewInt(0), n: 4, result: []byte{0, 0, 0, 0}},
  114. {num: big.NewInt(1), n: 4, result: []byte{0, 0, 0, 1}},
  115. {num: big.NewInt(512), n: 4, result: []byte{0, 0, 2, 0}},
  116. {num: BigPow(2, 32), n: 4, result: []byte{1, 0, 0, 0, 0}},
  117. }
  118. for _, test := range tests {
  119. if result := PaddedBigBytes(test.num, test.n); !bytes.Equal(result, test.result) {
  120. t.Errorf("PaddedBigBytes(%d, %d) = %v, want %v", test.num, test.n, result, test.result)
  121. }
  122. }
  123. }
  124. func BenchmarkPaddedBigBytesLargePadding(b *testing.B) {
  125. bigint := MustParseBig256("123456789123456789123456789123456789")
  126. for i := 0; i < b.N; i++ {
  127. PaddedBigBytes(bigint, 200)
  128. }
  129. }
  130. func BenchmarkPaddedBigBytesSmallPadding(b *testing.B) {
  131. bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
  132. for i := 0; i < b.N; i++ {
  133. PaddedBigBytes(bigint, 5)
  134. }
  135. }
  136. func BenchmarkPaddedBigBytesSmallOnePadding(b *testing.B) {
  137. bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
  138. for i := 0; i < b.N; i++ {
  139. PaddedBigBytes(bigint, 32)
  140. }
  141. }
  142. func BenchmarkByteAtBrandNew(b *testing.B) {
  143. bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
  144. for i := 0; i < b.N; i++ {
  145. bigEndianByteAt(bigint, 15)
  146. }
  147. }
  148. func BenchmarkByteAt(b *testing.B) {
  149. bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
  150. for i := 0; i < b.N; i++ {
  151. bigEndianByteAt(bigint, 15)
  152. }
  153. }
  154. func BenchmarkByteAtOld(b *testing.B) {
  155. bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
  156. for i := 0; i < b.N; i++ {
  157. PaddedBigBytes(bigint, 32)
  158. }
  159. }
  160. func TestReadBits(t *testing.T) {
  161. check := func(input string) {
  162. want, _ := hex.DecodeString(input)
  163. int, _ := new(big.Int).SetString(input, 16)
  164. buf := make([]byte, len(want))
  165. ReadBits(int, buf)
  166. if !bytes.Equal(buf, want) {
  167. t.Errorf("have: %x\nwant: %x", buf, want)
  168. }
  169. }
  170. check("000000000000000000000000000000000000000000000000000000FEFCF3F8F0")
  171. check("0000000000012345000000000000000000000000000000000000FEFCF3F8F0")
  172. check("18F8F8F1000111000110011100222004330052300000000000000000FEFCF3F8F0")
  173. }
  174. func TestU256(t *testing.T) {
  175. tests := []struct{ x, y *big.Int }{
  176. {x: big.NewInt(0), y: big.NewInt(0)},
  177. {x: big.NewInt(1), y: big.NewInt(1)},
  178. {x: BigPow(2, 255), y: BigPow(2, 255)},
  179. {x: BigPow(2, 256), y: big.NewInt(0)},
  180. {x: new(big.Int).Add(BigPow(2, 256), big.NewInt(1)), y: big.NewInt(1)},
  181. // negative values
  182. {x: big.NewInt(-1), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1))},
  183. {x: big.NewInt(-2), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2))},
  184. {x: BigPow(2, -255), y: big.NewInt(1)},
  185. }
  186. for _, test := range tests {
  187. if y := U256(new(big.Int).Set(test.x)); y.Cmp(test.y) != 0 {
  188. t.Errorf("U256(%x) = %x, want %x", test.x, y, test.y)
  189. }
  190. }
  191. }
  192. func TestBigEndianByteAt(t *testing.T) {
  193. tests := []struct {
  194. x string
  195. y int
  196. exp byte
  197. }{
  198. {"00", 0, 0x00},
  199. {"01", 1, 0x00},
  200. {"00", 1, 0x00},
  201. {"01", 0, 0x01},
  202. {"0000000000000000000000000000000000000000000000000000000000102030", 0, 0x30},
  203. {"0000000000000000000000000000000000000000000000000000000000102030", 1, 0x20},
  204. {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 31, 0xAB},
  205. {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 32, 0x00},
  206. {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 500, 0x00},
  207. }
  208. for _, test := range tests {
  209. v := new(big.Int).SetBytes(common.Hex2Bytes(test.x))
  210. actual := bigEndianByteAt(v, test.y)
  211. if actual != test.exp {
  212. t.Fatalf("Expected [%v] %v:th byte to be %v, was %v.", test.x, test.y, test.exp, actual)
  213. }
  214. }
  215. }
  216. func TestLittleEndianByteAt(t *testing.T) {
  217. tests := []struct {
  218. x string
  219. y int
  220. exp byte
  221. }{
  222. {"00", 0, 0x00},
  223. {"01", 1, 0x00},
  224. {"00", 1, 0x00},
  225. {"01", 0, 0x00},
  226. {"0000000000000000000000000000000000000000000000000000000000102030", 0, 0x00},
  227. {"0000000000000000000000000000000000000000000000000000000000102030", 1, 0x00},
  228. {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 31, 0x00},
  229. {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 32, 0x00},
  230. {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 0, 0xAB},
  231. {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 1, 0xCD},
  232. {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 0, 0x00},
  233. {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 1, 0xCD},
  234. {"0000000000000000000000000000000000000000000000000000000000102030", 31, 0x30},
  235. {"0000000000000000000000000000000000000000000000000000000000102030", 30, 0x20},
  236. {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 32, 0x0},
  237. {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 31, 0xFF},
  238. {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0xFFFF, 0x0},
  239. }
  240. for _, test := range tests {
  241. v := new(big.Int).SetBytes(common.Hex2Bytes(test.x))
  242. actual := Byte(v, 32, 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 TestS256(t *testing.T) {
  249. tests := []struct{ x, y *big.Int }{
  250. {x: big.NewInt(0), y: big.NewInt(0)},
  251. {x: big.NewInt(1), y: big.NewInt(1)},
  252. {x: big.NewInt(2), y: big.NewInt(2)},
  253. {
  254. x: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)),
  255. y: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)),
  256. },
  257. {
  258. x: BigPow(2, 255),
  259. y: new(big.Int).Neg(BigPow(2, 255)),
  260. },
  261. {
  262. x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1)),
  263. y: big.NewInt(-1),
  264. },
  265. {
  266. x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2)),
  267. y: big.NewInt(-2),
  268. },
  269. }
  270. for _, test := range tests {
  271. if y := S256(test.x); y.Cmp(test.y) != 0 {
  272. t.Errorf("S256(%x) = %x, want %x", test.x, y, test.y)
  273. }
  274. }
  275. }
  276. func TestExp(t *testing.T) {
  277. tests := []struct{ base, exponent, result *big.Int }{
  278. {base: big.NewInt(0), exponent: big.NewInt(0), result: big.NewInt(1)},
  279. {base: big.NewInt(1), exponent: big.NewInt(0), result: big.NewInt(1)},
  280. {base: big.NewInt(1), exponent: big.NewInt(1), result: big.NewInt(1)},
  281. {base: big.NewInt(1), exponent: big.NewInt(2), result: big.NewInt(1)},
  282. {base: big.NewInt(3), exponent: big.NewInt(144), result: MustParseBig256("507528786056415600719754159741696356908742250191663887263627442114881")},
  283. {base: big.NewInt(2), exponent: big.NewInt(255), result: MustParseBig256("57896044618658097711785492504343953926634992332820282019728792003956564819968")},
  284. }
  285. for _, test := range tests {
  286. if result := Exp(test.base, test.exponent); result.Cmp(test.result) != 0 {
  287. t.Errorf("Exp(%d, %d) = %d, want %d", test.base, test.exponent, result, test.result)
  288. }
  289. }
  290. }