Skip to content

Commit

Permalink
feat: enhance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
driemworks committed Oct 25, 2024
1 parent fe641cf commit da6590e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
33 changes: 27 additions & 6 deletions core/src/murmur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,9 @@ impl MurmurStore {
mut rng: R,
) -> Result<(MerkleProof<Leaf, MergeLeaves>, Vec<u8>, 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()?;
Expand Down Expand Up @@ -244,7 +243,7 @@ impl MurmurStore {
/// * `rng`: A CSPRNG
///
#[cfg(feature = "client")]
fn generate_witness<R: Rng + CryptoRng + Sized>(mut seed: Vec<u8>, mut rng: R) -> [u8; 32] {
pub fn generate_witness<R: Rng + CryptoRng + Sized>(mut seed: Vec<u8>, mut rng: R) -> [u8; 32] {
let mut transcript = Transcript::new_labeled(MURMUR_PROTO);
transcript.write_bytes(&seed);
seed.zeroize();
Expand Down Expand Up @@ -348,11 +347,11 @@ pub mod verifier {
// deserialize proof and pubkey
let proof = ThinVrfProof::<<E::SignatureGroup as CurveGroup>::Affine>::
deserialize_compressed(&mut &serialized_proof[..])
.map_err(|_| VerificationError::UnserializableProof)?;
.map_err(|_| VerificationError::UnserializableProof)?;

let pk = PublicKey::<<E::SignatureGroup as CurveGroup>::Affine>::
deserialize_compressed(&mut &serialized_pubkey[..])
.map_err(|_| VerificationError::UnserializablePubkey)?;
.map_err(|_| VerificationError::UnserializablePubkey)?;

Ok(pk.vrf_verify_detached(transcript, &[], &proof).is_ok())
}
Expand All @@ -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] = &[
Expand Down Expand Up @@ -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::<TinyBLS377>::deserialize_compressed(&mut &bytes[..]).unwrap();

let seed = vec![1, 2, 3];

let murmur_store = MurmurStore::new::<TinyBLS377, DummyIdBuilder, ChaCha20Rng>(
Expand All @@ -600,9 +604,26 @@ mod tests {
// now verify the proof for nonce = 0
assert!(verifier::verify_update::<TinyBLS377>(
proof,
pk,
pk.clone(),
0,
).is_ok());

let another_murmur_store = MurmurStore::new::<TinyBLS377, DummyIdBuilder, ChaCha20Rng>(
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::<TinyBLS377>(
another_proof,
pk,
1,
).is_ok());
}

#[test]
Expand Down
23 changes: 10 additions & 13 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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;

Expand Down

0 comments on commit da6590e

Please sign in to comment.