Skip to content

Commit

Permalink
feat: put alltogether memories
Browse files Browse the repository at this point in the history
  • Loading branch information
HatemMn committed Dec 2, 2024
1 parent dc3a1f9 commit af8d9e0
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 153 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ redis = { version = "0.27.5", features = [
"tokio-comp",
"connection-manager",
], optional = true }
cosmian_crypto_core = "9.6.1"

[dev-dependencies]
criterion = "0.5.1"
Expand Down
4 changes: 2 additions & 2 deletions benches/benches.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{collections::HashSet, time::Duration};

use cosmian_findex::{Findex, InMemory, IndexADT, MemoryADT, Op, Secret, WORD_LENGTH};
#[cfg(feature = "test-utils")]
use cosmian_findex::{dummy_decode, dummy_encode};
use cosmian_findex::{Findex, InMemory, IndexADT, MemoryADT, Op, Secret, WORD_LENGTH};
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
use futures::{executor::block_on, future::join_all};
use lazy_static::lazy_static;
use rand_chacha::ChaChaRng;
Expand Down
2 changes: 1 addition & 1 deletion examples/insert.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::HashSet;

use cosmian_findex::{Findex, InMemory, IndexADT, Secret, Value};
#[cfg(feature = "test-utils")]
use cosmian_findex::{dummy_decode, dummy_encode};
use cosmian_findex::{Findex, InMemory, IndexADT, Secret, Value};
use futures::executor::block_on;
use rand_chacha::ChaChaRng;
use rand_core::{CryptoRngCore, SeedableRng};
Expand Down
1 change: 1 addition & 0 deletions rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nightly-x86_64-unknown-linux-gnu
59 changes: 26 additions & 33 deletions src/encryption_layer.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::{fmt::Debug, ops::Deref, sync::Arc};

use aes::{
cipher::{generic_array::GenericArray, BlockEncrypt, KeyInit},
Aes256,
cipher::{BlockEncrypt, KeyInit, generic_array::GenericArray},
};
use xts_mode::Xts128;

use crate::{
address::Address, error::Error, secret::Secret, symmetric_key::SymmetricKey, MemoryADT,
ADDRESS_LENGTH, KEY_LENGTH,
ADDRESS_LENGTH, KEY_LENGTH, MemoryADT, address::Address, error::Error, secret::Secret,
symmetric_key::SymmetricKey,
};

#[derive(Clone)]
Expand Down Expand Up @@ -44,9 +44,9 @@ pub struct MemoryEncryptionLayer<
}

impl<
const WORD_LENGTH: usize,
Memory: Send + Sync + MemoryADT<Address = Address<ADDRESS_LENGTH>, Word = [u8; WORD_LENGTH]>,
> MemoryEncryptionLayer<WORD_LENGTH, Memory>
const WORD_LENGTH: usize,
Memory: Send + Sync + MemoryADT<Address = Address<ADDRESS_LENGTH>, Word = [u8; WORD_LENGTH]>,
> MemoryEncryptionLayer<WORD_LENGTH, Memory>
{
/// Instantiates a new memory encryption layer.
pub fn new(seed: Secret<KEY_LENGTH>, stm: Memory) -> Self {
Expand Down Expand Up @@ -80,9 +80,9 @@ impl<
}

impl<
const WORD_LENGTH: usize,
Memory: Send + Sync + MemoryADT<Address = Address<ADDRESS_LENGTH>, Word = [u8; WORD_LENGTH]>,
> MemoryADT for MemoryEncryptionLayer<WORD_LENGTH, Memory>
const WORD_LENGTH: usize,
Memory: Send + Sync + MemoryADT<Address = Address<ADDRESS_LENGTH>, Word = [u8; WORD_LENGTH]>,
> MemoryADT for MemoryEncryptionLayer<WORD_LENGTH, Memory>
{
type Address = Address<ADDRESS_LENGTH>;
type Error = Error<Self::Address, Memory::Error>;
Expand Down Expand Up @@ -126,20 +126,20 @@ impl<
#[cfg(test)]
mod tests {
use aes::{
cipher::{generic_array::GenericArray, BlockDecrypt, KeyInit},
Aes256,
cipher::{BlockDecrypt, KeyInit, generic_array::GenericArray},
};
use futures::executor::block_on;
use rand_chacha::ChaChaRng;
use rand_core::SeedableRng;

use crate::{
MemoryADT,
address::Address,
encryption_layer::{MemoryEncryptionLayer, ADDRESS_LENGTH},
encryption_layer::{ADDRESS_LENGTH, MemoryEncryptionLayer},
memory::in_memory_store::InMemory,
secret::Secret,
symmetric_key::SymmetricKey,
MemoryADT,
};

const WORD_LENGTH: usize = 128;
Expand Down Expand Up @@ -192,40 +192,33 @@ mod tests {
let val_addr_4 = Address::<ADDRESS_LENGTH>::random(&mut rng);

assert_eq!(
block_on(obf.guarded_write(
(header_addr.clone(), None),
vec![
(header_addr.clone(), [2; WORD_LENGTH]),
(val_addr_1.clone(), [1; WORD_LENGTH]),
(val_addr_2.clone(), [1; WORD_LENGTH])
]
))
block_on(obf.guarded_write((header_addr.clone(), None), vec![
(header_addr.clone(), [2; WORD_LENGTH]),
(val_addr_1.clone(), [1; WORD_LENGTH]),
(val_addr_2.clone(), [1; WORD_LENGTH])
]))
.unwrap(),
None
);

assert_eq!(
block_on(obf.guarded_write(
(header_addr.clone(), None),
vec![
(header_addr.clone(), [2; WORD_LENGTH]),
(val_addr_1.clone(), [3; WORD_LENGTH]),
(val_addr_2.clone(), [3; WORD_LENGTH])
]
))
block_on(obf.guarded_write((header_addr.clone(), None), vec![
(header_addr.clone(), [2; WORD_LENGTH]),
(val_addr_1.clone(), [3; WORD_LENGTH]),
(val_addr_2.clone(), [3; WORD_LENGTH])
]))
.unwrap(),
Some([2; WORD_LENGTH])
);

assert_eq!(
block_on(obf.guarded_write(
(header_addr.clone(), Some([2; WORD_LENGTH])),
vec![
block_on(
obf.guarded_write((header_addr.clone(), Some([2; WORD_LENGTH])), vec![
(header_addr.clone(), [4; WORD_LENGTH]),
(val_addr_3.clone(), [2; WORD_LENGTH]),
(val_addr_4.clone(), [2; WORD_LENGTH])
]
))
])
)
.unwrap(),
Some([2; WORD_LENGTH])
);
Expand Down
45 changes: 0 additions & 45 deletions src/findex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,49 +263,4 @@ mod tests {
res
);
}

#[cfg(feature = "redis-mem")]
fn get_redis_url() -> String {
if let Ok(var_env) = std::env::var("REDIS_HOST") {
format!("redis://{var_env}:6379")
} else {
"redis://localhost:6379".to_owned()
}
}

#[tokio::test]
#[cfg(feature = "redis-mem")]
async fn test_redis_insert_search_delete_search() {
use crate::RedisStore;

let mut rng = ChaChaRng::from_entropy();
let seed = Secret::random(&mut rng);
const TEST_ADR_WORD_LENGTH: usize = 16;
let memory = RedisStore::<Address<TEST_ADR_WORD_LENGTH>, TEST_ADR_WORD_LENGTH>::connect(
&get_redis_url(),
)
.await
.unwrap();
let findex = Findex::new(seed, memory, dummy_encode::<WORD_LENGTH, _>, dummy_decode);
let bindings = HashMap::<&str, HashSet<Value>>::from_iter([
(
"cat",
HashSet::from_iter([Value::from(1), Value::from(3), Value::from(5)]),
),
(
"dog",
HashSet::from_iter([Value::from(0), Value::from(2), Value::from(4)]),
),
]);
findex.insert(bindings.clone().into_iter()).await.unwrap(); // using block_on here causes a never ending execution
let res = findex.search(bindings.keys().copied()).await.unwrap();
assert_eq!(bindings, res);

findex.delete(bindings.clone().into_iter()).await.unwrap();
let res = findex.search(bindings.keys().copied()).await.unwrap();
assert_eq!(
HashMap::from_iter([("cat", HashSet::new()), ("dog", HashSet::new())]),
res
);
}
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ pub use encoding::{Op, WORD_LENGTH};
pub use encoding::{dummy_decode, dummy_encode};
pub use error::Error;
pub use findex::Findex;
#[cfg(feature = "redis-mem")]
pub use memory::db_stores::redis_store::RedisStore;
#[cfg(feature = "bench")]
pub use memory::in_memory_store::InMemory;
#[cfg(feature = "redis-mem")]
pub use memory::redis_store::RedisStore;
pub use secret::Secret;
#[cfg(feature = "test-utils")]
pub use test::memory::{
Expand Down
4 changes: 0 additions & 4 deletions src/memory/db_stores/mod.rs

This file was deleted.

10 changes: 0 additions & 10 deletions src/memory/db_stores/error.rs → src/memory/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use core::fmt::Display;
use std::num::TryFromIntError;

use cosmian_crypto_core::CryptoCoreError;

#[cfg(feature = "redis-mem")]
use super::redis_store::RedisStoreError;
use crate::{Address, error::Error as FindexCoreError};
Expand All @@ -17,7 +15,6 @@ pub(crate) enum DbStoreError {
#[cfg(feature = "redis-mem")]
Redis(RedisStoreError),
Findex(findex_core_error!()),
CryptoCore(CryptoCoreError),
IntConversion(TryFromIntError),
Io(std::io::Error),
}
Expand All @@ -27,7 +24,6 @@ impl Display for DbStoreError {
match self {
#[cfg(feature = "redis-mem")]
Self::Redis(err) => write!(f, "redis: {err}"),
Self::CryptoCore(err) => write!(f, "crypto_core: {err}"),
Self::Findex(err) => write!(f, "findex: {err}"),
Self::Io(err) => write!(f, "io: {err}"),
Self::IntConversion(err) => write!(f, "conversion: {err}"),
Expand All @@ -52,12 +48,6 @@ impl From<TryFromIntError> for DbStoreError {
}
}

impl From<CryptoCoreError> for DbStoreError {
fn from(e: CryptoCoreError) -> Self {
Self::CryptoCore(e)
}
}

impl From<findex_core_error!()> for DbStoreError {
fn from(e: findex_core_error!()) -> Self {
Self::Findex(e)
Expand Down
7 changes: 3 additions & 4 deletions src/memory/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// pub mod in_memory_store;
pub(crate) mod in_memory_store;

pub mod error;
pub mod in_memory_store;
#[cfg(feature = "redis-mem")]
pub(crate) mod db_stores;
pub mod redis_store;
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
//! Redis implementation of the Findex backends.
use std::{
fmt::{self, Debug, Display},
hash::Hash,
marker::PhantomData,
ops::Deref,
};

use redis::{aio::ConnectionManager, AsyncCommands};
use redis::{AsyncCommands, aio::ConnectionManager};

use crate::MemoryADT;

Expand Down Expand Up @@ -104,10 +103,10 @@ impl Display for RedisStoreError {
}

impl<
Address: Send + Sync + Hash + Eq + Debug + Clone + Deref<Target = [u8; ADDRESS_LENGTH]>,
const ADDRESS_LENGTH: usize,
const WORD_LENGTH: usize,
> MemoryADT for RedisStore<Address, WORD_LENGTH>
Address: Send + Sync + Hash + Eq + Debug + Clone + Deref<Target = [u8; ADDRESS_LENGTH]>,
const ADDRESS_LENGTH: usize,
const WORD_LENGTH: usize,
> MemoryADT for RedisStore<Address, WORD_LENGTH>
{
type Address = Address;
type Error = RedisStoreError;
Expand Down Expand Up @@ -158,10 +157,10 @@ mod tests {

use super::*;
use crate::{
Address,
test::memory::{
test_guarded_write_concurrent, test_single_write_and_read, test_wrong_guard,
},
Address,
};

pub(crate) fn get_redis_url() -> String {
Expand Down
28 changes: 14 additions & 14 deletions src/ovec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use std::{fmt::Debug, hash::Hash, ops::Add};

use crate::{adt::VectorADT, error::Error, MemoryADT};
use crate::{MemoryADT, adt::VectorADT, error::Error};

/// Headers contain a counter of the number of values stored in the vector.
// TODO: header could store metadata (e.g. sparsity budget)
Expand Down Expand Up @@ -80,10 +80,10 @@ pub(crate) struct IVec<
}

impl<
const WORD_LENGTH: usize,
Address: Clone,
Memory: Clone + MemoryADT<Address = Address, Word = [u8; WORD_LENGTH]>,
> Clone for IVec<WORD_LENGTH, Memory>
const WORD_LENGTH: usize,
Address: Clone,
Memory: Clone + MemoryADT<Address = Address, Word = [u8; WORD_LENGTH]>,
> Clone for IVec<WORD_LENGTH, Memory>
{
fn clone(&self) -> Self {
Self {
Expand All @@ -95,10 +95,10 @@ impl<
}

impl<
const WORD_LENGTH: usize,
Address: Hash + Eq + Debug + Clone + Add<u64, Output = Address>,
Memory: Clone + MemoryADT<Address = Address, Word = [u8; WORD_LENGTH]>,
> IVec<WORD_LENGTH, Memory>
const WORD_LENGTH: usize,
Address: Hash + Eq + Debug + Clone + Add<u64, Output = Address>,
Memory: Clone + MemoryADT<Address = Address, Word = [u8; WORD_LENGTH]>,
> IVec<WORD_LENGTH, Memory>
{
/// (Lazily) instantiates a new vector at this address in this memory: no
/// value is written before the first push.
Expand All @@ -108,10 +108,10 @@ impl<
}

impl<
const WORD_LENGTH: usize,
Address: Send + Sync + Hash + Eq + Debug + Clone + Add<u64, Output = Address>,
Memory: Send + Sync + Clone + MemoryADT<Address = Address, Word = [u8; WORD_LENGTH]>,
> VectorADT for IVec<WORD_LENGTH, Memory>
const WORD_LENGTH: usize,
Address: Send + Sync + Hash + Eq + Debug + Clone + Add<u64, Output = Address>,
Memory: Send + Sync + Clone + MemoryADT<Address = Address, Word = [u8; WORD_LENGTH]>,
> VectorADT for IVec<WORD_LENGTH, Memory>
where
Memory::Error: Send + Sync,
{
Expand Down Expand Up @@ -217,13 +217,13 @@ mod tests {
use rand_core::SeedableRng;

use crate::{
ADDRESS_LENGTH,
address::Address,
adt::tests::{test_vector_concurrent, test_vector_sequential},
encryption_layer::MemoryEncryptionLayer,
memory::in_memory_store::InMemory,
ovec::IVec,
secret::Secret,
ADDRESS_LENGTH,
};

const WORD_LENGTH: usize = 16;
Expand Down
Loading

0 comments on commit af8d9e0

Please sign in to comment.