github.com/r8d8/go-ethereum@v5.5.2+incompatible/common/big.go (about) 1 // Copyright 2014 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 package common 18 19 import "math/big" 20 21 // Common big integers often used 22 var ( 23 Big0 = big.NewInt(0) 24 Big1 = big.NewInt(1) 25 Big2 = big.NewInt(2) 26 Big3 = big.NewInt(3) 27 BigTrue = Big1 28 BigFalse = big.NewInt(0) 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, _ = new(big.Int).SetString("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16) 36 ) 37 38 // Big pow 39 // 40 // Returns the power of two big integers 41 func BigPow(a, b int) *big.Int { 42 c := new(big.Int) 43 c.Exp(big.NewInt(int64(a)), big.NewInt(int64(b)), big.NewInt(0)) 44 45 return c 46 } 47 48 func BitTest(num *big.Int, i int) bool { 49 return num.Bit(i) > 0 50 } 51 52 // To256 53 // 54 // "cast" the big int to a 256 big int (i.e., limit to) 55 var tt256 = new(big.Int).Lsh(big.NewInt(1), 256) 56 var tt256m1 = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1)) 57 var tt255 = new(big.Int).Lsh(big.NewInt(1), 255) 58 59 func U256(x *big.Int) *big.Int { 60 return x.And(x, tt256m1) 61 } 62 63 func S256(x *big.Int) *big.Int { 64 if x.Cmp(tt255) < 0 { 65 return x 66 } else { 67 // We don't want to modify x, ever 68 return new(big.Int).Sub(x, tt256) 69 } 70 } 71 72 func FirstBitSet(v *big.Int) int { 73 for i := 0; i < v.BitLen(); i++ { 74 if v.Bit(i) > 0 { 75 return i 76 } 77 } 78 79 return v.BitLen() 80 } 81 82 // Big to bytes 83 // 84 // Returns the bytes of a big integer with the size specified by **base** 85 // Attempts to pad the byte array with zeros. 86 func BigToBytes(num *big.Int, base int) []byte { 87 ret := make([]byte, base/8) 88 89 if len(num.Bytes()) > base/8 { 90 return num.Bytes() 91 } 92 93 return append(ret[:len(ret)-len(num.Bytes())], num.Bytes()...) 94 } 95 96 // Big max 97 // 98 // Returns the maximum size big integer 99 func BigMax(x, y *big.Int) *big.Int { 100 if x.Cmp(y) < 0 { 101 return y 102 } 103 104 return x 105 } 106 107 // Big min 108 // 109 // Returns the minimum size big integer 110 func BigMin(x, y *big.Int) *big.Int { 111 if x.Cmp(y) > 0 { 112 return y 113 } 114 115 return x 116 }