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