2021-08-17 08:07:17 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2021-08-17 08:07:17 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2021-09-30 15:14:03 +00:00
|
|
|
std/random,
|
2021-08-17 08:07:17 +00:00
|
|
|
unittest2,
|
2021-12-09 12:56:54 +00:00
|
|
|
../beacon_chain/spec/[crypto, helpers, signatures],
|
2021-09-30 15:14:03 +00:00
|
|
|
./testblockutil
|
2021-08-17 08:07:17 +00:00
|
|
|
|
|
|
|
suite "Message signatures":
|
2021-09-30 15:14:03 +00:00
|
|
|
var rnd = initRand(123)
|
2021-08-17 08:07:17 +00:00
|
|
|
let
|
|
|
|
fork0 = Fork(current_version: Version [byte 0x40, 0x21, 0x8c, 0xe8])
|
|
|
|
fork1 = Fork(current_version: Version [byte 0x3b, 0x4e, 0xf6, 0x1d])
|
|
|
|
genesis_validators_root0 = Eth2Digest.fromHex(
|
|
|
|
"0x8fbd3b999e4873fb182569b20fb090400332849240da5ceb925db7ff7a8d984b")
|
|
|
|
genesis_validators_root1 = Eth2Digest.fromHex(
|
|
|
|
"0x78fb3f89983b990a841b98bab7951dccc73a757d2394f496e318db3c4826654e")
|
2021-09-30 15:14:03 +00:00
|
|
|
index0 = cast[ValidatorIndex](rnd.next)
|
|
|
|
index1 = cast[ValidatorIndex](rnd.next)
|
|
|
|
pubkey0 = MockPubKeys[index0]
|
|
|
|
privkey0 = MockPrivKeys[index0]
|
|
|
|
privkey1 = MockPrivKeys[index1]
|
2021-08-17 08:07:17 +00:00
|
|
|
|
|
|
|
test "Slot signatures":
|
|
|
|
let
|
|
|
|
slot = default(Slot)
|
|
|
|
root = default(Eth2Digest)
|
|
|
|
|
|
|
|
check:
|
|
|
|
# Matching public/private keys and genesis validator roots
|
|
|
|
verify_block_signature(
|
|
|
|
fork0, genesis_validators_root0, slot, root, pubkey0,
|
|
|
|
get_block_signature(
|
|
|
|
fork0, genesis_validators_root0, slot, root, privkey0).toValidatorSig)
|
|
|
|
|
|
|
|
# Mismatched public/private keys
|
|
|
|
not verify_block_signature(
|
|
|
|
fork0, genesis_validators_root0, slot, root, pubkey0,
|
|
|
|
get_block_signature(
|
|
|
|
fork0, genesis_validators_root0, slot, root, privkey1).toValidatorSig)
|
|
|
|
|
|
|
|
# Mismatched forks
|
|
|
|
not verify_block_signature(
|
|
|
|
fork0, genesis_validators_root0, slot, root, pubkey0,
|
|
|
|
get_block_signature(
|
|
|
|
fork1, genesis_validators_root0, slot, root, privkey0).toValidatorSig)
|
|
|
|
|
|
|
|
# Mismatched genesis validator roots
|
|
|
|
not verify_block_signature(
|
|
|
|
fork0, genesis_validators_root0, slot, root, pubkey0,
|
|
|
|
get_block_signature(
|
|
|
|
fork0, genesis_validators_root1, slot, root, privkey0).toValidatorSig)
|
|
|
|
|
|
|
|
test "Aggregate and proof signatures":
|
|
|
|
let aggregate_and_proof = AggregateAndProof(
|
|
|
|
aggregate: Attestation(aggregation_bits: CommitteeValidatorsBits.init(8)))
|
|
|
|
|
|
|
|
check:
|
|
|
|
# Matching public/private keys and genesis validator roots
|
|
|
|
verify_aggregate_and_proof_signature(
|
|
|
|
fork0, genesis_validators_root0, aggregate_and_proof, pubkey0,
|
|
|
|
get_aggregate_and_proof_signature(
|
|
|
|
fork0, genesis_validators_root0, aggregate_and_proof,
|
|
|
|
privkey0).toValidatorSig)
|
|
|
|
|
|
|
|
# Mismatched public/private keys
|
|
|
|
not verify_aggregate_and_proof_signature(
|
|
|
|
fork0, genesis_validators_root0, aggregate_and_proof, pubkey0,
|
|
|
|
get_aggregate_and_proof_signature(
|
|
|
|
fork0, genesis_validators_root0, aggregate_and_proof,
|
|
|
|
privkey1).toValidatorSig)
|
|
|
|
|
|
|
|
# Mismatched forks
|
|
|
|
not verify_aggregate_and_proof_signature(
|
|
|
|
fork0, genesis_validators_root0, aggregate_and_proof, pubkey0,
|
|
|
|
get_aggregate_and_proof_signature(
|
|
|
|
fork1, genesis_validators_root0, aggregate_and_proof,
|
|
|
|
privkey0).toValidatorSig)
|
|
|
|
|
|
|
|
# Mismatched genesis validator roots
|
|
|
|
not verify_aggregate_and_proof_signature(
|
|
|
|
fork0, genesis_validators_root0, aggregate_and_proof, pubkey0,
|
|
|
|
get_aggregate_and_proof_signature(
|
|
|
|
fork0, genesis_validators_root1, aggregate_and_proof,
|
|
|
|
privkey0).toValidatorSig)
|
|
|
|
|
|
|
|
test "Attestation signatures":
|
|
|
|
let attestation_data = default(AttestationData)
|
|
|
|
|
|
|
|
check:
|
|
|
|
# Matching public/private keys and genesis validator roots
|
|
|
|
verify_attestation_signature(
|
|
|
|
fork0, genesis_validators_root0, attestation_data, [pubkey0],
|
|
|
|
get_attestation_signature(
|
|
|
|
fork0, genesis_validators_root0, attestation_data,
|
|
|
|
privkey0).toValidatorSig)
|
|
|
|
|
|
|
|
# Mismatched public/private keys
|
|
|
|
not verify_attestation_signature(
|
|
|
|
fork0, genesis_validators_root0, attestation_data, [pubkey0],
|
|
|
|
get_attestation_signature(
|
|
|
|
fork0, genesis_validators_root0, attestation_data,
|
|
|
|
privkey1).toValidatorSig)
|
|
|
|
|
|
|
|
# Mismatched forks
|
|
|
|
not verify_attestation_signature(
|
|
|
|
fork0, genesis_validators_root0, attestation_data, [pubkey0],
|
|
|
|
get_attestation_signature(
|
|
|
|
fork1, genesis_validators_root0, attestation_data,
|
|
|
|
privkey0).toValidatorSig)
|
|
|
|
|
|
|
|
# Mismatched genesis validator roots
|
|
|
|
not verify_attestation_signature(
|
|
|
|
fork0, genesis_validators_root0, attestation_data, [pubkey0],
|
|
|
|
get_attestation_signature(
|
|
|
|
fork0, genesis_validators_root1, attestation_data,
|
|
|
|
privkey0).toValidatorSig)
|
|
|
|
|
|
|
|
test "Deposit signatures":
|
|
|
|
let preset = default(RuntimeConfig)
|
|
|
|
|
|
|
|
check:
|
|
|
|
# Matching public/private keys and genesis validator roots
|
|
|
|
verify_deposit_signature(
|
|
|
|
preset, DepositData(
|
|
|
|
pubkey: pubkey0,
|
|
|
|
signature: get_deposit_signature(
|
|
|
|
preset, DepositData(pubkey: pubkey0), privkey0).toValidatorSig))
|
|
|
|
|
|
|
|
# Mismatched public/private keys
|
|
|
|
not verify_deposit_signature(
|
|
|
|
preset, DepositData(
|
|
|
|
pubkey: pubkey0,
|
|
|
|
signature: get_deposit_signature(
|
|
|
|
preset, DepositData(pubkey: pubkey0), privkey1).toValidatorSig))
|
|
|
|
|
|
|
|
test "Voluntary exit signatures":
|
|
|
|
let voluntary_exit = default(VoluntaryExit)
|
|
|
|
|
|
|
|
check:
|
|
|
|
# Matching public/private keys and genesis validator roots
|
|
|
|
verify_voluntary_exit_signature(
|
|
|
|
fork0, genesis_validators_root0, voluntary_exit, pubkey0,
|
|
|
|
get_voluntary_exit_signature(
|
|
|
|
fork0, genesis_validators_root0, voluntary_exit,
|
|
|
|
privkey0).toValidatorSig)
|
|
|
|
|
|
|
|
# Mismatched public/private keys
|
|
|
|
not verify_voluntary_exit_signature(
|
|
|
|
fork0, genesis_validators_root0, voluntary_exit, pubkey0,
|
|
|
|
get_voluntary_exit_signature(
|
|
|
|
fork0, genesis_validators_root0, voluntary_exit,
|
|
|
|
privkey1).toValidatorSig)
|
|
|
|
|
|
|
|
# Mismatched forks
|
|
|
|
not verify_voluntary_exit_signature(
|
|
|
|
fork0, genesis_validators_root0, voluntary_exit, pubkey0,
|
|
|
|
get_voluntary_exit_signature(
|
|
|
|
fork1, genesis_validators_root0, voluntary_exit,
|
|
|
|
privkey0).toValidatorSig)
|
|
|
|
|
|
|
|
# Mismatched genesis validator roots
|
|
|
|
not verify_voluntary_exit_signature(
|
|
|
|
fork0, genesis_validators_root0, voluntary_exit, pubkey0,
|
|
|
|
get_voluntary_exit_signature(
|
|
|
|
fork0, genesis_validators_root1, voluntary_exit,
|
|
|
|
privkey0).toValidatorSig)
|
|
|
|
|
|
|
|
test "Sync committee message signatures":
|
|
|
|
let
|
2021-12-09 12:56:54 +00:00
|
|
|
slot = default(Slot)
|
|
|
|
epoch = slot.epoch
|
2021-08-17 08:07:17 +00:00
|
|
|
block_root = default(Eth2Digest)
|
|
|
|
|
|
|
|
check:
|
|
|
|
# Matching public/private keys and genesis validator roots
|
|
|
|
verify_sync_committee_message_signature(
|
2021-12-09 12:56:54 +00:00
|
|
|
fork0, genesis_validators_root0, slot, block_root, load(pubkey0).get,
|
|
|
|
get_sync_committee_message_signature(
|
|
|
|
fork0, genesis_validators_root0, slot, block_root, privkey0).toValidatorSig())
|
2021-08-17 08:07:17 +00:00
|
|
|
|
|
|
|
# Mismatched public/private keys
|
|
|
|
not verify_sync_committee_message_signature(
|
2021-12-09 12:56:54 +00:00
|
|
|
fork0, genesis_validators_root0, slot, block_root, load(pubkey0).get,
|
|
|
|
get_sync_committee_message_signature(
|
|
|
|
fork0, genesis_validators_root0, slot, block_root, privkey1).toValidatorSig())
|
2021-08-17 08:07:17 +00:00
|
|
|
# Mismatched forks
|
|
|
|
not verify_sync_committee_message_signature(
|
2021-12-09 12:56:54 +00:00
|
|
|
fork0, genesis_validators_root0, slot, block_root, load(pubkey0).get,
|
|
|
|
get_sync_committee_message_signature(
|
|
|
|
fork1, genesis_validators_root0, slot, block_root, privkey0).toValidatorSig())
|
2021-08-17 08:07:17 +00:00
|
|
|
|
|
|
|
# Mismatched genesis validator roots
|
|
|
|
not verify_sync_committee_message_signature(
|
2021-12-09 12:56:54 +00:00
|
|
|
fork0, genesis_validators_root0, slot, block_root, load(pubkey0).get,
|
|
|
|
get_sync_committee_message_signature(
|
|
|
|
fork0, genesis_validators_root1, slot, block_root, privkey0).toValidatorSig())
|
2021-08-17 08:07:17 +00:00
|
|
|
|
|
|
|
test "Sync committee signed contribution and proof signatures":
|
2021-12-09 12:56:54 +00:00
|
|
|
let contribution_and_proof = default(ContributionAndProof)
|
2021-08-17 08:07:17 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
# Matching public/private keys and genesis validator roots
|
2021-12-09 12:56:54 +00:00
|
|
|
verify_contribution_and_proof_signature(
|
|
|
|
fork0, genesis_validators_root0, contribution_and_proof,
|
|
|
|
load(pubkey0).get,
|
|
|
|
get_contribution_and_proof_signature(
|
|
|
|
fork0, genesis_validators_root0,
|
|
|
|
contribution_and_proof, privkey0).toValidatorSig)
|
2021-08-17 08:07:17 +00:00
|
|
|
|
|
|
|
# Mismatched public/private keys
|
2021-12-09 12:56:54 +00:00
|
|
|
not verify_contribution_and_proof_signature(
|
|
|
|
fork0, genesis_validators_root0, contribution_and_proof,
|
|
|
|
load(pubkey0).get,
|
|
|
|
get_contribution_and_proof_signature(
|
|
|
|
fork0, genesis_validators_root0,
|
|
|
|
contribution_and_proof, privkey1).toValidatorSig)
|
2021-08-17 08:07:17 +00:00
|
|
|
|
|
|
|
# Mismatched forks
|
2021-12-09 12:56:54 +00:00
|
|
|
not verify_contribution_and_proof_signature(
|
|
|
|
fork0, genesis_validators_root0, contribution_and_proof,
|
|
|
|
load(pubkey0).get,
|
|
|
|
get_contribution_and_proof_signature(
|
|
|
|
fork1, genesis_validators_root0,
|
|
|
|
contribution_and_proof, privkey0).toValidatorSig)
|
2021-08-17 08:07:17 +00:00
|
|
|
|
|
|
|
# Mismatched genesis validator roots
|
2021-12-09 12:56:54 +00:00
|
|
|
not verify_contribution_and_proof_signature(
|
|
|
|
fork0, genesis_validators_root0, contribution_and_proof,
|
|
|
|
load(pubkey0).get,
|
|
|
|
get_contribution_and_proof_signature(
|
|
|
|
fork0, genesis_validators_root1,
|
|
|
|
contribution_and_proof, privkey0).toValidatorSig)
|
2021-08-17 08:07:17 +00:00
|
|
|
|
|
|
|
test "Sync committee selection proof signatures":
|
|
|
|
let
|
|
|
|
slot = default(Slot)
|
2022-05-10 10:03:40 +00:00
|
|
|
subcommittee_index = default(SyncSubcommitteeIndex)
|
2021-08-17 08:07:17 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
# Matching public/private keys and genesis validator roots
|
2021-12-09 12:56:54 +00:00
|
|
|
verify_sync_committee_selection_proof(
|
|
|
|
fork0, genesis_validators_root0, slot, subcommittee_index,
|
|
|
|
load(pubkey0).get, get_sync_committee_selection_proof(
|
|
|
|
fork0, genesis_validators_root0, slot,
|
|
|
|
subcommittee_index, privkey0).toValidatorSig)
|
2021-08-17 08:07:17 +00:00
|
|
|
|
|
|
|
# Mismatched public/private keys
|
2021-12-09 12:56:54 +00:00
|
|
|
not verify_sync_committee_selection_proof(
|
|
|
|
fork0, genesis_validators_root0, slot, subcommittee_index,
|
|
|
|
load(pubkey0).get, get_sync_committee_selection_proof(
|
|
|
|
fork0, genesis_validators_root0, slot,
|
|
|
|
subcommittee_index, privkey1).toValidatorSig)
|
2021-08-17 08:07:17 +00:00
|
|
|
|
|
|
|
# Mismatched forks
|
2021-12-09 12:56:54 +00:00
|
|
|
not verify_sync_committee_selection_proof(
|
|
|
|
fork0, genesis_validators_root0, slot, subcommittee_index,
|
|
|
|
load(pubkey0).get, get_sync_committee_selection_proof(
|
|
|
|
fork1, genesis_validators_root0, slot,
|
|
|
|
subcommittee_index, privkey0).toValidatorSig)
|
2021-08-17 08:07:17 +00:00
|
|
|
|
|
|
|
# Mismatched genesis validator roots
|
2021-12-09 12:56:54 +00:00
|
|
|
not verify_sync_committee_selection_proof(
|
|
|
|
fork0, genesis_validators_root0, slot, subcommittee_index,
|
|
|
|
load(pubkey0).get, get_sync_committee_selection_proof(
|
|
|
|
fork0, genesis_validators_root1, slot,
|
|
|
|
subcommittee_index, privkey0).toValidatorSig)
|