2020-06-16 05:45:04 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2020-06-16 05:45:04 +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.
|
|
|
|
|
2023-01-20 14:14:37 +00:00
|
|
|
{.push raises: [].}
|
2020-06-16 05:45:04 +00:00
|
|
|
|
2021-12-09 12:56:54 +00:00
|
|
|
## Signature production and verification for spec types - for every type of
|
|
|
|
## signature, there are 3 functions:
|
|
|
|
## * `compute_*_signing_root` - reduce message to the data that will be signed
|
|
|
|
## * `get_*_signature` - sign the signing root with a private key
|
|
|
|
## * `verify_*_signature` - verify a signature produced by `get_*_signature`
|
|
|
|
##
|
|
|
|
## See also `signatures_batch` for batch verification versions of these
|
|
|
|
## functions.
|
|
|
|
|
2020-06-16 05:45:04 +00:00
|
|
|
import
|
2023-02-21 13:21:38 +00:00
|
|
|
./datatypes/[phase0, altair, bellatrix], ./helpers, ./eth2_merkleization
|
2021-08-12 13:08:20 +00:00
|
|
|
|
2022-10-27 06:29:24 +00:00
|
|
|
from ./datatypes/capella import BLSToExecutionChange, SignedBLSToExecutionChange
|
|
|
|
|
2021-08-12 13:08:20 +00:00
|
|
|
export phase0, altair
|
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
|
|
|
|
|
2021-03-29 19:17:48 +00:00
|
|
|
func getDepositMessage(depositData: DepositData): DepositMessage =
|
|
|
|
DepositMessage(
|
|
|
|
pubkey: depositData.pubkey,
|
|
|
|
amount: depositData.amount,
|
|
|
|
withdrawal_credentials: depositData.withdrawal_credentials)
|
|
|
|
|
2021-12-09 12:56:54 +00:00
|
|
|
func compute_slot_signing_root*(
|
2020-09-01 13:44:40 +00:00
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest, slot: Slot
|
|
|
|
): Eth2Digest =
|
2020-06-16 05:45:04 +00:00
|
|
|
let
|
2022-01-11 10:01:54 +00:00
|
|
|
epoch = epoch(slot)
|
2020-06-16 05:45:04 +00:00
|
|
|
domain = get_domain(
|
|
|
|
fork, DOMAIN_SELECTION_PROOF, epoch, genesis_validators_root)
|
2021-08-17 08:07:17 +00:00
|
|
|
compute_signing_root(slot, domain)
|
2020-06-16 05:45:04 +00:00
|
|
|
|
2024-04-17 03:51:16 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/phase0/validator.md#aggregation-selection
|
2020-09-01 13:44:40 +00:00
|
|
|
func get_slot_signature*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest, slot: Slot,
|
2021-04-26 20:39:44 +00:00
|
|
|
privkey: ValidatorPrivKey): CookedSig =
|
2021-12-09 12:56:54 +00:00
|
|
|
let signing_root = compute_slot_signing_root(
|
|
|
|
fork, genesis_validators_root, slot)
|
|
|
|
|
2022-04-08 16:22:49 +00:00
|
|
|
blsSign(privkey, signing_root.data)
|
2020-06-16 05:45:04 +00:00
|
|
|
|
2021-12-09 12:56:54 +00:00
|
|
|
func compute_epoch_signing_root*(
|
2020-09-01 13:44:40 +00:00
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest, epoch: Epoch
|
|
|
|
): Eth2Digest =
|
2021-08-17 08:07:17 +00:00
|
|
|
let domain = get_domain(fork, DOMAIN_RANDAO, epoch, genesis_validators_root)
|
|
|
|
compute_signing_root(epoch, domain)
|
2020-09-01 13:44:40 +00:00
|
|
|
|
2024-03-14 06:26:36 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/phase0/validator.md#randao-reveal
|
2020-06-16 05:45:04 +00:00
|
|
|
func get_epoch_signature*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest, epoch: Epoch,
|
2021-04-26 20:39:44 +00:00
|
|
|
privkey: ValidatorPrivKey): CookedSig =
|
2021-12-09 12:56:54 +00:00
|
|
|
let signing_root = compute_epoch_signing_root(
|
|
|
|
fork, genesis_validators_root, epoch)
|
|
|
|
|
2022-04-08 16:22:49 +00:00
|
|
|
blsSign(privkey, signing_root.data)
|
2020-06-16 05:45:04 +00:00
|
|
|
|
2020-07-23 14:38:28 +00:00
|
|
|
proc verify_epoch_signature*(
|
2020-06-16 05:45:04 +00:00
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest, epoch: Epoch,
|
2021-06-01 11:13:40 +00:00
|
|
|
pubkey: ValidatorPubKey | CookedPubKey, signature: SomeSig): bool =
|
2020-06-25 10:23:10 +00:00
|
|
|
withTrust(signature):
|
2021-12-09 12:56:54 +00:00
|
|
|
let signing_root = compute_epoch_signing_root(
|
|
|
|
fork, genesis_validators_root, epoch)
|
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
|
|
|
|
2021-12-09 12:56:54 +00:00
|
|
|
func compute_block_signing_root*(
|
2020-06-16 05:45:04 +00:00
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest, slot: Slot,
|
2024-03-01 00:02:13 +00:00
|
|
|
blck: Eth2Digest | SomeForkyBeaconBlock | BeaconBlockHeader): Eth2Digest =
|
2020-06-16 05:45:04 +00:00
|
|
|
let
|
2022-01-11 10:01:54 +00:00
|
|
|
epoch = epoch(slot)
|
2020-06-16 05:45:04 +00:00
|
|
|
domain = get_domain(
|
|
|
|
fork, DOMAIN_BEACON_PROPOSER, epoch, genesis_validators_root)
|
2021-12-09 12:56:54 +00:00
|
|
|
compute_signing_root(blck, domain)
|
2020-06-16 05:45:04 +00:00
|
|
|
|
2024-03-14 06:26:36 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/phase0/validator.md#signature
|
2020-09-01 13:44:40 +00:00
|
|
|
func get_block_signature*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest, slot: Slot,
|
2021-04-26 20:39:44 +00:00
|
|
|
root: Eth2Digest, privkey: ValidatorPrivKey): CookedSig =
|
2021-12-09 12:56:54 +00:00
|
|
|
let signing_root = compute_block_signing_root(
|
|
|
|
fork, genesis_validators_root, slot, root)
|
|
|
|
|
2022-04-08 16:22:49 +00:00
|
|
|
blsSign(privkey, signing_root.data)
|
2020-06-16 05:45:04 +00:00
|
|
|
|
2020-07-23 14:38:28 +00:00
|
|
|
proc verify_block_signature*(
|
2020-06-16 05:45:04 +00:00
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest, slot: Slot,
|
2022-01-30 16:51:04 +00:00
|
|
|
blck: Eth2Digest | SomeForkyBeaconBlock | BeaconBlockHeader,
|
2021-06-01 11:13:40 +00:00
|
|
|
pubkey: ValidatorPubKey | CookedPubKey, signature: SomeSig): bool =
|
2020-06-25 10:23:10 +00:00
|
|
|
withTrust(signature):
|
|
|
|
let
|
2021-12-09 12:56:54 +00:00
|
|
|
signing_root = compute_block_signing_root(
|
|
|
|
fork, genesis_validators_root, slot, blck)
|
2020-06-25 10:23:10 +00:00
|
|
|
|
2021-12-09 12:56:54 +00:00
|
|
|
blsVerify(pubkey, signing_root.data, signature)
|
2020-06-16 05:45:04 +00:00
|
|
|
|
2021-12-09 12:56:54 +00:00
|
|
|
func compute_aggregate_and_proof_signing_root*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
2024-10-07 07:53:38 +00:00
|
|
|
aggregate_and_proof: phase0.AggregateAndProof | electra.AggregateAndProof):
|
|
|
|
Eth2Digest =
|
2020-06-16 05:45:04 +00:00
|
|
|
let
|
2022-01-11 10:01:54 +00:00
|
|
|
epoch = epoch(aggregate_and_proof.aggregate.data.slot)
|
2020-06-16 05:45:04 +00:00
|
|
|
domain = get_domain(
|
|
|
|
fork, DOMAIN_AGGREGATE_AND_PROOF, epoch, genesis_validators_root)
|
2021-08-17 08:07:17 +00:00
|
|
|
compute_signing_root(aggregate_and_proof, domain)
|
2020-06-16 05:45:04 +00:00
|
|
|
|
2024-03-14 06:26:36 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/phase0/validator.md#broadcast-aggregate
|
2024-10-07 07:53:38 +00:00
|
|
|
func get_aggregate_and_proof_signature*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
|
|
|
aggregate_and_proof: phase0.AggregateAndProof | electra.AggregateAndProof,
|
|
|
|
privkey: ValidatorPrivKey): CookedSig =
|
2021-12-09 12:56:54 +00:00
|
|
|
let signing_root = compute_aggregate_and_proof_signing_root(
|
|
|
|
fork, genesis_validators_root, aggregate_and_proof)
|
|
|
|
|
2022-04-08 16:22:49 +00:00
|
|
|
blsSign(privkey, signing_root.data)
|
2020-06-16 05:45:04 +00:00
|
|
|
|
2021-06-01 11:13:40 +00:00
|
|
|
proc verify_aggregate_and_proof_signature*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
2024-05-14 04:12:35 +00:00
|
|
|
aggregate_and_proof: phase0.AggregateAndProof,
|
2021-06-01 11:13:40 +00:00
|
|
|
pubkey: ValidatorPubKey | CookedPubKey, signature: SomeSig): bool =
|
2020-07-02 16:15:27 +00:00
|
|
|
withTrust(signature):
|
2021-12-09 12:56:54 +00:00
|
|
|
let signing_root = compute_aggregate_and_proof_signing_root(
|
|
|
|
fork, genesis_validators_root, aggregate_and_proof)
|
2020-07-02 16:15:27 +00:00
|
|
|
|
2021-12-22 12:37:31 +00:00
|
|
|
blsVerify(pubkey, signing_root.data, signature)
|
2020-07-02 16:15:27 +00:00
|
|
|
|
2021-12-09 12:56:54 +00:00
|
|
|
func compute_attestation_signing_root*(
|
2020-06-16 05:45:04 +00:00
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
2021-12-09 12:56:54 +00:00
|
|
|
attestation_data: AttestationData): Eth2Digest =
|
2020-06-16 05:45:04 +00:00
|
|
|
let
|
|
|
|
epoch = attestation_data.target.epoch
|
|
|
|
domain = get_domain(
|
|
|
|
fork, DOMAIN_BEACON_ATTESTER, epoch, genesis_validators_root)
|
2021-08-17 08:07:17 +00:00
|
|
|
compute_signing_root(attestation_data, domain)
|
|
|
|
|
2024-10-09 06:37:35 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.8/specs/phase0/validator.md#aggregate-signature
|
2020-09-01 13:44:40 +00:00
|
|
|
func get_attestation_signature*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
|
|
|
attestation_data: AttestationData,
|
2021-04-26 20:39:44 +00:00
|
|
|
privkey: ValidatorPrivKey): CookedSig =
|
2021-12-09 12:56:54 +00:00
|
|
|
let signing_root = compute_attestation_signing_root(
|
|
|
|
fork, genesis_validators_root, attestation_data)
|
|
|
|
|
2022-04-08 16:22:49 +00:00
|
|
|
blsSign(privkey, signing_root.data)
|
2020-06-16 05:45:04 +00:00
|
|
|
|
2020-07-23 14:38:28 +00:00
|
|
|
proc verify_attestation_signature*(
|
2020-06-16 05:45:04 +00:00
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
|
|
|
attestation_data: AttestationData,
|
2021-12-09 12:56:54 +00:00
|
|
|
pubkeys: auto, signature: SomeSig): bool =
|
2020-06-25 10:23:10 +00:00
|
|
|
withTrust(signature):
|
2021-12-09 12:56:54 +00:00
|
|
|
let signing_root = compute_attestation_signing_root(
|
|
|
|
fork, genesis_validators_root, attestation_data)
|
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
|
|
|
|
2024-04-26 07:18:44 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.0/specs/electra/beacon-chain.md#new-is_valid_deposit_signature
|
2023-05-14 22:56:14 +00:00
|
|
|
func compute_deposit_signing_root(
|
2021-12-09 12:56:54 +00:00
|
|
|
version: Version,
|
|
|
|
deposit_message: DepositMessage): Eth2Digest =
|
|
|
|
let
|
|
|
|
# Fork-agnostic domain since deposits are valid across forks
|
|
|
|
domain = compute_domain(DOMAIN_DEPOSIT, version)
|
|
|
|
compute_signing_root(deposit_message, domain)
|
|
|
|
|
2024-04-17 03:51:16 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/phase0/beacon-chain.md#deposits
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
func get_deposit_signature*(preset: RuntimeConfig,
|
2020-07-07 23:02:14 +00:00
|
|
|
deposit: DepositData,
|
2021-04-26 20:39:44 +00:00
|
|
|
privkey: ValidatorPrivKey): CookedSig =
|
2021-12-09 12:56:54 +00:00
|
|
|
let signing_root = compute_deposit_signing_root(
|
|
|
|
preset.GENESIS_FORK_VERSION, deposit.getDepositMessage())
|
2020-06-16 05:45:04 +00:00
|
|
|
|
2022-04-08 16:22:49 +00:00
|
|
|
blsSign(privkey, signing_root.data)
|
2020-06-16 05:45:04 +00:00
|
|
|
|
2021-11-30 01:20:21 +00:00
|
|
|
func get_deposit_signature*(message: DepositMessage, version: Version,
|
|
|
|
privkey: ValidatorPrivKey): CookedSig =
|
2021-12-09 12:56:54 +00:00
|
|
|
let signing_root = compute_deposit_signing_root(version, message)
|
|
|
|
|
2021-11-30 01:20:21 +00:00
|
|
|
blsSign(privkey, signing_root.data)
|
|
|
|
|
2023-05-14 22:56:14 +00:00
|
|
|
proc verify_deposit_signature(preset: RuntimeConfig,
|
|
|
|
deposit: DepositData,
|
|
|
|
pubkey: CookedPubKey): bool =
|
2020-06-16 05:45:04 +00:00
|
|
|
let
|
|
|
|
deposit_message = deposit.getDepositMessage()
|
2021-12-09 12:56:54 +00:00
|
|
|
signing_root = compute_deposit_signing_root(
|
|
|
|
preset.GENESIS_FORK_VERSION, deposit_message)
|
2020-06-16 05:45:04 +00:00
|
|
|
|
2022-09-07 18:49:32 +00:00
|
|
|
blsVerify(pubkey, signing_root.data, deposit.signature)
|
|
|
|
|
|
|
|
proc verify_deposit_signature*(preset: RuntimeConfig,
|
|
|
|
deposit: DepositData): bool =
|
|
|
|
# Deposits come with compressed public keys; uncompressing them is expensive.
|
|
|
|
# `blsVerify` fills an internal cache when using `ValidatorPubKey`.
|
|
|
|
# To avoid filling this cache unnecessarily, uncompress explicitly here.
|
2023-01-11 12:29:21 +00:00
|
|
|
let pubkey = deposit.pubkey.load().valueOr: # Loading the pubkey is slow!
|
2022-09-07 18:49:32 +00:00
|
|
|
return false
|
2023-01-11 12:29:21 +00:00
|
|
|
verify_deposit_signature(preset, deposit, pubkey)
|
2020-06-16 05:45:04 +00:00
|
|
|
|
2021-12-09 12:56:54 +00:00
|
|
|
func compute_voluntary_exit_signing_root*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
|
|
|
voluntary_exit: VoluntaryExit): Eth2Digest =
|
|
|
|
let
|
|
|
|
epoch = voluntary_exit.epoch
|
|
|
|
domain = get_domain(
|
|
|
|
fork, DOMAIN_VOLUNTARY_EXIT, epoch, genesis_validators_root)
|
|
|
|
compute_signing_root(voluntary_exit, domain)
|
|
|
|
|
2024-02-26 08:48:07 +00:00
|
|
|
func voluntary_exit_signature_fork(
|
|
|
|
is_post_deneb: static bool,
|
fix EIP-7044 implementation when using batch verification (#5953)
In #5120, EIP-7044 support got added to the state transition function to
force `CAPELLA_FORK_VERSION` to be used when validiting `VoluntaryExit`
messages, irrespective of their `epoch`.
In #5637, similar logic was added when batch verifying BLS signatures,
which is used during gossip validation (libp2p gossipsub, and req/resp).
However, that logic did not match the one introduced in #5120, and only
uses `CAPELLA_FORK_VERSION` when a `VoluntaryExit`'s `epoch` was set to
a value `>= CAPELLA_FORK_EPOCH`. Otherwise, `BELLATRIX_FORK_VERSION`
would still be used when validating `VoluntaryExit`, e.g., with `epoch`
set to `0`, as is the case in this Holesky block:
- https://holesky.beaconcha.in/slot/1076985#voluntary-exits
Extracting the correct logic from #5120 into a function, and reusing it
when verifying BLS signatures fixes this issue, and also leverages the
exhaustive EF test suite that covers the (correct) #5120 logic.
This fix only affects networks that have EIP-7044 applied (post-Deneb).
Without the fix, Deneb blocks with a `VoluntaryExit` with `epoch` set to
`< CAPELLA_FORK_EPOCH` incorrectly fail to validate despite being valid.
Incorrect blocks that contain a malicious `VoluntaryExit` with `epoch`
set to `< CAPELLA_FORK_EPOCH` and signed using `BELLATRIX_FORK_VERSION`
_would_ pass the BLS verification stage, but subsequently fail the state
transition logic. Such blocks would still correctly be labeled invalid.
2024-02-25 14:25:26 +00:00
|
|
|
state_fork: Fork,
|
|
|
|
capella_fork_version: Version): Fork =
|
2024-02-26 08:48:07 +00:00
|
|
|
when is_post_deneb:
|
fix EIP-7044 implementation when using batch verification (#5953)
In #5120, EIP-7044 support got added to the state transition function to
force `CAPELLA_FORK_VERSION` to be used when validiting `VoluntaryExit`
messages, irrespective of their `epoch`.
In #5637, similar logic was added when batch verifying BLS signatures,
which is used during gossip validation (libp2p gossipsub, and req/resp).
However, that logic did not match the one introduced in #5120, and only
uses `CAPELLA_FORK_VERSION` when a `VoluntaryExit`'s `epoch` was set to
a value `>= CAPELLA_FORK_EPOCH`. Otherwise, `BELLATRIX_FORK_VERSION`
would still be used when validating `VoluntaryExit`, e.g., with `epoch`
set to `0`, as is the case in this Holesky block:
- https://holesky.beaconcha.in/slot/1076985#voluntary-exits
Extracting the correct logic from #5120 into a function, and reusing it
when verifying BLS signatures fixes this issue, and also leverages the
exhaustive EF test suite that covers the (correct) #5120 logic.
This fix only affects networks that have EIP-7044 applied (post-Deneb).
Without the fix, Deneb blocks with a `VoluntaryExit` with `epoch` set to
`< CAPELLA_FORK_EPOCH` incorrectly fail to validate despite being valid.
Incorrect blocks that contain a malicious `VoluntaryExit` with `epoch`
set to `< CAPELLA_FORK_EPOCH` and signed using `BELLATRIX_FORK_VERSION`
_would_ pass the BLS verification stage, but subsequently fail the state
transition logic. Such blocks would still correctly be labeled invalid.
2024-02-25 14:25:26 +00:00
|
|
|
# Always use Capella fork version, disregarding `VoluntaryExit` epoch
|
|
|
|
# [Modified in Deneb:EIP7044]
|
|
|
|
Fork(
|
|
|
|
previous_version: capella_fork_version,
|
|
|
|
current_version: capella_fork_version,
|
|
|
|
epoch: GENESIS_EPOCH) # irrelevant when current/previous identical
|
|
|
|
else:
|
|
|
|
state_fork
|
|
|
|
|
2024-02-26 08:48:07 +00:00
|
|
|
func voluntary_exit_signature_fork*(
|
|
|
|
consensusFork: static ConsensusFork,
|
|
|
|
state_fork: Fork,
|
|
|
|
capella_fork_version: Version): Fork =
|
|
|
|
const is_post_deneb = (consensusFork >= ConsensusFork.Deneb)
|
|
|
|
voluntary_exit_signature_fork(is_post_deneb, state_fork, capella_fork_version)
|
|
|
|
|
|
|
|
func voluntary_exit_signature_fork*(
|
|
|
|
state_fork: Fork,
|
|
|
|
capella_fork_version: Version,
|
|
|
|
current_epoch: Epoch,
|
|
|
|
deneb_fork_epoch: Epoch): Fork =
|
|
|
|
if current_epoch >= deneb_fork_epoch:
|
|
|
|
const is_post_deneb = true
|
|
|
|
voluntary_exit_signature_fork(
|
|
|
|
is_post_deneb, state_fork, capella_fork_version)
|
|
|
|
else:
|
|
|
|
const is_post_deneb = false
|
|
|
|
voluntary_exit_signature_fork(
|
|
|
|
is_post_deneb, state_fork, capella_fork_version)
|
|
|
|
|
2020-11-27 19:48:33 +00:00
|
|
|
func get_voluntary_exit_signature*(
|
2021-12-09 12:56:54 +00:00
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
2020-11-27 19:48:33 +00:00
|
|
|
voluntary_exit: VoluntaryExit,
|
2021-04-26 20:39:44 +00:00
|
|
|
privkey: ValidatorPrivKey): CookedSig =
|
2021-12-09 12:56:54 +00:00
|
|
|
let signing_root = compute_voluntary_exit_signing_root(
|
|
|
|
fork, genesis_validators_root, voluntary_exit)
|
2020-11-27 19:48:33 +00:00
|
|
|
|
2022-04-08 16:22:49 +00:00
|
|
|
blsSign(privkey, signing_root.data)
|
2020-11-27 19:48:33 +00:00
|
|
|
|
2020-07-23 14:38:28 +00:00
|
|
|
proc verify_voluntary_exit_signature*(
|
2021-12-09 12:56:54 +00:00
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
2020-06-16 05:45:04 +00:00
|
|
|
voluntary_exit: VoluntaryExit,
|
2021-12-09 12:56:54 +00:00
|
|
|
pubkey: ValidatorPubKey | CookedPubKey, signature: SomeSig): bool =
|
2020-06-25 10:23:10 +00:00
|
|
|
withTrust(signature):
|
2021-12-09 12:56:54 +00:00
|
|
|
let signing_root = compute_voluntary_exit_signing_root(
|
|
|
|
fork, genesis_validators_root, voluntary_exit)
|
2020-06-25 10:23:10 +00:00
|
|
|
|
|
|
|
blsVerify(pubkey, signing_root.data, signature)
|
2021-08-17 08:07:17 +00:00
|
|
|
|
2024-10-09 06:37:35 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.8/specs/altair/validator.md#prepare-sync-committee-message
|
2021-12-09 12:56:54 +00:00
|
|
|
func compute_sync_committee_message_signing_root*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
|
|
|
slot: Slot, beacon_block_root: Eth2Digest): Eth2Digest =
|
|
|
|
let domain = get_domain(
|
|
|
|
fork, DOMAIN_SYNC_COMMITTEE, slot.epoch, genesis_validators_root)
|
|
|
|
compute_signing_root(beacon_block_root, domain)
|
2021-11-30 01:20:21 +00:00
|
|
|
|
2021-12-09 12:56:54 +00:00
|
|
|
func get_sync_committee_message_signature*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
|
|
|
slot: Slot, beacon_block_root: Eth2Digest,
|
|
|
|
privkey: ValidatorPrivKey): CookedSig =
|
|
|
|
let signing_root = compute_sync_committee_message_signing_root(
|
|
|
|
fork, genesis_validators_root, slot, beacon_block_root)
|
2021-11-30 01:20:21 +00:00
|
|
|
|
|
|
|
blsSign(privkey, signing_root.data)
|
|
|
|
|
2021-08-17 08:07:17 +00:00
|
|
|
proc verify_sync_committee_message_signature*(
|
2021-12-09 12:56:54 +00:00
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
|
|
|
slot: Slot, beacon_block_root: Eth2Digest,
|
|
|
|
pubkey: ValidatorPubKey | CookedPubKey, signature: SomeSig): bool =
|
|
|
|
let signing_root = compute_sync_committee_message_signing_root(
|
|
|
|
fork, genesis_validators_root, slot, beacon_block_root)
|
2021-08-17 08:07:17 +00:00
|
|
|
|
|
|
|
blsVerify(pubkey, signing_root.data, signature)
|
|
|
|
|
2021-12-09 12:56:54 +00:00
|
|
|
proc verify_sync_committee_signature*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
|
|
|
slot: Slot, beacon_block_root: Eth2Digest,
|
|
|
|
pubkeys: auto, signature: SomeSig): bool =
|
|
|
|
let signing_root = compute_sync_committee_message_signing_root(
|
|
|
|
fork, genesis_validators_root, slot, beacon_block_root)
|
|
|
|
|
|
|
|
blsFastAggregateVerify(pubkeys, signing_root.data, signature)
|
|
|
|
|
2024-10-09 06:37:35 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.8/specs/altair/validator.md#aggregation-selection
|
2021-12-09 12:56:54 +00:00
|
|
|
func compute_sync_committee_selection_proof_signing_root*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
2022-05-10 10:03:40 +00:00
|
|
|
slot: Slot, subcommittee_index: SyncSubcommitteeIndex): Eth2Digest =
|
2021-12-09 12:56:54 +00:00
|
|
|
let
|
|
|
|
domain = get_domain(fork, DOMAIN_SYNC_COMMITTEE_SELECTION_PROOF,
|
|
|
|
slot.epoch, genesis_validators_root)
|
|
|
|
signing_data = SyncAggregatorSelectionData(
|
|
|
|
slot: slot,
|
2022-05-10 10:03:40 +00:00
|
|
|
subcommittee_index: uint64 subcommittee_index)
|
2021-12-09 12:56:54 +00:00
|
|
|
compute_signing_root(signing_data, domain)
|
|
|
|
|
|
|
|
func get_sync_committee_selection_proof*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
2022-05-10 10:03:40 +00:00
|
|
|
slot: Slot, subcommittee_index: SyncSubcommitteeIndex,
|
2021-12-09 12:56:54 +00:00
|
|
|
privkey: ValidatorPrivKey): CookedSig =
|
|
|
|
let signing_root = compute_sync_committee_selection_proof_signing_root(
|
|
|
|
fork, genesis_validators_root, slot, subcommittee_index)
|
|
|
|
|
|
|
|
blsSign(privkey, signing_root.data)
|
|
|
|
|
|
|
|
proc verify_sync_committee_selection_proof*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
2022-05-10 10:03:40 +00:00
|
|
|
slot: Slot, subcommittee_index: SyncSubcommitteeIndex,
|
2021-12-09 12:56:54 +00:00
|
|
|
pubkey: ValidatorPubKey | CookedPubKey, signature: SomeSig): bool =
|
|
|
|
withTrust(signature):
|
|
|
|
let signing_root = compute_sync_committee_selection_proof_signing_root(
|
|
|
|
fork, genesis_validators_root, slot, subcommittee_index)
|
|
|
|
|
|
|
|
blsVerify(pubkey, signing_root.data, signature)
|
|
|
|
|
2024-10-09 06:37:35 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.8/specs/altair/validator.md#signature
|
2021-12-09 12:56:54 +00:00
|
|
|
func compute_contribution_and_proof_signing_root*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
|
|
|
msg: ContributionAndProof): Eth2Digest =
|
|
|
|
let domain = get_domain(fork, DOMAIN_CONTRIBUTION_AND_PROOF,
|
|
|
|
msg.contribution.slot.epoch,
|
|
|
|
genesis_validators_root)
|
|
|
|
compute_signing_root(msg, domain)
|
|
|
|
|
|
|
|
proc get_contribution_and_proof_signature*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
|
|
|
msg: ContributionAndProof,
|
|
|
|
privkey: ValidatorPrivKey): CookedSig =
|
|
|
|
let signing_root = compute_contribution_and_proof_signing_root(
|
|
|
|
fork, genesis_validators_root, msg)
|
|
|
|
|
|
|
|
blsSign(privkey, signing_root.data)
|
|
|
|
|
2024-10-09 06:37:35 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.8/specs/altair/validator.md#aggregation-selection
|
2022-05-17 13:56:19 +00:00
|
|
|
func is_sync_committee_aggregator*(signature: ValidatorSig): bool =
|
2021-08-17 08:07:17 +00:00
|
|
|
let
|
|
|
|
signatureDigest = eth2digest(signature.blob)
|
|
|
|
modulo = max(1'u64, (SYNC_COMMITTEE_SIZE div SYNC_COMMITTEE_SUBNET_COUNT) div TARGET_AGGREGATORS_PER_SYNC_SUBCOMMITTEE)
|
|
|
|
bytes_to_uint64(signatureDigest.data.toOpenArray(0, 7)) mod modulo == 0
|
|
|
|
|
2021-12-09 12:56:54 +00:00
|
|
|
proc verify_contribution_and_proof_signature*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
2021-08-17 08:07:17 +00:00
|
|
|
msg: ContributionAndProof,
|
2021-12-09 12:56:54 +00:00
|
|
|
pubkey: ValidatorPubKey | CookedPubKey, signature: SomeSig): bool =
|
|
|
|
let signing_root = compute_contribution_and_proof_signing_root(
|
|
|
|
fork, genesis_validators_root, msg)
|
2021-08-17 08:07:17 +00:00
|
|
|
|
2021-12-09 12:56:54 +00:00
|
|
|
blsVerify(pubkey, signing_root.data, signature)
|
2022-08-01 06:41:47 +00:00
|
|
|
|
2024-01-22 07:36:46 +00:00
|
|
|
# https://github.com/ethereum/builder-specs/blob/v0.4.0/specs/bellatrix/builder.md#signing
|
2023-05-14 22:56:14 +00:00
|
|
|
func compute_builder_signing_root(
|
2023-02-21 13:21:38 +00:00
|
|
|
fork: Fork,
|
2024-11-13 03:29:14 +00:00
|
|
|
msg: deneb_mev.BuilderBid | electra_mev.BuilderBid | fulu_mev.BuilderBid |
|
2024-04-09 10:04:33 +00:00
|
|
|
ValidatorRegistrationV1): Eth2Digest =
|
2022-08-01 06:41:47 +00:00
|
|
|
# Uses genesis fork version regardless
|
|
|
|
doAssert fork.current_version == fork.previous_version
|
|
|
|
|
|
|
|
let domain = get_domain(
|
|
|
|
fork, DOMAIN_APPLICATION_BUILDER, GENESIS_EPOCH, ZERO_HASH)
|
|
|
|
compute_signing_root(msg, domain)
|
|
|
|
|
|
|
|
proc get_builder_signature*(
|
|
|
|
fork: Fork, msg: ValidatorRegistrationV1, privkey: ValidatorPrivKey):
|
|
|
|
CookedSig =
|
|
|
|
let signing_root = compute_builder_signing_root(fork, msg)
|
|
|
|
blsSign(privkey, signing_root.data)
|
|
|
|
|
|
|
|
proc verify_builder_signature*(
|
2024-11-13 03:29:14 +00:00
|
|
|
fork: Fork, msg: deneb_mev.BuilderBid | electra_mev.BuilderBid |
|
|
|
|
fulu_mev.BuilderBid,
|
2022-08-01 06:41:47 +00:00
|
|
|
pubkey: ValidatorPubKey | CookedPubKey, signature: SomeSig): bool =
|
|
|
|
let signing_root = compute_builder_signing_root(fork, msg)
|
|
|
|
blsVerify(pubkey, signing_root.data, signature)
|
2022-10-27 06:29:24 +00:00
|
|
|
|
2024-06-16 00:59:25 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.3/specs/capella/beacon-chain.md#new-process_bls_to_execution_change
|
2023-02-17 13:35:12 +00:00
|
|
|
func compute_bls_to_execution_change_signing_root*(
|
2023-01-14 04:20:51 +00:00
|
|
|
genesisFork: Fork, genesis_validators_root: Eth2Digest,
|
2023-02-03 15:28:28 +00:00
|
|
|
msg: BLSToExecutionChange): Eth2Digest =
|
|
|
|
# So the epoch doesn't matter when calling get_domain
|
|
|
|
doAssert genesisFork.previous_version == genesisFork.current_version
|
|
|
|
|
2023-02-17 13:35:12 +00:00
|
|
|
# Fork-agnostic domain since address changes are valid across forks
|
2022-10-27 06:29:24 +00:00
|
|
|
let domain = get_domain(
|
2023-02-03 15:28:28 +00:00
|
|
|
genesisFork, DOMAIN_BLS_TO_EXECUTION_CHANGE, GENESIS_EPOCH,
|
2023-01-14 04:20:51 +00:00
|
|
|
genesis_validators_root)
|
2022-10-27 06:29:24 +00:00
|
|
|
compute_signing_root(msg, domain)
|
|
|
|
|
2023-02-03 15:28:28 +00:00
|
|
|
proc get_bls_to_execution_change_signature*(
|
|
|
|
genesisFork: Fork, genesis_validators_root: Eth2Digest,
|
|
|
|
msg: BLSToExecutionChange, privkey: ValidatorPrivKey):
|
|
|
|
CookedSig =
|
|
|
|
let signing_root = compute_bls_to_execution_change_signing_root(
|
|
|
|
genesisFork, genesis_validators_root, msg)
|
|
|
|
blsSign(privkey, signing_root.data)
|
|
|
|
|
2022-10-27 06:29:24 +00:00
|
|
|
proc verify_bls_to_execution_change_signature*(
|
2023-02-03 15:28:28 +00:00
|
|
|
genesisFork: Fork, genesis_validators_root: Eth2Digest,
|
2022-10-27 06:29:24 +00:00
|
|
|
msg: SignedBLSToExecutionChange,
|
|
|
|
pubkey: ValidatorPubKey | CookedPubKey, signature: SomeSig): bool =
|
|
|
|
let signing_root = compute_bls_to_execution_change_signing_root(
|
2023-02-03 15:28:28 +00:00
|
|
|
genesisFork, genesis_validators_root, msg.message)
|
2022-10-27 06:29:24 +00:00
|
|
|
blsVerify(pubkey, signing_root.data, signature)
|