common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 22:21:51 +01:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// Package math provides integer math utilities.
|
|
|
|
package math
|
|
|
|
|
|
|
|
import (
|
2017-03-07 23:19:44 +01:00
|
|
|
"fmt"
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 22:21:51 +01:00
|
|
|
"math/big"
|
|
|
|
)
|
|
|
|
|
2018-06-14 02:52:50 -07:00
|
|
|
// Various big integer limit values.
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 22:21:51 +01:00
|
|
|
var (
|
|
|
|
tt255 = BigPow(2, 255)
|
|
|
|
tt256 = BigPow(2, 256)
|
|
|
|
tt256m1 = new(big.Int).Sub(tt256, big.NewInt(1))
|
2017-05-04 12:53:42 +02:00
|
|
|
tt63 = BigPow(2, 63)
|
2018-06-14 02:52:50 -07:00
|
|
|
MaxBig256 = new(big.Int).Set(tt256m1)
|
2017-05-04 12:53:42 +02:00
|
|
|
MaxBig63 = new(big.Int).Sub(tt63, big.NewInt(1))
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 22:21:51 +01:00
|
|
|
)
|
|
|
|
|
2017-02-28 15:09:11 +01:00
|
|
|
const (
|
|
|
|
// number of bits in a big.Word
|
|
|
|
wordBits = 32 << (uint64(^big.Word(0)) >> 63)
|
|
|
|
// number of bytes in a big.Word
|
|
|
|
wordBytes = wordBits / 8
|
|
|
|
)
|
|
|
|
|
2017-03-07 23:19:44 +01:00
|
|
|
// HexOrDecimal256 marshals big.Int as hex or decimal.
|
|
|
|
type HexOrDecimal256 big.Int
|
|
|
|
|
2019-06-03 16:56:05 +02:00
|
|
|
// NewHexOrDecimal256 creates a new HexOrDecimal256
|
|
|
|
func NewHexOrDecimal256(x int64) *HexOrDecimal256 {
|
|
|
|
b := big.NewInt(x)
|
|
|
|
h := HexOrDecimal256(*b)
|
|
|
|
return &h
|
|
|
|
}
|
|
|
|
|
2023-02-22 13:55:09 +02:00
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
|
|
|
//
|
|
|
|
// It is similar to UnmarshalText, but allows parsing real decimals too, not just
|
|
|
|
// quoted decimal strings.
|
|
|
|
func (i *HexOrDecimal256) UnmarshalJSON(input []byte) error {
|
|
|
|
if len(input) > 0 && input[0] == '"' {
|
|
|
|
input = input[1 : len(input)-1]
|
|
|
|
}
|
|
|
|
return i.UnmarshalText(input)
|
|
|
|
}
|
|
|
|
|
2017-03-07 23:19:44 +01:00
|
|
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
|
|
|
func (i *HexOrDecimal256) UnmarshalText(input []byte) error {
|
|
|
|
bigint, ok := ParseBig256(string(input))
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid hex or decimal integer %q", input)
|
|
|
|
}
|
|
|
|
*i = HexOrDecimal256(*bigint)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalText implements encoding.TextMarshaler.
|
|
|
|
func (i *HexOrDecimal256) MarshalText() ([]byte, error) {
|
2017-04-12 16:27:23 +02:00
|
|
|
if i == nil {
|
|
|
|
return []byte("0x0"), nil
|
|
|
|
}
|
2017-03-07 23:19:44 +01:00
|
|
|
return []byte(fmt.Sprintf("%#x", (*big.Int)(i))), nil
|
|
|
|
}
|
|
|
|
|
2020-09-29 17:40:08 +02:00
|
|
|
// Decimal256 unmarshals big.Int as a decimal string. When unmarshalling,
|
|
|
|
// it however accepts either "0x"-prefixed (hex encoded) or non-prefixed (decimal)
|
|
|
|
type Decimal256 big.Int
|
|
|
|
|
2023-06-26 00:33:50 +09:00
|
|
|
// NewDecimal256 creates a new Decimal256
|
2020-09-29 17:40:08 +02:00
|
|
|
func NewDecimal256(x int64) *Decimal256 {
|
|
|
|
b := big.NewInt(x)
|
|
|
|
d := Decimal256(*b)
|
|
|
|
return &d
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
|
|
|
func (i *Decimal256) UnmarshalText(input []byte) error {
|
|
|
|
bigint, ok := ParseBig256(string(input))
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid hex or decimal integer %q", input)
|
|
|
|
}
|
|
|
|
*i = Decimal256(*bigint)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalText implements encoding.TextMarshaler.
|
|
|
|
func (i *Decimal256) MarshalText() ([]byte, error) {
|
|
|
|
return []byte(i.String()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String implements Stringer.
|
|
|
|
func (i *Decimal256) String() string {
|
|
|
|
if i == nil {
|
|
|
|
return "0"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%#d", (*big.Int)(i))
|
|
|
|
}
|
|
|
|
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 22:21:51 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-05-29 03:42:21 -07:00
|
|
|
// MustParseBig256 parses s as a 256 bit big integer and panics if the string is invalid.
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 22:21:51 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// FirstBitSet returns the index of the first 1 bit in v, counting from LSB.
|
|
|
|
func FirstBitSet(v *big.Int) int {
|
|
|
|
for i := 0; i < v.BitLen(); i++ {
|
|
|
|
if v.Bit(i) > 0 {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v.BitLen()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2017-02-28 15:09:11 +01:00
|
|
|
if bigint.BitLen()/8 >= n {
|
|
|
|
return bigint.Bytes()
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 22:21:51 +01:00
|
|
|
}
|
|
|
|
ret := make([]byte, n)
|
2017-02-28 15:09:11 +01:00
|
|
|
ReadBits(bigint, ret)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2017-06-06 18:38:38 +02:00
|
|
|
// bigEndianByteAt returns the byte at position n,
|
2017-06-08 23:16:05 +02:00
|
|
|
// in Big-Endian encoding
|
2017-06-06 18:38:38 +02:00
|
|
|
// So n==0 returns the least significant byte
|
|
|
|
func bigEndianByteAt(bigint *big.Int, n int) byte {
|
2017-05-28 23:39:33 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-06-06 18:38:38 +02:00
|
|
|
// Byte returns the byte at position n,
|
2017-06-08 23:16:05 +02:00
|
|
|
// with the supplied padlength in Little-Endian encoding.
|
|
|
|
// n==0 returns the MSB
|
|
|
|
// Example: bigint '5', padlength 32, n=31 => 5
|
2017-06-06 18:38:38 +02:00
|
|
|
func Byte(bigint *big.Int, padlength, n int) byte {
|
|
|
|
if n >= padlength {
|
2017-05-28 23:39:33 +02:00
|
|
|
return byte(0)
|
|
|
|
}
|
2017-06-06 18:38:38 +02:00
|
|
|
return bigEndianByteAt(bigint, padlength-1-n)
|
2017-05-28 23:39:33 +02:00
|
|
|
}
|
|
|
|
|
2017-02-28 15:09:11 +01:00
|
|
|
// ReadBits encodes the absolute value of bigint as big-endian bytes. Callers must ensure
|
|
|
|
// that buf has enough space. If buf is too short the result will be incomplete.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 22:21:51 +01:00
|
|
|
}
|
|
|
|
|
2024-03-12 20:05:31 +08:00
|
|
|
// U256 encodes x as a 256 bit two's complement number. This operation is destructive.
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 22:21:51 +01:00
|
|
|
func U256(x *big.Int) *big.Int {
|
|
|
|
return x.And(x, tt256m1)
|
|
|
|
}
|
|
|
|
|
2020-05-04 14:09:14 +02:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 22:21:51 +01:00
|
|
|
// 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.
|
|
|
|
//
|
2022-09-10 13:25:40 +02:00
|
|
|
// S256(0) = 0
|
|
|
|
// S256(1) = 1
|
|
|
|
// S256(2**255) = -2**255
|
|
|
|
// S256(2**256-1) = -1
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 22:21:51 +01:00
|
|
|
func S256(x *big.Int) *big.Int {
|
|
|
|
if x.Cmp(tt255) < 0 {
|
|
|
|
return x
|
|
|
|
}
|
2018-05-29 03:42:21 -07:00
|
|
|
return new(big.Int).Sub(x, tt256)
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 22:21:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2024-03-12 20:05:31 +08:00
|
|
|
copyBase := new(big.Int).Set(base)
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 22:21:51 +01:00
|
|
|
result := big.NewInt(1)
|
|
|
|
|
|
|
|
for _, word := range exponent.Bits() {
|
2017-02-28 15:09:11 +01:00
|
|
|
for i := 0; i < wordBits; i++ {
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 22:21:51 +01:00
|
|
|
if word&1 == 1 {
|
2024-03-12 20:05:31 +08:00
|
|
|
U256(result.Mul(result, copyBase))
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 22:21:51 +01:00
|
|
|
}
|
2024-03-12 20:05:31 +08:00
|
|
|
U256(copyBase.Mul(copyBase, copyBase))
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 22:21:51 +01:00
|
|
|
word >>= 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|