chia_protocol/
partial_proof.rs

1use crate::Bytes32;
2use chia_sha2::Sha256;
3use chia_streamable_macro::streamable;
4
5#[cfg(feature = "py-bindings")]
6use pyo3::pymethods;
7
8#[streamable]
9pub struct PartialProof {
10    #[cfg_attr(feature = "serde", serde(with = "serde_arrays"))]
11    fragments: [u64; 16],
12}
13
14impl PartialProof {
15    pub fn get_string(&self, strength: u8) -> Bytes32 {
16        let mut sha256 = Sha256::new();
17        sha256.update(serialize_quality(&self.fragments, strength));
18        sha256.finalize().into()
19    }
20}
21
22const NUM_CHAIN_LINKS: usize = 16;
23
24/// out must point to exactly 129 bytes
25/// serializes the QualityProof into the form that will be hashed together with
26/// the challenge to determine the quality of ths proof. The quality is used to
27/// check if it passes the current difficulty. The format is:
28/// 1 byte: plot strength
29/// repeat 16 times:
30///   8 bytes: little-endian proof fragment
31fn 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}