From e1bd4eb3b4c1d8fb098d68458d7de60c1af9af37 Mon Sep 17 00:00:00 2001 From: Valentin Staykov Date: Fri, 27 Sep 2024 15:10:53 +0000 Subject: [PATCH] perf: remove unnecessary allocs in hash contract bytecode --- core/state/intra_block_state.go | 4 ++-- smt/pkg/blockinfo/block_info.go | 3 +-- smt/pkg/smt/entity_storage.go | 4 +--- smt/pkg/utils/util_test.go | 14 ++++++++++++++ smt/pkg/utils/utils.go | 24 +++++++++++++++--------- zk/stages/stage_interhashes.go | 3 +-- zk/tx/tx.go | 4 ++-- 7 files changed, 36 insertions(+), 20 deletions(-) diff --git a/core/state/intra_block_state.go b/core/state/intra_block_state.go index 244a9a49326..22cbeabbdd8 100644 --- a/core/state/intra_block_state.go +++ b/core/state/intra_block_state.go @@ -362,8 +362,8 @@ func (sdb *IntraBlockState) SetCode(addr libcommon.Address, code []byte) { return } - hashedBytecode := utils.HashContractBytecode(hex.EncodeToString(code)) - stateObject.SetCode(libcommon.HexToHash(hashedBytecode), code) + hashedBytecode := utils.HashContractBytecodeBigInt(hex.EncodeToString(code)) + stateObject.SetCode(libcommon.BigToHash(hashedBytecode), code) } } diff --git a/smt/pkg/blockinfo/block_info.go b/smt/pkg/blockinfo/block_info.go index 831d2c73d97..5e435ff5a06 100644 --- a/smt/pkg/blockinfo/block_info.go +++ b/smt/pkg/blockinfo/block_info.go @@ -325,8 +325,7 @@ func (b *BlockInfoTree) GenerateBlockTxKeysVals( logToEncode := "0x" + hex.EncodeToString(rLog.Data) + reducedTopics - hash := utils.HashContractBytecode(logToEncode) - logEncodedBig := utils.ConvertHexToBigInt(hash) + logEncodedBig := utils.HashContractBytecodeBigInt(logToEncode) key, val, err = generateTxLog(txIndexBig, big.NewInt(logIndex), logEncodedBig) if err != nil { return nil, nil, err diff --git a/smt/pkg/smt/entity_storage.go b/smt/pkg/smt/entity_storage.go index b64969f6e60..6cb4baaacb8 100644 --- a/smt/pkg/smt/entity_storage.go +++ b/smt/pkg/smt/entity_storage.go @@ -375,7 +375,7 @@ func appendToValuesBatchStorageBigInt(valuesBatchStorage []*utils.NodeValue8, va func convertBytecodeToBigInt(bytecode string) (*big.Int, int, error) { var parsedBytecode string - hashedBytecode := utils.HashContractBytecode(bytecode) + bi := utils.HashContractBytecodeBigInt(bytecode) if strings.HasPrefix(bytecode, "0x") { parsedBytecode = bytecode[2:] @@ -389,8 +389,6 @@ func convertBytecodeToBigInt(bytecode string) (*big.Int, int, error) { bytecodeLength := len(parsedBytecode) / 2 - bi := utils.ConvertHexToBigInt(hashedBytecode) - if len(bytecode) == 0 { bytecodeLength = 0 bi = big.NewInt(0) diff --git a/smt/pkg/utils/util_test.go b/smt/pkg/utils/util_test.go index 8b155152a0a..c1f9815bb99 100644 --- a/smt/pkg/utils/util_test.go +++ b/smt/pkg/utils/util_test.go @@ -49,6 +49,20 @@ func BenchmarkConvertBigIntToHex(b *testing.B) { } } +func BenchmarkHashContractBytecode(b *testing.B) { + str := strings.Repeat("e", 1000) + b.Run("1", func(b *testing.B) { + for n := 0; n < b.N; n++ { + HashContractBytecode(str) + } + }) + b.Run("2", func(b *testing.B) { + for n := 0; n < b.N; n++ { + HashContractBytecodeBigInt(str) + } + }) +} + func TestConvertBigIntToHex(t *testing.T) { testCases := []struct { name string diff --git a/smt/pkg/utils/utils.go b/smt/pkg/utils/utils.go index 84733ba717e..2b723be5749 100644 --- a/smt/pkg/utils/utils.go +++ b/smt/pkg/utils/utils.go @@ -674,6 +674,10 @@ func KeyContractStorage(ethAddr []*big.Int, storagePosition string) NodeKey { } func HashContractBytecode(bc string) string { + return ConvertBigIntToHex(HashContractBytecodeBigInt(bc)) +} + +func HashContractBytecodeBigInt(bc string) *big.Int { bytecode := bc if strings.HasPrefix(bc, "0x") { @@ -700,10 +704,15 @@ func HashContractBytecode(bc string) string { tmpHash := [4]uint64{0, 0, 0, 0} bytesPointer := 0 + maxBytesToAdd := BYTECODE_ELEMENTS_HASH * BYTECODE_BYTES_ELEMENT + var elementsToHash []uint64 + var in [8]uint64 + var capacity [4]uint64 + scalar := new(big.Int) + tmpScalar := new(big.Int) + var byteToAdd string for i := 0; i < numHashes; i++ { - maxBytesToAdd := BYTECODE_ELEMENTS_HASH * BYTECODE_BYTES_ELEMENT - var elementsToHash []uint64 - elementsToHash = append(elementsToHash, tmpHash[:]...) + elementsToHash = tmpHash[:] subsetBytecode := bytecode[bytesPointer : bytesPointer+maxBytesToAdd*2] bytesPointer += maxBytesToAdd * 2 @@ -712,7 +721,7 @@ func HashContractBytecode(bc string) string { counter := 0 for j := 0; j < maxBytesToAdd; j++ { - byteToAdd := "00" + byteToAdd = "00" if j < len(subsetBytecode)/2 { byteToAdd = subsetBytecode[j*2 : (j+1)*2] } @@ -721,23 +730,20 @@ func HashContractBytecode(bc string) string { counter += 1 if counter == BYTECODE_BYTES_ELEMENT { - tmpScalar, _ := new(big.Int).SetString(tmpElem, 16) + tmpScalar, _ = scalar.SetString(tmpElem, 16) elementsToHash = append(elementsToHash, tmpScalar.Uint64()) tmpElem = "" counter = 0 } } - var in [8]uint64 copy(in[:], elementsToHash[4:12]) - - var capacity [4]uint64 copy(capacity[:], elementsToHash[:4]) tmpHash = Hash(in, capacity) } - return ConvertBigIntToHex(ArrayToScalar(tmpHash[:])) + return ArrayToScalar(tmpHash[:]) } func ResizeHashTo32BytesByPrefixingWithZeroes(hashValue []byte) []byte { diff --git a/zk/stages/stage_interhashes.go b/zk/stages/stage_interhashes.go index fe0ab0dbf5d..2b26ffa403a 100644 --- a/zk/stages/stage_interhashes.go +++ b/zk/stages/stage_interhashes.go @@ -683,14 +683,13 @@ func processAccount(db smt.DB, a *accounts.Account, as map[string]string, inc ui func insertContractBytecodeToKV(db smt.DB, keys []utils.NodeKey, ethAddr string, bytecode string) ([]utils.NodeKey, error) { keyContractCode := utils.KeyContractCode(ethAddr) keyContractLength := utils.KeyContractLength(ethAddr) - hashedBytecode := utils.HashContractBytecode(bytecode) + bi := utils.HashContractBytecodeBigInt(bytecode) parsedBytecode := strings.TrimPrefix(bytecode, "0x") if len(parsedBytecode)%2 != 0 { parsedBytecode = "0" + parsedBytecode } - bi := utils.ConvertHexToBigInt(hashedBytecode) bytecodeLength := len(parsedBytecode) / 2 x := utils.ScalarToArrayBig(bi) diff --git a/zk/tx/tx.go b/zk/tx/tx.go index e9b7c05dd36..394dcd4b45f 100644 --- a/zk/tx/tx.go +++ b/zk/tx/tx.go @@ -505,8 +505,8 @@ func ComputeL2TxHash( } hash += fromPart - hashed := utils.HashContractBytecode(hash) - return common.HexToHash(hashed), nil + hashed := utils.HashContractBytecodeBigInt(hash) + return common.BigToHash(hashed), nil } var re = regexp.MustCompile("^[0-9a-fA-F]*$")