github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/mobile/big.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 // Contains all the wrappers from the math/big package. 19 20 package geth 21 22 import ( 23 "errors" 24 "math/big" 25 26 "github.com/AigarNetwork/aigar/common" 27 ) 28 29 // A BigInt represents a signed multi-precision integer. 30 type BigInt struct { 31 bigint *big.Int 32 } 33 34 // NewBigInt allocates and returns a new BigInt set to x. 35 func NewBigInt(x int64) *BigInt { 36 return &BigInt{big.NewInt(x)} 37 } 38 39 // GetBytes returns the absolute value of x as a big-endian byte slice. 40 func (bi *BigInt) GetBytes() []byte { 41 return bi.bigint.Bytes() 42 } 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 49 // GetInt64 returns the int64 representation of x. If x cannot be represented in 50 // an int64, the result is undefined. 51 func (bi *BigInt) GetInt64() int64 { 52 return bi.bigint.Int64() 53 } 54 55 // SetBytes interprets buf as the bytes of a big-endian unsigned integer and sets 56 // the big int to that value. 57 func (bi *BigInt) SetBytes(buf []byte) { 58 bi.bigint.SetBytes(common.CopyBytes(buf)) 59 } 60 61 // SetInt64 sets the big int to x. 62 func (bi *BigInt) SetInt64(x int64) { 63 bi.bigint.SetInt64(x) 64 } 65 66 // Sign returns: 67 // 68 // -1 if x < 0 69 // 0 if x == 0 70 // +1 if x > 0 71 // 72 func (bi *BigInt) Sign() int { 73 return bi.bigint.Sign() 74 } 75 76 // SetString sets the big int to x. 77 // 78 // The string prefix determines the actual conversion base. A prefix of "0x" or 79 // "0X" selects base 16; the "0" prefix selects base 8, and a "0b" or "0B" prefix 80 // selects base 2. Otherwise the selected base is 10. 81 func (bi *BigInt) SetString(x string, base int) { 82 bi.bigint.SetString(x, base) 83 } 84 85 // BigInts represents a slice of big ints. 86 type BigInts struct{ bigints []*big.Int } 87 88 // NewBigInts creates a slice of uninitialized big numbers. 89 func NewBigInts(size int) *BigInts { 90 return &BigInts{ 91 bigints: make([]*big.Int, size), 92 } 93 } 94 95 // Size returns the number of big ints in the slice. 96 func (bi *BigInts) Size() int { 97 return len(bi.bigints) 98 } 99 100 // Get returns the bigint at the given index from the slice. 101 func (bi *BigInts) Get(index int) (bigint *BigInt, _ error) { 102 if index < 0 || index >= len(bi.bigints) { 103 return nil, errors.New("index out of bounds") 104 } 105 return &BigInt{bi.bigints[index]}, nil 106 } 107 108 // Set sets the big int at the given index in the slice. 109 func (bi *BigInts) Set(index int, bigint *BigInt) error { 110 if index < 0 || index >= len(bi.bigints) { 111 return errors.New("index out of bounds") 112 } 113 bi.bigints[index] = bigint.bigint 114 return nil 115 } 116 117 // GetString returns the value of x as a formatted string in some number base. 118 func (bi *BigInt) GetString(base int) string { 119 return bi.bigint.Text(base) 120 }