2020-06-16 05:45:04 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2018-2020 Status Research & Development GmbH
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
import
|
2020-07-07 23:02:14 +00:00
|
|
|
../ssz/merkleization,
|
|
|
|
./crypto, ./digest, ./datatypes, ./helpers, ./presets
|
2020-06-16 05:45:04 +00:00
|
|
|
|
2020-06-25 10:23:10 +00:00
|
|
|
template withTrust(sig: SomeSig, body: untyped): bool =
|
|
|
|
when sig is TrustedSig:
|
|
|
|
true
|
|
|
|
else:
|
|
|
|
body
|
|
|
|
|
2020-06-16 05:45:04 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.1/specs/phase0/validator.md#aggregation-selection
|
|
|
|
func get_slot_signature*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest, slot: Slot,
|
|
|
|
privkey: ValidatorPrivKey): ValidatorSig =
|
|
|
|
let
|
|
|
|
epoch = compute_epoch_at_slot(slot)
|
|
|
|
domain = get_domain(
|
|
|
|
fork, DOMAIN_SELECTION_PROOF, epoch, genesis_validators_root)
|
|
|
|
signing_root = compute_signing_root(slot, domain)
|
|
|
|
|
|
|
|
blsSign(privKey, signing_root.data)
|
|
|
|
|
2020-07-02 16:15:27 +00:00
|
|
|
func verify_slot_signature*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest, slot: Slot,
|
|
|
|
pubkey: ValidatorPubKey, signature: SomeSig): bool =
|
|
|
|
withTrust(signature):
|
|
|
|
let
|
|
|
|
epoch = compute_epoch_at_slot(slot)
|
|
|
|
domain = get_domain(
|
|
|
|
fork, DOMAIN_SELECTION_PROOF, epoch, genesis_validators_root)
|
|
|
|
signing_root = compute_signing_root(slot, domain)
|
|
|
|
|
|
|
|
blsVerify(pubkey, signing_root.data, signature)
|
|
|
|
|
2020-06-16 05:45:04 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.1/specs/phase0/validator.md#randao-reveal
|
|
|
|
func get_epoch_signature*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest, epoch: Epoch,
|
|
|
|
privkey: ValidatorPrivKey): ValidatorSig =
|
|
|
|
let
|
|
|
|
domain = get_domain(fork, DOMAIN_RANDAO, epoch, genesis_validators_root)
|
|
|
|
signing_root = compute_signing_root(epoch, domain)
|
|
|
|
|
|
|
|
blsSign(privKey, signing_root.data)
|
|
|
|
|
|
|
|
func verify_epoch_signature*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest, epoch: Epoch,
|
2020-06-25 10:23:10 +00:00
|
|
|
pubkey: ValidatorPubKey, signature: SomeSig): bool =
|
|
|
|
withTrust(signature):
|
|
|
|
let
|
|
|
|
domain = get_domain(fork, DOMAIN_RANDAO, epoch, genesis_validators_root)
|
|
|
|
signing_root = compute_signing_root(epoch, domain)
|
2020-06-16 05:45:04 +00:00
|
|
|
|
2020-06-25 10:23:10 +00:00
|
|
|
blsVerify(pubkey, signing_root.data, signature)
|
2020-06-16 05:45:04 +00:00
|
|
|
|
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.1/specs/phase0/validator.md#signature
|
|
|
|
func get_block_signature*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest, slot: Slot,
|
|
|
|
root: Eth2Digest, privkey: ValidatorPrivKey): ValidatorSig =
|
|
|
|
let
|
|
|
|
epoch = compute_epoch_at_slot(slot)
|
|
|
|
domain = get_domain(
|
|
|
|
fork, DOMAIN_BEACON_PROPOSER, epoch, genesis_validators_root)
|
|
|
|
signing_root = compute_signing_root(root, domain)
|
|
|
|
|
|
|
|
blsSign(privKey, signing_root.data)
|
|
|
|
|
|
|
|
func verify_block_signature*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest, slot: Slot,
|
2020-06-25 10:23:10 +00:00
|
|
|
blck: Eth2Digest | SomeBeaconBlock | BeaconBlockHeader,
|
|
|
|
pubkey: ValidatorPubKey,
|
|
|
|
signature: SomeSig): bool =
|
|
|
|
withTrust(signature):
|
|
|
|
let
|
|
|
|
epoch = compute_epoch_at_slot(slot)
|
|
|
|
domain = get_domain(
|
|
|
|
fork, DOMAIN_BEACON_PROPOSER, epoch, genesis_validators_root)
|
|
|
|
signing_root = compute_signing_root(blck, domain)
|
|
|
|
|
|
|
|
blsVerify(pubKey, signing_root.data, signature)
|
2020-06-16 05:45:04 +00:00
|
|
|
|
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.1/specs/phase0/validator.md#broadcast-aggregate
|
|
|
|
func get_aggregate_and_proof_signature*(fork: Fork, genesis_validators_root: Eth2Digest,
|
|
|
|
aggregate_and_proof: AggregateAndProof,
|
|
|
|
privKey: ValidatorPrivKey): ValidatorSig =
|
|
|
|
let
|
|
|
|
epoch = compute_epoch_at_slot(aggregate_and_proof.aggregate.data.slot)
|
|
|
|
domain = get_domain(
|
|
|
|
fork, DOMAIN_AGGREGATE_AND_PROOF, epoch, genesis_validators_root)
|
|
|
|
signing_root = compute_signing_root(aggregate_and_proof, domain)
|
|
|
|
|
|
|
|
blsSign(privKey, signing_root.data)
|
|
|
|
|
2020-07-02 16:15:27 +00:00
|
|
|
func verify_aggregate_and_proof_signature*(fork: Fork, genesis_validators_root: Eth2Digest,
|
|
|
|
aggregate_and_proof: AggregateAndProof,
|
|
|
|
pubkey: ValidatorPubKey, signature: SomeSig): bool =
|
|
|
|
withTrust(signature):
|
|
|
|
let
|
|
|
|
epoch = compute_epoch_at_slot(aggregate_and_proof.aggregate.data.slot)
|
|
|
|
domain = get_domain(
|
|
|
|
fork, DOMAIN_AGGREGATE_AND_PROOF, epoch, genesis_validators_root)
|
|
|
|
signing_root = compute_signing_root(aggregate_and_proof, domain)
|
|
|
|
|
|
|
|
blsVerify(pubKey, signing_root.data, signature)
|
|
|
|
|
2020-06-16 05:45:04 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.1/specs/phase0/validator.md#aggregate-signature
|
|
|
|
func get_attestation_signature*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
|
|
|
attestation_data: AttestationData,
|
|
|
|
privkey: ValidatorPrivKey): ValidatorSig =
|
|
|
|
let
|
|
|
|
epoch = attestation_data.target.epoch
|
|
|
|
domain = get_domain(
|
|
|
|
fork, DOMAIN_BEACON_ATTESTER, epoch, genesis_validators_root)
|
|
|
|
signing_root = compute_signing_root(attestation_data, domain)
|
|
|
|
|
|
|
|
blsSign(privKey, signing_root.data)
|
|
|
|
|
|
|
|
func verify_attestation_signature*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
|
|
|
attestation_data: AttestationData,
|
|
|
|
pubkeys: openArray[ValidatorPubKey],
|
2020-06-25 10:23:10 +00:00
|
|
|
signature: SomeSig): bool =
|
|
|
|
withTrust(signature):
|
|
|
|
let
|
|
|
|
epoch = attestation_data.target.epoch
|
|
|
|
domain = get_domain(
|
|
|
|
fork, DOMAIN_BEACON_ATTESTER, epoch, genesis_validators_root)
|
|
|
|
signing_root = compute_signing_root(attestation_data, domain)
|
2020-06-16 05:45:04 +00:00
|
|
|
|
2020-06-25 10:23:10 +00:00
|
|
|
blsFastAggregateVerify(pubkeys, signing_root.data, signature)
|
2020-06-16 05:45:04 +00:00
|
|
|
|
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.1/specs/phase0/beacon-chain.md#deposits
|
2020-07-07 23:02:14 +00:00
|
|
|
func get_deposit_signature*(preset: RuntimePreset,
|
|
|
|
deposit: DepositData,
|
|
|
|
privkey: ValidatorPrivKey): ValidatorSig =
|
2020-06-16 05:45:04 +00:00
|
|
|
let
|
|
|
|
deposit_message = deposit.getDepositMessage()
|
|
|
|
# Fork-agnostic domain since deposits are valid across forks
|
2020-07-07 23:02:14 +00:00
|
|
|
domain = compute_domain(DOMAIN_DEPOSIT, preset.GENESIS_FORK_VERSION)
|
2020-06-16 05:45:04 +00:00
|
|
|
signing_root = compute_signing_root(deposit_message, domain)
|
|
|
|
|
|
|
|
blsSign(privKey, signing_root.data)
|
|
|
|
|
2020-07-07 23:02:14 +00:00
|
|
|
func verify_deposit_signature*(preset: RuntimePreset,
|
|
|
|
deposit: DepositData): bool =
|
2020-06-16 05:45:04 +00:00
|
|
|
let
|
|
|
|
deposit_message = deposit.getDepositMessage()
|
|
|
|
# Fork-agnostic domain since deposits are valid across forks
|
2020-07-07 23:02:14 +00:00
|
|
|
domain = compute_domain(DOMAIN_DEPOSIT, preset.GENESIS_FORK_VERSION)
|
2020-06-16 05:45:04 +00:00
|
|
|
signing_root = compute_signing_root(deposit_message, domain)
|
|
|
|
|
|
|
|
blsVerify(deposit.pubkey, signing_root.data, deposit.signature)
|
|
|
|
|
|
|
|
func verify_voluntary_exit_signature*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
|
|
|
voluntary_exit: VoluntaryExit,
|
2020-06-25 10:23:10 +00:00
|
|
|
pubkey: ValidatorPubKey, signature: SomeSig): bool =
|
|
|
|
withTrust(signature):
|
|
|
|
let
|
|
|
|
domain = get_domain(
|
|
|
|
fork, DOMAIN_VOLUNTARY_EXIT, voluntary_exit.epoch, genesis_validators_root)
|
|
|
|
signing_root = compute_signing_root(voluntary_exit, domain)
|
|
|
|
|
|
|
|
blsVerify(pubkey, signing_root.data, signature)
|