2021-07-27 15:47:03 +00:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
2021-09-15 22:55:11 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/crypto/hash"
|
2021-09-21 15:02:48 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/encoding/ssz"
|
2021-07-29 21:45:17 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2021-07-27 15:47:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// syncCommitteeRoot computes the HashTreeRoot Merkleization of a commitee root.
|
|
|
|
// a SyncCommitteeRoot struct according to the eth2
|
|
|
|
// Simple Serialize specification.
|
2021-07-29 21:45:17 +00:00
|
|
|
func syncCommitteeRoot(committee *ethpb.SyncCommittee) ([32]byte, error) {
|
2021-09-15 22:55:11 +00:00
|
|
|
hasher := hash.CustomSHA256Hasher()
|
2021-07-27 15:47:03 +00:00
|
|
|
var fieldRoots [][32]byte
|
|
|
|
if committee == nil {
|
|
|
|
return [32]byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Field 1: Vector[BLSPubkey, SYNC_COMMITTEE_SIZE]
|
|
|
|
pubKeyRoots := make([][32]byte, 0)
|
|
|
|
for _, pubkey := range committee.Pubkeys {
|
|
|
|
r, err := merkleizePubkey(hasher, pubkey)
|
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, err
|
|
|
|
}
|
|
|
|
pubKeyRoots = append(pubKeyRoots, r)
|
|
|
|
}
|
2021-09-21 15:02:48 +00:00
|
|
|
pubkeyRoot, err := ssz.BitwiseMerkleizeArrays(hasher, pubKeyRoots, uint64(len(pubKeyRoots)), uint64(len(pubKeyRoots)))
|
2021-07-27 15:47:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Field 2: BLSPubkey
|
|
|
|
aggregateKeyRoot, err := merkleizePubkey(hasher, committee.AggregatePubkey)
|
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, err
|
|
|
|
}
|
|
|
|
fieldRoots = [][32]byte{pubkeyRoot, aggregateKeyRoot}
|
|
|
|
|
2021-09-21 15:02:48 +00:00
|
|
|
return ssz.BitwiseMerkleizeArrays(hasher, fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
|
2021-07-27 15:47:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 15:02:48 +00:00
|
|
|
func merkleizePubkey(hasher ssz.HashFn, pubkey []byte) ([32]byte, error) {
|
|
|
|
chunks, err := ssz.Pack([][]byte{pubkey})
|
2021-07-27 15:47:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, err
|
|
|
|
}
|
2021-09-21 15:02:48 +00:00
|
|
|
return ssz.BitwiseMerkleize(hasher, chunks, uint64(len(chunks)), uint64(len(chunks)))
|
2021-07-27 15:47:03 +00:00
|
|
|
}
|