// Copyright 2025 The Lux Authors // This file is part of the Lux library. // // The Lux library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // The Lux library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with the Lux library. If not, see . // Package math provides integer math utilities. package math import ( "fmt" "math/big" "math/bits" ) // Various big integer limit values. var ( tt256 = new(big.Int).Lsh(big.NewInt(1), 256) tt256m1 = new(big.Int).Sub(tt256, big.NewInt(1)) tt255 = new(big.Int).Lsh(big.NewInt(1), 255) MaxBig256 = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1)) MaxBig63 = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 63), big.NewInt(1)) ) // ParseBig256 parses s as a 256 bit integer in decimal or hexadecimal syntax. // Leading zeros are accepted. The empty string parses as zero. func ParseBig256(s string) (*big.Int, bool) { if s == "" { return new(big.Int), true } var bigint *big.Int var ok bool if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") { bigint, ok = new(big.Int).SetString(s[2:], 16) } else { bigint, ok = new(big.Int).SetString(s, 10) } if ok && bigint.BitLen() > 256 { bigint, ok = nil, false } return bigint, ok } // MustParseBig256 parses s as a 256 bit big integer and panics if the string is invalid. func MustParseBig256(s string) *big.Int { v, ok := ParseBig256(s) if !ok { panic("invalid 256 bit integer: " + s) } return v } // BigPow returns a ** b as a big integer. func BigPow(a, b int64) *big.Int { r := big.NewInt(a) return r.Exp(r, big.NewInt(b), nil) } // BigMax returns the larger of x or y. func BigMax(x, y *big.Int) *big.Int { if x.Cmp(y) < 0 { return y } return x } // BigMin returns the smaller of x or y. func BigMin(x, y *big.Int) *big.Int { if x.Cmp(y) > 0 { return y } return x } // PaddedBigBytes encodes a big integer as a big-endian byte slice. The length // of the slice is at least n bytes. func PaddedBigBytes(bigint *big.Int, n int) []byte { if bigint.BitLen()/8 >= n { return bigint.Bytes() } ret := make([]byte, n) bigint.FillBytes(ret) return ret } // ReadBits encodes the absolute value of bigint as big-endian bytes. Callers must ensure // that bigint is non-negative. func ReadBits(bigint *big.Int, buf []byte) { i := len(buf) for _, d := range bigint.Bits() { for j := 0; j < wordBytes && i > 0; j++ { i-- buf[i] = byte(d) d >>= 8 } } } // U256 encodes as a 256 bit two's complement number. This operation is destructive. func U256(x *big.Int) *big.Int { return x.And(x, tt256m1) } // U256Bytes converts a big Int into a 256bit EVM number. // This operation is destructive. func U256Bytes(n *big.Int) []byte { return PaddedBigBytes(U256(n), 32) } // S256 interprets x as a two's complement number. // x must not exceed 256 bits (the result is undefined if it does) and is not modified. // // S256(0) = 0 // S256(1) = 1 // S256(2**255) = -2**255 // S256(2**256-1) = -1 func S256(x *big.Int) *big.Int { if x.Cmp(tt255) < 0 { return x } return new(big.Int).Sub(x, tt256) } // SafeSub returns x-y and checks for overflow. func SafeSub(x, y uint64) (uint64, bool) { diff, borrowOut := bits.Sub64(x, y, 0) return diff, borrowOut != 0 } // SafeAdd returns x+y and checks for overflow. func SafeAdd(x, y uint64) (uint64, bool) { sum, carryOut := bits.Add64(x, y, 0) return sum, carryOut != 0 } // SafeMul returns x*y and checks for overflow. func SafeMul(x, y uint64) (uint64, bool) { hi, lo := bits.Mul64(x, y) return lo, hi != 0 } // SafeDiv returns x/y and checks for division by zero. func SafeDiv(x, y uint64) (uint64, error) { if y == 0 { return 0, fmt.Errorf("division by zero") } return x / y, nil } // Byte returns the byte at position n, // with the supplied padlength in Little Endian encoding. // n==0 returns the MSB // Example: bigint '5', padlength 32, n=31 => 5 func Byte(bigint *big.Int, padlength, n int) byte { if n >= padlength { return byte(0) } return bigEndianByteAt(bigint, padlength-1-n) } // bigEndianByteAt returns the byte at position n, // in Big Endian encoding // So n==0 returns the least significant byte func bigEndianByteAt(bigint *big.Int, n int) byte { words := bigint.Bits() // Check word-bucket the byte will reside in i := n / wordBytes if i >= len(words) { return byte(0) } word := words[i] // Offset of the byte shift := 8 * uint(n%wordBytes) return byte(word >> shift) } // Exp implements exponentiation by squaring. // Exp returns a newly-allocated big integer and does not change // base or exponent. The result is truncated to 256 bits. // // Courtesy @karalabe and @chfast func Exp(base, exponent *big.Int) *big.Int { copyBase := new(big.Int).Set(base) result := big.NewInt(1) for _, word := range exponent.Bits() { for i := 0; i < wordBits; i++ { if word&1 == 1 { U256(result.Mul(result, copyBase)) } U256(copyBase.Mul(copyBase, copyBase)) word >>= 1 } } return result } // Architecture-dependent constants const ( // wordBits is the number of bits in a big.Word. wordBits = 32 << (uint64(^big.Word(0)) >> 63) // wordBytes is the number of bytes in a big.Word. wordBytes = wordBits / 8 )