-
Notifications
You must be signed in to change notification settings - Fork 136
/
utils.rs
38 lines (33 loc) · 1.15 KB
/
utils.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use lambdaworks_crypto::merkle_tree::proof::Proof;
use lambdaworks_math::errors::DeserializationError;
use super::config::Commitment;
pub fn serialize_proof(proof: &Proof<Commitment>) -> Vec<u8> {
let mut bytes = vec![];
bytes.extend(proof.merkle_path.len().to_be_bytes());
for commitment in &proof.merkle_path {
bytes.extend(commitment);
}
bytes
}
pub fn deserialize_proof(bytes: &[u8]) -> Result<(Proof<Commitment>, &[u8]), DeserializationError> {
let mut bytes = bytes;
let mut merkle_path = vec![];
let merkle_path_len = usize::from_be_bytes(
bytes
.get(..8)
.ok_or(DeserializationError::InvalidAmountOfBytes)?
.try_into()
.map_err(|_| DeserializationError::InvalidAmountOfBytes)?,
);
bytes = &bytes[8..];
for _ in 0..merkle_path_len {
let commitment = bytes
.get(..32)
.ok_or(DeserializationError::InvalidAmountOfBytes)?
.try_into()
.map_err(|_| DeserializationError::InvalidAmountOfBytes)?;
merkle_path.push(commitment);
bytes = &bytes[32..];
}
Ok((Proof { merkle_path }, bytes))
}