ab_proof_of_space/
chia.rs1#[cfg(feature = "alloc")]
4use crate::PosProofs;
5#[cfg(feature = "alloc")]
6use crate::TableGenerator;
7use crate::chiapos::Tables;
8#[cfg(feature = "alloc")]
9use crate::chiapos::TablesCache;
10use crate::{PosTableType, Table};
11use ab_core_primitives::pos::{PosProof, PosSeed};
12use ab_core_primitives::sectors::SBucket;
13#[cfg(feature = "alloc")]
14use alloc::boxed::Box;
15
16const K: u8 = PosProof::K;
17
18#[derive(Debug, Default, Clone)]
22#[cfg(feature = "alloc")]
23pub struct ChiaTableGenerator {
24 tables_cache: TablesCache,
25}
26
27#[cfg(feature = "alloc")]
28impl TableGenerator<ChiaTable> for ChiaTableGenerator {
29 fn create_proofs(&self, seed: &PosSeed) -> Box<PosProofs> {
30 Tables::<K>::create_proofs((*seed).into(), &self.tables_cache).into()
31 }
32
33 #[cfg(feature = "parallel")]
34 fn create_proofs_parallel(&self, seed: &PosSeed) -> Box<PosProofs> {
35 Tables::<K>::create_proofs_parallel((*seed).into(), &self.tables_cache).into()
36 }
37}
38
39#[derive(Debug)]
43pub struct ChiaTable;
44
45impl ab_core_primitives::solutions::SolutionPotVerifier for ChiaTable {
46 fn is_proof_valid(seed: &PosSeed, s_bucket: SBucket, proof: &PosProof) -> bool {
47 Tables::<K>::verify_only_raw(seed, u32::from(s_bucket), proof)
48 }
49}
50
51impl Table for ChiaTable {
52 const TABLE_TYPE: PosTableType = PosTableType::Chia;
53 #[cfg(feature = "alloc")]
54 type Generator = ChiaTableGenerator;
55
56 fn is_proof_valid(seed: &PosSeed, s_bucket: SBucket, proof: &PosProof) -> bool {
57 <Self as ab_core_primitives::solutions::SolutionPotVerifier>::is_proof_valid(
58 seed, s_bucket, proof,
59 )
60 }
61}
62
63#[cfg(all(feature = "alloc", test, not(miri)))]
64mod tests {
65 use super::*;
66 use ab_core_primitives::sectors::SBucket;
67
68 #[test]
69 fn basic() {
70 let seed = PosSeed::from([
71 35, 2, 52, 4, 51, 55, 23, 84, 91, 10, 111, 12, 13, 222, 151, 16, 228, 211, 254, 45, 92,
72 198, 204, 10, 9, 10, 11, 129, 139, 171, 15, 23,
73 ]);
74
75 let generator = ChiaTableGenerator::default();
76 let proofs = generator.create_proofs(&seed);
77 #[cfg(feature = "parallel")]
78 let proofs_parallel = generator.create_proofs_parallel(&seed);
79
80 let s_bucket_without_proof = SBucket::from(15651);
81 assert!(proofs.for_s_bucket(s_bucket_without_proof).is_none());
82 #[cfg(feature = "parallel")]
83 assert!(
84 proofs_parallel
85 .for_s_bucket(s_bucket_without_proof)
86 .is_none()
87 );
88
89 {
90 let s_bucket_with_proof = SBucket::from(31500);
91 let proof = proofs.for_s_bucket(s_bucket_with_proof).unwrap();
92 #[cfg(feature = "parallel")]
93 assert_eq!(
94 proof,
95 proofs_parallel.for_s_bucket(s_bucket_with_proof).unwrap()
96 );
97 assert!(ChiaTable::is_proof_valid(
98 &seed,
99 s_bucket_with_proof,
100 &proof
101 ));
102 }
103 }
104}