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