subspace_proof_of_space/
chiapos.rs

1//! Chia proof of space reimplementation in Rust
2
3mod constants;
4mod table;
5mod tables;
6mod utils;
7
8#[cfg(feature = "alloc")]
9pub use crate::chiapos::table::TablesCache;
10use crate::chiapos::table::{metadata_size_bytes, num_buckets};
11use crate::chiapos::tables::TablesGeneric;
12use crate::chiapos::utils::EvaluatableUsize;
13
14type Seed = [u8; 32];
15#[cfg(any(feature = "full-chiapos", test))]
16type Challenge = [u8; 32];
17#[cfg(any(feature = "full-chiapos", test))]
18type Quality = [u8; 32];
19
20/// Collection of Chia tables
21#[derive(Debug)]
22pub struct Tables<const K: u8>(TablesGeneric<K>)
23where
24    EvaluatableUsize<{ metadata_size_bytes(K, 7) }>: Sized,
25    [(); 1 << K]:,
26    [(); num_buckets(K)]:,
27    [(); num_buckets(K) - 1]:;
28
29macro_rules! impl_any {
30    ($($k: expr$(,)? )*) => {
31        $(
32impl Tables<$k> {
33    /// Create Chia proof of space tables. There also exists [`Self::create_parallel()`] that trades
34    /// memory usage for lower latency and higher CPU efficiency.
35    #[cfg(feature = "alloc")]
36    pub fn create(seed: Seed, cache: &TablesCache) -> Self {
37        Self(TablesGeneric::<$k>::create(
38            seed, cache,
39        ))
40    }
41
42    /// Almost the same as [`Self::create()`], but uses parallelism internally for better
43    /// latency and performance (though higher memory usage).
44    #[cfg(feature = "parallel")]
45    pub fn create_parallel(seed: Seed, cache: &TablesCache) -> Self {
46        Self(TablesGeneric::<$k>::create_parallel(
47            seed, cache,
48        ))
49    }
50
51    /// Find proof of space quality for a given challenge.
52    #[cfg(all(feature = "alloc", any(feature = "full-chiapos", test)))]
53    pub fn find_quality<'a>(
54        &'a self,
55        challenge: &'a Challenge,
56    ) -> impl Iterator<Item = Quality> + 'a {
57        self.0.find_quality(challenge)
58    }
59
60    /// Find proof of space for a given challenge.
61    #[cfg(feature = "alloc")]
62    pub fn find_proof<'a>(
63        &'a self,
64        first_challenge_bytes: [u8; 4],
65    ) -> impl Iterator<Item = [u8; 64 * $k / 8]> + 'a {
66        self.0.find_proof(first_challenge_bytes)
67    }
68
69    /// Verify proof of space for a given seed and challenge
70    pub fn verify_only(
71        seed: &Seed,
72        first_challenge_bytes: [u8; 4],
73        proof_of_space: &[u8; 64 * $k as usize / 8],
74    ) -> bool {
75        TablesGeneric::<$k>::verify_only(seed, first_challenge_bytes, proof_of_space)
76    }
77
78    /// Verify proof of space for a given seed and challenge.
79    ///
80    /// Similar to [`Self::verify_only()`], but also returns quality on successful verification.
81    #[cfg(any(feature = "full-chiapos", test))]
82    pub fn verify(
83        seed: &Seed,
84        challenge: &Challenge,
85        proof_of_space: &[u8; 64 * $k as usize / 8],
86    ) -> Option<Quality> {
87        TablesGeneric::<$k>::verify(seed, challenge, proof_of_space)
88    }
89}
90        )*
91    }
92}
93
94// Only these k values are supported by the current implementation
95#[cfg(feature = "full-chiapos")]
96impl_any!(15, 16, 18, 19, 21, 22, 23, 24, 25);
97#[cfg(any(feature = "full-chiapos", test))]
98impl_any!(17);
99impl_any!(20);