big.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // GetBytes returns the absolute value of x as a big-endian byte slice.
  32. func (bi *BigInt) GetBytes() []byte {
  33. return bi.bigint.Bytes()
  34. }
  35. // String returns the value of x as a formatted decimal string.
  36. func (bi *BigInt) String() string {
  37. return bi.bigint.String()
  38. }
  39. // GetInt64 returns the int64 representation of x. If x cannot be represented in
  40. // an int64, the result is undefined.
  41. func (bi *BigInt) GetInt64() int64 {
  42. return bi.bigint.Int64()
  43. }
  44. // SetBytes interprets buf as the bytes of a big-endian unsigned integer and sets
  45. // the big int to that value.
  46. func (bi *BigInt) SetBytes(buf []byte) {
  47. bi.bigint.SetBytes(common.CopyBytes(buf))
  48. }
  49. // SetInt64 sets the big int to x.
  50. func (bi *BigInt) SetInt64(x int64) {
  51. bi.bigint.SetInt64(x)
  52. }
  53. // Sign returns:
  54. //
  55. // -1 if x < 0
  56. // 0 if x == 0
  57. // +1 if x > 0
  58. //
  59. func (bi *BigInt) Sign() int {
  60. return bi.bigint.Sign()
  61. }
  62. // SetString sets the big int to x.
  63. //
  64. // The string prefix determines the actual conversion base. A prefix of "0x" or
  65. // "0X" selects base 16; the "0" prefix selects base 8, and a "0b" or "0B" prefix
  66. // selects base 2. Otherwise the selected base is 10.
  67. func (bi *BigInt) SetString(x string, base int) {
  68. bi.bigint.SetString(x, base)
  69. }
  70. // BigInts represents a slice of big ints.
  71. type BigInts struct{ bigints []*big.Int }
  72. // Size returns the number of big ints in the slice.
  73. func (bi *BigInts) Size() int {
  74. return len(bi.bigints)
  75. }
  76. // Get returns the bigint at the given index from the slice.
  77. func (bi *BigInts) Get(index int) (bigint *BigInt, _ error) {
  78. if index < 0 || index >= len(bi.bigints) {
  79. return nil, errors.New("index out of bounds")
  80. }
  81. return &BigInt{bi.bigints[index]}, nil
  82. }
  83. // Set sets the big int at the given index in the slice.
  84. func (bi *BigInts) Set(index int, bigint *BigInt) error {
  85. if index < 0 || index >= len(bi.bigints) {
  86. return errors.New("index out of bounds")
  87. }
  88. bi.bigints[index] = bigint.bigint
  89. return nil
  90. }
  91. // GetString returns the value of x as a formatted string in some number base.
  92. func (bi *BigInt) GetString(base int) string {
  93. return bi.bigint.Text(base)
  94. }