Skip to content

Commit e9ebeb2

Browse files
committed
refactor(common): enhance 'MKMapProof' conversion to binary
Using 'bincode' crate.
1 parent d4db57a commit e9ebeb2

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

mithril-common/src/crypto_helper/codec/binary.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,23 +230,21 @@ mod binary_opcert {
230230
}
231231

232232
mod binary_mk_proof {
233-
use serde::{de::DeserializeOwned, Serialize};
233+
use serde::{Deserialize, Serialize};
234234

235-
use crate::crypto_helper::{key_decode_hex, key_encode_hex, MKMapKey, MKMapProof, MKProof};
235+
use crate::crypto_helper::{MKMapKey, MKMapProof, MKProof};
236236

237237
use super::*;
238238

239-
impl<T: MKMapKey + Serialize> TryToBytes for MKMapProof<T> {
239+
impl<T: MKMapKey + Serialize + for<'de> Deserialize<'de>> TryToBytes for MKMapProof<T> {
240240
fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
241-
// TODO: Use a more efficient serialization method
242-
Ok(key_encode_hex(self)?.into_bytes())
241+
self.to_bytes()
243242
}
244243
}
245244

246-
impl<T: MKMapKey + DeserializeOwned> TryFromBytes for MKMapProof<T> {
245+
impl<T: MKMapKey + Serialize + for<'de> Deserialize<'de>> TryFromBytes for MKMapProof<T> {
247246
fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
248-
// TODO: Use a more efficient deserialization method
249-
key_decode_hex(std::str::from_utf8(bytes)?).map_err(|e| e.into())
247+
Self::from_bytes(bytes)
250248
}
251249
}
252250

mithril-common/src/crypto_helper/merkle_map.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,22 @@ impl<K: MKMapKey> MKMapProof<K> {
351351
}
352352
}
353353

354+
impl<K: MKMapKey + Serialize + for<'de> Deserialize<'de>> MKMapProof<K> {
355+
/// Convert the proof to bytes
356+
pub fn to_bytes(&self) -> StdResult<Vec<u8>> {
357+
bincode::serde::encode_to_vec(self, bincode::config::standard()).map_err(|e| e.into())
358+
}
359+
360+
/// Convert the proof from bytes
361+
pub fn from_bytes(bytes: &[u8]) -> StdResult<Self> {
362+
let (res, _) =
363+
bincode::serde::decode_from_slice::<Self, _>(bytes, bincode::config::standard())
364+
.map_err(|e| anyhow!(e))?;
365+
366+
Ok(res)
367+
}
368+
}
369+
354370
impl<K: MKMapKey> From<MKProof> for MKMapProof<K> {
355371
fn from(other: MKProof) -> Self {
356372
MKMapProof::new(other, BTreeMap::default())
@@ -821,6 +837,31 @@ mod tests {
821837
assert_eq!(mktree_nodes_to_certify.to_vec(), mk_proof_leaves);
822838
}
823839

840+
#[test]
841+
fn test_mk_map_should_serialize_deserialize_proof() {
842+
let entries = generate_merkle_trees(10, 3);
843+
let mktree_nodes_to_certify = [
844+
entries[0].1.leaves()[0].clone(),
845+
entries[1].1.leaves()[0].clone(),
846+
entries[1].1.leaves()[1].clone(),
847+
entries[2].1.leaves()[1].clone(),
848+
];
849+
let mk_map_full =
850+
MKMap::<_, _, MKTreeStoreInMemory>::new(&into_mkmap_tree_entries(entries)).unwrap();
851+
let mk_map_proof = mk_map_full.compute_proof(&mktree_nodes_to_certify).unwrap();
852+
853+
let serialized_mk_map_proof = mk_map_proof
854+
.to_bytes()
855+
.expect("Serialization should not fail");
856+
let deserialized_mk_map_proof =
857+
MKMapProof::<BlockRange>::from_bytes(&serialized_mk_map_proof)
858+
.expect("Deserialization should not fail");
859+
assert_eq!(
860+
mk_map_proof, deserialized_mk_map_proof,
861+
"Deserialized proof should match the original"
862+
);
863+
}
864+
824865
#[test]
825866
fn test_mk_map_should_compute_and_verify_valid_proof_recursively() {
826867
let entries = generate_merkle_trees(100, 3);

0 commit comments

Comments
 (0)