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