1 // Copyright 2014 The go-ethereum Authors
2 // This file is part of the go-ethereum library.
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.
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.
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/>.
21 // Common big integers often used
29 Big32 = big.NewInt(32)
30 Big36 = big.NewInt(36)
31 Big97 = big.NewInt(97)
32 Big98 = big.NewInt(98)
33 Big256 = big.NewInt(0xff)
34 Big257 = big.NewInt(257)
35 MaxBig = String2Big("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
40 // Returns the power of two big integers
41 func BigPow(a, b int) *big.Int {
43 c.Exp(big.NewInt(int64(a)), big.NewInt(int64(b)), big.NewInt(0))
50 // Shortcut for new(big.Int).SetString(..., 0)
51 func Big(num string) *big.Int {
60 func BytesToBig(data []byte) *big.Int {
66 func Bytes2Big(data []byte) *big.Int { return BytesToBig(data) }
67 func BigD(data []byte) *big.Int { return BytesToBig(data) }
69 func String2Big(num string) *big.Int {
75 func BitTest(num *big.Int, i int) bool {
81 // "cast" the big int to a 256 big int (i.e., limit to)
82 var tt256 = new(big.Int).Lsh(big.NewInt(1), 256)
83 var tt256m1 = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1))
84 var tt255 = new(big.Int).Lsh(big.NewInt(1), 255)
86 func U256(x *big.Int) *big.Int {
87 //if x.Cmp(Big0) < 0 {
88 // return new(big.Int).Add(tt256, x)
96 func S256(x *big.Int) *big.Int {
100 // We don't want to modify x, ever
101 return new(big.Int).Sub(x, tt256)
105 func FirstBitSet(v *big.Int) int {
106 for i := 0; i < v.BitLen(); i++ {
117 // Returns the bytes of a big integer with the size specified by **base**
118 // Attempts to pad the byte array with zeros.
119 func BigToBytes(num *big.Int, base int) []byte {
120 ret := make([]byte, base/8)
122 if len(num.Bytes()) > base/8 {
126 return append(ret[:len(ret)-len(num.Bytes())], num.Bytes()...)
131 // Creates a copy of the given big integer
132 func BigCopy(src *big.Int) *big.Int {
133 return new(big.Int).Set(src)
138 // Returns the maximum size big integer
139 func BigMax(x, y *big.Int) *big.Int {
149 // Returns the minimum size big integer
150 func BigMin(x, y *big.Int) *big.Int {