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