ab_proof_of_space/
lib.rs

1//! Proof of space implementation
2#![cfg_attr(not(feature = "std"), 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_convert,
8    const_trait_impl,
9    exact_size_is_empty,
10    generic_const_exprs,
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
24use ab_core_primitives::pos::{PosProof, PosSeed};
25use ab_core_primitives::solutions::SolutionPotVerifier;
26#[cfg(feature = "alloc")]
27use core::fmt;
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#[cfg(feature = "alloc")]
40pub trait TableGenerator<T: Table>: fmt::Debug + Default + Clone + Send + Sized + 'static {
41    /// Generate new table with 32 bytes seed.
42    ///
43    /// There is also [`Self::generate_parallel()`] that can achieve lower latency.
44    fn generate(&mut self, seed: &PosSeed) -> T;
45
46    /// Generate new table with 32 bytes seed using parallelism.
47    ///
48    /// This implementation will trade efficiency of CPU and memory usage for lower latency, prefer
49    /// [`Self::generate()`] unless lower latency is critical.
50    #[cfg(any(feature = "parallel", test))]
51    fn generate_parallel(&mut self, seed: &PosSeed) -> T {
52        self.generate(seed)
53    }
54}
55
56/// Proof of space kind
57pub trait Table: SolutionPotVerifier + Sized + Send + Sync + 'static {
58    /// Proof of space table type
59    const TABLE_TYPE: PosTableType;
60    /// Instance that can be used to generate tables with better performance
61    #[cfg(feature = "alloc")]
62    type Generator: TableGenerator<Self>;
63
64    /// Generate new table with 32 bytes seed.
65    ///
66    /// There is also [`Self::generate_parallel()`] that can achieve lower latency.
67    #[cfg(feature = "alloc")]
68    fn generate(seed: &PosSeed) -> Self;
69
70    /// Generate new table with 32 bytes seed using parallelism.
71    ///
72    /// This implementation will trade efficiency of CPU and memory usage for lower latency, prefer
73    /// [`Self::generate()`] unless lower latency is critical.
74    #[cfg(all(feature = "alloc", any(feature = "parallel", test)))]
75    fn generate_parallel(seed: &PosSeed) -> Self {
76        Self::generate(seed)
77    }
78
79    /// Try to find proof at `challenge_index` if it exists
80    #[cfg(feature = "alloc")]
81    fn find_proof(&self, challenge_index: u32) -> Option<PosProof>;
82
83    /// Check whether proof created earlier is valid
84    fn is_proof_valid(seed: &PosSeed, challenge_index: u32, proof: &PosProof) -> bool;
85
86    /// Returns a stateful table generator with better performance
87    #[cfg(feature = "alloc")]
88    fn generator() -> Self::Generator {
89        Self::Generator::default()
90    }
91}