1#![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 inherent_associated_types,
12 macroless_generic_const_args,
13 min_generic_const_args,
14 step_trait
15)]
16#![cfg_attr(test, feature(float_erf))]
17#![cfg_attr(feature = "parallel", feature(exact_size_is_empty, sync_unsafe_cell))]
18#![cfg_attr(
19 feature = "alloc",
20 feature(iter_array_chunks, maybe_uninit_fill, ptr_as_uninit)
21)]
22#![cfg_attr(any(feature = "alloc", test), feature(portable_simd))]
23
24pub mod chia;
25pub mod chiapos;
26pub mod shim;
27
28#[cfg(feature = "alloc")]
29extern crate alloc;
30
31#[cfg(feature = "alloc")]
32use ab_core_primitives::pieces::Record;
33use ab_core_primitives::pos::{PosProof, PosSeed};
34use ab_core_primitives::sectors::SBucket;
35use ab_core_primitives::solutions::SolutionPotVerifier;
36#[cfg(feature = "alloc")]
37use alloc::boxed::Box;
38#[cfg(feature = "alloc")]
39use core::fmt;
40
41#[derive(Debug, Clone, Copy)]
43pub enum PosTableType {
44 Chia,
46 Shim,
48}
49
50#[derive(Debug)]
54#[cfg(feature = "alloc")]
55#[repr(C)]
56pub struct PosProofs {
57 pub found_proofs: [u8; Record::NUM_S_BUCKETS / u8::BITS as usize],
65 pub proofs: [PosProof; const { Record::NUM_CHUNKS }],
67}
68
69#[cfg(feature = "alloc")]
71impl PosProofs {
72 #[inline]
77 pub fn for_s_bucket(&self, s_bucket: SBucket) -> Option<PosProof> {
78 let proof_index = Self::proof_index_for_s_bucket(&self.found_proofs, s_bucket)?;
79
80 Some(self.proofs[proof_index])
81 }
82
83 #[inline(always)]
84 fn proof_index_for_s_bucket(
85 found_proofs: &[u8; Record::NUM_S_BUCKETS / u8::BITS as usize],
86 s_bucket: SBucket,
87 ) -> Option<usize> {
88 let bits_offset = usize::from(s_bucket);
89 let found_proofs_byte_offset = bits_offset / u8::BITS as usize;
90 let found_proofs_bit_offset = bits_offset as u32 % u8::BITS;
91 let (found_proofs_before, found_proofs_after) =
92 found_proofs.split_at(found_proofs_byte_offset);
93 if (found_proofs_after[0] & (1 << found_proofs_bit_offset)) == 0 {
94 return None;
95 }
96 let proof_index = found_proofs_before
97 .iter()
98 .map(|&bits| bits.count_ones())
99 .sum::<u32>()
100 + found_proofs_after[0]
101 .unbounded_shl(u8::BITS - found_proofs_bit_offset)
102 .count_ones();
103
104 Some(proof_index as usize)
105 }
106}
107
108#[cfg(feature = "alloc")]
113pub trait TableGenerator<T: Table>:
114 fmt::Debug + Default + Clone + Send + Sync + Sized + 'static
115{
116 fn create_proofs(&self, seed: &PosSeed) -> Box<PosProofs>;
121
122 #[cfg(feature = "parallel")]
125 fn create_proofs_parallel(&self, seed: &PosSeed) -> Box<PosProofs> {
126 self.create_proofs(seed)
127 }
128}
129
130pub trait Table: SolutionPotVerifier + Sized + Send + Sync + 'static {
132 const TABLE_TYPE: PosTableType;
134 #[cfg(feature = "alloc")]
136 type Generator: TableGenerator<Self>;
137
138 fn is_proof_valid(seed: &PosSeed, s_bucket: SBucket, proof: &PosProof) -> bool;
140
141 #[cfg(feature = "alloc")]
143 fn generator() -> Self::Generator {
144 Self::Generator::default()
145 }
146}