github.com/core-coin/go-core/v2@v2.1.9/mobile/big.go (about) 1 // Copyright 2016 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>. 16 17 // Contains all the wrappers from the math/big package. 18 19 package gocore 20 21 import ( 22 "errors" 23 "math/big" 24 25 "github.com/core-coin/go-core/v2/common" 26 ) 27 28 // A BigInt represents a signed multi-precision integer. 29 type BigInt struct { 30 bigint *big.Int 31 } 32 33 // NewBigInt allocates and returns a new BigInt set to x. 34 func NewBigInt(x int64) *BigInt { 35 return &BigInt{big.NewInt(x)} 36 } 37 38 // NewBigIntFromString allocates and returns a new BigInt set to x 39 // interpreted in the provided base. 40 func NewBigIntFromString(x string, base int) *BigInt { 41 b, success := new(big.Int).SetString(x, base) 42 if !success { 43 return nil 44 } 45 return &BigInt{b} 46 } 47 48 // GetBytes returns the absolute value of x as a big-endian byte slice. 49 func (bi *BigInt) GetBytes() []byte { 50 return bi.bigint.Bytes() 51 } 52 53 // String returns the value of x as a formatted decimal string. 54 func (bi *BigInt) String() string { 55 return bi.bigint.String() 56 } 57 58 // GetInt64 returns the int64 representation of x. If x cannot be represented in 59 // an int64, the result is undefined. 60 func (bi *BigInt) GetInt64() int64 { 61 return bi.bigint.Int64() 62 } 63 64 // SetBytes interprets buf as the bytes of a big-endian unsigned integer and sets 65 // the big int to that value. 66 func (bi *BigInt) SetBytes(buf []byte) { 67 bi.bigint.SetBytes(common.CopyBytes(buf)) 68 } 69 70 // SetInt64 sets the big int to x. 71 func (bi *BigInt) SetInt64(x int64) { 72 bi.bigint.SetInt64(x) 73 } 74 75 // Sign returns: 76 // 77 // -1 if x < 0 78 // 0 if x == 0 79 // +1 if x > 0 80 func (bi *BigInt) Sign() int { 81 return bi.bigint.Sign() 82 } 83 84 // SetString sets the big int to x. 85 // 86 // The string prefix determines the actual conversion base. A prefix of "0x" or 87 // "0X" selects base 16; the "0" prefix selects base 8, and a "0b" or "0B" prefix 88 // selects base 2. Otherwise the selected base is 10. 89 func (bi *BigInt) SetString(x string, base int) { 90 bi.bigint.SetString(x, base) 91 } 92 93 // BigInts represents a slice of big ints. 94 type BigInts struct{ bigints []*big.Int } 95 96 // NewBigInts creates a slice of uninitialized big numbers. 97 func NewBigInts(size int) *BigInts { 98 return &BigInts{ 99 bigints: make([]*big.Int, size), 100 } 101 } 102 103 // Size returns the number of big ints in the slice. 104 func (bi *BigInts) Size() int { 105 return len(bi.bigints) 106 } 107 108 // Get returns the bigint at the given index from the slice. 109 func (bi *BigInts) Get(index int) (bigint *BigInt, _ error) { 110 if index < 0 || index >= len(bi.bigints) { 111 return nil, errors.New("index out of bounds") 112 } 113 return &BigInt{bi.bigints[index]}, nil 114 } 115 116 // Set sets the big int at the given index in the slice. 117 func (bi *BigInts) Set(index int, bigint *BigInt) error { 118 if index < 0 || index >= len(bi.bigints) { 119 return errors.New("index out of bounds") 120 } 121 bi.bigints[index] = bigint.bigint 122 return nil 123 } 124 125 // GetString returns the value of x as a formatted string in some number base. 126 func (bi *BigInt) GetString(base int) string { 127 return bi.bigint.Text(base) 128 }