From ff1d5bd0c1ac2b4d95696fb734058f4f8fde4b59 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Fri, 29 Mar 2024 10:24:03 -0600 Subject: [PATCH] add test for simple proof --- ssz-rs/src/merkleization/proofs.rs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/ssz-rs/src/merkleization/proofs.rs b/ssz-rs/src/merkleization/proofs.rs index 891b937d..f7618410 100644 --- a/ssz-rs/src/merkleization/proofs.rs +++ b/ssz-rs/src/merkleization/proofs.rs @@ -68,8 +68,8 @@ pub fn is_valid_merkle_branch_for_generalized_index>( generalized_index: GeneralizedIndex, root: Node, ) -> Result<(), Error> { - let index = log_2(generalized_index).ok_or(Error::InvalidGeneralizedIndex)? as usize; - let depth = get_subtree_index(generalized_index)?; + let depth = log_2(generalized_index).ok_or(Error::InvalidGeneralizedIndex)? as usize; + let index = get_subtree_index(generalized_index)?; is_valid_merkle_branch(leaf, branch, depth, index, root) } @@ -121,7 +121,7 @@ mod tests { } #[test] - fn test_basic_proof() { + fn test_is_valid_merkle_branch() { let leaf = decode_node_from_hex( "94159da973dfa9e40ed02535ee57023ba2d06bad1017e451055470967eb71cd5", ); @@ -142,6 +142,29 @@ mod tests { assert!(is_valid_merkle_branch(leaf, &branch, depth, index, root).is_ok()); } + #[test] + fn test_simple_proof() { + let leaf = decode_node_from_hex( + "94159da973dfa9e40ed02535ee57023ba2d06bad1017e451055470967eb71cd5", + ); + let branch = [ + "8f594dbb4f4219ad4967f86b9cccdb26e37e44995a291582a431eef36ecba45c", + "f8c2ed25e9c31399d4149dcaa48c51f394043a6a1297e65780a5979e3d7bb77c", + "382ba9638ce263e802593b387538faefbaed106e9f51ce793d405f161b105ee6", + ] + .into_iter() + .map(decode_node_from_hex) + .collect::>(); + let depth = 3; + let index = 2; + let proof = Proof { leaf, branch, index: 2usize.pow(depth) + index }; + let root = decode_node_from_hex( + "27097c728aade54ff1376d5954681f6d45c282a81596ef19183148441b754abb", + ); + let result = proof.verify(root); + assert!(result.is_ok()); + } + #[test] fn test_proving_primitives() { let mut data = 8u8;