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

perf: remove unnecessary allocs in hash contract bytecode #1245

Merged
merged 1 commit into from
Sep 27, 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
4 changes: 2 additions & 2 deletions core/state/intra_block_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
3 changes: 1 addition & 2 deletions smt/pkg/blockinfo/block_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions smt/pkg/smt/entity_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:]
Expand All @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions smt/pkg/utils/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 15 additions & 9 deletions smt/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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
Expand All @@ -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]
}
Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions zk/stages/stage_interhashes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions zk/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]*$")
Expand Down
Loading