30 lines
993 B
Rust
30 lines
993 B
Rust
use ledger_core::{ReadProofV0, verify_read_proof_v0};
|
|
|
|
fn leaf(i: u64) -> [u8; 32] {
|
|
blake3::hash(format!("leaf-{}", i).as_bytes()).into()
|
|
}
|
|
|
|
#[test]
|
|
fn read_proof_verifies_for_all_leaves() {
|
|
for n in 1..=33usize {
|
|
let leaves: Vec<[u8; 32]> = (0..n as u64).map(leaf).collect();
|
|
for idx in 0..n {
|
|
let proof = ReadProofV0::from_tree(&leaves, idx).expect("build proof");
|
|
verify_read_proof_v0(&proof).expect("verify proof");
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn read_proof_rejects_tampering() {
|
|
let leaves: Vec<[u8; 32]> = (0..8u64).map(leaf).collect();
|
|
let mut proof = ReadProofV0::from_tree(&leaves, 3).expect("build proof");
|
|
assert!(verify_read_proof_v0(&proof).is_ok());
|
|
|
|
// Flip one nibble in the first sibling hash.
|
|
let mut s = proof.path[0].sibling_hash_hex.clone();
|
|
s.replace_range(0..1, if &s[0..1] == "0" { "1" } else { "0" });
|
|
proof.path[0].sibling_hash_hex = s;
|
|
assert!(verify_read_proof_v0(&proof).is_err());
|
|
}
|