big.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2016 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. // Contains all the wrappers from the math/big package.
  17. package geth
  18. import (
  19. "errors"
  20. "math/big"
  21. "github.com/ethereum/go-ethereum/common"
  22. )
  23. // A BigInt represents a signed multi-precision integer.
  24. type BigInt struct {
  25. bigint *big.Int
  26. }
  27. // NewBigInt allocates and returns a new BigInt set to x.
  28. func NewBigInt(x int64) *BigInt {
  29. return &BigInt{big.NewInt(x)}
  30. }
  31. // NewBigIntFromString allocates and returns a new BigInt set to x
  32. // interpreted in the provided base.
  33. func NewBigIntFromString(x string, base int) *BigInt {
  34. b, success := new(big.Int).SetString(x, base)
  35. if !success {
  36. return nil
  37. }
  38. return &BigInt{b}
  39. }
  40. // GetBytes returns the absolute value of x as a big-endian byte slice.
  41. func (bi *BigInt) GetBytes() []byte {
  42. return bi.bigint.Bytes()
  43. }
  44. // String returns the value of x as a formatted decimal string.
  45. func (bi *BigInt) String() string {
  46. return bi.bigint.String()
  47. }
  48. // GetInt64 returns the int64 representation of x. If x cannot be represented in
  49. // an int64, the result is undefined.
  50. func (bi *BigInt) GetInt64() int64 {
  51. return bi.bigint.Int64()
  52. }
  53. // SetBytes interprets buf as the bytes of a big-endian unsigned integer and sets
  54. // the big int to that value.
  55. func (bi *BigInt) SetBytes(buf []byte) {
  56. bi.bigint.SetBytes(common.CopyBytes(buf))
  57. }
  58. // SetInt64 sets the big int to x.
  59. func (bi *BigInt) SetInt64(x int64) {
  60. bi.bigint.SetInt64(x)
  61. }
  62. // Sign returns:
  63. //
  64. // -1 if x < 0
  65. // 0 if x == 0
  66. // +1 if x > 0
  67. //
  68. func (bi *BigInt) Sign() int {
  69. return bi.bigint.Sign()
  70. }
  71. // SetString sets the big int to x.
  72. //
  73. // The string prefix determines the actual conversion base. A prefix of "0x" or
  74. // "0X" selects base 16; the "0" prefix selects base 8, and a "0b" or "0B" prefix
  75. // selects base 2. Otherwise the selected base is 10.
  76. func (bi *BigInt) SetString(x string, base int) {
  77. bi.bigint.SetString(x, base)
  78. }
  79. // BigInts represents a slice of big ints.
  80. type BigInts struct{ bigints []*big.Int }
  81. // NewBigInts creates a slice of uninitialized big numbers.
  82. func NewBigInts(size int) *BigInts {
  83. return &BigInts{
  84. bigints: make([]*big.Int, size),
  85. }
  86. }
  87. // Size returns the number of big ints in the slice.
  88. func (bi *BigInts) Size() int {
  89. return len(bi.bigints)
  90. }
  91. // Get returns the bigint at the given index from the slice.
  92. func (bi *BigInts) Get(index int) (bigint *BigInt, _ error) {
  93. if index < 0 || index >= len(bi.bigints) {
  94. return nil, errors.New("index out of bounds")
  95. }
  96. return &BigInt{bi.bigints[index]}, nil
  97. }
  98. // Set sets the big int at the given index in the slice.
  99. func (bi *BigInts) Set(index int, bigint *BigInt) error {
  100. if index < 0 || index >= len(bi.bigints) {
  101. return errors.New("index out of bounds")
  102. }
  103. bi.bigints[index] = bigint.bigint
  104. return nil
  105. }
  106. // GetString returns the value of x as a formatted string in some number base.
  107. func (bi *BigInt) GetString(base int) string {
  108. return bi.bigint.Text(base)
  109. }