diff --git a/block/raw.go b/block/raw.go deleted file mode 100644 index 2fd1bbdf6..000000000 --- a/block/raw.go +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright (c) 2018 The VeChainThor developers - -// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying -// file LICENSE or - -package block - -import ( - "bytes" - - "github.com/ethereum/go-ethereum/rlp" - "github.com/vechain/thor/tx" -) - -// Raw allows to partially decode components of a block. -type Raw []byte - -// DecodeHeader decode only the header. -func (r Raw) DecodeHeader() (*Header, error) { - content, _, err := rlp.SplitList(r) - if err != nil { - return nil, err - } - - var header Header - if err := rlp.Decode(bytes.NewReader(content), &header); err != nil { - return nil, err - } - return &header, nil -} - -// DecodeBody decode only the body. -func (r Raw) DecodeBody() (*Body, error) { - content, _, err := rlp.SplitList(r) - if err != nil { - return nil, err - } - - _, _, rest, err := rlp.Split(content) - if err != nil { - return nil, err - } - var txs tx.Transactions - if err := rlp.Decode(bytes.NewReader(rest), &txs); err != nil { - return nil, err - } - return &Body{txs}, nil -} diff --git a/builtin/executor_test.go b/builtin/executor_test.go index 9e2ecf3dd..038187e7d 100644 --- a/builtin/executor_test.go +++ b/builtin/executor_test.go @@ -12,7 +12,6 @@ import ( "testing" "time" - "github.com/ethereum/go-ethereum/crypto" "github.com/stretchr/testify/assert" "github.com/vechain/thor/builtin" "github.com/vechain/thor/chain" @@ -195,7 +194,7 @@ func TestExecutorProposal(t *testing.T) { proposalID := func() thor.Bytes32 { var b8 [8]byte binary.BigEndian.PutUint64(b8[:], test.rt.Context().Time) - return thor.Bytes32(crypto.Keccak256Hash(b8[:], approver[:])) + return thor.Keccak256(b8[:], approver[:]) }() test.Case("propose", target, data). Caller(approver). diff --git a/go.mod b/go.mod index aca423b91..4f0c4d2b4 100644 --- a/go.mod +++ b/go.mod @@ -25,8 +25,8 @@ require ( github.com/stretchr/testify v1.7.2 github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a github.com/vechain/go-ecvrf v0.0.0-20220525125849-96fa0442e765 - golang.org/x/crypto v0.14.0 - golang.org/x/sys v0.13.0 + golang.org/x/crypto v0.17.0 + golang.org/x/sys v0.15.0 gopkg.in/cheggaaa/pb.v1 v1.0.28 gopkg.in/urfave/cli.v1 v1.20.0 gopkg.in/yaml.v2 v2.4.0 @@ -52,7 +52,7 @@ require ( github.com/mattn/go-runewidth v0.0.4 // indirect github.com/rjeczalik/notify v0.9.3 // indirect golang.org/x/net v0.17.0 // indirect - golang.org/x/text v0.13.0 // indirect + golang.org/x/text v0.14.0 // indirect gopkg.in/karalabe/cookiejar.v2 v2.0.0-20150724131613-8dcd6a7f4951 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 4de2f0b98..aebcc2778 100644 --- a/go.sum +++ b/go.sum @@ -146,8 +146,11 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= +golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= +golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= +golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= @@ -191,13 +194,15 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= +golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= @@ -205,8 +210,9 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= diff --git a/state/cached_object_test.go b/state/cached_object_test.go index f400d4cba..b6cf35a60 100644 --- a/state/cached_object_test.go +++ b/state/cached_object_test.go @@ -10,7 +10,6 @@ import ( "math/rand" "testing" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/rlp" "github.com/stretchr/testify/assert" "github.com/vechain/thor/muxdb" @@ -44,7 +43,7 @@ func TestCachedObject(t *testing.T) { code := make([]byte, 100) rand.Read(code) - codeHash := crypto.Keccak256(code) + codeHash := thor.Keccak256(code).Bytes() db.NewStore(codeStoreName).Put(codeHash, code) account := Account{ diff --git a/state/stage_test.go b/state/stage_test.go index f747574eb..9cb8722ec 100644 --- a/state/stage_test.go +++ b/state/stage_test.go @@ -10,7 +10,6 @@ import ( "math/big" "testing" - "github.com/ethereum/go-ethereum/crypto" "github.com/stretchr/testify/assert" "github.com/vechain/thor/muxdb" "github.com/vechain/thor/thor" @@ -49,7 +48,7 @@ func TestStage(t *testing.T) { assert.Equal(t, M(balance, nil), M(state.GetBalance(addr))) assert.Equal(t, M(code, nil), M(state.GetCode(addr))) - assert.Equal(t, M(thor.Bytes32(crypto.Keccak256Hash(code)), nil), M(state.GetCodeHash(addr))) + assert.Equal(t, M(thor.Keccak256(code), nil), M(state.GetCodeHash(addr))) for k, v := range storage { assert.Equal(t, M(v, nil), M(state.GetStorage(addr, k))) diff --git a/state/state.go b/state/state.go index 8bbf69fb5..64a51d907 100644 --- a/state/state.go +++ b/state/state.go @@ -10,7 +10,6 @@ import ( "fmt" "math/big" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/rlp" "github.com/vechain/thor/lowrlp" "github.com/vechain/thor/muxdb" @@ -315,7 +314,7 @@ func (s *State) SetCode(addr thor.Address, code []byte) error { var codeHash []byte if len(code) > 0 { s.sm.Put(codeKey(addr), code) - codeHash = crypto.Keccak256(code) + codeHash = thor.Keccak256(code).Bytes() codeCache.Add(string(codeHash), code) } else { s.sm.Put(codeKey(addr), []byte(nil)) @@ -450,7 +449,7 @@ func (s *State) Stage(newBlockNum, newBlockConflicts uint32) (*Stage, error) { case codeKey: code := v.([]byte) if len(code) > 0 { - codes[thor.Bytes32(crypto.Keccak256Hash(code))] = code + codes[thor.Keccak256(code)] = code } case storageKey: if c, jerr = getChanged(key.addr); jerr != nil { diff --git a/state/state_test.go b/state/state_test.go index daa5007d7..ccb3f54b3 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -9,7 +9,6 @@ import ( "math/big" "testing" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/rlp" "github.com/stretchr/testify/assert" "github.com/vechain/thor/muxdb" @@ -39,7 +38,7 @@ func TestStateReadWrite(t *testing.T) { state.SetCode(addr, []byte("code")) assert.Equal(t, M([]byte("code"), nil), M(state.GetCode(addr))) - assert.Equal(t, M(thor.Bytes32(crypto.Keccak256Hash([]byte("code"))), nil), M(state.GetCodeHash(addr))) + assert.Equal(t, M(thor.Keccak256([]byte("code")), nil), M(state.GetCodeHash(addr))) assert.Equal(t, M(thor.Bytes32{}, nil), M(state.GetStorage(addr, storageKey))) state.SetStorage(addr, storageKey, thor.BytesToBytes32([]byte("storageValue"))) @@ -85,7 +84,7 @@ func TestStateRevert(t *testing.T) { v := values[len(values)-i-1] assert.Equal(t, M(v.balance, nil), M(state.GetBalance(addr))) assert.Equal(t, M(v.code, nil), M(state.GetCode(addr))) - assert.Equal(t, M(thor.Bytes32(crypto.Keccak256Hash(v.code)), nil), M(state.GetCodeHash(addr))) + assert.Equal(t, M(thor.Keccak256(v.code), nil), M(state.GetCodeHash(addr))) assert.Equal(t, M(v.storage, nil), M(state.GetStorage(addr, storageKey))) state.RevertTo(chk) chk-- diff --git a/thor/address.go b/thor/address.go index 5dc965020..bbce7fda1 100644 --- a/thor/address.go +++ b/thor/address.go @@ -13,7 +13,6 @@ import ( "strings" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" ) const ( @@ -108,5 +107,5 @@ func CreateContractAddress(txID Bytes32, clauseIndex uint32, creationCount uint3 var b4_1, b4_2 [4]byte binary.BigEndian.PutUint32(b4_1[:], clauseIndex) binary.BigEndian.PutUint32(b4_2[:], creationCount) - return BytesToAddress(crypto.Keccak256(txID[:], b4_1[:], b4_2[:])) + return BytesToAddress(Keccak256(txID[:], b4_1[:], b4_2[:]).Bytes()) } diff --git a/thor/hash.go b/thor/hash.go index b5f781ff1..60b4531b0 100644 --- a/thor/hash.go +++ b/thor/hash.go @@ -11,6 +11,7 @@ import ( "sync" "github.com/vechain/thor/blake2b" + "golang.org/x/crypto/sha3" ) // NewBlake2b return blake2b-256 hash. @@ -35,24 +36,59 @@ func Blake2b(data ...[]byte) Bytes32 { // Blake2bFn computes blake2b-256 checksum for the provided writer. func Blake2bFn(fn func(w io.Writer)) (h Bytes32) { - w := hstatePool.Get().(*hstate) + w := blake2bStatePool.Get().(*blake2bState) fn(w) w.Sum(w.b32[:0]) h = w.b32 // to avoid 1 alloc w.Reset() - hstatePool.Put(w) + blake2bStatePool.Put(w) return } -type hstate struct { +type blake2bState struct { hash.Hash b32 Bytes32 } -var hstatePool = sync.Pool{ +var blake2bStatePool = sync.Pool{ New: func() interface{} { - return &hstate{ + return &blake2bState{ Hash: NewBlake2b(), } }, } + +// keccakState wraps sha3.state. In addition to the usual hash methods, it also supports +// Read to get a variable amount of data from the hash state. Read is faster than Sum +// because it doesn't copy the internal state, but also modifies the internal state. +type keccakState interface { + hash.Hash + Read([]byte) (int, error) +} + +type keccak256 struct { + state keccakState + b32 Bytes32 +} + +var keccak256Pool = sync.Pool{ + New: func() interface{} { + return &keccak256{ + state: sha3.NewLegacyKeccak256().(keccakState), + } + }, +} + +func Keccak256(data ...[]byte) (h Bytes32) { + hasher := keccak256Pool.Get().(*keccak256) + + for _, b := range data { + hasher.state.Write(b) + } + hasher.state.Read(hasher.b32[:]) + h = hasher.b32 + + hasher.state.Reset() + keccak256Pool.Put(hasher) + return +} diff --git a/tracers/js/goja.go b/tracers/js/goja.go index a381b83a8..0f7e8c017 100644 --- a/tracers/js/goja.go +++ b/tracers/js/goja.go @@ -26,7 +26,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/ethereum/go-ethereum/crypto" "github.com/vechain/thor/thor" "github.com/vechain/thor/tracers" @@ -431,7 +430,7 @@ func (t *jsTracer) setBuiltinFunctions() { return nil } code = common.CopyBytes(code) - b := vm.CreateAddress2(addr, common.HexToHash(salt), crypto.Keccak256Hash(code).Bytes()).Bytes() + b := vm.CreateAddress2(addr, common.HexToHash(salt), thor.Keccak256(code).Bytes()).Bytes() res, err := t.toBuf(jsvm, b) if err != nil { jsvm.Interrupt(err) diff --git a/tracers/native/prestate.go b/tracers/native/prestate.go index 48f1135ab..caa27752e 100644 --- a/tracers/native/prestate.go +++ b/tracers/native/prestate.go @@ -24,7 +24,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/ethereum/go-ethereum/crypto" "github.com/vechain/thor/thor" "github.com/vechain/thor/tracers" "github.com/vechain/thor/vm" @@ -234,7 +233,7 @@ func (t *prestateTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, if err != nil { return } - inithash := crypto.Keccak256(init) + inithash := thor.Keccak256(init).Bytes() salt := stackData[stackLen-4] addr := vm.CreateAddress2(contract.Address(), salt.Bytes32(), inithash) t.lookupAccount(addr) diff --git a/trie/trie_test.go b/trie/trie_test.go index dfc3af56c..a04d6934b 100644 --- a/trie/trie_test.go +++ b/trie/trie_test.go @@ -30,7 +30,6 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/rlp" "github.com/stretchr/testify/assert" @@ -573,7 +572,7 @@ func BenchmarkHash(b *testing.B) { nonce = uint64(random.Int63()) balance = new(big.Int).Rand(random, new(big.Int).Exp(common.Big2, common.Big256, nil)) root = emptyRoot - code = crypto.Keccak256(nil) + code = thor.Keccak256(nil) ) accounts[i], _ = rlp.EncodeToBytes([]interface{}{nonce, balance, root, code}) } diff --git a/vm/analysis_test.go b/vm/analysis_test.go index 398861f8a..8b3ec6ddc 100644 --- a/vm/analysis_test.go +++ b/vm/analysis_test.go @@ -20,7 +20,7 @@ import ( "math/bits" "testing" - "github.com/ethereum/go-ethereum/crypto" + "github.com/vechain/thor/thor" ) func TestJumpDestAnalysis(t *testing.T) { @@ -77,7 +77,7 @@ func BenchmarkJumpdestHashing_1200k(bench *testing.B) { bench.SetBytes(analysisCodeSize) bench.ResetTimer() for i := 0; i < bench.N; i++ { - crypto.Keccak256Hash(code) + thor.Keccak256(code) } bench.StopTimer() } diff --git a/vm/contracts.go b/vm/contracts.go index 9b249253b..ec40bf6d2 100644 --- a/vm/contracts.go +++ b/vm/contracts.go @@ -29,6 +29,7 @@ import ( "golang.org/x/crypto/ripemd160" "github.com/vechain/thor/blake2b" + "github.com/vechain/thor/thor" "github.com/vechain/thor/vm/bn256" ) @@ -139,7 +140,7 @@ func (c *ecrecover) Run(input []byte) ([]byte, error) { } // the first byte of pubkey is bitcoin heritage - return common.LeftPadBytes(crypto.Keccak256(pubKey[1:])[12:], 32), nil + return common.LeftPadBytes(thor.Keccak256(pubKey[1:]).Bytes()[12:], 32), nil } // safe_ecrecover prevent touching the input buffer. @@ -177,7 +178,7 @@ func (c *safe_ecrecover) Run(input []byte) ([]byte, error) { } // the first byte of pubkey is bitcoin heritage - return common.LeftPadBytes(crypto.Keccak256(pubKey[1:])[12:], 32), nil + return common.LeftPadBytes(thor.Keccak256(pubKey[1:]).Bytes()[12:], 32), nil } // SHA256 implemented as a native contract. diff --git a/vm/evm.go b/vm/evm.go index 444f2e6b9..4a07acf3e 100644 --- a/vm/evm.go +++ b/vm/evm.go @@ -21,14 +21,14 @@ import ( "sync/atomic" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/params" "github.com/holiman/uint256" + "github.com/vechain/thor/thor" ) // emptyCodeHash is used by create to ensure deployment is disallowed to already // deployed contract addresses (relevant after the account abstraction). -var emptyCodeHash = crypto.Keccak256Hash(nil) +var emptyCodeHash = common.Hash(thor.Keccak256(nil)) type ( CanTransferFunc func(StateDB, common.Address, *big.Int) bool @@ -407,7 +407,7 @@ func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.I func (evm *EVM) Create2(caller ContractRef, code []byte, gas uint64, endowment *big.Int, salt *uint256.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) { // Cannot use crypto.CreateAddress2 function. // v1.8.14 -> v1.8.27 dependency issue. See patch.go file. - contractAddr = CreateAddress2(caller.Address(), salt.Bytes32(), crypto.Keccak256Hash(code).Bytes()) + contractAddr = CreateAddress2(caller.Address(), salt.Bytes32(), thor.Keccak256(code).Bytes()) // Capture the tracer start/end events in debug mode if evm.vmConfig.Tracer != nil { @@ -461,7 +461,7 @@ func (evm *EVM) create(caller ContractRef, code []byte, gas uint64, value *big.I // EVM. The contract is a scoped environment for this execution context // only. contract := NewContract(caller, AccountRef(contractAddr), value, gas) - contract.SetCallCode(&contractAddr, crypto.Keccak256Hash(code), code) + contract.SetCallCode(&contractAddr, common.Hash(thor.Keccak256(code)), code) if evm.vmConfig.NoRecursion && evm.depth > 0 { return nil, contractAddr, gas, nil diff --git a/vm/instructions.go b/vm/instructions.go index d5ddb25c3..3045a336f 100644 --- a/vm/instructions.go +++ b/vm/instructions.go @@ -19,14 +19,12 @@ package vm import ( "errors" "fmt" - "hash" - "sync" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/params" "github.com/holiman/uint256" - "golang.org/x/crypto/sha3" + "github.com/vechain/thor/thor" ) var ( @@ -35,27 +33,6 @@ var ( errMaxCodeSizeExceeded = errors.New("evm: max code size exceeded") ) -// keccakState wraps sha3.state. In addition to the usual hash methods, it also supports -// Read to get a variable amount of data from the hash state. Read is faster than Sum -// because it doesn't copy the internal state, but also modifies the internal state. -type keccakState interface { - hash.Hash - Read([]byte) (int, error) -} - -type keccak256 struct { - state keccakState - hash common.Hash -} - -var keccak256Pool = sync.Pool{ - New: func() interface{} { - return &keccak256{ - state: sha3.NewLegacyKeccak256().(keccakState), - } - }, -} - func opAdd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { x, y := stack.popptr(), stack.peek() y.Add(x, y) @@ -268,17 +245,12 @@ func opSha3(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta offset, size := stack.popptr(), stack.peek() data := memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64())) - hasher := keccak256Pool.Get().(*keccak256) - - hasher.state.Reset() - hasher.state.Write(data) - hasher.state.Read(hasher.hash[:]) + hash := thor.Keccak256(data) if evm.vmConfig.EnablePreimageRecording { - evm.StateDB.AddPreimage(hasher.hash, common.CopyBytes(data)) + evm.StateDB.AddPreimage(common.Hash(hash), common.CopyBytes(data)) } - size.SetBytes(hasher.hash[:]) - keccak256Pool.Put(hasher) + size.SetBytes(hash[:]) return nil, nil } diff --git a/vm/instructions_test.go b/vm/instructions_test.go index 10ea100f3..fc3b39392 100644 --- a/vm/instructions_test.go +++ b/vm/instructions_test.go @@ -21,9 +21,9 @@ import ( "testing" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/params" "github.com/holiman/uint256" + "github.com/vechain/thor/thor" ) type twoOperandTest struct { @@ -468,7 +468,7 @@ func TestCreate2Addreses(t *testing.T) { origin := common.BytesToAddress(common.FromHex(tt.origin)) salt := common.BytesToHash(common.FromHex(tt.salt)) code := common.FromHex(tt.code) - codeHash := crypto.Keccak256(code) + codeHash := thor.Keccak256(code).Bytes() // THOR: Cannot use crypto.CreateAddress2 function. // v1.8.14 -> v1.8.27 depedency issue. See patch.go file. address := CreateAddress2(origin, salt, codeHash) diff --git a/vm/patch.go b/vm/patch.go index 82e690b4c..b9ba90b42 100644 --- a/vm/patch.go +++ b/vm/patch.go @@ -10,12 +10,12 @@ package vm import ( "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" + "github.com/vechain/thor/thor" ) // CreateAddress2 creates an ethereum address given the address bytes, initial // contract code hash and a salt. // v1.8.27 func CreateAddress2(b common.Address, salt [32]byte, inithash []byte) common.Address { - return common.BytesToAddress(crypto.Keccak256([]byte{0xff}, b.Bytes(), salt[:], inithash)[12:]) + return common.BytesToAddress(thor.Keccak256([]byte{0xff}, b.Bytes(), salt[:], inithash).Bytes()[12:]) }