feat: add from_elements to NodeIndex

This commit is contained in:
Victor Lopez
2023-02-16 21:14:07 +01:00
parent 3af53e63cf
commit 9307178873
4 changed files with 20 additions and 8 deletions

View File

@@ -1,4 +1,4 @@
use super::RpoDigest;
use super::{Felt, MerkleError, RpoDigest, StarkField};
// NODE INDEX
// ================================================================================================
@@ -19,6 +19,18 @@ impl NodeIndex {
Self { depth, value }
}
/// Creates a node index from a pair of field elements representing the depth and value.
///
/// # Errors
///
/// Will error if the `u64` representation of the depth doesn't fit a `u8`.
pub fn from_elements(depth: &Felt, value: &Felt) -> Result<Self, MerkleError> {
let depth = depth.as_int();
let depth = u8::try_from(depth).map_err(|_| MerkleError::DepthTooBig(depth))?;
let value = value.as_int();
Ok(Self::new(depth, value))
}
/// Creates a new node index pointing to the root of the tree.
pub const fn root() -> Self {
Self { depth: 0, value: 0 }