OSDN Git Service

Merge pull request #41 from Bytom/dev
[bytom/vapor.git] / common / big.go
1 package common
2
3 import "math/big"
4
5 // Common big integers often used
6 var (
7         Big1     = big.NewInt(1)
8         Big2     = big.NewInt(2)
9         Big3     = big.NewInt(3)
10         Big0     = big.NewInt(0)
11         BigTrue  = Big1
12         BigFalse = Big0
13         Big32    = big.NewInt(32)
14         Big36    = big.NewInt(36)
15         Big97    = big.NewInt(97)
16         Big98    = big.NewInt(98)
17         Big256   = big.NewInt(0xff)
18         Big257   = big.NewInt(257)
19         MaxBig   = String2Big("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
20 )
21
22 // Big pow
23 //
24 // Returns the power of two big integers
25 func BigPow(a, b int) *big.Int {
26         c := new(big.Int)
27         c.Exp(big.NewInt(int64(a)), big.NewInt(int64(b)), big.NewInt(0))
28         return c
29 }
30
31 // Big
32 //
33 // Shortcut for new(big.Int).SetString(..., 0)
34 func Big(num string) *big.Int {
35         n := new(big.Int)
36         n.SetString(num, 0)
37         return n
38 }
39
40 // Bytes2Big
41 //
42 func BytesToBig(data []byte) *big.Int {
43         n := new(big.Int)
44         n.SetBytes(data)
45         return n
46 }
47
48 func Bytes2Big(data []byte) *big.Int { return BytesToBig(data) }
49
50 func BigD(data []byte) *big.Int { return BytesToBig(data) }
51
52 func String2Big(num string) *big.Int {
53         n := new(big.Int)
54         n.SetString(num, 0)
55         return n
56 }
57
58 func BitTest(num *big.Int, i int) bool {
59         return num.Bit(i) > 0
60 }
61
62 // To256
63 //
64 // "cast" the big int to a 256 big int (i.e., limit to)
65 var tt256 = new(big.Int).Lsh(big.NewInt(1), 256)
66 var tt256m1 = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1))
67 var tt255 = new(big.Int).Lsh(big.NewInt(1), 255)
68
69 func U256(x *big.Int) *big.Int {
70         //if x.Cmp(Big0) < 0 {
71         //              return new(big.Int).Add(tt256, x)
72         //      }
73
74         x.And(x, tt256m1)
75         return x
76 }
77
78 func S256(x *big.Int) *big.Int {
79         if x.Cmp(tt255) < 0 {
80                 return x
81         } else {
82                 // We don't want to modify x, ever
83                 return new(big.Int).Sub(x, tt256)
84         }
85 }
86
87 func FirstBitSet(v *big.Int) int {
88         for i := 0; i < v.BitLen(); i++ {
89                 if v.Bit(i) > 0 {
90                         return i
91                 }
92         }
93         return v.BitLen()
94 }
95
96 // Big to bytes
97 //
98 // Returns the bytes of a big integer with the size specified by **base**
99 // Attempts to pad the byte array with zeros.
100 func BigToBytes(num *big.Int, base int) []byte {
101         ret := make([]byte, base/8)
102
103         if len(num.Bytes()) > base/8 {
104                 return num.Bytes()
105         }
106
107         return append(ret[:len(ret)-len(num.Bytes())], num.Bytes()...)
108 }
109
110 // Big copy
111 //
112 // Creates a copy of the given big integer
113 func BigCopy(src *big.Int) *big.Int {
114         return new(big.Int).Set(src)
115 }
116
117 // Big max
118 //
119 // Returns the maximum size big integer
120 func BigMax(x, y *big.Int) *big.Int {
121         if x.Cmp(y) < 0 {
122                 return y
123         }
124         return x
125 }
126
127 // Big min
128 //
129 // Returns the minimum size big integer
130 func BigMin(x, y *big.Int) *big.Int {
131         if x.Cmp(y) > 0 {
132                 return y
133         }
134         return x
135 }