OSDN Git Service

Merge pull request #935 from Bytom/dev
[bytom/bytom.git] / common / big.go
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         Big1     = big.NewInt(1)
24         Big2     = big.NewInt(2)
25         Big3     = big.NewInt(3)
26         Big0     = big.NewInt(0)
27         BigTrue  = Big1
28         BigFalse = Big0
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")
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 // Big
49 //
50 // Shortcut for new(big.Int).SetString(..., 0)
51 func Big(num string) *big.Int {
52         n := new(big.Int)
53         n.SetString(num, 0)
54
55         return n
56 }
57
58 // Bytes2Big
59 //
60 func BytesToBig(data []byte) *big.Int {
61         n := new(big.Int)
62         n.SetBytes(data)
63
64         return n
65 }
66 func Bytes2Big(data []byte) *big.Int { return BytesToBig(data) }
67 func BigD(data []byte) *big.Int      { return BytesToBig(data) }
68
69 func String2Big(num string) *big.Int {
70         n := new(big.Int)
71         n.SetString(num, 0)
72         return n
73 }
74
75 func BitTest(num *big.Int, i int) bool {
76         return num.Bit(i) > 0
77 }
78
79 // To256
80 //
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)
85
86 func U256(x *big.Int) *big.Int {
87         //if x.Cmp(Big0) < 0 {
88         //              return new(big.Int).Add(tt256, x)
89         //      }
90
91         x.And(x, tt256m1)
92
93         return x
94 }
95
96 func S256(x *big.Int) *big.Int {
97         if x.Cmp(tt255) < 0 {
98                 return x
99         } else {
100                 // We don't want to modify x, ever
101                 return new(big.Int).Sub(x, tt256)
102         }
103 }
104
105 func FirstBitSet(v *big.Int) int {
106         for i := 0; i < v.BitLen(); i++ {
107                 if v.Bit(i) > 0 {
108                         return i
109                 }
110         }
111
112         return v.BitLen()
113 }
114
115 // Big to bytes
116 //
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)
121
122         if len(num.Bytes()) > base/8 {
123                 return num.Bytes()
124         }
125
126         return append(ret[:len(ret)-len(num.Bytes())], num.Bytes()...)
127 }
128
129 // Big copy
130 //
131 // Creates a copy of the given big integer
132 func BigCopy(src *big.Int) *big.Int {
133         return new(big.Int).Set(src)
134 }
135
136 // Big max
137 //
138 // Returns the maximum size big integer
139 func BigMax(x, y *big.Int) *big.Int {
140         if x.Cmp(y) < 0 {
141                 return y
142         }
143
144         return x
145 }
146
147 // Big min
148 //
149 // Returns the minimum size big integer
150 func BigMin(x, y *big.Int) *big.Int {
151         if x.Cmp(y) > 0 {
152                 return y
153         }
154
155         return x
156 }