diff --git a/common/math/big.go b/common/math/big.go index d9748d01a3..0e92ce178f 100644 --- a/common/math/big.go +++ b/common/math/big.go @@ -234,38 +234,3 @@ func U256(x *big.Int) *big.Int { 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) -} - -// 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 -} diff --git a/common/math/big_test.go b/common/math/big_test.go index ee8f09e7b4..d99c5c33f9 100644 --- a/common/math/big_test.go +++ b/common/math/big_test.go @@ -277,48 +277,3 @@ func TestLittleEndianByteAt(t *testing.T) { } } } - -func TestS256(t *testing.T) { - tests := []struct{ x, y *big.Int }{ - {x: big.NewInt(0), y: big.NewInt(0)}, - {x: big.NewInt(1), y: big.NewInt(1)}, - {x: big.NewInt(2), y: big.NewInt(2)}, - { - x: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)), - y: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)), - }, - { - x: BigPow(2, 255), - y: new(big.Int).Neg(BigPow(2, 255)), - }, - { - x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1)), - y: big.NewInt(-1), - }, - { - x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2)), - y: big.NewInt(-2), - }, - } - for _, test := range tests { - if y := S256(test.x); y.Cmp(test.y) != 0 { - t.Errorf("S256(%x) = %x, want %x", test.x, y, test.y) - } - } -} - -func TestExp(t *testing.T) { - tests := []struct{ base, exponent, result *big.Int }{ - {base: big.NewInt(0), exponent: big.NewInt(0), result: big.NewInt(1)}, - {base: big.NewInt(1), exponent: big.NewInt(0), result: big.NewInt(1)}, - {base: big.NewInt(1), exponent: big.NewInt(1), result: big.NewInt(1)}, - {base: big.NewInt(1), exponent: big.NewInt(2), result: big.NewInt(1)}, - {base: big.NewInt(3), exponent: big.NewInt(144), result: MustParseBig256("507528786056415600719754159741696356908742250191663887263627442114881")}, - {base: big.NewInt(2), exponent: big.NewInt(255), result: MustParseBig256("57896044618658097711785492504343953926634992332820282019728792003956564819968")}, - } - for _, test := range tests { - if result := Exp(test.base, test.exponent); result.Cmp(test.result) != 0 { - t.Errorf("Exp(%d, %d) = %d, want %d", test.base, test.exponent, result, test.result) - } - } -} diff --git a/common/math/integer.go b/common/math/integer.go index 82de96f927..25ced87053 100644 --- a/common/math/integer.go +++ b/common/math/integer.go @@ -22,22 +22,6 @@ import ( "strconv" ) -// Integer limit values. -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 -) - // HexOrDecimal64 marshals uint64 as hex or decimal. type HexOrDecimal64 uint64 diff --git a/common/math/integer_test.go b/common/math/integer_test.go index b31c7c26c2..4643a43f20 100644 --- a/common/math/integer_test.go +++ b/common/math/integer_test.go @@ -17,6 +17,7 @@ package math import ( + "math" "testing" ) @@ -36,8 +37,8 @@ func TestOverflow(t *testing.T) { op operation }{ // add operations - {MaxUint64, 1, true, add}, - {MaxUint64 - 1, 1, false, add}, + {math.MaxUint64, 1, true, add}, + {math.MaxUint64 - 1, 1, false, add}, // sub operations {0, 1, true, sub}, @@ -46,8 +47,8 @@ func TestOverflow(t *testing.T) { // mul operations {0, 0, false, mul}, {10, 10, false, mul}, - {MaxUint64, 2, true, mul}, - {MaxUint64, 1, false, mul}, + {math.MaxUint64, 2, true, mul}, + {math.MaxUint64, 1, false, mul}, } { var overflows bool switch test.op { diff --git a/core/blockchain_test.go b/core/blockchain_test.go index d8f7da0643..b8c4678ac8 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -19,6 +19,7 @@ package core import ( "errors" "fmt" + gomath "math" "math/big" "math/rand" "os" @@ -1949,11 +1950,11 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon gspec = &Genesis{ Config: &chainConfig, - Alloc: types.GenesisAlloc{addr: {Balance: big.NewInt(math.MaxInt64)}}, + Alloc: types.GenesisAlloc{addr: {Balance: big.NewInt(gomath.MaxInt64)}}, BaseFee: big.NewInt(params.InitialBaseFee), } signer = types.LatestSigner(gspec.Config) - mergeBlock = math.MaxInt32 + mergeBlock = gomath.MaxInt32 ) // Generate and import the canonical chain chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), nil, gspec, nil, engine, vm.Config{}, nil) @@ -2236,7 +2237,7 @@ func testInsertKnownChainDataWithMerging(t *testing.T, typ string, mergeHeight i Config: &chainConfig, } engine = beacon.New(ethash.NewFaker()) - mergeBlock = uint64(math.MaxUint64) + mergeBlock = uint64(gomath.MaxUint64) ) // Apply merging since genesis if mergeHeight == 0 { diff --git a/core/rawdb/freezer_batch.go b/core/rawdb/freezer_batch.go index 4e1b38df29..013d0b9d13 100644 --- a/core/rawdb/freezer_batch.go +++ b/core/rawdb/freezer_batch.go @@ -18,8 +18,8 @@ package rawdb import ( "fmt" + "math" - "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/rlp" "github.com/golang/snappy" ) diff --git a/core/rawdb/freezer_memory.go b/core/rawdb/freezer_memory.go index cd39101514..ee4f553919 100644 --- a/core/rawdb/freezer_memory.go +++ b/core/rawdb/freezer_memory.go @@ -19,10 +19,10 @@ package rawdb import ( "errors" "fmt" + "math" "sync" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rlp" diff --git a/core/state/snapshot/context.go b/core/state/snapshot/context.go index 8a19960501..3d293d27f4 100644 --- a/core/state/snapshot/context.go +++ b/core/state/snapshot/context.go @@ -20,10 +20,10 @@ import ( "bytes" "encoding/binary" "errors" + "math" "time" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb/memorydb" diff --git a/core/state_processor_test.go b/core/state_processor_test.go index 9678f2828c..b701ee651b 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -19,11 +19,11 @@ package core import ( "crypto/ecdsa" "encoding/binary" + "math" "math/big" "testing" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/consensus/beacon" "github.com/ethereum/go-ethereum/consensus/ethash" diff --git a/core/types/block_test.go b/core/types/block_test.go index 1af5b9d7bf..1f61be89a5 100644 --- a/core/types/block_test.go +++ b/core/types/block_test.go @@ -18,6 +18,7 @@ package types import ( "bytes" + gomath "math" "math/big" "reflect" "testing" @@ -277,9 +278,9 @@ func TestRlpDecodeParentHash(t *testing.T) { if rlpData, err := rlp.EncodeToBytes(&Header{ ParentHash: want, Difficulty: mainnetTd, - Number: new(big.Int).SetUint64(math.MaxUint64), + Number: new(big.Int).SetUint64(gomath.MaxUint64), Extra: make([]byte, 65+32), - BaseFee: new(big.Int).SetUint64(math.MaxUint64), + BaseFee: new(big.Int).SetUint64(gomath.MaxUint64), }); err != nil { t.Fatal(err) } else { diff --git a/core/vm/common.go b/core/vm/common.go index ba75950e37..658803b820 100644 --- a/core/vm/common.go +++ b/core/vm/common.go @@ -17,8 +17,9 @@ package vm import ( + "math" + "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/math" "github.com/holiman/uint256" ) diff --git a/core/vm/contracts.go b/core/vm/contracts.go index 104d2ba814..c3ce6d2af0 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -22,6 +22,7 @@ import ( "errors" "fmt" "maps" + gomath "math" "math/big" "github.com/consensys/gnark-crypto/ecc" @@ -416,7 +417,7 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 { // 2. Different divisor (`GQUADDIVISOR`) (3) gas.Div(gas, big3) if gas.BitLen() > 64 { - return math.MaxUint64 + return gomath.MaxUint64 } // 3. Minimum price of 200 gas if gas.Uint64() < 200 { @@ -429,7 +430,7 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 { gas.Div(gas, big20) if gas.BitLen() > 64 { - return math.MaxUint64 + return gomath.MaxUint64 } return gas.Uint64() } diff --git a/core/vm/interpreter_test.go b/core/vm/interpreter_test.go index a1369648da..7d4b2ddf36 100644 --- a/core/vm/interpreter_test.go +++ b/core/vm/interpreter_test.go @@ -17,11 +17,11 @@ package vm import ( + "math" "testing" "time" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/params" diff --git a/core/vm/operations_verkle.go b/core/vm/operations_verkle.go index 722d5ed2ce..3492994778 100644 --- a/core/vm/operations_verkle.go +++ b/core/vm/operations_verkle.go @@ -17,6 +17,8 @@ package vm import ( + gomath "math" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/params" @@ -126,7 +128,7 @@ func gasCodeCopyEip4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, ) uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow() if overflow { - uint64CodeOffset = math.MaxUint64 + uint64CodeOffset = gomath.MaxUint64 } _, copyOffset, nonPaddedCopyLength := getDataAndAdjustedBounds(contract.Code, uint64CodeOffset, length.Uint64()) if !contract.IsDeployment { diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index d66e32b8c2..a1f4304690 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -22,6 +22,7 @@ import ( "errors" "fmt" "maps" + gomath "math" "math/big" "strings" "time" @@ -432,7 +433,7 @@ func (api *PersonalAccountAPI) UnlockAccount(ctx context.Context, addr common.Ad return false, errors.New("account unlock with HTTP access is forbidden") } - const max = uint64(time.Duration(math.MaxInt64) / time.Second) + const max = uint64(time.Duration(gomath.MaxInt64) / time.Second) var d time.Duration if duration == nil { d = 300 * time.Second @@ -1183,7 +1184,7 @@ func doCall(ctx context.Context, b Backend, args TransactionArgs, state *state.S defer cancel() gp := new(core.GasPool) if globalGasCap == 0 { - gp.AddGas(math.MaxUint64) + gp.AddGas(gomath.MaxUint64) } else { gp.AddGas(globalGasCap) } @@ -1291,7 +1292,7 @@ func (api *BlockChainAPI) SimulateV1(ctx context.Context, opts simOpts, blockNrO } gasCap := api.b.RPCGasCap() if gasCap == 0 { - gasCap = math.MaxUint64 + gasCap = gomath.MaxUint64 } sim := &simulator{ b: api.b, diff --git a/internal/ethapi/transaction_args.go b/internal/ethapi/transaction_args.go index 08e3515f6b..e848f619d9 100644 --- a/internal/ethapi/transaction_args.go +++ b/internal/ethapi/transaction_args.go @@ -22,6 +22,7 @@ import ( "crypto/sha256" "errors" "fmt" + gomath "math" "math/big" "github.com/ethereum/go-ethereum/common" @@ -140,7 +141,7 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend, skipGas if skipGasEstimation { // Skip gas usage estimation if a precise gas limit is not critical, e.g., in non-transaction calls. gas := hexutil.Uint64(b.RPCGasCap()) if gas == 0 { - gas = hexutil.Uint64(math.MaxUint64 / 2) + gas = hexutil.Uint64(gomath.MaxUint64 / 2) } args.Gas = &gas } else { // Estimate the gas usage otherwise. @@ -378,7 +379,7 @@ func (args *TransactionArgs) CallDefaults(globalGasCap uint64, baseFee *big.Int, if args.Gas == nil { gas := globalGasCap if gas == 0 { - gas = uint64(math.MaxUint64 / 2) + gas = uint64(gomath.MaxUint64 / 2) } args.Gas = (*hexutil.Uint64)(&gas) } else { diff --git a/params/config_test.go b/params/config_test.go index fa444a1d0b..f658c336dc 100644 --- a/params/config_test.go +++ b/params/config_test.go @@ -17,12 +17,12 @@ package params import ( + "math" "math/big" "reflect" "testing" "time" - "github.com/ethereum/go-ethereum/common/math" "github.com/stretchr/testify/require" ) diff --git a/rlp/decode_test.go b/rlp/decode_test.go index 8479a95b25..3e492188e8 100644 --- a/rlp/decode_test.go +++ b/rlp/decode_test.go @@ -22,6 +22,7 @@ import ( "errors" "fmt" "io" + gomath "math" "math/big" "reflect" "strings" @@ -555,7 +556,7 @@ var decodeTests = []decodeTest{ // uint256 {input: "80", ptr: new(*uint256.Int), value: uint256.NewInt(0)}, {input: "01", ptr: new(*uint256.Int), value: uint256.NewInt(1)}, - {input: "88FFFFFFFFFFFFFFFF", ptr: new(*uint256.Int), value: uint256.NewInt(math.MaxUint64)}, + {input: "88FFFFFFFFFFFFFFFF", ptr: new(*uint256.Int), value: uint256.NewInt(gomath.MaxUint64)}, {input: "89FFFFFFFFFFFFFFFFFF", ptr: new(*uint256.Int), value: veryBigInt256}, {input: "10", ptr: new(uint256.Int), value: *uint256.NewInt(16)}, // non-pointer also works diff --git a/rpc/types_test.go b/rpc/types_test.go index 64833ffea6..aba40b5863 100644 --- a/rpc/types_test.go +++ b/rpc/types_test.go @@ -18,11 +18,11 @@ package rpc import ( "encoding/json" + "math" "reflect" "testing" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/math" ) func TestBlockNumberJSONUnmarshal(t *testing.T) {