chia_protocol/
partial_proof.rs1use crate::Bytes32;
2use chia_sha2::Sha256;
3use chia_streamable_macro::streamable;
4
5#[cfg(feature = "py-bindings")]
6use pyo3::pymethods;
7
8const NUM_CHAIN_LINKS: usize = 16;
9
10#[streamable]
11pub struct PartialProof {
12 #[cfg_attr(feature = "serde", serde(with = "serde_arrays"))]
13 fragments: [u64; NUM_CHAIN_LINKS],
14}
15
16impl PartialProof {
17 pub fn get_string(&self, strength: u8) -> Bytes32 {
18 let mut sha256 = Sha256::new();
19 sha256.update(serialize_quality(&self.fragments, strength));
20 sha256.finalize().into()
21 }
22}
23
24fn serialize_quality(
32 fragments: &[u64; NUM_CHAIN_LINKS],
33 strength: u8,
34) -> [u8; NUM_CHAIN_LINKS * 8 + 1] {
35 let mut ret = [0_u8; 129];
36
37 ret[0] = strength;
38 let mut idx = 1;
39 for cl in fragments {
40 ret[idx..(idx + 8)].clone_from_slice(&cl.to_le_bytes());
41 idx += 8;
42 }
43 ret
44}
45
46#[cfg(feature = "py-bindings")]
47#[pymethods]
48impl PartialProof {
49 #[pyo3(name = "get_string")]
50 fn py_get_string(&self, strength: u8) -> Bytes32 {
51 self.get_string(strength)
52 }
53}