Browse Source

Merge pull request #103 from 0xPolygonMiden/hacka-format-merkle-tree

Format merkle tree
al-gkr-basic-workflow
Bobbin Threadbare 2 years ago
committed by GitHub
parent
commit
b7678619b0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 9 deletions
  1. +1
    -6
      src/lib.rs
  2. +51
    -2
      src/merkle/merkle_tree.rs
  3. +1
    -1
      src/merkle/mod.rs
  4. +21
    -0
      src/utils.rs

+ 1
- 6
src/lib.rs

@ -7,18 +7,13 @@ extern crate alloc;
mod bit; mod bit;
pub mod hash; pub mod hash;
pub mod merkle; pub mod merkle;
pub mod utils;
// RE-EXPORTS // RE-EXPORTS
// ================================================================================================ // ================================================================================================
pub use winter_crypto::{RandomCoin, RandomCoinError}; pub use winter_crypto::{RandomCoin, RandomCoinError};
pub use winter_math::{fields::f64::BaseElement as Felt, FieldElement, StarkField}; pub use winter_math::{fields::f64::BaseElement as Felt, FieldElement, StarkField};
pub mod utils {
pub use winter_utils::{
collections, string, uninit_vector, ByteReader, ByteWriter, Deserializable,
DeserializationError, Serializable, SliceReader,
};
}
// TYPE ALIASES // TYPE ALIASES
// ================================================================================================ // ================================================================================================

+ 51
- 2
src/merkle/merkle_tree.rs

@ -1,6 +1,9 @@
use super::{Felt, MerkleError, MerklePath, NodeIndex, Rpo256, RpoDigest, Vec, Word}; 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; use winter_math::log2;
// MERKLE TREE // 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 // TESTS
// ================================================================================================ // ================================================================================================

+ 1
- 1
src/merkle/mod.rs

@ -15,7 +15,7 @@ mod index;
pub use index::NodeIndex; pub use index::NodeIndex;
mod merkle_tree; mod merkle_tree;
pub use merkle_tree::MerkleTree;
pub use merkle_tree::{path_to_text, tree_to_text, MerkleTree};
mod path; mod path;
pub use path::{MerklePath, RootPath, ValuePath}; pub use path::{MerklePath, RootPath, ValuePath};

+ 21
- 0
src/utils.rs

@ -0,0 +1,21 @@
use super::Word;
use crate::utils::string::String;
use core::fmt::{self, Write};
// RE-EXPORTS
// ================================================================================================
pub use winter_utils::{
collections, string, uninit_vector, ByteReader, ByteWriter, Deserializable,
DeserializationError, Serializable, SliceReader,
};
/// Converts a [Word] into hex.
pub fn word_to_hex(w: &Word) -> Result<String, fmt::Error> {
let mut s = String::new();
for byte in w.iter().flat_map(|e| e.to_bytes()) {
write!(s, "{byte:02x}")?;
}
Ok(s)
}

Loading…
Cancel
Save