Skip to main content

ab_client_proof_of_time/
lib.rs

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