Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"
members = [
"core",
"nomt",
"nomt-test-utils",
"fuzz",
"torture",
"examples/*",
Expand Down
2 changes: 2 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ digest = { workspace = true }

[dev-dependencies]
blake3.workspace = true
nomt-test-utils = { path = "../nomt-test-utils" }
quickcheck.workspace = true

[features]
default = ["std", "blake3-hasher", "sha2-hasher"]
Expand Down
76 changes: 75 additions & 1 deletion core/src/page_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,34 @@ impl Iterator for PageIdsIterator {
mod tests {
use super::{
ChildPageIdError, ChildPageIndex, InvalidPageIdBytes, Msb0, PageId, PageIdsIterator, Uint,
HIGHEST_ENCODED_42, MAX_CHILD_INDEX, ROOT_PAGE_ID,
HIGHEST_ENCODED_42, MAX_CHILD_INDEX, MAX_PAGE_DEPTH, ROOT_PAGE_ID,
};
use bitvec::prelude::*;
use nomt_test_utils::TestKeyPath;
use quickcheck::{Arbitrary, Gen, QuickCheck};

const LOWEST_ENCODED_42: Uint<256, 4> = Uint::from_be_bytes([
0, 65, 4, 16, 65, 4, 16, 65, 4, 16, 65, 4, 16, 65, 4, 16, 65, 4, 16, 65, 4, 16, 65, 4, 16,
65, 4, 16, 65, 4, 16, 65,
]);

#[derive(Clone, Debug)]
struct PagePathCase {
key_path: [u8; 32],
level: usize,
child_index: u8,
}

impl Arbitrary for PagePathCase {
fn arbitrary(g: &mut Gen) -> Self {
Self {
key_path: TestKeyPath::arbitrary(g).into_inner(),
level: usize::arbitrary(g) % MAX_PAGE_DEPTH,
child_index: u8::arbitrary(g) & MAX_CHILD_INDEX,
}
}
}

fn child_page_id(page_id: &PageId, child_index: u8) -> Result<PageId, ChildPageIdError> {
page_id.child_page_id(ChildPageIndex::new(child_index).unwrap())
}
Expand Down Expand Up @@ -518,4 +537,59 @@ mod tests {
}
assert_eq!(min_page.max_key_path(), key_path);
}

#[test]
fn property_iterator_chain_contains_key_path() {
fn property(test_key_path: TestKeyPath) -> bool {
let key_path = test_key_path.into_inner();
let page_ids = PageIdsIterator::new(key_path).collect::<Vec<_>>();

assert_eq!(page_ids.len(), MAX_PAGE_DEPTH + 1);
assert_eq!(page_ids.first(), Some(&ROOT_PAGE_ID));

for (depth, page_id) in page_ids.iter().enumerate() {
assert_eq!(page_id.depth(), depth);
assert!(page_id.min_key_path() <= key_path);
assert!(key_path <= page_id.max_key_path());
}

for pair in page_ids.windows(2) {
let parent = &pair[0];
let child = &pair[1];
let child_index = child.child_index_at_level(parent.depth());

assert_eq!(parent.child_page_id(child_index).unwrap(), child.clone());
assert_eq!(child.parent_page_id(), parent.clone());
}

true
}

QuickCheck::new()
.tests(64)
.quickcheck(property as fn(TestKeyPath) -> bool);
}

#[test]
fn property_child_parent_roundtrip() {
fn property(case: PagePathCase) -> bool {
let page_id = PageIdsIterator::new(case.key_path).nth(case.level).unwrap();
let child_index = ChildPageIndex::new(case.child_index).unwrap();
let child = page_id.child_page_id(child_index).unwrap();

assert!(page_id.min_key_path() <= case.key_path);
assert!(case.key_path <= page_id.max_key_path());
assert_eq!(child.parent_page_id(), page_id.clone());
assert_eq!(child.depth(), page_id.depth() + 1);
assert!(child.is_descendant_of(&page_id));
assert!(page_id.min_key_path() <= child.min_key_path());
assert!(child.max_key_path() <= page_id.max_key_path());

true
}

QuickCheck::new()
.tests(64)
.quickcheck(property as fn(PagePathCase) -> bool);
}
}
29 changes: 6 additions & 23 deletions core/src/proof/multi_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,11 +909,12 @@ mod tests {
use crate::{
hasher::{Blake3Hasher, NodeHasher},
proof::{PathProof, PathProofTerminal},
trie::{InternalData, KeyPath, LeafData, ValueHash, TERMINATOR},
trie::{InternalData, LeafData, ValueHash, TERMINATOR},
trie_pos::TriePosition,
update::build_trie,
};
use bitvec::prelude::*;
use nomt_test_utils::key_with_prefix;

#[test]
pub fn test_multiproof_creation_single_path_proof() {
Expand Down Expand Up @@ -1891,26 +1892,8 @@ mod tests {
// 1. Define paths with prefix relationship
let bits_prefix = bitvec![u8, Msb0; 1, 0, 1, 0]; // length 4
let bits_longer = bitvec![u8, Msb0; 1, 0, 1, 0, 1, 1]; // length 6 (prefix + 2 bits)

// Helper to create KeyPath from BitVec (simplified, assumes short paths)
let keypath_from_bits = |bits: &BitSlice<u8, Msb0>| -> KeyPath {
let mut kp = KeyPath::default();

for (i, bit) in bits.iter().by_vals().enumerate() {
if i >= 256 {
break;
}

if bit {
kp[i / 8] |= 1 << (7 - (i % 8));
}
}

kp
};

let kp_prefix = keypath_from_bits(&bits_prefix);
let kp_longer = keypath_from_bits(&bits_longer);
let kp_prefix = key_with_prefix(bits_prefix.iter().by_vals());
let kp_longer = key_with_prefix(bits_longer.iter().by_vals());

// 2. Create corresponding LeafData and Nodes
let leaf_prefix = LeafData {
Expand Down Expand Up @@ -1962,13 +1945,13 @@ mod tests {

key_op1_bits.push(false); // e.g., 10100 (falls under 1010)

let key_op1 = keypath_from_bits(&key_op1_bits);
let key_op1 = key_with_prefix(key_op1_bits.iter().by_vals());

let mut key_op2_bits = bits_longer.clone();

key_op2_bits.push(true); // e.g., 1010111 (falls under 101011)

let key_op2 = keypath_from_bits(&key_op2_bits);
let key_op2 = key_with_prefix(key_op2_bits.iter().by_vals());

let ops = vec![
(key_op1, Some(ValueHash::default())), // Op under prefix path
Expand Down
15 changes: 15 additions & 0 deletions nomt-test-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "nomt-test-utils"
version = "0.1.0"
authors.workspace = true
homepage.workspace = true
repository.workspace = true
edition.workspace = true
license.workspace = true
publish = false

[dependencies]
nomt-core = { path = "../core", default-features = false, features = ["std"] }
quickcheck.workspace = true
rand.workspace = true
rand_pcg.workspace = true
Loading