Skip to content

Commit

Permalink
add tests for hacked namespace proofs, tidy test
Browse files Browse the repository at this point in the history
  • Loading branch information
ggutoski committed Jul 11, 2024
1 parent 8dd135f commit 321943b
Showing 1 changed file with 76 additions and 36 deletions.
112 changes: 76 additions & 36 deletions types/src/v0/impls/block/full_payload/ns_proof/test.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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<VidSchemeType>,
ns_proofs: Vec<NsProof>,
}

let blocks: Vec<BlockInfo> = {
// compute blocks separately to avoid async error `captured variable
// cannot escape `FnMut` closure body` caused by mutable variable `vid`
// below.
Expand All @@ -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<NsProof> = 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)
Expand All @@ -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)
Expand All @@ -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());
}
}

0 comments on commit 321943b

Please sign in to comment.