Skip to main content

subspace_proof_of_space/
chiapos.rs

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