Skip to main content

ab_proof_of_space/
lib.rs

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