subspace_proof_of_space/
lib.rs

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