qualia_core_db/bundle/stream/proof/
codec.rs1use super::{ProofError, MAX_SIBLINGS};
2use crate::net::qdnf::types::StrongDigest;
3
4pub fn encode_proof(siblings: &[StrongDigest], out: &mut [u8]) -> Result<usize, ProofError> {
5 if siblings.len() > MAX_SIBLINGS {
6 return Err(ProofError::ProofLength);
7 }
8 let length = 1 + siblings.len() * 48;
9 if out.len() < length {
10 return Err(ProofError::Capacity);
11 }
12 out[0] = siblings.len() as u8;
13 for (chunk, digest) in out[1..length].chunks_exact_mut(48).zip(siblings) {
14 chunk.copy_from_slice(digest.as_bytes());
15 }
16 Ok(length)
17}
18pub fn decode_proof<'a>(
19 encoded: &[u8],
20 out: &'a mut [StrongDigest],
21) -> Result<&'a [StrongDigest], ProofError> {
22 let count = *encoded.first().ok_or(ProofError::ProofLength)? as usize;
23 if count > MAX_SIBLINGS || encoded.len() != 1 + count * 48 {
24 return Err(ProofError::ProofLength);
25 }
26 if out.len() < count {
27 return Err(ProofError::Capacity);
28 }
29 for (slot, bytes) in out[..count].iter_mut().zip(encoded[1..].chunks_exact(48)) {
30 slot.0.copy_from_slice(bytes);
31 }
32 Ok(&out[..count])
33}