From 8a7e6ef9084036d424067b0b99b8b3d4cae2c8ad Mon Sep 17 00:00:00 2001 From: driemworks Date: Mon, 28 Oct 2024 20:59:40 -0500 Subject: [PATCH] chore: fmt --- core/src/murmur.rs | 537 ++++++++++++++++--------------------- core/src/otp.rs | 56 ++-- lib/src/bin/murmur/main.rs | 24 +- lib/src/lib.rs | 55 ++-- test-utils/Cargo.toml | 2 +- test-utils/src/lib.rs | 13 +- 6 files changed, 302 insertions(+), 385 deletions(-) diff --git a/core/src/murmur.rs b/core/src/murmur.rs index 1b7c6ed..1740892 100644 --- a/core/src/murmur.rs +++ b/core/src/murmur.rs @@ -20,8 +20,7 @@ use alloc::{collections::BTreeMap, vec, vec::Vec}; #[cfg(feature = "client")] use crate::otp::BOTPGenerator; -use ark_std::rand::SeedableRng; -use ark_std::rand::{CryptoRng, Rng}; +use ark_std::rand::{CryptoRng, Rng, SeedableRng}; #[cfg(feature = "client")] use zeroize::Zeroize; @@ -39,10 +38,7 @@ use ckb_merkle_mountain_range::{ }; use codec::{Decode, Encode}; use dleq_vrf::{EcVrfVerifier, PublicKey, SecretKey}; -use etf_crypto_primitives::{ - encryption::tlock::*, - ibe::fullident::Identity -}; +use etf_crypto_primitives::{encryption::tlock::*, ibe::fullident::Identity}; use sha3::Digest; use w3f_bls::{DoublePublicKey, EngineBLS}; @@ -91,14 +87,12 @@ pub struct MurmurStore { #[cfg(feature = "client")] impl MurmurStore { - /// Create a new Murmur store /// /// * `seed`: An any-length seed (i.e. password) /// * `block_schedule`: The blocks for which OTP codes will be generated /// * `ephemeral_msk`: Any 32 bytes /// * `round_public_key`: The IDN beacon's public key - /// pub fn new, R>( mut seed: Vec, block_schedule: Vec, @@ -110,7 +104,7 @@ impl MurmurStore { R: Rng + CryptoRng + SeedableRng + Sized, { let mut witness = generate_witness(seed.clone(), rng); - let mut secret_key = + let mut secret_key = SecretKey::<::Affine>::from_seed(&witness); let pubkey = secret_key.as_publickey(); let mut pubkey_bytes = Vec::new(); @@ -175,7 +169,6 @@ impl MurmurStore { /// * `seed`: The seed used to create the mmr /// * `when`: The block number when the wallet is being used (or will be) /// * `call_data`: The call to be executed with the wallet (at `when`) - /// pub fn execute( &self, mut seed: Vec, @@ -202,7 +195,6 @@ impl MurmurStore { /// * `seed`: The seed used to generated the MMR /// * `when`: The block number when the commitment is verifiable /// * `data`: The data to commit to - /// fn commit( mut seed: Vec, when: BlockNumber, @@ -210,8 +202,7 @@ impl MurmurStore { mut rng: R, ) -> Result, Error> { let mut witness = generate_witness(seed.clone(), &mut rng); - let botp = BOTPGenerator::new(witness.to_vec()) - .map_err(|_| Error::InvalidSeed)?; + let botp = BOTPGenerator::new(witness.to_vec()).map_err(|_| Error::InvalidSeed)?; seed.zeroize(); witness.zeroize(); @@ -229,7 +220,6 @@ impl MurmurStore { /// Builds an mmr from the mmr store /// /// * `mmr`: a MemMMR instance (to be populated) - /// fn to_mmr(&self) -> Result, Error> { let store = MemStore::default(); let mut mmr = MemMMR::<_, MergeLeaves>::new(0, store); @@ -241,24 +231,18 @@ impl MurmurStore { } } -/// construct a 32-byte witness by seeding a transcript with the given 'seed' +/// construct a 32-byte witness by seeding a transcript with the given 'seed' /// using a CSPRNG to generate the witness /// /// * `seed`: The value written to the transcript /// * `rng`: A CSPRNG -/// #[cfg(feature = "client")] -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(); - - transcript.clone() - .witness(&mut rng) - .read_byte_array() +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(); + + transcript.clone().witness(&mut rng).read_byte_array() } /// A helper function to perform timelock encryption @@ -268,14 +252,13 @@ pub fn generate_witness( /// * `ephemeral_msk`: A randomly sampled 32-byte secret key /// * `message`: The message to be timelock encrypted /// * `rng`: A CSPRNG -/// #[cfg(feature = "client")] fn timelock_encrypt( - identity: Identity, - pk: E::PublicKeyGroup, - ephemeral_msk: [u8; 32], - message: &[u8], - rng: R, + identity: Identity, + pk: E::PublicKeyGroup, + ephemeral_msk: [u8; 32], + message: &[u8], + rng: R, ) -> Result, Error> { let ciphertext = tle::(pk, ephemeral_msk, message, identity, rng).map_err(|_| Error::TlockFailed)?; @@ -290,8 +273,8 @@ fn timelock_encrypt( /// These functions would typically be called by an untrusted verifier (e.g. a blockchain runtime) pub mod verifier { use super::*; - use ark_serialize::CanonicalDeserialize; - use dleq_vrf::{ThinVrfProof}; + use ark_serialize::CanonicalDeserialize; + use dleq_vrf::ThinVrfProof; #[derive(Debug, PartialEq)] pub enum VerificationError { @@ -299,21 +282,20 @@ pub mod verifier { UnserializablePubkey, } - /// Verify the correctness of execution parameters by checking that the Merkle proof and hash are valid - /// The function outputs true if both conditions are true: + /// Verify the correctness of execution parameters by checking that the Merkle proof and hash + /// are valid The function outputs true if both conditions are true: /// proof.verify(root, [(pos, Leaf(ciphertext))]) - /// hash = Sha256(otp || aux_data) + /// hash = Sha256(otp || aux_data) //// /// It outputs false otherwise. /// /// * `root`: The root of the MMR /// * `proof`: The Merkle proof to verify /// * `hash`: A (potential) commitment to the OTP and aux_data - /// * `ciphertext`: A timelocked ciphertext + /// * `ciphertext`: A timelocked ciphertext /// * `otp`: The OTP /// * `aux_data`: The expected aux data used to generate the commitment /// * `pos`: The position of the Ciphertext within the MMR - /// pub fn verify_execute( root: Leaf, proof: MerkleProof, @@ -323,8 +305,7 @@ pub mod verifier { aux_data: &[u8], pos: u64, ) -> bool { - let mut validity = proof.verify(root, vec![(pos, Leaf(ciphertext))]) - .unwrap_or(false); + let mut validity = proof.verify(root, vec![(pos, Leaf(ciphertext))]).unwrap_or(false); if validity { let mut hasher = sha3::Sha3_256::default(); @@ -338,30 +319,33 @@ pub mod verifier { } /// Verifies a Schnorr proof - /// This is used to ensure that subsequent calls to the 'new' function are called with the same seed + /// This is used to ensure that subsequent calls to the 'new' function are called with the same + /// seed /// /// * `serialized_proof`: The serialized proof /// * `serialized_pubkey`: The serialized public key /// * `nonce`: A nonce value - /// pub fn verify_update( - serialized_proof: Vec, - serialized_pubkey: Vec, - nonce: u64, - ) -> Result { - // build transcript - let mut transcript = Transcript::new_labeled(MURMUR_PROTO); - transcript.write_bytes(&nonce.to_be_bytes()); - + serialized_proof: Vec, + serialized_pubkey: Vec, + nonce: u64, + ) -> Result { + // build transcript + let mut transcript = Transcript::new_labeled(MURMUR_PROTO); + transcript.write_bytes(&nonce.to_be_bytes()); + // deserialize proof and pubkey - let proof = ThinVrfProof::<::Affine>:: - deserialize_compressed(&mut &serialized_proof[..]) - .map_err(|_| VerificationError::UnserializableProof)?; - - let pk = PublicKey::<::Affine>:: - deserialize_compressed(&mut &serialized_pubkey[..]) - .map_err(|_| VerificationError::UnserializablePubkey)?; - + let proof = + ThinVrfProof::<::Affine>::deserialize_compressed( + &mut &serialized_proof[..], + ) + .map_err(|_| VerificationError::UnserializableProof)?; + + let pk = PublicKey::<::Affine>::deserialize_compressed( + &mut &serialized_pubkey[..], + ) + .map_err(|_| VerificationError::UnserializablePubkey)?; + Ok(pk.vrf_verify_detached(transcript, &[], &proof).is_ok()) } } @@ -369,17 +353,16 @@ pub mod verifier { #[cfg(test)] mod tests { - use super::*; - use w3f_bls::{DoublePublicKeyScheme, TinyBLS377}; - use rand_chacha::ChaCha20Rng; - use ark_std::rand::SeedableRng; - use ark_serialize::CanonicalDeserialize; + use super::*; + use ark_serialize::CanonicalDeserialize; + use ark_std::rand::SeedableRng; + use rand_chacha::ChaCha20Rng; use rand_core::OsRng; + use w3f_bls::{DoublePublicKeyScheme, TinyBLS377}; - /// - pub const BLOCK_SCHEDULE: &[BlockNumber] = &[ - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, - ]; + /// + pub const BLOCK_SCHEDULE: &[BlockNumber] = + &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; pub const WHEN: BlockNumber = 10; pub const OTP: &[u8] = b"823185"; @@ -399,273 +382,224 @@ mod tests { let double_public: DoublePublicKey = DoublePublicKey(keypair.into_public_key_in_signature_group().0, keypair.public.0); - let seed = vec![1, 2, 3]; - - let murmur_store = MurmurStore::new::( - seed.clone(), - BLOCK_SCHEDULE.to_vec(), - 0, - double_public, - &mut rng, - ).unwrap(); - - assert!(murmur_store.metadata.keys().len() == BLOCK_SCHEDULE.len()); - assert!(murmur_store.root.0.len() == 32); - assert!(murmur_store.proof.len() == 80); - assert!(murmur_store.public_key.len() == 48); - } - - #[cfg(feature = "client")] - #[test] - pub fn it_can_generate_valid_output_and_verify_it() { - let mut rng = ChaCha20Rng::seed_from_u64(0); - let keypair = w3f_bls::KeypairVT::::generate(&mut rng); - let double_public: DoublePublicKey = DoublePublicKey( - keypair.into_public_key_in_signature_group().0, - keypair.public.0, - ); - - let seed = vec![1, 2, 3]; - let aux_data = vec![2, 3, 4, 5]; - - let murmur_store = MurmurStore::new::( - seed.clone(), - BLOCK_SCHEDULE.to_vec(), - 0, - double_public, - &mut rng, - ).unwrap(); - - let root = murmur_store.root.clone(); - let (proof, commitment, ciphertext, pos) = murmur_store - .execute( - seed.clone(), - WHEN, - aux_data.clone(), - &mut rng, - ) - .unwrap(); - - assert!(verifier::verify_execute( - root, - proof, - commitment, - ciphertext, - OTP, - &aux_data, - pos, - )); - } - - #[cfg(feature = "client")] - #[test] - pub fn it_fails_to_generate_execute_output_when_ciphertext_dne() { + let seed = vec![1, 2, 3]; + + let murmur_store = MurmurStore::new::( + seed.clone(), + BLOCK_SCHEDULE.to_vec(), + 0, + double_public, + &mut rng, + ) + .unwrap(); + + assert!(murmur_store.metadata.keys().len() == BLOCK_SCHEDULE.len()); + assert!(murmur_store.root.0.len() == 32); + assert!(murmur_store.proof.len() == 80); + assert!(murmur_store.public_key.len() == 48); + } + + #[cfg(feature = "client")] + #[test] + pub fn it_can_generate_valid_output_and_verify_it() { let mut rng = ChaCha20Rng::seed_from_u64(0); - let keypair = w3f_bls::KeypairVT::::generate(&mut rng); - let double_public: DoublePublicKey = DoublePublicKey( - keypair.into_public_key_in_signature_group().0, - keypair.public.0, - ); - - let seed = vec![1, 2, 3]; - - let murmur_store = MurmurStore::new::( - seed.clone(), - BLOCK_SCHEDULE.to_vec(), - 0, - double_public, - &mut rng, - ).unwrap(); + let keypair = w3f_bls::KeypairVT::::generate(&mut rng); + let double_public: DoublePublicKey = + DoublePublicKey(keypair.into_public_key_in_signature_group().0, keypair.public.0); + let seed = vec![1, 2, 3]; let aux_data = vec![2, 3, 4, 5]; - match murmur_store.execute(seed.clone(), 10000, aux_data.clone(), &mut rng) { - Ok(_) => panic!("There should be an error"), - Err(e) => assert_eq!(e, Error::NoCiphertextFound), - } - } - - #[cfg(feature = "client")] - #[test] - pub fn it_fails_on_verify_bad_aux_data() { - let mut rng = ChaCha20Rng::seed_from_u64(0); - let keypair = w3f_bls::KeypairVT::::generate(&mut rng); - let double_public: DoublePublicKey = DoublePublicKey( - keypair.into_public_key_in_signature_group().0, - keypair.public.0, - ); - - let seed = vec![1, 2, 3]; - let aux_data = vec![2, 3, 4, 5]; - - let murmur_store = MurmurStore::new::( - seed.clone(), - BLOCK_SCHEDULE.to_vec(), - 0, - double_public, - &mut rng, - ).unwrap(); - - let root = murmur_store.root.clone(); - let (proof, commitment, ciphertext, pos) = murmur_store - .execute( - seed.clone(), - WHEN, - aux_data.clone(), - &mut rng, - ) - .unwrap(); - - let bad_aux = vec![2, 3, 13, 3]; - assert!(!verifier::verify_execute( - root, - proof, - commitment, - ciphertext, - OTP, - &bad_aux, - pos, - )); - } - - #[test] - pub fn it_fails_on_verify_bad_proof() { + let murmur_store = MurmurStore::new::( + seed.clone(), + BLOCK_SCHEDULE.to_vec(), + 0, + double_public, + &mut rng, + ) + .unwrap(); + + let root = murmur_store.root.clone(); + let (proof, commitment, ciphertext, pos) = + murmur_store.execute(seed.clone(), WHEN, aux_data.clone(), &mut rng).unwrap(); + + assert!( + verifier::verify_execute(root, proof, commitment, ciphertext, OTP, &aux_data, pos,) + ); + } + + #[cfg(feature = "client")] + #[test] + pub fn it_fails_to_generate_execute_output_when_ciphertext_dne() { let mut rng = ChaCha20Rng::seed_from_u64(0); - let keypair = w3f_bls::KeypairVT::::generate(&mut rng); - let double_public: DoublePublicKey = DoublePublicKey( - keypair.into_public_key_in_signature_group().0, - keypair.public.0, - ); + let keypair = w3f_bls::KeypairVT::::generate(&mut rng); + let double_public: DoublePublicKey = + DoublePublicKey(keypair.into_public_key_in_signature_group().0, keypair.public.0); + + let seed = vec![1, 2, 3]; + + let murmur_store = MurmurStore::new::( + seed.clone(), + BLOCK_SCHEDULE.to_vec(), + 0, + double_public, + &mut rng, + ) + .unwrap(); + + let aux_data = vec![2, 3, 4, 5]; + + match murmur_store.execute(seed.clone(), 10000, aux_data.clone(), &mut rng) { + Ok(_) => panic!("There should be an error"), + Err(e) => assert_eq!(e, Error::NoCiphertextFound), + } + } + + #[cfg(feature = "client")] + #[test] + pub fn it_fails_on_verify_bad_aux_data() { + let mut rng = ChaCha20Rng::seed_from_u64(0); + let keypair = w3f_bls::KeypairVT::::generate(&mut rng); + let double_public: DoublePublicKey = + DoublePublicKey(keypair.into_public_key_in_signature_group().0, keypair.public.0); + + let seed = vec![1, 2, 3]; + let aux_data = vec![2, 3, 4, 5]; + + let murmur_store = MurmurStore::new::( + seed.clone(), + BLOCK_SCHEDULE.to_vec(), + 0, + double_public, + &mut rng, + ) + .unwrap(); + + let root = murmur_store.root.clone(); + let (proof, commitment, ciphertext, pos) = + murmur_store.execute(seed.clone(), WHEN, aux_data.clone(), &mut rng).unwrap(); + + let bad_aux = vec![2, 3, 13, 3]; + assert!( + !verifier::verify_execute(root, proof, commitment, ciphertext, OTP, &bad_aux, pos,) + ); + } + + #[test] + pub fn it_fails_on_verify_bad_proof() { + let mut rng = ChaCha20Rng::seed_from_u64(0); + let keypair = w3f_bls::KeypairVT::::generate(&mut rng); + let double_public: DoublePublicKey = + DoublePublicKey(keypair.into_public_key_in_signature_group().0, keypair.public.0); let other_keypair = w3f_bls::KeypairVT::::generate(&mut rng); - let other_double_public: DoublePublicKey = DoublePublicKey( - other_keypair.into_public_key_in_signature_group().0, - other_keypair.public.0, - ); - - let seed = vec![1, 2, 3]; - let other_seed = vec![2,3,4]; - - let murmur_store = MurmurStore::new::( - seed.clone(), - BLOCK_SCHEDULE.to_vec(), - 0, - double_public, - &mut rng, - ).unwrap(); - - let other_murmur_store = MurmurStore::new::( - other_seed.clone(), - BLOCK_SCHEDULE.to_vec(), - 0, - other_double_public, - &mut rng, - ).unwrap(); + let other_double_public: DoublePublicKey = DoublePublicKey( + other_keypair.into_public_key_in_signature_group().0, + other_keypair.public.0, + ); + + let seed = vec![1, 2, 3]; + let other_seed = vec![2, 3, 4]; + + let murmur_store = MurmurStore::new::( + seed.clone(), + BLOCK_SCHEDULE.to_vec(), + 0, + double_public, + &mut rng, + ) + .unwrap(); + + let other_murmur_store = MurmurStore::new::( + other_seed.clone(), + BLOCK_SCHEDULE.to_vec(), + 0, + other_double_public, + &mut rng, + ) + .unwrap(); let aux_data = vec![2, 3, 13, 3]; - // the block number when this would execute - let root = murmur_store.root.clone(); - let (proof, commitment, ciphertext, pos) = other_murmur_store - .execute( - other_seed.clone(), - WHEN, - aux_data.clone(), - &mut rng, - ) - .unwrap(); - - assert!(!verifier::verify_execute( - root, - proof, - commitment, - ciphertext, - OTP, - &aux_data, - pos, - )); - } + // the block number when this would execute + let root = murmur_store.root.clone(); + let (proof, commitment, ciphertext, pos) = other_murmur_store + .execute(other_seed.clone(), WHEN, aux_data.clone(), &mut rng) + .unwrap(); + + assert!(!verifier::verify_execute( + root, proof, commitment, ciphertext, OTP, &aux_data, pos, + )); + } #[test] fn it_can_generate_and_verify_schnorr_proofs() { let mut rng = ChaCha20Rng::seed_from_u64(0); let keypair = w3f_bls::KeypairVT::::generate(&mut OsRng); - let double_public: DoublePublicKey = DoublePublicKey( - keypair.into_public_key_in_signature_group().0, - keypair.public.0, - ); + let double_public: DoublePublicKey = + DoublePublicKey(keypair.into_public_key_in_signature_group().0, 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 same_double_public = + DoublePublicKey::::deserialize_compressed(&mut &bytes[..]).unwrap(); - let seed = vec![1, 2, 3]; + let seed = vec![1, 2, 3]; - let murmur_store = MurmurStore::new::( - seed.clone(), - BLOCK_SCHEDULE.to_vec(), - 0, - double_public, - &mut rng, - ).unwrap(); + let murmur_store = MurmurStore::new::( + seed.clone(), + BLOCK_SCHEDULE.to_vec(), + 0, + double_public, + &mut rng, + ) + .unwrap(); let proof = murmur_store.proof; let pk = murmur_store.public_key; // now verify the proof for nonce = 0 - assert!(verifier::verify_update::( - proof, - pk.clone(), - 0, - ).unwrap()); + assert!(verifier::verify_update::(proof, pk.clone(), 0,).unwrap()); let mut same_rng = ChaCha20Rng::seed_from_u64(0); let another_murmur_store = MurmurStore::new::( - seed.clone(), - BLOCK_SCHEDULE.to_vec(), - 1, - same_double_public, - &mut same_rng, - ).unwrap(); + seed.clone(), + BLOCK_SCHEDULE.to_vec(), + 1, + same_double_public, + &mut same_rng, + ) + .unwrap(); let another_proof = another_murmur_store.proof; let another_pk = another_murmur_store.public_key; assert!(pk == another_pk); // now verify the proof for nonce = 1 - assert!(verifier::verify_update::( - another_proof, - pk, - 1, - ).unwrap()); + assert!(verifier::verify_update::(another_proof, pk, 1,).unwrap()); } #[test] fn it_cannot_verify_schnorr_proof_with_bad_nonce() { let mut rng = ChaCha20Rng::seed_from_u64(0); - let keypair = w3f_bls::KeypairVT::::generate(&mut rng); - let double_public: DoublePublicKey = DoublePublicKey( - keypair.into_public_key_in_signature_group().0, - keypair.public.0, - ); - - let seed = vec![1, 2, 3]; - - let murmur_store = MurmurStore::new::( - seed.clone(), - BLOCK_SCHEDULE.to_vec(), - 0, - double_public, - &mut rng, - ).unwrap(); + let keypair = w3f_bls::KeypairVT::::generate(&mut rng); + let double_public: DoublePublicKey = + DoublePublicKey(keypair.into_public_key_in_signature_group().0, keypair.public.0); + + let seed = vec![1, 2, 3]; + + let murmur_store = MurmurStore::new::( + seed.clone(), + BLOCK_SCHEDULE.to_vec(), + 0, + double_public, + &mut rng, + ) + .unwrap(); let proof = murmur_store.proof; let pk = murmur_store.public_key; // now verify the proof for nonce = 1 - assert!(!verifier::verify_update::( - proof, - pk, - 1, - ).unwrap()); + assert!(!verifier::verify_update::(proof, pk, 1,).unwrap()); } #[test] @@ -674,20 +608,19 @@ mod tests { let rng = ChaCha20Rng::seed_from_u64(0); let witness1 = generate_witness(seed.clone(), rng.clone()); let witness2 = generate_witness(seed, rng); - + assert_eq!(witness1, witness2, "Witnesses should be identical for the same seed"); - let secret_key_1 = - SecretKey::<<::SignatureGroup as CurveGroup>::Affine>::from_seed(&witness1); + let secret_key_1 = SecretKey::< + <::SignatureGroup as CurveGroup>::Affine, + >::from_seed(&witness1); let pubkey_1 = secret_key_1.as_publickey(); - - let secret_key_2 = - SecretKey::<<::SignatureGroup as CurveGroup>::Affine>::from_seed(&witness2); + let secret_key_2 = SecretKey::< + <::SignatureGroup as CurveGroup>::Affine, + >::from_seed(&witness2); let pubkey_2 = secret_key_2.as_publickey(); assert!(pubkey_1 == pubkey_2); - } - } diff --git a/core/src/otp.rs b/core/src/otp.rs index b6a314a..eb79f3f 100644 --- a/core/src/otp.rs +++ b/core/src/otp.rs @@ -14,11 +14,8 @@ * limitations under the License. */ -use alloc::{ - vec::Vec, - string::String, -}; -use totp_rs::{Secret, TOTP, Algorithm}; +use alloc::{string::String, vec::Vec}; +use totp_rs::{Algorithm, Secret, TOTP}; use zeroize::Zeroize; #[derive(Debug)] @@ -34,32 +31,31 @@ pub struct BOTPGenerator { } impl BOTPGenerator { - /// Create a new BOTP generator with the given seed - /// - /// * `seed`: The seed used to generate OTP codes - /// - pub fn new(mut seed: Vec) -> Result { - let mut secret = Secret::Raw(seed.clone()).to_bytes() - .map_err(|_| OTPError::InvalidSecret)?; - seed.zeroize(); - let totp = TOTP::new( - Algorithm::SHA256, // algorithm - 6, // num digits - 1, // skew - 1, // step - secret.clone() // secret - ).map_err(|_| OTPError::InvalidSecret)?; - secret.zeroize(); - Ok(BOTPGenerator { totp }) - } + /// Create a new BOTP generator with the given seed + /// + /// * `seed`: The seed used to generate OTP codes + pub fn new(mut seed: Vec) -> Result { + let mut secret = + Secret::Raw(seed.clone()).to_bytes().map_err(|_| OTPError::InvalidSecret)?; + seed.zeroize(); + let totp = TOTP::new( + Algorithm::SHA256, // algorithm + 6, // num digits + 1, // skew + 1, // step + secret.clone(), // secret + ) + .map_err(|_| OTPError::InvalidSecret)?; + secret.zeroize(); + Ok(BOTPGenerator { totp }) + } - /// Generate an otp code - /// - /// * `block_height`: The block for which the code is valid - /// - pub fn generate(&self, block_height: u64) -> String { - self.totp.generate(block_height) - } + /// Generate an otp code + /// + /// * `block_height`: The block for which the code is valid + pub fn generate(&self, block_height: u64) -> String { + self.totp.generate(block_height) + } } #[cfg(test)] diff --git a/lib/src/bin/murmur/main.rs b/lib/src/bin/murmur/main.rs index f6d9ad4..8b43439 100644 --- a/lib/src/bin/murmur/main.rs +++ b/lib/src/bin/murmur/main.rs @@ -15,17 +15,13 @@ */ use clap::{Parser, Subcommand}; -use murmur_lib::{ - create, etf, prepare_execute, BlockNumber, BoundedVec, MurmurStore, RuntimeCall, -}; +use murmur_lib::{create, etf, prepare_execute, BlockNumber, BoundedVec, MurmurStore, RuntimeCall}; -use rand_core::{OsRng, SeedableRng}; use rand_chacha::ChaCha20Rng; +use rand_core::{OsRng, SeedableRng}; use sp_core::crypto::Ss58Codec; use std::{fs::File, time::Instant}; -use subxt::{ - backend::rpc::RpcClient, client::OnlineClient, config::SubstrateConfig, -}; +use subxt::{backend::rpc::RpcClient, client::OnlineClient, config::SubstrateConfig}; use subxt_signer::sr25519::dev; use thiserror::Error; @@ -94,7 +90,7 @@ async fn main() -> Result<(), Box> { let before = Instant::now(); let (client, current_block_number, round_pubkey_bytes) = idn_connect().await?; - let mut rng = ChaCha20Rng::from_rng(&mut OsRng).unwrap(); + let mut rng = ChaCha20Rng::from_rng(&mut OsRng).unwrap(); match &cli.commands { Commands::New(args) => { @@ -109,13 +105,9 @@ async fn main() -> Result<(), Box> { } // 2. create mmr - let mmr_store = create( - args.seed.as_bytes().to_vec(), - 0, - schedule, - round_pubkey_bytes, - &mut rng, - ).map_err(|_| CLIError::MurmurCreationFailed)?; + let mmr_store = + create(args.seed.as_bytes().to_vec(), 0, schedule, round_pubkey_bytes, &mut rng) + .map_err(|_| CLIError::MurmurCreationFailed)?; // 3. add to storage write_mmr_store(mmr_store.clone(), MMR_STORE_FILEPATH); @@ -156,7 +148,7 @@ async fn main() -> Result<(), Box> { current_block_number + 1, store, &balance_transfer_call, - &mut rng, + &mut rng, ) .map_err(|_| CLIError::MurmurExecutionFailed)?; diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 6867cb5..bbb70e1 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -57,12 +57,12 @@ pub struct CreateData { pub root: Vec, /// The size of the MMR pub size: u64, - /// The murmur store (map of block nubmer to ciphertext) + /// The murmur store (map of block nubmer to ciphertext) pub mmr_store: MurmurStore, - /// The serialized VRF public key - pub public_key_bytes: Vec, - /// The serialized Schnorr signature - pub proof_bytes: Vec, + /// The serialized VRF public key + pub public_key_bytes: Vec, + /// The serialized Schnorr signature + pub proof_bytes: Vec, } #[derive(Serialize)] @@ -71,11 +71,11 @@ pub struct ProxyData { pub position: u64, /// The hash of the commitment pub hash: Vec, - /// The timelocked ciphertext + /// The timelocked ciphertext pub ciphertext: Vec, - /// The Merkle proof items + /// The Merkle proof items pub proof_items: Vec>, - /// The size of the Merkle proof + /// The size of the Merkle proof pub size: u64, } @@ -149,14 +149,14 @@ mod tests { let mut rng = ChaCha20Rng::from_rng(&mut OsRng).unwrap(); let mmr_store = create(seed.clone(), 0, block_schedule.clone(), double_public_bytes.clone(), &mut rng) - .unwrap(); + .unwrap(); // let mmr_store = MurmurStore::new::( // seed, // block_schedule, // 0, // DoublePublicKey::::from_bytes(&double_public_bytes).unwrap(), - // &mut rng, + // &mut rng, // ).unwrap(); assert_eq!(mmr_store.root.0.len(), 32); @@ -169,19 +169,14 @@ mod tests { let block_schedule = vec![1, 2, 3, 4, 5, 6, 7]; let double_public_bytes = murmur_test_utils::get_dummy_beacon_pubkey(); let mut rng = ChaCha20Rng::from_rng(&mut OsRng).unwrap(); - let mmr_store = create( - seed.clone(), - 0, - block_schedule, - double_public_bytes, - &mut rng - ).unwrap(); + let mmr_store = + create(seed.clone(), 0, block_schedule, double_public_bytes, &mut rng).unwrap(); // let size = proof.mmr_size(); // let proof_items: Vec> = // proof.proof_items().iter().map(|leaf| leaf.0.clone()).collect::>(); - let bob = subxt_signer::sr25519::dev::bob().public_key(); + let bob = subxt_signer::sr25519::dev::bob().public_key(); let balance_transfer_call = etf::runtime_types::node_template_runtime::RuntimeCall::Balances( etf::balances::Call::transfer_allow_death { @@ -190,14 +185,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; @@ -213,9 +208,11 @@ mod tests { // let (proof, commitment, ciphertext, _pos) = create_data.mmr_store // .execute(seed.clone(), when, balance_transfer_call_2.encode(), &mut rng) // .unwrap(); - // let expected_commitment = [71, 71, 72, 200, 197, 44, 120, 151, 127, 6, 162, 244, 138, 122, 196, 183, 30, 47, 111, 239, 225, 32, 57, 141, 186, 229, 164, 113, 113, 44, 131, 168]; - // let expected_ciphertext = [76, 42, 82, 184, 114, 58, 31, 205, 146, 16, 41, 191, 126, 213, 18, 65, 42, 149, 78, 140, 243, 164, 39, 54, 13, 96, 159, 93, 200, 83, 227, 179]; - // let size = proof.mmr_size(); + // let expected_commitment = [71, 71, 72, 200, 197, 44, 120, 151, 127, 6, 162, 244, 138, + // 122, 196, 183, 30, 47, 111, 239, 225, 32, 57, 141, 186, 229, 164, 113, 113, 44, 131, + // 168]; let expected_ciphertext = [76, 42, 82, 184, 114, 58, 31, 205, 146, 16, 41, 191, + // 126, 213, 18, 65, 42, 149, 78, 140, 243, 164, 39, 54, 13, 96, 159, 93, 200, 83, 227, + // 179]; let size = proof.mmr_size(); // let proof_items: Vec> = // proof.proof_items().iter().map(|leaf| leaf.0.clone()).collect::>(); assert_eq!(proxy_data.position, 0); diff --git a/test-utils/Cargo.toml b/test-utils/Cargo.toml index 875d1ba..d14bccd 100644 --- a/test-utils/Cargo.toml +++ b/test-utils/Cargo.toml @@ -33,6 +33,6 @@ std = [ "ark-std/std", "w3f-bls/std", "murmur-core/std", -"dleq_vrf/std", + "dleq_vrf/std", ] no_std = [] \ No newline at end of file diff --git a/test-utils/src/lib.rs b/test-utils/src/lib.rs index 17520cd..37ac82c 100644 --- a/test-utils/src/lib.rs +++ b/test-utils/src/lib.rs @@ -19,12 +19,12 @@ //! various utilities helpful for testing use alloc::vec::Vec; +use ark_ec::CurveGroup; use ark_serialize::CanonicalSerialize; use ark_std::rand::{CryptoRng, Rng}; +use dleq_vrf::SecretKey; use rand_core::OsRng; -use ark_ec::CurveGroup; use w3f_bls::{DoublePublicKey, DoublePublicKeyScheme, EngineBLS, TinyBLS377}; -use dleq_vrf::SecretKey; extern crate alloc; @@ -35,13 +35,12 @@ pub use murmur_core::murmur::MurmurStore; pub use murmur_core::murmur::generate_witness; pub fn otp( - seed: Vec, - when: u64, - rng: &mut R + seed: Vec, + when: u64, + rng: &mut R, ) -> Vec { let witness = generate_witness(seed.clone(), rng); - let secret_key = - SecretKey::<::Affine>::from_seed(&witness); + let secret_key = SecretKey::<::Affine>::from_seed(&witness); let pubkey = secret_key.as_publickey(); let mut pubkey_bytes = Vec::new(); pubkey.serialize_compressed(&mut pubkey_bytes).unwrap();