subspace_proof_of_space/
lib.rs

1//! Proof of space implementation
2#![no_std]
3#![expect(incomplete_features, reason = "generic_const_exprs")]
4#![warn(rust_2018_idioms, missing_debug_implementations, missing_docs)]
5#![feature(
6    array_windows,
7    const_trait_impl,
8    exact_size_is_empty,
9    generic_const_exprs,
10    get_mut_unchecked,
11    maybe_uninit_fill,
12    maybe_uninit_slice,
13    maybe_uninit_write_slice,
14    portable_simd,
15    step_trait,
16    sync_unsafe_cell,
17    vec_into_raw_parts
18)]
19
20pub mod chia;
21pub mod chiapos;
22pub mod shim;
23
24#[cfg(feature = "alloc")]
25extern crate alloc;
26
27#[cfg(feature = "alloc")]
28use core::fmt;
29use subspace_core_primitives::pos::{PosProof, PosSeed};
30use subspace_core_primitives::solutions::SolutionPotVerifier;
31
32/// Proof of space table type
33#[derive(Debug, Clone, Copy)]
34pub enum PosTableType {
35    /// Chia table
36    Chia,
37    /// Shim table
38    Shim,
39}
40
41/// Stateful table generator with better performance.
42///
43/// Prefer cloning it over creating multiple separate generators.
44#[cfg(feature = "alloc")]
45pub trait TableGenerator<T: Table>:
46    fmt::Debug + Default + Clone + Send + Sync + Sized + 'static
47{
48    /// Generate a new table with 32 bytes seed.
49    ///
50    /// There is also [`Self::generate_parallel()`] that can achieve lower latency.
51    fn generate(&self, seed: &PosSeed) -> T;
52
53    /// Generate a new table with 32 bytes seed using parallelism.
54    ///
55    /// This implementation will trade efficiency of CPU and memory usage for lower latency, prefer
56    /// [`Self::generate()`] unless lower latency is critical.
57    #[cfg(feature = "parallel")]
58    fn generate_parallel(&self, seed: &PosSeed) -> T {
59        self.generate(seed)
60    }
61}
62
63/// Proof of space kind
64pub trait Table: SolutionPotVerifier + Sized + Send + Sync + 'static {
65    /// Proof of space table type
66    const TABLE_TYPE: PosTableType;
67    /// Instance that can be used to generate tables with better performance
68    #[cfg(feature = "alloc")]
69    type Generator: TableGenerator<Self>;
70
71    /// Try to find proof at `challenge_index` if it exists
72    #[cfg(feature = "alloc")]
73    fn find_proof(&self, challenge_index: u32) -> Option<PosProof>;
74
75    /// Check whether proof created earlier is valid
76    fn is_proof_valid(seed: &PosSeed, challenge_index: u32, proof: &PosProof) -> bool;
77
78    /// Returns a stateful table generator with better performance
79    #[cfg(feature = "alloc")]
80    fn generator() -> Self::Generator {
81        Self::Generator::default()
82    }
83}