From 00d800d2212f683f1a58e3ced42838e0e682ff10 Mon Sep 17 00:00:00 2001 From: Kevaundray Wedderburn Date: Tue, 8 Jul 2025 22:35:04 +0100 Subject: [PATCH] micro-opt --- core/vm/common.go | 6 +++++- core/vm/contracts.go | 26 ++++++++++++++++---------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/core/vm/common.go b/core/vm/common.go index 658803b820..00ce22db47 100644 --- a/core/vm/common.go +++ b/core/vm/common.go @@ -53,6 +53,10 @@ func calcMemSize64WithUint(off *uint256.Int, length64 uint64) (uint64, bool) { // getData returns a slice from the data based on the start and size and pads // up to size with zero's. This function is overflow safe. func getData(data []byte, start uint64, size uint64) []byte { + unpaddedData := getDataNoPadding(data, start, size) + return common.RightPadBytes(unpaddedData, int(size)) +} +func getDataNoPadding(data []byte, start uint64, size uint64) []byte { length := uint64(len(data)) if start > length { start = length @@ -61,7 +65,7 @@ func getData(data []byte, start uint64, size uint64) []byte { if end > length { end = length } - return common.RightPadBytes(data[start:end], int(size)) + return data[start:end] } func getDataAndAdjustedBounds(data []byte, start uint64, size uint64) (codeCopyPadded []byte, actualStart uint64, sizeNonPadded uint64) { diff --git a/core/vm/contracts.go b/core/vm/contracts.go index 05974ab078..6a6a01f369 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -498,13 +498,19 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 { } func (c *bigModExp) Run(input []byte) ([]byte, error) { - - // Note: This can be done more efficiently - var ( - baseLen = new(big.Int).SetBytes(getData(input, 0, 32)).Uint64() - expLen = new(big.Int).SetBytes(getData(input, 32, 32)).Uint64() - modLen = new(big.Int).SetBytes(getData(input, 64, 32)).Uint64() - ) + // Extract lengths - they're 32-byte big-endian integers + // with the actual uint64 value in the last 8 bytes + var baseLen, expLen, modLen uint64 + if len(input) >= 32 { + baseLen = binary.BigEndian.Uint64(input[24:32]) + } + if len(input) >= 64 { + expLen = binary.BigEndian.Uint64(input[56:64]) + } + if len(input) >= 96 { + modLen = binary.BigEndian.Uint64(input[88:96]) + } + // TODO: We could combine this if second if statement with the one above if len(input) > 96 { input = input[96:] } else { @@ -520,9 +526,9 @@ func (c *bigModExp) Run(input []byte) ([]byte, error) { } // Retrieve the operands and execute the exponentiation var ( - base = getData(input, 0, baseLen) - exp = getData(input, baseLen, expLen) - mod = getData(input, baseLen+expLen, modLen) + base = getDataNoPadding(input, 0, baseLen) + exp = getDataNoPadding(input, baseLen, expLen) + mod = getDataNoPadding(input, baseLen+expLen, modLen) ) // Use the modexp implementation