gitee.com/lh-her-team/common@v1.5.1/evmutils/evmInt256.go (about) 1 /* 2 * Copyright 2020 The SealEVM Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package evmutils 18 19 import ( 20 "encoding/hex" 21 "math/big" 22 ) 23 24 func maxUint(bits uint) *big.Int { 25 maxA1 := big.NewInt(0).Lsh(big.NewInt(1), bits) 26 return maxA1.Sub(maxA1, big.NewInt(1)) 27 } 28 29 func bit(n uint) *big.Int { 30 i := big.NewInt(1) 31 i.Lsh(i, n) 32 return i 33 } 34 35 func pow(x, y int64) *big.Int { 36 bx := big.NewInt(x) 37 by := big.NewInt(y) 38 return bx.Exp(bx, by, nil) 39 } 40 41 const ( 42 maxBits = 256 43 ) 44 45 var ( 46 uint256MAX = maxUint(maxBits) 47 int256MAX = pow(2, maxBits-1) 48 bit256 = bit(maxBits) 49 one = big.NewInt(1) 50 ) 51 52 type Int struct { 53 *big.Int 54 } 55 56 func (i *Int) AsStringKey() string { 57 return string(i.Bytes()) 58 } 59 60 func (i Int) Clone() *Int { 61 return FromBigInt(i.Int) 62 } 63 64 func New(i int64) *Int { 65 return &Int{big.NewInt(i)} 66 } 67 68 func FromBigInt(i *big.Int) *Int { 69 return &Int{big.NewInt(0).Set(i)} 70 } 71 72 // FromString from fmt data Int.AsStringKey 73 func FromString(ss string) *Int { 74 s := hex.EncodeToString([]byte(ss)) 75 return FromHexString(s) 76 } 77 78 // FromString from fmt data hex.encodeToString([]byte(Int.AsStringKey)) 79 func FromHexString(s string) *Int { 80 bi := big.NewInt(0) 81 _, success := bi.SetString(s, 16) 82 if success { 83 i := &Int{bi} 84 i.toI256() 85 return i 86 } 87 return nil 88 } 89 90 // FromString from fmt data Int.String() 91 func FromDecimalString(s string) *Int { 92 bi := big.NewInt(0) 93 _, success := bi.SetString(s, 10) 94 if success { 95 i := &Int{bi} 96 i.toI256() 97 return i 98 } 99 return nil 100 } 101 102 func (i *Int) toI256() *Int { 103 i.Int.And(i.Int, uint256MAX) 104 return i 105 } 106 107 func (i *Int) GetSigned() *Int { 108 ensure256 := big.NewInt(0).And(i.Int, uint256MAX) 109 if ensure256.Cmp(int256MAX) >= 0 { 110 ensure256.Sub(ensure256, bit256) 111 } 112 return &Int{ensure256} 113 } 114 115 func (i *Int) Add(y *Int) *Int { 116 i.Int.Add(i.Int, y.Int) 117 return i.toI256() 118 } 119 120 func (i *Int) Mul(y *Int) *Int { 121 i.Int.Mul(i.Int, y.Int) 122 return i.toI256() 123 } 124 125 func (i *Int) Sub(y *Int) *Int { 126 i.Int.Sub(i.Int, y.Int) 127 return i.toI256() 128 } 129 130 func (i *Int) Div(y *Int) *Int { 131 if y.Sign() == 0 { 132 i.Int.SetUint64(0) 133 return i 134 } 135 136 i.Int.Div(i.Int, y.Int) 137 return i.toI256() 138 } 139 140 func (i *Int) SDiv(y *Int) *Int { 141 if y.Sign() == 0 { 142 i.SetUint64(0) 143 return i 144 } 145 needNeg := i.Sign() != y.Sign() 146 i.Int.Div(i.Int.Abs(i.Int), y.Int.Abs(y.Int)) 147 if needNeg { 148 i.Neg(i.Int) 149 } 150 return i.toI256() 151 } 152 153 func (i *Int) Mod(m *Int) *Int { 154 if m.Sign() == 0 || i.Sign() == 0 || m.Int.Cmp(one) == 0 { 155 i.SetUint64(0) 156 return i 157 } 158 i.Int.Mod(i.Int, m.Int) 159 return i.toI256() 160 } 161 162 func (i *Int) SMod(m *Int) *Int { 163 mAbs := big.NewInt(0) 164 mAbs.Abs(m.Int) 165 if m.Sign() == 0 || i.Sign() == 0 || m.Int.Cmp(one) == 0 { 166 i.SetUint64(0) 167 return i 168 } 169 needNeg := i.Sign() < 0 170 i.Int.Mod(i.Abs(i.Int), mAbs) 171 if needNeg { 172 i.Neg(i.Int) 173 } 174 return i.toI256() 175 } 176 177 func (i *Int) AddMod(y *Int, m *Int) *Int { 178 if m.Sign() <= 0 || m.Int.Cmp(one) == 0 { 179 i.SetUint64(0) 180 return i 181 } 182 i.Int.Add(i.Int, y.Int) 183 return i.Mod(m) 184 } 185 186 func (i *Int) MulMod(y *Int, m *Int) *Int { 187 if m.Sign() <= 0 || m.Int.Cmp(one) == 0 { 188 i.SetUint64(0) 189 return i 190 } 191 i.Int.Mul(i.Int, y.Int) 192 return i.Mod(m) 193 } 194 195 func (i *Int) Exp(e *Int) *Int { 196 i.Int.Exp(i.Int, e.Int, nil) 197 return i.toI256() 198 } 199 200 func (i *Int) SignExtend(baseBytes *Int) *Int { 201 if baseBytes.Cmp(big.NewInt(31)) < 0 { 202 bits := uint(baseBytes.Uint64()*8 + 7) 203 mask := maxUint(bits) 204 if i.Int.Bit(int(bits)) > 0 { 205 i.Int.Or(i.Int, mask.Not(mask)) 206 } else { 207 i.Int.And(i.Int, mask) 208 } 209 } 210 return i.toI256() 211 } 212 213 func (i *Int) LT(y *Int) bool { 214 return i.Int.Cmp(y.Int) < 0 215 } 216 217 func (i *Int) GT(y *Int) bool { 218 return i.Int.Cmp(y.Int) > 0 219 } 220 221 func (i *Int) SLT(y *Int) bool { 222 si := i.GetSigned() 223 sy := y.GetSigned() 224 return si.Int.Cmp(sy.Int) < 0 225 } 226 227 func (i *Int) SGT(y *Int) bool { 228 si := i.GetSigned() 229 sy := y.GetSigned() 230 return si.Int.Cmp(sy.Int) > 0 231 } 232 233 func (i *Int) EQ(y *Int) bool { 234 return i.Int.Cmp(y.Int) == 0 235 } 236 237 func (i *Int) IsZero() bool { 238 return i.Sign() <= 0 239 } 240 241 func MinInt(i, j *Int) *Int { 242 if i.GT(j) { 243 return j 244 } 245 return i 246 } 247 func MinI(i, j int64) int64 { 248 if i > j { 249 return j 250 } 251 return i 252 } 253 254 func (i *Int) And(y *Int) *Int { 255 i.Int.And(i.Int, y.Int) 256 return i.toI256() 257 } 258 259 func (i *Int) Or(y *Int) *Int { 260 i.Int.Or(i.Int, y.Int) 261 return i.toI256() 262 } 263 264 func (i *Int) XOr(y *Int) *Int { 265 i.Int.Xor(i.Int, y.Int) 266 return i.toI256() 267 } 268 269 func (i *Int) Not(y *Int) *Int { 270 i.Int.Not(i.Int) 271 return i.toI256() 272 } 273 274 func (i *Int) ByteAt(n int) byte { 275 if n > 31 { 276 return 0 277 } 278 bnBytes := i.Int.Bytes() 279 bnLen := len(bnBytes) 280 bytePos := bnLen - n - 1 281 if bytePos < 0 { 282 return 0 283 } 284 return bnBytes[bytePos] 285 } 286 287 func (i *Int) SHL(n uint64) *Int { 288 if n >= maxBits { 289 i.Int.SetUint64(0) 290 } else { 291 i.Int.Lsh(i.Int, uint(n)) 292 } 293 return i.toI256() 294 } 295 296 func (i *Int) SHR(n uint64) *Int { 297 if n >= maxBits { 298 i.Int.SetUint64(0) 299 } else { 300 i.Int.Rsh(i.Int, uint(n)) 301 } 302 return i.toI256() 303 } 304 305 func (i *Int) SAR(n uint64) *Int { 306 si := i.GetSigned() 307 if n >= maxBits { 308 if si.Sign() >= 0 { 309 si.SetUint64(0) 310 } else { 311 si.SetInt64(-1) 312 } 313 } else { 314 si.Rsh(si.Int, uint(n)) 315 } 316 i.Set(si.Int) 317 return i.toI256() 318 }