diff --git a/arbnode/delayed.go b/arbnode/delayed.go index 51b22c58bc..498aa0475f 100644 --- a/arbnode/delayed.go +++ b/arbnode/delayed.go @@ -117,7 +117,7 @@ func (m *DelayedInboxMessage) AfterInboxAcc() common.Hash { arbmath.UintToBytes(m.Message.Header.BlockNumber), arbmath.UintToBytes(m.Message.Header.Timestamp), m.Message.Header.RequestId.Bytes(), - math.U256Bytes(m.Message.Header.L1BaseFee), + arbmath.U256Bytes(m.Message.Header.L1BaseFee), crypto.Keccak256(m.Message.L2msg), ) return crypto.Keccak256Hash(m.BeforeInboxAcc[:], hash) diff --git a/arbnode/inbox_test.go b/arbnode/inbox_test.go index 3060ae2ae6..92d216f765 100644 --- a/arbnode/inbox_test.go +++ b/arbnode/inbox_test.go @@ -21,7 +21,6 @@ import ( "github.com/offchainlabs/nitro/util/testhelpers" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/types" @@ -148,10 +147,10 @@ func TestTransactionStreamer(t *testing.T) { var gas uint64 = 100000 var l2Message []byte l2Message = append(l2Message, arbos.L2MessageKind_ContractTx) - l2Message = append(l2Message, math.U256Bytes(new(big.Int).SetUint64(gas))...) - l2Message = append(l2Message, math.U256Bytes(big.NewInt(l2pricing.InitialBaseFeeWei))...) + l2Message = append(l2Message, arbmath.Uint64ToU256Bytes(gas)...) + l2Message = append(l2Message, arbmath.Uint64ToU256Bytes(l2pricing.InitialBaseFeeWei)...) l2Message = append(l2Message, dest.Hash().Bytes()...) - l2Message = append(l2Message, math.U256Bytes(value)...) + l2Message = append(l2Message, arbmath.U256Bytes(value)...) var requestId common.Hash binary.BigEndian.PutUint64(requestId.Bytes()[:8], uint64(i)) messages = append(messages, arbostypes.MessageWithMetadata{ diff --git a/arbnode/transaction_streamer.go b/arbnode/transaction_streamer.go index 31e7751e56..3cbad93c9a 100644 --- a/arbnode/transaction_streamer.go +++ b/arbnode/transaction_streamer.go @@ -24,7 +24,6 @@ import ( "github.com/syndtr/goleveldb/leveldb" "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/params" @@ -35,6 +34,7 @@ import ( "github.com/offchainlabs/nitro/broadcaster" "github.com/offchainlabs/nitro/execution" "github.com/offchainlabs/nitro/staker" + "github.com/offchainlabs/nitro/util/arbmath" "github.com/offchainlabs/nitro/util/sharedmetrics" "github.com/offchainlabs/nitro/util/stopwaiter" ) @@ -531,8 +531,7 @@ func (s *TransactionStreamer) AddFakeInitMessage() error { if err != nil { return fmt.Errorf("failed to serialize chain config: %w", err) } - // TODO: once we have a safe U256Bytes that does a copy internally, use that instead of doing an explicit copy here - chainIdBytes := math.U256Bytes(new(big.Int).Set(s.chainConfig.ChainID)) + chainIdBytes := arbmath.U256Bytes(s.chainConfig.ChainID) msg := append(append(chainIdBytes, 0), chainConfigJson...) return s.AddMessages(0, false, []arbostypes.MessageWithMetadata{{ Message: &arbostypes.L1IncomingMessage{ diff --git a/arbos/parse_l2.go b/arbos/parse_l2.go index e76781a6f3..d2df3bdf89 100644 --- a/arbos/parse_l2.go +++ b/arbos/parse_l2.go @@ -9,7 +9,6 @@ import ( "time" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/log" @@ -40,8 +39,8 @@ func ParseL2Transactions(msg *arbostypes.L1IncomingMessage, chainId *big.Int, ba return nil, errors.New("cannot issue L2 funded by L1 tx without L1 request id") } kind := msg.L2msg[0] - depositRequestId := crypto.Keccak256Hash(msg.Header.RequestId[:], math.U256Bytes(common.Big0)) - unsignedRequestId := crypto.Keccak256Hash(msg.Header.RequestId[:], math.U256Bytes(common.Big1)) + depositRequestId := crypto.Keccak256Hash(msg.Header.RequestId[:], arbmath.U256Bytes(common.Big0)) + unsignedRequestId := crypto.Keccak256Hash(msg.Header.RequestId[:], arbmath.U256Bytes(common.Big1)) tx, err := parseUnsignedTx(bytes.NewReader(msg.L2msg[1:]), msg.Header.Poster, &unsignedRequestId, chainId, kind) if err != nil { return nil, err @@ -146,7 +145,7 @@ func parseL2Message(rd io.Reader, poster common.Address, timestamp uint64, reque var nextRequestId *common.Hash if requestId != nil { - subRequestId := crypto.Keccak256Hash(requestId[:], math.U256Bytes(index)) + subRequestId := crypto.Keccak256Hash(requestId[:], arbmath.U256Bytes(index)) nextRequestId = &subRequestId } nestedSegments, err := parseL2Message(bytes.NewReader(nextMsg), poster, timestamp, nextRequestId, chainId, depth+1) diff --git a/arbos/storage/storage_test.go b/arbos/storage/storage_test.go index a8d424d14e..b2e8bdb2ea 100644 --- a/arbos/storage/storage_test.go +++ b/arbos/storage/storage_test.go @@ -53,7 +53,7 @@ func TestStorageBackedBigInt(t *testing.T) { t.Fatal(err) } // Verify that our encoding matches geth's signed complement impl - expectedRawVal := common.BigToHash(math.U256(new(big.Int).Set(in))) + expectedRawVal := common.BigToHash(arbmath.U256(in)) if rawVal != expectedRawVal { t.Fatal("for input", in, "expected raw value", expectedRawVal, "but got", rawVal) } diff --git a/precompiles/ArbSys.go b/precompiles/ArbSys.go index dc92baf448..0d3df3bbfe 100644 --- a/precompiles/ArbSys.go +++ b/precompiles/ArbSys.go @@ -8,7 +8,6 @@ import ( "math/big" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core/vm" "github.com/offchainlabs/nitro/arbos/util" "github.com/offchainlabs/nitro/util/arbmath" @@ -121,9 +120,9 @@ func (con *ArbSys) SendTxToL1(c ctx, evm mech, value huge, destination addr, cal sendHash, err := arbosState.KeccakHash( c.caller.Bytes(), destination.Bytes(), - math.U256Bytes(evm.Context.BlockNumber), - math.U256Bytes(bigL1BlockNum), - math.U256Bytes(&t), + arbmath.U256Bytes(evm.Context.BlockNumber), + arbmath.U256Bytes(bigL1BlockNum), + arbmath.U256Bytes(&t), common.BigToHash(value).Bytes(), calldataForL1, ) diff --git a/staker/assertion.go b/staker/assertion.go index 8d09abf866..3047721e56 100644 --- a/staker/assertion.go +++ b/staker/assertion.go @@ -7,9 +7,10 @@ import ( "math/big" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/crypto" + "github.com/offchainlabs/nitro/solgen/go/rollupgen" + "github.com/offchainlabs/nitro/util/arbmath" "github.com/offchainlabs/nitro/validator" ) @@ -39,8 +40,8 @@ func HashChallengeState( hashesBytes = append(hashesBytes, h[:]...) } return crypto.Keccak256Hash( - math.U256Bytes(new(big.Int).SetUint64(segmentStart)), - math.U256Bytes(new(big.Int).SetUint64(segmentLength)), + arbmath.Uint64ToU256Bytes(segmentStart), + arbmath.Uint64ToU256Bytes(segmentLength), hashesBytes, ) } diff --git a/system_tests/contract_tx_test.go b/system_tests/contract_tx_test.go index 56d79b36d9..7d66e516b4 100644 --- a/system_tests/contract_tx_test.go +++ b/system_tests/contract_tx_test.go @@ -12,7 +12,6 @@ import ( "time" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/params" @@ -64,10 +63,10 @@ func TestContractTxDeploy(t *testing.T) { Data: deployCode, } l2Msg := []byte{arbos.L2MessageKind_ContractTx} - l2Msg = append(l2Msg, math.U256Bytes(arbmath.UintToBig(contractTx.Gas))...) - l2Msg = append(l2Msg, math.U256Bytes(contractTx.GasFeeCap)...) + l2Msg = append(l2Msg, arbmath.Uint64ToU256Bytes(contractTx.Gas)...) + l2Msg = append(l2Msg, arbmath.U256Bytes(contractTx.GasFeeCap)...) l2Msg = append(l2Msg, common.Hash{}.Bytes()...) // to is zero, translated into nil - l2Msg = append(l2Msg, math.U256Bytes(contractTx.Value)...) + l2Msg = append(l2Msg, arbmath.U256Bytes(contractTx.Value)...) l2Msg = append(l2Msg, contractTx.Data...) err = builder.L2.ConsensusNode.TxStreamer.AddMessages(pos, true, []arbostypes.MessageWithMetadata{ diff --git a/system_tests/deployment_test.go b/system_tests/deployment_test.go index c7767fef9a..6cb8dcd172 100644 --- a/system_tests/deployment_test.go +++ b/system_tests/deployment_test.go @@ -6,18 +6,18 @@ package arbtest import ( "bytes" "context" - "math/big" "strings" "testing" "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/params" + + "github.com/offchainlabs/nitro/util/arbmath" ) func testContractDeployment(t *testing.T, ctx context.Context, client *ethclient.Client, contractCode []byte, accountInfo *AccountInfo, expectedEstimateGasError error) { @@ -26,7 +26,7 @@ func testContractDeployment(t *testing.T, ctx context.Context, client *ethclient 0x7F, // PUSH32 } // len(contractCode) - deployCode = append(deployCode, math.U256Bytes(big.NewInt(int64(len(contractCode))))...) + deployCode = append(deployCode, arbmath.Uint64ToU256Bytes(uint64(len(contractCode)))...) var codeOffset byte = 42 deployCode = append(deployCode, []byte{ 0x80, // DUP diff --git a/system_tests/reorg_resequencing_test.go b/system_tests/reorg_resequencing_test.go index fcc6603aed..b188504acb 100644 --- a/system_tests/reorg_resequencing_test.go +++ b/system_tests/reorg_resequencing_test.go @@ -9,9 +9,10 @@ import ( "testing" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/params" + "github.com/offchainlabs/nitro/arbos/arbostypes" + "github.com/offchainlabs/nitro/util/arbmath" ) func TestReorgResequencing(t *testing.T) { @@ -69,7 +70,7 @@ func TestReorgResequencing(t *testing.T) { RequestId: &delayedIndexHash, L1BaseFee: common.Big0, }, - L2msg: append(builder.L2Info.GetAddress("User4").Bytes(), math.U256Bytes(big.NewInt(params.Ether))...), + L2msg: append(builder.L2Info.GetAddress("User4").Bytes(), arbmath.Uint64ToU256Bytes(params.Ether)...), } err = builder.L2.ConsensusNode.TxStreamer.AddMessages(startMsgCount, true, []arbostypes.MessageWithMetadata{{ Message: newMessage, diff --git a/util/arbmath/math.go b/util/arbmath/math.go index 467ee58a14..2945dd5a29 100644 --- a/util/arbmath/math.go +++ b/util/arbmath/math.go @@ -8,6 +8,7 @@ import ( "math/big" "math/bits" + eth_math "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/params" ) @@ -376,3 +377,20 @@ func BalancePerEther(balance *big.Int) float64 { balancePerEther, _ := new(big.Float).Quo(new(big.Float).SetInt(balance), new(big.Float).SetFloat64(params.Ether)).Float64() return balancePerEther } + +// U256Bytes converts big Int to 256bit EVM number. +// This operation makes a copy of big Int. +func U256Bytes(n *big.Int) []byte { + return eth_math.U256Bytes(new(big.Int).Set(n)) +} + +// U256 encodes as a 256 bit two's complement number. +// This operation makes a copy of big Int. +func U256(x *big.Int) *big.Int { + return eth_math.U256(new(big.Int).Set(x)) +} + +// Uint64ToU256Bytes converts uint64 to 256bit EVM number. +func Uint64ToU256Bytes(n uint64) []byte { + return eth_math.U256Bytes(UintToBig(n)) +}