Skip to content

Commit

Permalink
switch keccak hash's upstream to golang.org/x/crypto/sha3 (#625)
Browse files Browse the repository at this point in the history
* remove unused block/raw.go

* switch keccak hash's upstream to golang.org/x/crypto/sha3
  • Loading branch information
libotony authored Dec 19, 2023
1 parent 4d26a2f commit 2fdb17f
Show file tree
Hide file tree
Showing 19 changed files with 81 additions and 123 deletions.
48 changes: 0 additions & 48 deletions block/raw.go

This file was deleted.

3 changes: 1 addition & 2 deletions builtin/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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).
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
Expand Down
12 changes: 9 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -191,22 +194,25 @@ 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=
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=
Expand Down
3 changes: 1 addition & 2 deletions state/cached_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down
3 changes: 1 addition & 2 deletions state/stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)))
Expand Down
5 changes: 2 additions & 3 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")))
Expand Down Expand Up @@ -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--
Expand Down
3 changes: 1 addition & 2 deletions thor/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"strings"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)

const (
Expand Down Expand Up @@ -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())
}
46 changes: 41 additions & 5 deletions thor/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sync"

"github.com/vechain/thor/blake2b"
"golang.org/x/crypto/sha3"
)

// NewBlake2b return blake2b-256 hash.
Expand All @@ -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
}
3 changes: 1 addition & 2 deletions tracers/js/goja.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions tracers/native/prestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions trie/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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})
}
Expand Down
4 changes: 2 additions & 2 deletions vm/analysis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"math/bits"
"testing"

"github.com/ethereum/go-ethereum/crypto"
"github.com/vechain/thor/thor"
)

func TestJumpDestAnalysis(t *testing.T) {
Expand Down Expand Up @@ -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()
}
Expand Down
5 changes: 3 additions & 2 deletions vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
Loading

0 comments on commit 2fdb17f

Please sign in to comment.