From 321943b3f783363e4838951982c2e5bfd4823662 Mon Sep 17 00:00:00 2001 From: Gus Gutoski Date: Wed, 10 Jul 2024 20:04:27 -0400 Subject: [PATCH] add tests for hacked namespace proofs, tidy test --- .../impls/block/full_payload/ns_proof/test.rs | 112 ++++++++++++------ 1 file changed, 76 insertions(+), 36 deletions(-) diff --git a/types/src/v0/impls/block/full_payload/ns_proof/test.rs b/types/src/v0/impls/block/full_payload/ns_proof/test.rs index 5861f81f17..8d8a7e1c73 100644 --- a/types/src/v0/impls/block/full_payload/ns_proof/test.rs +++ b/types/src/v0/impls/block/full_payload/ns_proof/test.rs @@ -1,9 +1,12 @@ -use crate::{v0::impls::block::test::ValidTest, NsIndex, NsProof, Payload}; +use crate::{v0::impls::block::test::ValidTest, NsProof, Payload}; use async_compatibility_layer::logging::{setup_backtrace, setup_logging}; use futures::future; use hotshot::traits::BlockPayload; -use hotshot_types::{traits::EncodeBytes, vid::vid_scheme}; -use jf_vid::VidScheme; +use hotshot_types::{ + traits::EncodeBytes, + vid::{vid_scheme, VidSchemeType}, +}; +use jf_vid::{VidDisperse, VidScheme}; #[async_std::test] async fn ns_proof() { @@ -18,8 +21,13 @@ async fn ns_proof() { let mut rng = jf_utils::test_rng(); let mut tests = ValidTest::many_from_tx_lengths(test_cases, &mut rng); - // each item is a tuple: (block, vid_info, list_of_ns_proofs) - let blocks: Vec<(Payload, _, Vec<(NsIndex, NsProof)>)> = { + struct BlockInfo { + block: Payload, + vid: VidDisperse, + ns_proofs: Vec, + } + + let blocks: Vec = { // compute blocks separately to avoid async error `captured variable // cannot escape `FnMut` closure body` caused by mutable variable `vid` // below. @@ -36,25 +44,32 @@ async fn ns_proof() { .into_iter() .map(|block| { let vid = vid.disperse(block.encode()).unwrap(); - let ns_proofs: Vec<(NsIndex, NsProof)> = block + let ns_proofs: Vec = block .ns_table() .iter() - .map(|ns_index| { - ( - ns_index.clone(), - NsProof::new(&block, &ns_index, &vid.common).unwrap(), - ) - }) + .map(|ns_index| NsProof::new(&block, &ns_index, &vid.common).unwrap()) .collect(); - (block, vid, ns_proofs) + BlockInfo { + block, + vid, + ns_proofs, + } }) .collect() }; // sanity: verify all valid namespace proofs - for ((block, vid, ns_proofs), test) in blocks.iter().zip(tests.iter_mut()) { - for (ns_index, ns_proof) in ns_proofs.iter() { - let ns_id = block.ns_table().read_ns_id(ns_index).unwrap(); + for ( + BlockInfo { + block, + vid, + ns_proofs, + }, + test, + ) in blocks.iter().zip(tests.iter_mut()) + { + for ns_proof in ns_proofs.iter() { + let ns_id = block.ns_table().read_ns_id(&ns_proof.ns_index).unwrap(); let txs = test .nss .remove(&ns_id) @@ -70,18 +85,18 @@ async fn ns_proof() { } } - // mix and match ns_table, vid_commit, vid_common - { - assert!(blocks.len() >= 2, "need at least 2 test_cases"); + assert!(blocks.len() >= 2, "need at least 2 test_cases"); - let ns_proof_0_0 = &blocks[0].2[0].1; - let ns_table_0 = blocks[0].0.ns_table(); - let ns_table_1 = blocks[1].0.ns_table(); - let vid_commit_0 = &blocks[0].1.commit; - let vid_commit_1 = &blocks[1].1.commit; - let vid_common_0 = &blocks[0].1.common; - let vid_common_1 = &blocks[1].1.common; + let ns_proof_0_0 = &blocks[0].ns_proofs[0]; + let ns_table_0 = blocks[0].block.ns_table(); + let ns_table_1 = blocks[1].block.ns_table(); + let vid_commit_0 = &blocks[0].vid.commit; + let vid_commit_1 = &blocks[1].vid.commit; + let vid_common_0 = &blocks[0].vid.common; + let vid_common_1 = &blocks[1].vid.common; + // mix and match ns_table, vid_commit, vid_common + { // wrong ns_table assert!(ns_proof_0_0 .verify(ns_table_1, vid_commit_0, vid_common_0) @@ -96,16 +111,41 @@ async fn ns_proof() { assert!(ns_proof_0_0 .verify(ns_table_0, vid_commit_0, vid_common_1) .is_none()); + + // wrong ns_proof + assert!(ns_proof_0_0 + .verify(ns_table_1, vid_commit_1, vid_common_1) + .is_none()); } - // ensure well-formed test case - // assert!(blocks.len() >= 2, "need at least 2 test_cases"); - // assert!( - // blocks[0].ns_table().len().in_bounds(&ns_index), - // "block[0] has too few namespaces" - // ); - // assert!( - // blocks[0].ns_table().len().as_usize() > blocks[1].ns_table().len().as_usize(), - // "block[0] should have fewer namespaces than block[1]" - // ); + // hack the proof + { + ns_proof_0_0 + .verify(ns_table_0, vid_commit_0, vid_common_0) + .expect("sanity: correct proof should succeed"); + + let wrong_ns_index_ns_proof_0_0 = NsProof { + ns_index: blocks[0].ns_proofs[1].ns_index.clone(), + ..ns_proof_0_0.clone() + }; + assert!(wrong_ns_index_ns_proof_0_0 + .verify(ns_table_0, vid_commit_0, vid_common_0) + .is_none()); + + let wrong_ns_payload_ns_proof_0_0 = NsProof { + ns_payload: blocks[0].ns_proofs[1].ns_payload.clone(), + ..ns_proof_0_0.clone() + }; + assert!(wrong_ns_payload_ns_proof_0_0 + .verify(ns_table_0, vid_commit_0, vid_common_0) + .is_none()); + + let wrong_proof_ns_proof_0_0 = NsProof { + ns_proof: blocks[0].ns_proofs[1].ns_proof.clone(), + ..ns_proof_0_0.clone() + }; + assert!(wrong_proof_ns_proof_0_0 + .verify(ns_table_0, vid_commit_0, vid_common_0) + .is_none()); + } }