ab_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(
6    array_windows,
7    const_convert,
8    const_trait_impl,
9    exact_size_is_empty,
10    float_erf,
11    generic_const_exprs,
12    get_mut_unchecked,
13    iter_array_chunks,
14    maybe_uninit_fill,
15    maybe_uninit_slice,
16    maybe_uninit_write_slice,
17    portable_simd,
18    ptr_as_ref_unchecked,
19    ptr_as_uninit,
20    step_trait,
21    sync_unsafe_cell,
22    vec_into_raw_parts
23)]
24
25pub mod chia;
26pub mod chiapos;
27pub mod shim;
28
29#[cfg(feature = "alloc")]
30extern crate alloc;
31
32#[cfg(feature = "alloc")]
33use ab_core_primitives::pieces::Record;
34use ab_core_primitives::pos::{PosProof, PosSeed};
35use ab_core_primitives::sectors::SBucket;
36use ab_core_primitives::solutions::SolutionPotVerifier;
37#[cfg(feature = "alloc")]
38use alloc::boxed::Box;
39#[cfg(feature = "alloc")]
40use core::fmt;
41
42/// Proof of space table type
43#[derive(Debug, Clone, Copy)]
44pub enum PosTableType {
45    /// Chia table
46    Chia,
47    /// Shim table
48    Shim,
49}
50
51// TODO: Return a single full proof and the rest as hashes instead to optimize memory usage and
52//  parallelize compute more easily
53/// Proof-of-space proofs
54#[derive(Debug)]
55#[cfg(feature = "alloc")]
56#[repr(C)]
57pub struct PosProofs {
58    /// S-buckets at which proofs were found.
59    ///
60    /// S-buckets are grouped by 8, within each `u8` bits right to left (LSB) indicate the presence
61    /// of a proof for corresponding s-bucket, so that the whole array of bytes can be thought as a
62    /// large set of bits.
63    ///
64    /// There will be at most [`Record::NUM_CHUNKS`] proofs produced/bits set to `1`.
65    pub found_proofs: [u8; Record::NUM_S_BUCKETS / u8::BITS as usize],
66    /// [`Record::NUM_CHUNKS`] proofs, corresponding to set bits of `found_proofs`.
67    pub proofs: [PosProof; Record::NUM_CHUNKS],
68}
69
70// TODO: A method that returns hashed proofs (with SIMD) for all s-buckets for plotting
71#[cfg(feature = "alloc")]
72impl PosProofs {
73    /// Get proof for specified s-bucket (if exists).
74    ///
75    /// Note that this is not the most efficient API possible, so prefer using the `proofs` field
76    /// directly if the use case allows.
77    #[inline]
78    pub fn for_s_bucket(&self, s_bucket: SBucket) -> Option<PosProof> {
79        let proof_index = Self::proof_index_for_s_bucket(self.found_proofs, s_bucket)?;
80
81        Some(self.proofs[proof_index])
82    }
83
84    #[inline(always)]
85    fn proof_index_for_s_bucket(
86        found_proofs: [u8; Record::NUM_S_BUCKETS / u8::BITS as usize],
87        s_bucket: SBucket,
88    ) -> Option<usize> {
89        let bits_offset = usize::from(s_bucket);
90        let found_proofs_byte_offset = bits_offset / u8::BITS as usize;
91        let found_proofs_bit_offset = bits_offset as u32 % u8::BITS;
92        let (found_proofs_before, found_proofs_after) =
93            found_proofs.split_at(found_proofs_byte_offset);
94        if (found_proofs_after[0] & (1 << found_proofs_bit_offset)) == 0 {
95            return None;
96        }
97        let proof_index = found_proofs_before
98            .iter()
99            .map(|&bits| bits.count_ones())
100            .sum::<u32>()
101            + found_proofs_after[0]
102                .unbounded_shl(u8::BITS - found_proofs_bit_offset)
103                .count_ones();
104
105        Some(proof_index as usize)
106    }
107}
108
109// TODO: Think about redesigning this API now that proofs are the output rather than tables
110/// Stateful table generator with better performance.
111///
112/// Prefer cloning it over creating multiple separate generators.
113#[cfg(feature = "alloc")]
114pub trait TableGenerator<T: Table>:
115    fmt::Debug + Default + Clone + Send + Sync + Sized + 'static
116{
117    /// Create proofs with 32 bytes seed.
118    ///
119    /// There is also `Self::create_proofs_parallel()` that can achieve higher performance and
120    /// lower latency at the cost of lower CPU efficiency and higher memory usage.
121    fn create_proofs(&self, seed: &PosSeed) -> Box<PosProofs>;
122
123    /// Almost the same as [`Self::create_proofs()`], but uses parallelism internally for better
124    /// performance and lower latency at the cost of lower CPU efficiency and higher memory usage
125    #[cfg(feature = "parallel")]
126    fn create_proofs_parallel(&self, seed: &PosSeed) -> Box<PosProofs> {
127        self.create_proofs(seed)
128    }
129}
130
131/// Proof of space kind
132pub trait Table: SolutionPotVerifier + Sized + Send + Sync + 'static {
133    /// Proof of space table type
134    const TABLE_TYPE: PosTableType;
135    /// Instance that can be used to generate tables with better performance
136    #[cfg(feature = "alloc")]
137    type Generator: TableGenerator<Self>;
138
139    /// Check whether proof created earlier is valid
140    fn is_proof_valid(seed: &PosSeed, s_bucket: SBucket, proof: &PosProof) -> bool;
141
142    /// Returns a stateful table generator with better performance
143    #[cfg(feature = "alloc")]
144    fn generator() -> Self::Generator {
145        Self::Generator::default()
146    }
147}