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-27 00:21:51 +03: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/>.
|
|
|
|
|
2017-01-04 22:17:24 +03:00
|
|
|
package math
|
|
|
|
|
2017-03-08 01:19:44 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
2020-07-01 20:35:26 +03:00
|
|
|
"math/bits"
|
2017-03-08 01:19:44 +03:00
|
|
|
"strconv"
|
|
|
|
)
|
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-27 00:21:51 +03:00
|
|
|
|
2018-06-14 12:52:50 +03:00
|
|
|
// 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-27 00:21:51 +03:00
|
|
|
const (
|
|
|
|
MaxInt8 = 1<<7 - 1
|
|
|
|
MinInt8 = -1 << 7
|
|
|
|
MaxInt16 = 1<<15 - 1
|
|
|
|
MinInt16 = -1 << 15
|
|
|
|
MaxInt32 = 1<<31 - 1
|
|
|
|
MinInt32 = -1 << 31
|
|
|
|
MaxInt64 = 1<<63 - 1
|
|
|
|
MinInt64 = -1 << 63
|
|
|
|
MaxUint8 = 1<<8 - 1
|
|
|
|
MaxUint16 = 1<<16 - 1
|
|
|
|
MaxUint32 = 1<<32 - 1
|
|
|
|
MaxUint64 = 1<<64 - 1
|
|
|
|
)
|
|
|
|
|
2017-03-08 01:19:44 +03:00
|
|
|
// HexOrDecimal64 marshals uint64 as hex or decimal.
|
|
|
|
type HexOrDecimal64 uint64
|
|
|
|
|
|
|
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
|
|
|
func (i *HexOrDecimal64) UnmarshalText(input []byte) error {
|
|
|
|
int, ok := ParseUint64(string(input))
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid hex or decimal integer %q", input)
|
|
|
|
}
|
|
|
|
*i = HexOrDecimal64(int)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalText implements encoding.TextMarshaler.
|
|
|
|
func (i HexOrDecimal64) MarshalText() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf("%#x", uint64(i))), nil
|
|
|
|
}
|
|
|
|
|
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-27 00:21:51 +03:00
|
|
|
// ParseUint64 parses s as an integer in decimal or hexadecimal syntax.
|
|
|
|
// Leading zeros are accepted. The empty string parses as zero.
|
|
|
|
func ParseUint64(s string) (uint64, bool) {
|
|
|
|
if s == "" {
|
|
|
|
return 0, true
|
|
|
|
}
|
|
|
|
if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") {
|
|
|
|
v, err := strconv.ParseUint(s[2:], 16, 64)
|
|
|
|
return v, err == nil
|
|
|
|
}
|
|
|
|
v, err := strconv.ParseUint(s, 10, 64)
|
|
|
|
return v, err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustParseUint64 parses s as an integer and panics if the string is invalid.
|
|
|
|
func MustParseUint64(s string) uint64 {
|
|
|
|
v, ok := ParseUint64(s)
|
|
|
|
if !ok {
|
|
|
|
panic("invalid unsigned 64 bit integer: " + s)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
2017-01-04 22:17:24 +03:00
|
|
|
|
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-27 00:21:51 +03:00
|
|
|
// NOTE: The following methods need to be optimised using either bit checking or asm
|
2017-01-04 22:17:24 +03:00
|
|
|
|
|
|
|
// SafeSub returns subtraction result and whether overflow occurred.
|
|
|
|
func SafeSub(x, y uint64) (uint64, bool) {
|
|
|
|
return x - y, x < y
|
|
|
|
}
|
|
|
|
|
|
|
|
// SafeAdd returns the result and whether overflow occurred.
|
|
|
|
func SafeAdd(x, y uint64) (uint64, bool) {
|
2020-07-01 20:35:26 +03:00
|
|
|
sum, carry := bits.Add64(x, y, 0)
|
|
|
|
return sum, carry != 0
|
2017-01-04 22:17:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SafeMul returns multiplication result and whether overflow occurred.
|
|
|
|
func SafeMul(x, y uint64) (uint64, bool) {
|
2020-07-01 20:35:26 +03:00
|
|
|
hi, lo := bits.Mul64(x, y)
|
|
|
|
return lo, hi != 0
|
2017-01-04 22:17:24 +03:00
|
|
|
}
|