mirror of
https://github.com/arnaucube/miden-crypto.git
synced 2026-01-12 00:51:29 +01:00
feat: added utility to format MerkleTree and MerklePath to hex
Example formatted MerkleTree:
```
880abe452320966617646e7740b014954300f19a28780a0889d62ff33f4b0534
1ade1369091efa31201e9b60c9c28874d0ddce5362b335135a6bb4c917285983
3e60a9c843b4bb19f7a0572102e6507195f5240767a396335fd21981b048b807
0100000000000000000000000000000000000000000000000000000000000000
0200000000000000000000000000000000000000000000000000000000000000
0300000000000000000000000000000000000000000000000000000000000000
0400000000000000000000000000000000000000000000000000000000000000
```
Example formatted MerklePath:
```
[0400000000000000000000000000000000000000000000000000000000000000, 1ade1369091efa31201e9b60c9c28874d0ddce5362b335135a6bb4c917285983]
```
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
use super::{Felt, MerkleError, MerklePath, NodeIndex, Rpo256, RpoDigest, Vec, Word};
|
||||
use crate::{utils::uninit_vector, FieldElement};
|
||||
use core::slice;
|
||||
use crate::{
|
||||
utils::{string::String, uninit_vector, word_to_hex},
|
||||
FieldElement,
|
||||
};
|
||||
use core::{fmt, slice};
|
||||
use winter_math::log2;
|
||||
|
||||
// MERKLE TREE
|
||||
@@ -157,6 +160,52 @@ impl MerkleTree {
|
||||
}
|
||||
}
|
||||
|
||||
/// Utility to vizualize a [MerkleTree] in text.
|
||||
pub fn tree_to_text(tree: &MerkleTree) -> Result<String, fmt::Error> {
|
||||
let indent = " ";
|
||||
let mut s = String::new();
|
||||
s.push_str(&word_to_hex(&tree.root())?);
|
||||
s.push('\n');
|
||||
for d in 1..=tree.depth() {
|
||||
let entries = 2u64.pow(d.into());
|
||||
for i in 0..entries {
|
||||
let index = NodeIndex::new(d, i);
|
||||
|
||||
let node = tree
|
||||
.get_node(index)
|
||||
.expect("The index must always be valid");
|
||||
|
||||
for _ in 0..d {
|
||||
s.push_str(indent);
|
||||
}
|
||||
s.push_str(&word_to_hex(&node)?);
|
||||
s.push('\n');
|
||||
}
|
||||
}
|
||||
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
/// Utility to vizualize a [MerklePath] in text.
|
||||
pub fn path_to_text(path: &MerklePath) -> Result<String, fmt::Error> {
|
||||
let mut s = String::new();
|
||||
s.push('[');
|
||||
|
||||
for el in path.iter() {
|
||||
s.push_str(&word_to_hex(el)?);
|
||||
s.push_str(", ");
|
||||
}
|
||||
|
||||
// remove the last ", "
|
||||
if path.len() != 0 {
|
||||
s.pop();
|
||||
s.pop();
|
||||
}
|
||||
s.push(']');
|
||||
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
// TESTS
|
||||
// ================================================================================================
|
||||
|
||||
|
||||
Reference in New Issue
Block a user