From da6590e7795116434e4c5ef678598c19e3e9cf59 Mon Sep 17 00:00:00 2001 From: driemworks Date: Thu, 24 Oct 2024 21:39:33 -0500 Subject: [PATCH] feat: enhance tests --- core/src/murmur.rs | 33 +++++++++++++++++++++++++++------ lib/src/lib.rs | 23 ++++++++++------------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/core/src/murmur.rs b/core/src/murmur.rs index e37dd7c..d3541c1 100644 --- a/core/src/murmur.rs +++ b/core/src/murmur.rs @@ -182,10 +182,9 @@ impl MurmurStore { mut rng: R, ) -> Result<(MerkleProof, Vec, Ciphertext, u64), Error> { if let Some(ciphertext) = self.metadata.get(&when) { + let commitment = MurmurStore::commit(seed.clone(), when, &call_data.clone(), &mut rng)?; seed.zeroize(); - // let idx = get_key_index(&self.metadata, &when) - // .expect("The key must exist within the metadata."); let idx = self.metadata.keys().position(|k| k == &when).expect("The leaf should exist"); let pos = leaf_index_to_pos(idx as u64); let mmr = self.to_mmr()?; @@ -244,7 +243,7 @@ impl MurmurStore { /// * `rng`: A CSPRNG /// #[cfg(feature = "client")] -fn generate_witness(mut seed: Vec, mut rng: R) -> [u8; 32] { +pub fn generate_witness(mut seed: Vec, mut rng: R) -> [u8; 32] { let mut transcript = Transcript::new_labeled(MURMUR_PROTO); transcript.write_bytes(&seed); seed.zeroize(); @@ -348,11 +347,11 @@ pub mod verifier { // deserialize proof and pubkey let proof = ThinVrfProof::<::Affine>:: deserialize_compressed(&mut &serialized_proof[..]) - .map_err(|_| VerificationError::UnserializableProof)?; + .map_err(|_| VerificationError::UnserializableProof)?; let pk = PublicKey::<::Affine>:: deserialize_compressed(&mut &serialized_pubkey[..]) - .map_err(|_| VerificationError::UnserializablePubkey)?; + .map_err(|_| VerificationError::UnserializablePubkey)?; Ok(pk.vrf_verify_detached(transcript, &[], &proof).is_ok()) } @@ -365,6 +364,7 @@ mod tests { use w3f_bls::{DoublePublicKeyScheme, TinyBLS377}; use rand_chacha::ChaCha20Rng; use ark_std::rand::SeedableRng; + use ark_serialize::CanonicalDeserialize; /// pub const BLOCK_SCHEDULE: &[BlockNumber] = &[ @@ -585,6 +585,10 @@ mod tests { keypair.public.0, ); + let mut bytes = Vec::new(); + double_public.serialize_compressed(&mut bytes).unwrap(); + let same_double_public = DoublePublicKey::::deserialize_compressed(&mut &bytes[..]).unwrap(); + let seed = vec![1, 2, 3]; let murmur_store = MurmurStore::new::( @@ -600,9 +604,26 @@ mod tests { // now verify the proof for nonce = 0 assert!(verifier::verify_update::( proof, - pk, + pk.clone(), 0, ).is_ok()); + + let another_murmur_store = MurmurStore::new::( + seed.clone(), + BLOCK_SCHEDULE.to_vec(), + 1, + same_double_public, + &mut rng, + ).unwrap(); + + let another_proof = another_murmur_store.proof; + // let another_pk = another_murmur_store.public_key; + // now verify the proof for nonce = 0 + assert!(verifier::verify_update::( + another_proof, + pk, + 1, + ).is_ok()); } #[test] diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 53f2d9d..6867cb5 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -137,14 +137,12 @@ pub fn prepare_execute( #[cfg(test)] mod tests { - use super::*; - + // use super::*; use super::*; use rand_core::{OsRng, SeedableRng}; #[test] pub fn it_can_create_an_mmr_store_and_call_data() { - let name = b"name".to_vec(); let seed = b"seed".to_vec(); let block_schedule = vec![1, 2, 3, 4, 5, 6, 7]; let double_public_bytes = murmur_test_utils::get_dummy_beacon_pubkey(); @@ -162,12 +160,11 @@ mod tests { // ).unwrap(); assert_eq!(mmr_store.root.0.len(), 32); - assert_eq!(mmr_store.size, 7); + assert_eq!(mmr_store.metadata.keys().len(), 7); } #[test] pub fn it_can_prepare_valid_execution_call_data() { - let name = b"name".to_vec(); let seed = b"seed".to_vec(); let block_schedule = vec![1, 2, 3, 4, 5, 6, 7]; let double_public_bytes = murmur_test_utils::get_dummy_beacon_pubkey(); @@ -193,14 +190,14 @@ mod tests { }, ); - let bob2 = subxt_signer::sr25519::dev::bob().public_key(); - let balance_transfer_call_2 = - etf::runtime_types::node_template_runtime::RuntimeCall::Balances( - etf::balances::Call::transfer_allow_death { - dest: subxt::utils::MultiAddress::<_, u32>::from(bob2), - value: 1, - }, - ); + // let bob2 = subxt_signer::sr25519::dev::bob().public_key(); + // let balance_transfer_call_2 = + // etf::runtime_types::node_template_runtime::RuntimeCall::Balances( + // etf::balances::Call::transfer_allow_death { + // dest: subxt::utils::MultiAddress::<_, u32>::from(bob2), + // value: 1, + // }, + // ); let when = 1;