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_chunks,
7    array_windows,
8    generic_const_exprs,
9    portable_simd,
10    step_trait
11)]
12
13pub mod chia;
14pub mod chiapos;
15pub mod shim;
16
17use ab_core_primitives::pos::{PosProof, PosSeed};
18use ab_core_primitives::solutions::SolutionPotVerifier;
19#[cfg(feature = "alloc")]
20use core::fmt;
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#[cfg(feature = "alloc")]
33pub trait TableGenerator<T: Table>: fmt::Debug + Default + Clone + Send + Sized + 'static {
34    /// Generate new table with 32 bytes seed.
35    ///
36    /// There is also [`Self::generate_parallel()`] that can achieve lower latency.
37    fn generate(&mut self, seed: &PosSeed) -> T;
38
39    /// Generate new table with 32 bytes seed using parallelism.
40    ///
41    /// This implementation will trade efficiency of CPU and memory usage for lower latency, prefer
42    /// [`Self::generate()`] unless lower latency is critical.
43    #[cfg(any(feature = "parallel", test))]
44    fn generate_parallel(&mut self, seed: &PosSeed) -> T {
45        self.generate(seed)
46    }
47}
48
49/// Proof of space kind
50pub trait Table: SolutionPotVerifier + Sized + Send + Sync + 'static {
51    /// Proof of space table type
52    const TABLE_TYPE: PosTableType;
53    /// Instance that can be used to generate tables with better performance
54    #[cfg(feature = "alloc")]
55    type Generator: TableGenerator<Self>;
56
57    /// Generate new table with 32 bytes seed.
58    ///
59    /// There is also [`Self::generate_parallel()`] that can achieve lower latency.
60    #[cfg(feature = "alloc")]
61    fn generate(seed: &PosSeed) -> Self;
62
63    /// Generate new table with 32 bytes seed using parallelism.
64    ///
65    /// This implementation will trade efficiency of CPU and memory usage for lower latency, prefer
66    /// [`Self::generate()`] unless lower latency is critical.
67    #[cfg(all(feature = "alloc", any(feature = "parallel", test)))]
68    fn generate_parallel(seed: &PosSeed) -> Self {
69        Self::generate(seed)
70    }
71
72    /// Try to find proof at `challenge_index` if it exists
73    #[cfg(feature = "alloc")]
74    fn find_proof(&self, challenge_index: u32) -> Option<PosProof>;
75
76    /// Check whether proof created earlier is valid
77    fn is_proof_valid(seed: &PosSeed, challenge_index: u32, proof: &PosProof) -> bool;
78
79    /// Returns a stateful table generator with better performance
80    #[cfg(feature = "alloc")]
81    fn generator() -> Self::Generator {
82        Self::Generator::default()
83    }
84}