big.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2014 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 common
  17. import "math/big"
  18. // Common big integers often used
  19. var (
  20. Big1 = big.NewInt(1)
  21. Big2 = big.NewInt(2)
  22. Big3 = big.NewInt(3)
  23. Big0 = big.NewInt(0)
  24. BigTrue = Big1
  25. BigFalse = Big0
  26. Big32 = big.NewInt(32)
  27. Big36 = big.NewInt(36)
  28. Big97 = big.NewInt(97)
  29. Big98 = big.NewInt(98)
  30. Big256 = big.NewInt(0xff)
  31. Big257 = big.NewInt(257)
  32. MaxBig = String2Big("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
  33. )
  34. // Big pow
  35. //
  36. // Returns the power of two big integers
  37. func BigPow(a, b int) *big.Int {
  38. c := new(big.Int)
  39. c.Exp(big.NewInt(int64(a)), big.NewInt(int64(b)), big.NewInt(0))
  40. return c
  41. }
  42. // Big
  43. //
  44. // Shortcut for new(big.Int).SetString(..., 0)
  45. func Big(num string) *big.Int {
  46. n := new(big.Int)
  47. n.SetString(num, 0)
  48. return n
  49. }
  50. // Bytes2Big
  51. //
  52. func BytesToBig(data []byte) *big.Int {
  53. n := new(big.Int)
  54. n.SetBytes(data)
  55. return n
  56. }
  57. func Bytes2Big(data []byte) *big.Int { return BytesToBig(data) }
  58. func BigD(data []byte) *big.Int { return BytesToBig(data) }
  59. func String2Big(num string) *big.Int {
  60. n := new(big.Int)
  61. n.SetString(num, 0)
  62. return n
  63. }
  64. func BitTest(num *big.Int, i int) bool {
  65. return num.Bit(i) > 0
  66. }
  67. // To256
  68. //
  69. // "cast" the big int to a 256 big int (i.e., limit to)
  70. var tt256 = new(big.Int).Lsh(big.NewInt(1), 256)
  71. var tt256m1 = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1))
  72. var tt255 = new(big.Int).Lsh(big.NewInt(1), 255)
  73. func U256(x *big.Int) *big.Int {
  74. //if x.Cmp(Big0) < 0 {
  75. // return new(big.Int).Add(tt256, x)
  76. // }
  77. x.And(x, tt256m1)
  78. return x
  79. }
  80. func S256(x *big.Int) *big.Int {
  81. if x.Cmp(tt255) < 0 {
  82. return x
  83. } else {
  84. // We don't want to modify x, ever
  85. return new(big.Int).Sub(x, tt256)
  86. }
  87. }
  88. func FirstBitSet(v *big.Int) int {
  89. for i := 0; i < v.BitLen(); i++ {
  90. if v.Bit(i) > 0 {
  91. return i
  92. }
  93. }
  94. return v.BitLen()
  95. }
  96. // Big to bytes
  97. //
  98. // Returns the bytes of a big integer with the size specified by **base**
  99. // Attempts to pad the byte array with zeros.
  100. func BigToBytes(num *big.Int, base int) []byte {
  101. ret := make([]byte, base/8)
  102. if len(num.Bytes()) > base/8 {
  103. return num.Bytes()
  104. }
  105. return append(ret[:len(ret)-len(num.Bytes())], num.Bytes()...)
  106. }
  107. // Big copy
  108. //
  109. // Creates a copy of the given big integer
  110. func BigCopy(src *big.Int) *big.Int {
  111. return new(big.Int).Set(src)
  112. }
  113. // Big max
  114. //
  115. // Returns the maximum size big integer
  116. func BigMax(x, y *big.Int) *big.Int {
  117. if x.Cmp(y) < 0 {
  118. return y
  119. }
  120. return x
  121. }
  122. // Big min
  123. //
  124. // Returns the minimum size big integer
  125. func BigMin(x, y *big.Int) *big.Int {
  126. if x.Cmp(y) > 0 {
  127. return y
  128. }
  129. return x
  130. }