//! Unordered current proofs with fixed-size or runtime-sized bitmap chunks. use crate::{ merkle::Graftable, qmdb::{ any::unordered::{Operation, Update}, current::proof::operation::Proof as OperationProof, operation::Key, value::ValueEncoding, }, }; use bytes::BufMut; use commonware_codec::{Buf, Encode, EncodeSize, Read, Write}; use commonware_cryptography::{Digest, Hasher}; /// Proofs with fixed-size bitmap chunks. pub mod constant { /// Proof information for verifying a key has a particular value in the database. pub type KeyValueProof = super::KeyValueProof; } /// Proofs with runtime-sized bitmap chunks. pub mod dynamic { /// Proof information for verifying a key has a particular value in the database. pub type KeyValueProof = super::KeyValueProof; } /// Proof information for verifying a key has a particular value in the database. /// /// `C` stores the embedded operation proof's bitmap chunk. Verify using [Self::verify]. #[derive(Clone, Eq, PartialEq, Debug)] pub struct KeyValueProof { /// The proof authenticating the active update operation. pub proof: OperationProof, } impl> KeyValueProof { /// Return true if the proof authenticates that `key` currently has `value` in the database /// with the provided `root`. /// /// `V` selects the fixed or variable value encoding used by the database. /// /// # Examples /// /// ``` /// use commonware_codec::{Decode, Input}; /// use commonware_cryptography::{Sha256, sha256::Digest}; /// use commonware_storage::{ /// merkle::mmr, /// qmdb::{current::unordered::proof::dynamic::KeyValueProof, value::FixedEncoding}, /// }; /// /// fn verify( /// encoded: impl Input, /// chunk_size: usize, /// max_digests: usize, /// key: Digest, /// value: Digest, /// root: &Digest, /// ) -> bool { /// let Ok(proof) = KeyValueProof::::decode_cfg( /// encoded, /// &(chunk_size, max_digests), /// ) else { /// return false; /// }; /// proof.verify::, _>(key, value, root) /// } /// ``` pub fn verify(&self, key: K, value: V::Value, root: &D) -> bool where H: Hasher, V: ValueEncoding, K: Key, Operation: Encode, { let op = Operation::::Update(Update(key, value)); self.proof.verify::(op, root) } } impl> Write for KeyValueProof { fn write(&self, buf: &mut impl BufMut) { self.proof.write(buf); } } impl> EncodeSize for KeyValueProof { fn encode_size(&self) -> usize { self.proof.encode_size() } } impl Read for KeyValueProof where OperationProof: Read, { /// The read configuration for the embedded operation proof. type Cfg = as Read>::Cfg; fn read_cfg(buf: &mut impl Buf, cfg: &Self::Cfg) -> Result { let proof = OperationProof::::read_cfg(buf, cfg)?; Ok(Self { proof }) } } #[cfg(feature = "arbitrary")] impl arbitrary::Arbitrary<'_> for KeyValueProof where OperationProof: for<'a> arbitrary::Arbitrary<'a>, { fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result { Ok(Self { proof: u.arbitrary()?, }) } }