big.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. )
  22. // A BigInt represents a signed multi-precision integer.
  23. type BigInt struct {
  24. bigint *big.Int
  25. }
  26. // NewBigInt allocates and returns a new BigInt set to x.
  27. func NewBigInt(x int64) *BigInt {
  28. return &BigInt{big.NewInt(x)}
  29. }
  30. // GetBytes returns the absolute value of x as a big-endian byte slice.
  31. func (bi *BigInt) GetBytes() []byte {
  32. return bi.bigint.Bytes()
  33. }
  34. // String returns the value of x as a formatted decimal string.
  35. func (bi *BigInt) String() string {
  36. return bi.bigint.String()
  37. }
  38. // GetInt64 returns the int64 representation of x. If x cannot be represented in
  39. // an int64, the result is undefined.
  40. func (bi *BigInt) GetInt64() int64 {
  41. return bi.bigint.Int64()
  42. }
  43. // SetBytes interprets buf as the bytes of a big-endian unsigned integer and sets
  44. // the big int to that value.
  45. func (bi *BigInt) SetBytes(buf []byte) {
  46. bi.bigint.SetBytes(buf)
  47. }
  48. // SetInt64 sets the big int to x.
  49. func (bi *BigInt) SetInt64(x int64) {
  50. bi.bigint.SetInt64(x)
  51. }
  52. // SetString sets the big int to x.
  53. //
  54. // The string prefix determines the actual conversion base. A prefix of "0x" or
  55. // "0X" selects base 16; the "0" prefix selects base 8, and a "0b" or "0B" prefix
  56. // selects base 2. Otherwise the selected base is 10.
  57. func (bi *BigInt) SetString(x string, base int) {
  58. bi.bigint.SetString(x, base)
  59. }
  60. // BigInts represents a slice of big ints.
  61. type BigInts struct{ bigints []*big.Int }
  62. // Size returns the number of big ints in the slice.
  63. func (bi *BigInts) Size() int {
  64. return len(bi.bigints)
  65. }
  66. // Get returns the bigint at the given index from the slice.
  67. func (bi *BigInts) Get(index int) (*BigInt, error) {
  68. if index < 0 || index >= len(bi.bigints) {
  69. return nil, errors.New("index out of bounds")
  70. }
  71. return &BigInt{bi.bigints[index]}, nil
  72. }
  73. // Set sets the big int at the given index in the slice.
  74. func (bi *BigInts) Set(index int, bigint *BigInt) error {
  75. if index < 0 || index >= len(bi.bigints) {
  76. return errors.New("index out of bounds")
  77. }
  78. bi.bigints[index] = bigint.bigint
  79. return nil
  80. }