ab_client_proof_of_time/
lib.rs

1//! Client-side proof of time implementation.
2
3pub mod source;
4pub mod verifier;
5
6use ab_core_primitives::pot::{PotOutput, PotParametersChange, PotSeed, SlotNumber};
7use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
8use std::num::NonZeroU32;
9
10/// Next slot input for proof of time evaluation
11#[derive(Debug, Copy, Clone, PartialEq, Eq, Decode, Encode, MaxEncodedLen)]
12pub struct PotNextSlotInput {
13    /// Slot number
14    pub slot: SlotNumber,
15    /// Slot iterations for this slot
16    pub slot_iterations: NonZeroU32,
17    /// Seed for this slot
18    pub seed: PotSeed,
19}
20
21impl PotNextSlotInput {
22    /// Derive next slot input while taking parameters change into account.
23    ///
24    /// NOTE: `base_slot_iterations` doesn't have to be parent block, just something that is after
25    /// prior parameters change (if any) took effect, in most cases this value corresponds to parent
26    /// block's slot.
27    pub fn derive(
28        base_slot_iterations: NonZeroU32,
29        parent_slot: SlotNumber,
30        parent_output: PotOutput,
31        pot_parameters_change: &Option<PotParametersChange>,
32    ) -> Self {
33        let next_slot = parent_slot + SlotNumber::ONE;
34        let slot_iterations;
35        let seed;
36
37        // The change to number of iterations might have happened before `next_slot`
38        if let Some(parameters_change) = pot_parameters_change
39            && parameters_change.slot <= next_slot
40        {
41            slot_iterations = parameters_change.slot_iterations;
42            // Only if entropy injection happens exactly on next slot we need to mix it in
43            if parameters_change.slot == next_slot {
44                seed = parent_output.seed_with_entropy(&parameters_change.entropy);
45            } else {
46                seed = parent_output.seed();
47            }
48        } else {
49            slot_iterations = base_slot_iterations;
50            seed = parent_output.seed();
51        }
52
53        Self {
54            slot: next_slot,
55            slot_iterations,
56            seed,
57        }
58    }
59}