2023-01-20 12:19:49 +00:00
|
|
|
# Nimbus
|
2024-02-28 17:31:45 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-01-20 12:19:49 +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.
|
|
|
|
|
|
|
|
#
|
|
|
|
# Example of how the beacon state historical_roots field could be provided with
|
2023-05-11 17:08:34 +00:00
|
|
|
# a Merkle proof that can be verified against the right beacon state root.
|
2023-01-20 12:19:49 +00:00
|
|
|
# These historical_roots with their proof could for example be provided over the
|
|
|
|
# network and verified on the receivers end.
|
|
|
|
#
|
2023-05-11 17:08:34 +00:00
|
|
|
# Note:
|
|
|
|
# Since Capella the historical_roots field is frozen. Thus providing the
|
|
|
|
# historical_roots with a Proof against the latest state seems a bit silly.
|
|
|
|
# One idea could be to embed it into the client just as is done for the
|
|
|
|
# execution header accumulator.
|
|
|
|
#
|
2023-01-20 12:19:49 +00:00
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2023-01-20 12:19:49 +00:00
|
|
|
|
2024-10-24 18:31:17 +00:00
|
|
|
import results, beacon_chain/spec/forks
|
2023-01-20 12:19:49 +00:00
|
|
|
|
|
|
|
export results
|
|
|
|
|
|
|
|
type
|
|
|
|
HistoricalRoots* = HashList[Eth2Digest, Limit HISTORICAL_ROOTS_LIMIT]
|
|
|
|
HistoricalRootsProof* = array[5, Digest]
|
|
|
|
HistoricalRootsWithProof* = object
|
|
|
|
historical_roots: HistoricalRoots
|
|
|
|
proof: HistoricalRootsProof
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
func buildProof*(state: ForkedHashedBeaconState): Result[HistoricalRootsProof, string] =
|
2023-01-20 12:19:49 +00:00
|
|
|
let gIndex = GeneralizedIndex(39) # 31 + 8 = 39
|
|
|
|
|
|
|
|
var proof: HistoricalRootsProof
|
|
|
|
withState(state):
|
2024-02-28 17:31:45 +00:00
|
|
|
?forkyState.data.build_proof(gIndex, proof)
|
2023-01-20 12:19:49 +00:00
|
|
|
|
|
|
|
ok(proof)
|
|
|
|
|
|
|
|
func verifyProof*(
|
2024-02-28 17:31:45 +00:00
|
|
|
historical_roots: HistoricalRoots, proof: HistoricalRootsProof, stateRoot: Digest
|
|
|
|
): bool =
|
2023-01-20 12:19:49 +00:00
|
|
|
let
|
|
|
|
gIndex = GeneralizedIndex(39)
|
|
|
|
leave = hash_tree_root(historical_roots)
|
|
|
|
|
|
|
|
verify_merkle_multiproof(@[leave], proof, @[gIndex], stateRoot)
|