diff --git a/Cargo.toml b/Cargo.toml index 9ee988c..4dd12fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nmt-rs" -version = "0.2.1" +version = "0.2.3" edition = "2021" description = "A namespaced merkle tree compatible with Celestia" license = "MIT OR Apache-2.0" @@ -21,7 +21,7 @@ nmt-rs = { path = ".", features = ["borsh", "serde"] } borsh = { version = "1" } serde_json = "1.0.96" postcard = { version = "1.0.4", features = ["use-std"] } -tendermint = { version = "0.35.0" } +tendermint = { version = "0.39.1" } [features] default = ["std"] diff --git a/README.md b/README.md index d436d15..fd718f8 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ This code has not been audited, and may contain critical vulnerabilities. Do not - [x] Verify namespace range proofs +- [x] Narrow namespace range proofs: supply part of the range to generate a proof for the remaining sub-range ## License diff --git a/src/nmt_proof.rs b/src/nmt_proof.rs index 600a99a..f46fc57 100644 --- a/src/nmt_proof.rs +++ b/src/nmt_proof.rs @@ -3,7 +3,6 @@ //! - A range of leaves forms a complete namespace //! - A range of leaves all exists in the same namespace use crate::maybestd::{mem, vec::Vec}; -use crate::simple_merkle::db::MemDb; use crate::{ namespaced_hash::{NamespaceId, NamespaceMerkleHasher, NamespacedHash}, simple_merkle::{ @@ -118,23 +117,18 @@ where return Err(RangeProofError::WrongAmountOfLeavesProvided); } - // TODO: make this more concise - let left_extra_hashes: Vec<_> = left_extra_raw_leaves - .iter() - .map(|data| { - M::with_ignore_max_ns(self.ignores_max_ns()) - .hash_leaf_with_namespace(data.as_ref(), leaf_namespace) - }) - .collect(); - let right_extra_hashes: Vec<_> = right_extra_raw_leaves - .iter() - .map(|data| { - M::with_ignore_max_ns(self.ignores_max_ns()) - .hash_leaf_with_namespace(data.as_ref(), leaf_namespace) - }) - .collect(); + let leaves_to_hashes = |l: &[L]| -> Vec> { + l.iter() + .map(|data| { + M::with_ignore_max_ns(self.ignores_max_ns()) + .hash_leaf_with_namespace(data.as_ref(), leaf_namespace) + }) + .collect() + }; + let left_extra_hashes = leaves_to_hashes(left_extra_raw_leaves); + let right_extra_hashes = leaves_to_hashes(right_extra_raw_leaves); - let mut tree = NamespaceMerkleTree::, M, NS_ID_SIZE>::with_hasher( + let mut tree = NamespaceMerkleTree::::with_hasher( M::with_ignore_max_ns(self.ignores_max_ns()), ); diff --git a/src/simple_merkle/proof.rs b/src/simple_merkle/proof.rs index d39ed13..6e8a232 100644 --- a/src/simple_merkle/proof.rs +++ b/src/simple_merkle/proof.rs @@ -1,7 +1,7 @@ use core::ops::Range; use super::{ - db::{MemDb, NoopDb}, + db::NoopDb, error::RangeProofError, tree::{MerkleHash, MerkleTree}, utils::compute_num_left_siblings, @@ -100,9 +100,12 @@ where let new_start_idx = (self.start_idx() as usize) .checked_add(left_extra_leaves.len()) .ok_or(RangeProofError::TreeTooLarge)?; - let new_end_idx = new_start_idx + self.range_len() - new_leaf_len as usize; // TODO safe arithmetic + let new_end_idx = new_start_idx + .checked_add(self.range_len()) + .and_then(|i| i.checked_sub(new_leaf_len)) + .ok_or(RangeProofError::TreeTooLarge)?; - let mut tree = MerkleTree::, M>::with_hasher(hasher); + let mut tree = MerkleTree::::with_hasher(hasher); tree.narrow_range_proof( left_extra_leaves, new_start_idx..new_end_idx,