big.go 3.2 KB

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