big.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // SetString sets the big int to x.
  54. //
  55. // The string prefix determines the actual conversion base. A prefix of "0x" or
  56. // "0X" selects base 16; the "0" prefix selects base 8, and a "0b" or "0B" prefix
  57. // selects base 2. Otherwise the selected base is 10.
  58. func (bi *BigInt) SetString(x string, base int) {
  59. bi.bigint.SetString(x, base)
  60. }
  61. // BigInts represents a slice of big ints.
  62. type BigInts struct{ bigints []*big.Int }
  63. // Size returns the number of big ints in the slice.
  64. func (bi *BigInts) Size() int {
  65. return len(bi.bigints)
  66. }
  67. // Get returns the bigint at the given index from the slice.
  68. func (bi *BigInts) Get(index int) (bigint *BigInt, _ error) {
  69. if index < 0 || index >= len(bi.bigints) {
  70. return nil, errors.New("index out of bounds")
  71. }
  72. return &BigInt{bi.bigints[index]}, nil
  73. }
  74. // Set sets the big int at the given index in the slice.
  75. func (bi *BigInts) Set(index int, bigint *BigInt) error {
  76. if index < 0 || index >= len(bi.bigints) {
  77. return errors.New("index out of bounds")
  78. }
  79. bi.bigints[index] = bigint.bigint
  80. return nil
  81. }
  82. // GetString returns the value of x as a formatted string in some number base.
  83. func (bi *BigInt) GetString(base int) string {
  84. return bi.bigint.Text(base)
  85. }