Skip to content

Commit

Permalink
Merge pull request #20 from ideal-lab5/feat/mmr-store-encodable
Browse files Browse the repository at this point in the history
[Companion] feat: make mumurstore encodable and decodable
  • Loading branch information
juangirini authored Oct 9, 2024
2 parents 54f6b6c + e773af8 commit 7365f01
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
18 changes: 9 additions & 9 deletions core/src/murmur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use alloc::{collections::BTreeMap, vec, vec::Vec};
#[cfg(feature = "client")]
use crate::otp::BOTPGenerator;

use rand_chacha::ChaCha20Rng;
use ark_std::rand::SeedableRng;
use ark_std::rand::{CryptoRng, Rng};
use rand_chacha::ChaCha20Rng;

#[cfg(feature = "client")]
use zeroize::Zeroize;
Expand All @@ -37,6 +37,7 @@ use ckb_merkle_mountain_range::{
util::{MemMMR, MemStore},
MerkleProof,
};
use codec::{Decode, Encode};
use etf_crypto_primitives::{encryption::tlock::*, ibe::fullident::Identity};
use sha3::Digest;
use w3f_bls::{DoublePublicKey, EngineBLS};
Expand All @@ -55,15 +56,15 @@ pub enum Error {
TlockFailed,
/// The buffer does not have enough space allocated
InvalidBufferSize,
/// The seed was invalid
/// The seed was invalid
InvalidSeed,
/// The public key was invalid (could not be decoded)
InvalidPubkey,
}

/// The murmur store contains minimal data required to use a murmur wallet
#[cfg(feature = "client")]
#[derive(Clone, serde::Serialize, serde::Deserialize)]
#[derive(Clone, serde::Serialize, serde::Deserialize, Encode, Decode)]
pub struct MurmurStore {
/// A map of block numbers to leaf positions in the mmr
pub metadata: BTreeMap<BlockNumber, Ciphertext>,
Expand Down Expand Up @@ -194,8 +195,8 @@ pub fn timelock_encrypt<E: EngineBLS, R: CryptoRng + Rng + Sized>(
message: &[u8],
rng: R,
) -> Result<Vec<u8>, Error> {
let ciphertext = tle::<E, R>(pk, ephemeral_msk, message, identity, rng)
.map_err(|_| Error::TlockFailed)?;
let ciphertext =
tle::<E, R>(pk, ephemeral_msk, message, identity, rng).map_err(|_| Error::TlockFailed)?;
let mut ct_bytes = Vec::new();
ciphertext
.serialize_compressed(&mut ct_bytes)
Expand All @@ -210,8 +211,7 @@ fn build_generator(mut seed: Vec<u8>) -> Result<BOTPGenerator, Error> {
hasher.update(&seed);
seed.zeroize();
let hash = hasher.finalize();
BOTPGenerator::new(hash.to_vec())
.map_err(|_| Error::InvalidSeed)
BOTPGenerator::new(hash.to_vec()).map_err(|_| Error::InvalidSeed)
}

// verify the correctness of execution parameters
Expand Down Expand Up @@ -249,10 +249,10 @@ pub fn get_key_index<K: Ord>(b: &BTreeMap<K, impl alloc::fmt::Debug>, key: &K) -
mod tests {

use super::*;
use w3f_bls::{DoublePublicKeyScheme, TinyBLS377};
use ark_std::rand::SeedableRng;
use rand_chacha::ChaCha20Rng;
use rand_core::OsRng;
use ark_std::rand::SeedableRng;
use w3f_bls::{DoublePublicKeyScheme, TinyBLS377};

pub struct DummyIdBuilder;
impl IdentityBuilder<BlockNumber> for DummyIdBuilder {
Expand Down
13 changes: 7 additions & 6 deletions core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
* limitations under the License.
*/

use alloc::vec::Vec;
use ckb_merkle_mountain_range::{Merge, Result as MMRResult};
pub use etf_crypto_primitives::ibe::fullident::Identity;
use codec::{Decode, Encode};
use sha3::Digest;
use alloc::vec::Vec;

pub use etf_crypto_primitives::ibe::fullident::Identity;

/// The type to represent a block number
pub type BlockNumber = u32;
Expand All @@ -28,8 +30,7 @@ pub type Ciphertext = Vec<u8>;
/// A leaf in the MMR
/// The payload is an opaque, any-length vec
#[derive(
Eq, PartialEq, Clone, Debug, Default,
serde::Serialize, serde::Deserialize
Eq, PartialEq, Clone, Debug, Default, serde::Serialize, serde::Deserialize, Encode, Decode,
)]
pub struct Leaf(pub Vec<u8>);
impl From<Vec<u8>> for Leaf {
Expand All @@ -47,7 +48,7 @@ pub struct MergeLeaves;
impl Merge for MergeLeaves {
type Item = Leaf;
fn merge(lhs: &Self::Item, rhs: &Self::Item) -> MMRResult<Self::Item> {
let mut hasher = sha3::Sha3_256::default();
let mut hasher = sha3::Sha3_256::default();
hasher.update(&lhs.0);
hasher.update(&rhs.0);
let hash = hasher.finalize();
Expand All @@ -58,4 +59,4 @@ impl Merge for MergeLeaves {
/// Something that builds unique identities (e.g. using crypto hash function) for any block number
pub trait IdentityBuilder<BlockNumber> {
fn build_identity(at: BlockNumber) -> Identity;
}
}

0 comments on commit 7365f01

Please sign in to comment.