big.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 provides integer math utilities.
  17. package math
  18. import (
  19. "math/big"
  20. )
  21. var (
  22. tt255 = BigPow(2, 255)
  23. tt256 = BigPow(2, 256)
  24. tt256m1 = new(big.Int).Sub(tt256, big.NewInt(1))
  25. MaxBig256 = new(big.Int).Set(tt256m1)
  26. )
  27. // ParseBig256 parses s as a 256 bit integer in decimal or hexadecimal syntax.
  28. // Leading zeros are accepted. The empty string parses as zero.
  29. func ParseBig256(s string) (*big.Int, bool) {
  30. if s == "" {
  31. return new(big.Int), true
  32. }
  33. var bigint *big.Int
  34. var ok bool
  35. if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") {
  36. bigint, ok = new(big.Int).SetString(s[2:], 16)
  37. } else {
  38. bigint, ok = new(big.Int).SetString(s, 10)
  39. }
  40. if ok && bigint.BitLen() > 256 {
  41. bigint, ok = nil, false
  42. }
  43. return bigint, ok
  44. }
  45. // MustParseBig parses s as a 256 bit big integer and panics if the string is invalid.
  46. func MustParseBig256(s string) *big.Int {
  47. v, ok := ParseBig256(s)
  48. if !ok {
  49. panic("invalid 256 bit integer: " + s)
  50. }
  51. return v
  52. }
  53. // BigPow returns a ** b as a big integer.
  54. func BigPow(a, b int64) *big.Int {
  55. r := big.NewInt(a)
  56. return r.Exp(r, big.NewInt(b), nil)
  57. }
  58. // BigMax returns the larger of x or y.
  59. func BigMax(x, y *big.Int) *big.Int {
  60. if x.Cmp(y) < 0 {
  61. return y
  62. }
  63. return x
  64. }
  65. // BigMin returns the smaller of x or y.
  66. func BigMin(x, y *big.Int) *big.Int {
  67. if x.Cmp(y) > 0 {
  68. return y
  69. }
  70. return x
  71. }
  72. // FirstBitSet returns the index of the first 1 bit in v, counting from LSB.
  73. func FirstBitSet(v *big.Int) int {
  74. for i := 0; i < v.BitLen(); i++ {
  75. if v.Bit(i) > 0 {
  76. return i
  77. }
  78. }
  79. return v.BitLen()
  80. }
  81. // PaddedBigBytes encodes a big integer as a big-endian byte slice. The length
  82. // of the slice is at least n bytes.
  83. func PaddedBigBytes(bigint *big.Int, n int) []byte {
  84. bytes := bigint.Bytes()
  85. if len(bytes) >= n {
  86. return bytes
  87. }
  88. ret := make([]byte, n)
  89. return append(ret[:len(ret)-len(bytes)], bytes...)
  90. }
  91. // U256 encodes as a 256 bit two's complement number. This operation is destructive.
  92. func U256(x *big.Int) *big.Int {
  93. return x.And(x, tt256m1)
  94. }
  95. // S256 interprets x as a two's complement number.
  96. // x must not exceed 256 bits (the result is undefined if it does) and is not modified.
  97. //
  98. // S256(0) = 0
  99. // S256(1) = 1
  100. // S256(2**255) = -2**255
  101. // S256(2**256-1) = -1
  102. func S256(x *big.Int) *big.Int {
  103. if x.Cmp(tt255) < 0 {
  104. return x
  105. } else {
  106. return new(big.Int).Sub(x, tt256)
  107. }
  108. }
  109. // wordSize is the size number of bits in a big.Word.
  110. const wordSize = 32 << (uint64(^big.Word(0)) >> 63)
  111. // Exp implements exponentiation by squaring.
  112. // Exp returns a newly-allocated big integer and does not change
  113. // base or exponent. The result is truncated to 256 bits.
  114. //
  115. // Courtesy @karalabe and @chfast
  116. func Exp(base, exponent *big.Int) *big.Int {
  117. result := big.NewInt(1)
  118. for _, word := range exponent.Bits() {
  119. for i := 0; i < wordSize; i++ {
  120. if word&1 == 1 {
  121. U256(result.Mul(result, base))
  122. }
  123. U256(base.Mul(base, base))
  124. word >>= 1
  125. }
  126. }
  127. return result
  128. }