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 gca_const_items,
10 gca_macroless_args,
11 gca_min_const_items,
12 generic_const_items,
13 impl_restriction,
14 inherent_associated_types,
15 mut_restriction,
16 portable_simd,
17 step_trait
18)]
19#![cfg_attr(all(test, feature = "alloc"), feature(float_erf))]
20#![cfg_attr(feature = "parallel", feature(exact_size_is_empty, sync_unsafe_cell))]
21#![cfg_attr(feature = "alloc", feature(maybe_uninit_fill, ptr_as_uninit))]
22
23pub mod chia;
24pub mod chiapos;
25pub mod shim;
26
27#[cfg(feature = "alloc")]
28extern crate alloc;
29
30#[cfg(feature = "alloc")]
31use ab_core_primitives::pieces::Record;
32use ab_core_primitives::pos::{PosProof, PosSeed};
33use ab_core_primitives::sectors::SBucket;
34use ab_core_primitives::solutions::SolutionPotVerifier;
35#[cfg(feature = "alloc")]
36use alloc::boxed::Box;
37#[cfg(feature = "alloc")]
38use core::fmt;
39#[cfg(feature = "alloc")]
40use core::hint;
41
42#[derive(Debug, Clone, Copy)]
44pub enum PosTableType {
45 Chia,
47 Shim,
49}
50
51#[derive(Debug)]
55#[cfg(feature = "alloc")]
56#[repr(C)]
57pub struct PosProofs {
58 pub mut(self) found_proofs: [u8; Record::NUM_S_BUCKETS / u8::BITS as usize],
69 pub mut(self) proofs: [PosProof; const { Record::NUM_CHUNKS }],
71}
72
73#[cfg(feature = "alloc")]
75impl PosProofs {
76 pub const PLACEHOLDER_PROOF: [u8; PosProof::SIZE] = [0; PosProof::SIZE];
82
83 #[inline]
88 #[cfg_attr(feature = "no-panic", no_panic::no_panic)]
89 pub fn for_s_bucket(&self, s_bucket: SBucket) -> Option<PosProof> {
90 let proof_index = Self::proof_index_for_s_bucket(&self.found_proofs, s_bucket)?;
91
92 unsafe {
94 hint::assert_unchecked(proof_index < Record::NUM_CHUNKS);
95 }
96
97 Some(self.proofs[proof_index])
98 }
99
100 #[inline(always)]
101 #[cfg_attr(feature = "no-panic", no_panic::no_panic)]
102 fn proof_index_for_s_bucket(
103 found_proofs: &[u8; Record::NUM_S_BUCKETS / u8::BITS as usize],
104 s_bucket: SBucket,
105 ) -> Option<usize> {
106 let bits_offset = usize::from(s_bucket);
107 let found_proofs_byte_offset = bits_offset / u8::BITS as usize;
108 let found_proofs_bit_offset = bits_offset as u32 % u8::BITS;
109 let (found_proofs_before, found_proofs_after) =
110 found_proofs.split_at(found_proofs_byte_offset);
111 if (found_proofs_after[0] & (1 << found_proofs_bit_offset)) == 0 {
112 return None;
113 }
114 let proof_index = found_proofs_before
115 .iter()
116 .map(|&bits| bits.count_ones())
117 .sum::<u32>()
118 + found_proofs_after[0]
119 .unbounded_shl(u8::BITS - found_proofs_bit_offset)
120 .count_ones();
121
122 Some(proof_index as usize)
123 }
124}
125
126#[cfg(feature = "alloc")]
131pub trait TableGenerator<T: Table>:
132 fmt::Debug + Default + Clone + Send + Sync + Sized + 'static
133{
134 fn create_proofs(&self, seed: &PosSeed) -> Box<PosProofs>;
139
140 #[cfg(feature = "parallel")]
143 fn create_proofs_parallel(&self, seed: &PosSeed) -> Box<PosProofs> {
144 self.create_proofs(seed)
145 }
146}
147
148pub trait Table: SolutionPotVerifier + Sized + Send + Sync + 'static {
150 const TABLE_TYPE: PosTableType;
152 #[cfg(feature = "alloc")]
154 type Generator: TableGenerator<Self>;
155
156 fn is_proof_valid(seed: &PosSeed, s_bucket: SBucket, proof: &PosProof) -> bool;
158
159 #[cfg(feature = "alloc")]
161 fn generator() -> Self::Generator {
162 Self::Generator::default()
163 }
164}