big.go 4.1 KB

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