mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-10 07:58:22 -05:00
Make blake2b hash util in shared/ (#649)
This commit is contained in:
committed by
Raul Jordan
parent
dc8d40d10d
commit
6d46dda33c
@@ -26,11 +26,11 @@ go_test(
|
||||
"//beacon-chain/types:go_default_library",
|
||||
"//proto/beacon/p2p/v1:go_default_library",
|
||||
"//shared/event:go_default_library",
|
||||
"//shared/hashutil:go_default_library",
|
||||
"//shared/p2p:go_default_library",
|
||||
"//shared/testutil:go_default_library",
|
||||
"@com_github_golang_protobuf//proto:go_default_library",
|
||||
"@com_github_sirupsen_logrus//:go_default_library",
|
||||
"@com_github_sirupsen_logrus//hooks/test:go_default_library",
|
||||
"@org_golang_x_crypto//blake2b:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -10,11 +10,11 @@ import (
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/types"
|
||||
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
||||
"github.com/prysmaticlabs/prysm/shared/event"
|
||||
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
||||
"github.com/prysmaticlabs/prysm/shared/p2p"
|
||||
"github.com/prysmaticlabs/prysm/shared/testutil"
|
||||
"github.com/sirupsen/logrus"
|
||||
logTest "github.com/sirupsen/logrus/hooks/test"
|
||||
"golang.org/x/crypto/blake2b"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -88,7 +88,7 @@ func TestProcessBlockHash(t *testing.T) {
|
||||
exitRoutine <- true
|
||||
}()
|
||||
|
||||
announceHash := blake2b.Sum512([]byte{})
|
||||
announceHash := hashutil.Hash([]byte{})
|
||||
hashAnnounce := &pb.BeaconBlockHashAnnounce{
|
||||
Hash: announceHash[:],
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ go_library(
|
||||
"//proto/beacon/p2p/v1:go_default_library",
|
||||
"//shared/bitutil:go_default_library",
|
||||
"//shared/event:go_default_library",
|
||||
"//shared/hashutil:go_default_library",
|
||||
"//shared/p2p:go_default_library",
|
||||
"@com_github_ethereum_go_ethereum//:go_default_library",
|
||||
"@com_github_ethereum_go_ethereum//common:go_default_library",
|
||||
@@ -28,7 +29,6 @@ go_library(
|
||||
"@com_github_golang_protobuf//proto:go_default_library",
|
||||
"@com_github_golang_protobuf//ptypes:go_default_library_gen",
|
||||
"@com_github_sirupsen_logrus//:go_default_library",
|
||||
"@org_golang_x_crypto//blake2b:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/params"
|
||||
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
||||
"github.com/prysmaticlabs/prysm/shared/bitutil"
|
||||
"golang.org/x/crypto/blake2b"
|
||||
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
||||
)
|
||||
|
||||
// ActiveState contains fields of current state of beacon chain,
|
||||
@@ -60,10 +60,7 @@ func (a *ActiveState) Hash() ([32]byte, error) {
|
||||
if err != nil {
|
||||
return [32]byte{}, err
|
||||
}
|
||||
var hash [32]byte
|
||||
h := blake2b.Sum512(data)
|
||||
copy(hash[:], h[:32])
|
||||
return hash, nil
|
||||
return hashutil.Hash(data), nil
|
||||
}
|
||||
|
||||
// PendingAttestations returns attestations that have not yet been processed.
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/params"
|
||||
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
||||
"golang.org/x/crypto/blake2b"
|
||||
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
||||
)
|
||||
|
||||
// Attestation is the primary source of load on the beacon chain, it's used to
|
||||
@@ -53,10 +53,7 @@ func (a *Attestation) Hash() ([32]byte, error) {
|
||||
if err != nil {
|
||||
return [32]byte{}, fmt.Errorf("could not marshal attestation proto data: %v", err)
|
||||
}
|
||||
var hash [32]byte
|
||||
h := blake2b.Sum512(data)
|
||||
copy(hash[:], h[:32])
|
||||
return hash, nil
|
||||
return hashutil.Hash(data), nil
|
||||
}
|
||||
|
||||
// Key generates the blake2b hash of the following attestation fields:
|
||||
@@ -70,11 +67,7 @@ func (a *Attestation) Key() [32]byte {
|
||||
for _, pHash := range a.ObliqueParentHashes() {
|
||||
key = append(key, pHash[:]...)
|
||||
}
|
||||
|
||||
var hash [32]byte
|
||||
h := blake2b.Sum512(key)
|
||||
copy(hash[:], h[:32])
|
||||
return hash
|
||||
return hashutil.Hash(key)
|
||||
}
|
||||
|
||||
// SlotNumber of the block, which this attestation is attesting to.
|
||||
@@ -157,11 +150,7 @@ func AttestationMsg(parentHashes [][32]byte, blockHash []byte, slot uint64, shar
|
||||
binary.PutUvarint(msg, shardID)
|
||||
msg = append(msg, blockHash...)
|
||||
binary.PutUvarint(msg, justifiedSlot)
|
||||
|
||||
var hashMsg [32]byte
|
||||
h := blake2b.Sum512(msg)
|
||||
copy(hashMsg[:], h[:32])
|
||||
return hashMsg
|
||||
return hashutil.Hash(msg)
|
||||
}
|
||||
|
||||
// ContainsValidator checks if the validator is included in the attestation.
|
||||
|
||||
@@ -13,8 +13,8 @@ import (
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/utils"
|
||||
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
||||
"github.com/prysmaticlabs/prysm/shared/bitutil"
|
||||
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
||||
"github.com/sirupsen/logrus"
|
||||
"golang.org/x/crypto/blake2b"
|
||||
)
|
||||
|
||||
var log = logrus.WithField("prefix", "types")
|
||||
@@ -80,10 +80,7 @@ func (b *Block) Hash() ([32]byte, error) {
|
||||
if err != nil {
|
||||
return [32]byte{}, fmt.Errorf("could not marshal block proto data: %v", err)
|
||||
}
|
||||
var hash [32]byte
|
||||
h := blake2b.Sum512(data)
|
||||
copy(hash[:], h[:32])
|
||||
return hash, nil
|
||||
return hashutil.Hash(data), nil
|
||||
}
|
||||
|
||||
// ParentHash corresponding to parent beacon block.
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/params"
|
||||
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
||||
"github.com/prysmaticlabs/prysm/shared/bitutil"
|
||||
"golang.org/x/crypto/blake2b"
|
||||
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
||||
)
|
||||
|
||||
var shardCount = params.GetConfig().ShardCount
|
||||
@@ -138,10 +138,7 @@ func (c *CrystallizedState) Hash() ([32]byte, error) {
|
||||
if err != nil {
|
||||
return [32]byte{}, err
|
||||
}
|
||||
var hash [32]byte
|
||||
h := blake2b.Sum512(data)
|
||||
copy(hash[:], h[:32])
|
||||
return hash, nil
|
||||
return hashutil.Hash(data), nil
|
||||
}
|
||||
|
||||
// LastStateRecalculationSlot returns when the last time crystallized state recalculated.
|
||||
|
||||
@@ -12,9 +12,9 @@ go_library(
|
||||
visibility = ["//beacon-chain:__subpackages__"],
|
||||
deps = [
|
||||
"//beacon-chain/params:go_default_library",
|
||||
"//shared/hashutil:go_default_library",
|
||||
"@com_github_ethereum_go_ethereum//common:go_default_library",
|
||||
"@com_github_urfave_cli//:go_default_library",
|
||||
"@org_golang_x_crypto//blake2b:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/params"
|
||||
"golang.org/x/crypto/blake2b"
|
||||
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
||||
)
|
||||
|
||||
// ShuffleIndices returns a list of pseudorandomly sampled
|
||||
@@ -19,7 +19,7 @@ func ShuffleIndices(seed common.Hash, validatorList []uint32) ([]uint32, error)
|
||||
return nil, errors.New("exceeded upper bound for validator shuffle")
|
||||
}
|
||||
|
||||
hashSeed := blake2b.Sum512(seed[:])
|
||||
hashSeed := hashutil.Hash(seed[:])
|
||||
validatorCount := len(validatorList)
|
||||
|
||||
// Shuffle stops at the second to last index.
|
||||
@@ -31,7 +31,7 @@ func ShuffleIndices(seed common.Hash, validatorList []uint32) ([]uint32, error)
|
||||
swapPos := swapNum%remaining + i
|
||||
validatorList[i], validatorList[swapPos] = validatorList[swapPos], validatorList[i]
|
||||
}
|
||||
hashSeed = blake2b.Sum512(seed[:])
|
||||
hashSeed = hashutil.Hash(seed[:])
|
||||
}
|
||||
return validatorList, nil
|
||||
}
|
||||
|
||||
15
shared/hashutil/BUILD.bazel
Normal file
15
shared/hashutil/BUILD.bazel
Normal file
@@ -0,0 +1,15 @@
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["hash.go"],
|
||||
importpath = "github.com/prysmaticlabs/prysm/shared/hashutil",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = ["@org_golang_x_crypto//blake2b:go_default_library"],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["hash_test.go"],
|
||||
embed = [":go_default_library"],
|
||||
)
|
||||
15
shared/hashutil/hash.go
Normal file
15
shared/hashutil/hash.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package hashutil
|
||||
|
||||
import (
|
||||
"golang.org/x/crypto/blake2b"
|
||||
)
|
||||
|
||||
// Hash defines a function that returns the
|
||||
// blake2b hash of the data passed in.
|
||||
// https://github.com/ethereum/eth2.0-specs/blob/master/specs/beacon-chain.md#appendix-a---hash-function
|
||||
func Hash(data []byte) [32]byte {
|
||||
var hash [32]byte
|
||||
h := blake2b.Sum512(data)
|
||||
copy(hash[:], h[:32])
|
||||
return hash
|
||||
}
|
||||
25
shared/hashutil/hash_test.go
Normal file
25
shared/hashutil/hash_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package hashutil_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
||||
)
|
||||
|
||||
func TestHash(t *testing.T) {
|
||||
hashOf0 := [32]byte{47, 163, 246, 134, 223, 135, 105, 149, 22, 126, 124, 46, 93, 116, 196, 199, 182, 228, 143, 128, 104, 254, 14, 68, 32, 131, 68, 212, 128, 247, 144, 76}
|
||||
hash := hashutil.Hash([]byte{0})
|
||||
if hash != hashOf0 {
|
||||
t.Fatalf("expected hash and computed hash are not equal %d, %d", hash, hashOf0)
|
||||
}
|
||||
|
||||
hashOf1 := [32]byte{149, 69, 186, 55, 178, 48, 216, 162, 231, 22, 196, 112, 117, 134, 84, 39, 128, 129, 91, 124, 64, 136, 237, 203, 154, 246, 169, 69, 45, 80, 243, 36}
|
||||
hash = hashutil.Hash([]byte{1})
|
||||
if hash != hashOf1 {
|
||||
t.Fatalf("expected hash and computed hash are not equal %d, %d", hash, hashOf1)
|
||||
}
|
||||
|
||||
if hashOf0 == hashOf1 {
|
||||
t.Fatalf("expected hash and computed hash are equal %d, %d", hash, hashOf1)
|
||||
}
|
||||
}
|
||||
@@ -10,9 +10,9 @@ go_library(
|
||||
"//proto/beacon/rpc/v1:go_default_library",
|
||||
"//shared/bitutil:go_default_library",
|
||||
"//shared/event:go_default_library",
|
||||
"//shared/hashutil:go_default_library",
|
||||
"@com_github_gogo_protobuf//proto:go_default_library",
|
||||
"@com_github_sirupsen_logrus//:go_default_library",
|
||||
"@org_golang_x_crypto//blake2b:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
@@ -10,8 +10,8 @@ import (
|
||||
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
|
||||
"github.com/prysmaticlabs/prysm/shared/bitutil"
|
||||
"github.com/prysmaticlabs/prysm/shared/event"
|
||||
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
||||
"github.com/sirupsen/logrus"
|
||||
"golang.org/x/crypto/blake2b"
|
||||
)
|
||||
|
||||
var log = logrus.WithField("prefix", "attester")
|
||||
@@ -91,7 +91,7 @@ func (a *Attester) run(attester pb.AttesterServiceClient, validator pb.Validator
|
||||
log.Errorf("could not marshal latest beacon block: %v", err)
|
||||
continue
|
||||
}
|
||||
latestBlockHash := blake2b.Sum512(data)
|
||||
latestBlockHash := hashutil.Hash(data)
|
||||
|
||||
pubKeyReq := &pb.PublicKey{
|
||||
PublicKey: a.beaconService.PublicKey(),
|
||||
|
||||
@@ -9,10 +9,10 @@ go_library(
|
||||
"//proto/beacon/p2p/v1:go_default_library",
|
||||
"//proto/beacon/rpc/v1:go_default_library",
|
||||
"//shared/event:go_default_library",
|
||||
"//shared/hashutil:go_default_library",
|
||||
"@com_github_golang_protobuf//proto:go_default_library",
|
||||
"@com_github_golang_protobuf//ptypes:go_default_library_gen",
|
||||
"@com_github_sirupsen_logrus//:go_default_library",
|
||||
"@org_golang_x_crypto//blake2b:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
@@ -12,8 +12,8 @@ import (
|
||||
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
||||
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
|
||||
"github.com/prysmaticlabs/prysm/shared/event"
|
||||
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
||||
"github.com/sirupsen/logrus"
|
||||
blake2b "golang.org/x/crypto/blake2b"
|
||||
)
|
||||
|
||||
var log = logrus.WithField("prefix", "proposer")
|
||||
@@ -162,7 +162,7 @@ func (p *Proposer) run(done <-chan struct{}, client pb.ProposerServiceClient) {
|
||||
log.Errorf("Could not marshal latest beacon block: %v", err)
|
||||
continue
|
||||
}
|
||||
latestBlockHash := blake2b.Sum512(data)
|
||||
latestBlockHash := hashutil.Hash(data)
|
||||
|
||||
// To prevent any unaccounted attestations from being added.
|
||||
p.lock.Lock()
|
||||
|
||||
@@ -11,6 +11,7 @@ go_library(
|
||||
importpath = "github.com/prysmaticlabs/prysm/validator/types",
|
||||
visibility = ["//validator:__subpackages__"],
|
||||
deps = [
|
||||
"//shared/hashutil:go_default_library",
|
||||
"//shared/shardutil:go_default_library",
|
||||
"//validator/params:go_default_library",
|
||||
"@com_github_ethereum_go_ethereum//common:go_default_library",
|
||||
@@ -20,7 +21,6 @@ go_library(
|
||||
"@com_github_sirupsen_logrus//:go_default_library",
|
||||
"@com_github_syndtr_goleveldb//leveldb/errors:go_default_library",
|
||||
"@com_github_urfave_cli//:go_default_library",
|
||||
"@org_golang_x_crypto//blake2b:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -33,11 +33,11 @@ go_test(
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//shared/database:go_default_library",
|
||||
"//shared/hashutil:go_default_library",
|
||||
"//shared/shardutil:go_default_library",
|
||||
"@com_github_ethereum_go_ethereum//common:go_default_library",
|
||||
"@com_github_ethereum_go_ethereum//core/types:go_default_library",
|
||||
"@com_github_ethereum_go_ethereum//ethdb:go_default_library",
|
||||
"@com_github_ethereum_go_ethereum//rlp:go_default_library",
|
||||
"@org_golang_x_crypto//blake2b:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -8,9 +8,9 @@ import (
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
gethTypes "github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
||||
"github.com/prysmaticlabs/prysm/shared/shardutil"
|
||||
"github.com/prysmaticlabs/prysm/validator/params"
|
||||
"golang.org/x/crypto/blake2b"
|
||||
)
|
||||
|
||||
// Collation defines a base struct that serves as a primitive equivalent of a "block"
|
||||
@@ -71,9 +71,7 @@ func (h *CollationHeader) Hash() (hash common.Hash) {
|
||||
if err != nil {
|
||||
log.Errorf("Failed to RLP encode data: %v", err)
|
||||
}
|
||||
blakeHash := blake2b.Sum512(encoded)
|
||||
copy(hash[:], blakeHash[:32])
|
||||
return hash
|
||||
return hashutil.Hash(encoded)
|
||||
}
|
||||
|
||||
// AddSig adds the signature of proposer after collationHeader gets signed.
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
sharedDB "github.com/prysmaticlabs/prysm/shared/database"
|
||||
"golang.org/x/crypto/blake2b"
|
||||
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
||||
)
|
||||
|
||||
type mockShardDB struct {
|
||||
@@ -42,14 +42,12 @@ func (m *mockShardDB) NewBatch() ethdb.Batch {
|
||||
}
|
||||
|
||||
// Hash returns the hash of a collation's entire contents. Useful for comparison tests.
|
||||
func (c *Collation) Hash() (hash common.Hash) {
|
||||
func (c *Collation) Hash() common.Hash {
|
||||
encoded, err := rlp.EncodeToBytes(c)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to RLP encode data: %v", err)
|
||||
}
|
||||
blakeHash := blake2b.Sum512(encoded)
|
||||
copy(hash[:], blakeHash[:32])
|
||||
return hash
|
||||
return hashutil.Hash(encoded)
|
||||
}
|
||||
|
||||
func TestShard_ValidateShardID(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user