mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 13:28:01 -05:00
Update Domain Related Functions (#2832)
This commit is contained in:
@@ -194,7 +194,7 @@ func verifyBlockRandao(beaconState *pb.BeaconState, body *pb.BeaconBlockBody, pr
|
||||
currentEpoch := helpers.CurrentEpoch(beaconState)
|
||||
buf := make([]byte, 32)
|
||||
binary.LittleEndian.PutUint64(buf, currentEpoch)
|
||||
domain := helpers.DomainVersion(beaconState, currentEpoch, params.BeaconConfig().DomainRandao)
|
||||
domain := helpers.Domain(beaconState, currentEpoch, params.BeaconConfig().DomainRandao)
|
||||
sig, err := bls.SignatureFromBytes(body.RandaoReveal)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not deserialize block randao reveal: %v", err)
|
||||
|
||||
@@ -64,7 +64,7 @@ func TestProcessBlockHeader_WrongProposerSig(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
currentEpoch := helpers.CurrentEpoch(state)
|
||||
dt := helpers.DomainVersion(state, currentEpoch, params.BeaconConfig().DomainBeaconProposer)
|
||||
dt := helpers.Domain(state, currentEpoch, params.BeaconConfig().DomainBeaconProposer)
|
||||
priv, err := bls.RandKey(rand.Reader)
|
||||
if err != nil {
|
||||
t.Errorf("failed to generate private key got: %v", err)
|
||||
@@ -122,7 +122,7 @@ func TestProcessBlockHeader_DifferentSlots(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
currentEpoch := helpers.CurrentEpoch(state)
|
||||
dt := helpers.DomainVersion(state, currentEpoch, params.BeaconConfig().DomainBeaconProposer)
|
||||
dt := helpers.Domain(state, currentEpoch, params.BeaconConfig().DomainBeaconProposer)
|
||||
priv, err := bls.RandKey(rand.Reader)
|
||||
if err != nil {
|
||||
t.Errorf("failed to generate private key got: %v", err)
|
||||
@@ -171,7 +171,7 @@ func TestProcessBlockHeader_PreviousBlockRootNotSignedRoot(t *testing.T) {
|
||||
}
|
||||
|
||||
currentEpoch := helpers.CurrentEpoch(state)
|
||||
dt := helpers.DomainVersion(state, currentEpoch, params.BeaconConfig().DomainBeaconProposer)
|
||||
dt := helpers.Domain(state, currentEpoch, params.BeaconConfig().DomainBeaconProposer)
|
||||
priv, err := bls.RandKey(rand.Reader)
|
||||
if err != nil {
|
||||
t.Errorf("failed to generate private key got: %v", err)
|
||||
@@ -224,7 +224,7 @@ func TestProcessBlockHeader_SlashedProposer(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
currentEpoch := helpers.CurrentEpoch(state)
|
||||
dt := helpers.DomainVersion(state, currentEpoch, params.BeaconConfig().DomainBeaconProposer)
|
||||
dt := helpers.Domain(state, currentEpoch, params.BeaconConfig().DomainBeaconProposer)
|
||||
priv, err := bls.RandKey(rand.Reader)
|
||||
if err != nil {
|
||||
t.Errorf("failed to generate private key got: %v", err)
|
||||
@@ -281,7 +281,7 @@ func TestProcessBlockHeader_OK(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
currentEpoch := helpers.CurrentEpoch(state)
|
||||
dt := helpers.DomainVersion(state, currentEpoch, params.BeaconConfig().DomainBeaconProposer)
|
||||
dt := helpers.Domain(state, currentEpoch, params.BeaconConfig().DomainBeaconProposer)
|
||||
priv, err := bls.RandKey(rand.Reader)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to generate private key got: %v", err)
|
||||
@@ -331,7 +331,7 @@ func TestProcessRandao_IncorrectProposerFailsVerification(t *testing.T) {
|
||||
epoch := uint64(0)
|
||||
buf := make([]byte, 32)
|
||||
binary.LittleEndian.PutUint64(buf, epoch)
|
||||
domain := helpers.DomainVersion(beaconState, epoch, params.BeaconConfig().DomainRandao)
|
||||
domain := helpers.Domain(beaconState, epoch, params.BeaconConfig().DomainRandao)
|
||||
|
||||
// We make the previous validator's index sign the message instead of the proposer.
|
||||
epochSignature := privKeys[proposerIdx-1].Sign(buf, domain)
|
||||
|
||||
@@ -97,7 +97,7 @@ func CreateRandaoReveal(beaconState *pb.BeaconState, epoch uint64, privKeys []*b
|
||||
}
|
||||
buf := make([]byte, 32)
|
||||
binary.LittleEndian.PutUint64(buf, epoch)
|
||||
domain := DomainVersion(beaconState, epoch, params.BeaconConfig().DomainRandao)
|
||||
domain := Domain(beaconState, epoch, params.BeaconConfig().DomainRandao)
|
||||
// We make the previous validator's index sign the message instead of the proposer.
|
||||
epochSignature := privKeys[proposerIdx].Sign(buf, domain)
|
||||
return epochSignature.Marshal(), nil
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
|
||||
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
||||
"github.com/prysmaticlabs/prysm/shared/bls"
|
||||
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
||||
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
||||
"github.com/prysmaticlabs/prysm/shared/params"
|
||||
@@ -214,30 +215,24 @@ func BeaconProposerIndex(state *pb.BeaconState) (uint64, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// DomainVersion returns the domain version for BLS private key to sign and verify.
|
||||
// Domain returns the domain version for BLS private key to sign and verify.
|
||||
//
|
||||
// Spec pseudocode definition:
|
||||
// def get_domain(state: BeaconState,
|
||||
// domain_type: int,
|
||||
// message_epoch: int=None) -> int:
|
||||
// message_epoch: Epoch=None) -> int:
|
||||
// """
|
||||
// Return the signature domain (fork version concatenated with domain type) of a message.
|
||||
// """
|
||||
// epoch = get_current_epoch(state) if message_epoch is None else message_epoch
|
||||
// fork_version = state.fork.previous_version if epoch < state.fork.epoch else state.fork.current_version
|
||||
// return bytes_to_int(fork_version + int_to_bytes(domain_type, length=4))
|
||||
func DomainVersion(state *pb.BeaconState, epoch uint64, domainType uint64) uint64 {
|
||||
if epoch == 0 {
|
||||
epoch = CurrentEpoch(state)
|
||||
}
|
||||
// return bls_domain(domain_type, fork_version)
|
||||
func Domain(state *pb.BeaconState, epoch uint64, domainType uint64) uint64 {
|
||||
var forkVersion []byte
|
||||
if epoch < state.Fork.Epoch {
|
||||
forkVersion = state.Fork.PreviousVersion
|
||||
} else {
|
||||
forkVersion = state.Fork.CurrentVersion
|
||||
}
|
||||
by := []byte{}
|
||||
by = append(by, forkVersion[:4]...)
|
||||
by = append(by, bytesutil.Bytes4(domainType)...)
|
||||
return bytesutil.FromBytes8(by)
|
||||
return bls.Domain(domainType, forkVersion)
|
||||
}
|
||||
|
||||
@@ -158,24 +158,21 @@ func TestDomain_OK(t *testing.T) {
|
||||
PreviousVersion: []byte{0, 0, 0, 2},
|
||||
CurrentVersion: []byte{0, 0, 0, 3},
|
||||
},
|
||||
Slot: 70,
|
||||
}
|
||||
|
||||
if DomainVersion(state, 9, 1) != 4345298944 {
|
||||
t.Errorf("fork Version not equal to 4345298944 %d", DomainVersion(state, 1, 9))
|
||||
tests := []struct {
|
||||
epoch uint64
|
||||
domainType uint64
|
||||
version uint64
|
||||
}{
|
||||
{epoch: 1, domainType: 4, version: 144115188075855876},
|
||||
{epoch: 2, domainType: 4, version: 144115188075855876},
|
||||
{epoch: 2, domainType: 5, version: 144115188075855877},
|
||||
{epoch: 3, domainType: 4, version: 216172782113783812},
|
||||
{epoch: 3, domainType: 5, version: 216172782113783813},
|
||||
}
|
||||
|
||||
if DomainVersion(state, 9, 2) != 8640266240 {
|
||||
t.Errorf("fork Version not equal to 8640266240 %d", DomainVersion(state, 2, 9))
|
||||
}
|
||||
|
||||
if DomainVersion(state, 2, 1) != 4328521728 {
|
||||
t.Errorf("fork Version not equal to 4328521728 %d", DomainVersion(state, 1, 2))
|
||||
}
|
||||
if DomainVersion(state, 2, 2) != 8623489024 {
|
||||
t.Errorf("fork Version not equal to 8623489024 %d", DomainVersion(state, 2, 2))
|
||||
}
|
||||
if DomainVersion(state, 0, 1) != 4328521728 {
|
||||
t.Errorf("fork Version not equal to 4328521728 %d", DomainVersion(state, 1, 0))
|
||||
for _, tt := range tests {
|
||||
if Domain(state, tt.epoch, tt.domainType) != tt.version {
|
||||
t.Errorf("wanted domain version: %d, got: %d", tt.version, Domain(state, tt.epoch, tt.domainType))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -781,7 +781,7 @@ func BenchmarkProcessBlk_65536Validators_FullBlock(b *testing.B) {
|
||||
s.ValidatorRegistry[proposerIdx].Pubkey = priv.PublicKey().Marshal()
|
||||
buf := make([]byte, 32)
|
||||
binary.LittleEndian.PutUint64(buf, 0)
|
||||
domain := helpers.DomainVersion(s, 0, params.BeaconConfig().DomainRandao)
|
||||
domain := helpers.Domain(s, 0, params.BeaconConfig().DomainRandao)
|
||||
epochSignature := priv.Sign(buf, domain)
|
||||
|
||||
// Set up transfer object for block
|
||||
|
||||
@@ -461,7 +461,7 @@ func (vs *ValidatorServer) DomainData(ctx context.Context, request *pb.DomainReq
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not retrieve beacon state: %v", err)
|
||||
}
|
||||
dv := helpers.DomainVersion(state, request.Epoch, params.BeaconConfig().DomainRandao)
|
||||
dv := helpers.Domain(state, request.Epoch, params.BeaconConfig().DomainRandao)
|
||||
return &pb.DomainResponse{
|
||||
SignatureDomain: dv,
|
||||
}, nil
|
||||
|
||||
@@ -124,3 +124,18 @@ func AggregateSignatures(sigs []*Signature) *Signature {
|
||||
}
|
||||
return &Signature{val: g1.AggregateSignatures(ss)}
|
||||
}
|
||||
|
||||
// Domain returns the bls domain given by the domain type and the operation 4 byte fork version.
|
||||
//
|
||||
// Spec pseudocode definition:
|
||||
// def bls_domain(domain_type: int, fork_version: bytes=b'\x00\x00\x00\x00') -> int:
|
||||
// """
|
||||
// Return the bls domain given by the ``domain_type`` and optional 4 byte ``fork_version`` (defaults to zero).
|
||||
// """
|
||||
// return bytes_to_int(int_to_bytes(domain_type, length=4) + fork_version)
|
||||
func Domain(domainType uint64, forkVersion []byte) uint64 {
|
||||
b := []byte{}
|
||||
b = append(b, bytesutil.Bytes4(domainType)...)
|
||||
b = append(b, forkVersion[:4]...)
|
||||
return bytesutil.FromBytes8(b)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user