Skip to main content

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