Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: get rid of custom MaxUint64 and MaxUint64 #30636

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions common/math/big.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
45 changes: 0 additions & 45 deletions common/math/big_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
16 changes: 0 additions & 16 deletions common/math/integer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 5 additions & 4 deletions common/math/integer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package math

import (
"math"
"testing"
)

Expand All @@ -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},
Expand All @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package core
import (
"errors"
"fmt"
gomath "math"
"math/big"
"math/rand"
"os"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion core/rawdb/freezer_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
2 changes: 1 addition & 1 deletion core/rawdb/freezer_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion core/state/snapshot/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion core/state_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 3 additions & 2 deletions core/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package types

import (
"bytes"
gomath "math"
"math/big"
"reflect"
"testing"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion core/vm/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
5 changes: 3 additions & 2 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"maps"
gomath "math"
"math/big"

"github.com/consensys/gnark-crypto/ecc"
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion core/vm/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion core/vm/operations_verkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"maps"
gomath "math"
"math/big"
"strings"
"time"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions internal/ethapi/transaction_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"crypto/sha256"
"errors"
"fmt"
gomath "math"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion params/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
3 changes: 2 additions & 1 deletion rlp/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"io"
gomath "math"
"math/big"
"reflect"
"strings"
Expand Down Expand Up @@ -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

Expand Down
Loading