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