Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

review #87

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cosmian_crypto_core::{
CsRng, Secret,
};
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use findex::{dummy_decode, dummy_encode, Findex, IndexADT, KvStore, MemoryADT, Op, WORD_LENGTH};
use findex::{dummy_decode, dummy_encode, Findex, IndexADT, InMemory, MemoryADT, Op, WORD_LENGTH};
use futures::{executor::block_on, future::join_all};
use lazy_static::lazy_static;

Expand Down Expand Up @@ -54,7 +54,7 @@ fn build_benchmarking_keywords_index(
fn bench_search_multiple_bindings(c: &mut Criterion) {
let mut rng = CsRng::from_entropy();
let seed = Secret::random(&mut rng);
let stm = KvStore::default();
let stm = InMemory::default();
let index = build_benchmarking_bindings_index(&mut rng);
let findex = Findex::new(
seed.clone(),
Expand Down Expand Up @@ -85,7 +85,7 @@ fn bench_search_multiple_keywords(c: &mut Criterion) {
let mut rng = CsRng::from_entropy();
let seed = Secret::random(&mut rng);

let stm = KvStore::default();
let stm = InMemory::default();
let index = build_benchmarking_keywords_index(&mut rng);
let findex = Findex::new(
seed,
Expand Down Expand Up @@ -150,7 +150,7 @@ fn bench_insert_multiple_bindings(c: &mut Criterion) {
{
let mut group = c.benchmark_group("write n words to memory");
for (_, vals) in index.clone().into_iter() {
let stm = KvStore::with_capacity(n_max + 1);
let stm = InMemory::with_capacity(n_max + 1);
group
.bench_function(BenchmarkId::from_parameter(vals.len()), |b| {
b.iter_batched(
Expand Down Expand Up @@ -178,7 +178,7 @@ fn bench_insert_multiple_bindings(c: &mut Criterion) {
{
let mut group = c.benchmark_group("Multiple bindings insert (same keyword)");
for (kw, vals) in index.clone().into_iter() {
let stm = KvStore::with_capacity(n_max + 1);
let stm = InMemory::with_capacity(n_max + 1);
let findex = Findex::new(
seed.clone(),
stm.clone(),
Expand Down Expand Up @@ -213,7 +213,7 @@ fn bench_insert_multiple_keywords(c: &mut Criterion) {
let mut group = c.benchmark_group("write 2n words to memory");
for i in scale.iter() {
let n = 10f32.powf(*i).ceil() as usize;
let stm = KvStore::with_capacity(2 * n);
let stm = InMemory::with_capacity(2 * n);
group
.bench_function(BenchmarkId::from_parameter(n), |b| {
b.iter_batched(
Expand Down Expand Up @@ -244,7 +244,7 @@ fn bench_insert_multiple_keywords(c: &mut Criterion) {
let mut group = c.benchmark_group("Multiple keywords insert (one binding each)");
for i in scale.iter() {
let n = 10f32.powf(*i).ceil() as usize;
let stm = KvStore::with_capacity(2 * n);
let stm = InMemory::with_capacity(2 * n);
let findex = Findex::new(
seed.clone(),
stm.clone(),
Expand Down Expand Up @@ -292,7 +292,7 @@ fn bench_contention(c: &mut Criterion) {
let mut group =
c.benchmark_group("Parallel clients ({N_BINDINGS} binding, different keywords)");
for i in 1..=N_CLIENTS {
let stm = KvStore::with_capacity(N_BINDINGS * i + 1);
let stm = InMemory::with_capacity(N_BINDINGS * i + 1);
let findex = Findex::new(
seed.clone(),
stm.clone(),
Expand Down Expand Up @@ -346,7 +346,7 @@ fn bench_contention(c: &mut Criterion) {
{
let mut group = c.benchmark_group("Concurrent clients (single binding, same keyword)");
for i in 1..=N_CLIENTS {
let stm = KvStore::with_capacity(N_BINDINGS * i + 1);
let stm = InMemory::with_capacity(N_BINDINGS * i + 1);
let findex = Findex::new(
seed.clone(),
stm.clone(),
Expand Down
4 changes: 2 additions & 2 deletions examples/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use cosmian_crypto_core::{
reexport::rand_core::{CryptoRngCore, SeedableRng},
CsRng, Secret,
};
use findex::{dummy_decode, dummy_encode, Findex, IndexADT, KvStore, Value};
use findex::{dummy_decode, dummy_encode, Findex, IndexADT, InMemory, Value};
use futures::executor::block_on;

fn build_benchmarking_index(rng: &mut impl CryptoRngCore) -> Vec<([u8; 8], HashSet<Value>)> {
Expand All @@ -25,7 +25,7 @@ fn main() {
let seed = Secret::random(&mut rng);
let findex = Findex::new(
seed,
KvStore::default(),
InMemory::default(),
dummy_encode::<16, _>,
dummy_decode,
);
Expand Down
5 changes: 2 additions & 3 deletions src/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ use std::{
};

/// An index stores *bindings*, that associate a keyword with a value. All values bound to the same
/// keyword are said to be *indexed under* this keyword. The number of such values is called the
/// volume of a keyword.
/// keyword are said to be *indexed under* this keyword.
pub trait IndexADT<Keyword: Send + Sync + Hash, Value: Send + Sync + Hash> {
type Error: Send + Sync + std::error::Error;

Expand Down Expand Up @@ -71,7 +70,7 @@ pub trait MemoryADT {
) -> impl Send + Sync + Future<Output = Result<Vec<Option<Self::Word>>, Self::Error>>;

/// Write the given words at the given addresses if the word currently stored at the guard
/// address is the one given, and returns this guard word.
/// address is the given one, and returns this guard word.
fn guarded_write(
&self,
guard: (Self::Address, Option<Self::Word>),
Expand Down
4 changes: 2 additions & 2 deletions src/encoding.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module defines encoding operations that are used to serialize an operation.
//! Currently, the only supported operations are the insertion and deletion, but there is no
//! theorical restriction on the kind of operation that can be used.
//! theoretical restriction on the kind of operation that can be used.

#![allow(dead_code)]

Expand Down Expand Up @@ -98,7 +98,7 @@ pub fn dummy_encode<const WORD_LENGTH: usize, Value: AsRef<[u8]>>(
let bytes = v.as_ref();
if WORD_LENGTH - 2 < bytes.len() {
return Err(format!(
"unsufficient bytes in a word to fit a value of length {}",
"insufficient bytes in a word to fit a value of length {}",
bytes.len(),
));
}
Expand Down
16 changes: 8 additions & 8 deletions src/encryption_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ mod tests {
use crate::{
address::Address,
encryption_layer::{MemoryEncryptionLayer, ADDRESS_LENGTH},
kv::KvStore,
in_memory_store::InMemory,
MemoryADT,
};

Expand All @@ -145,8 +145,8 @@ mod tests {
let seed = Secret::random(&mut rng);
let k_p = SymmetricKey::<32>::derive(&seed, &[0]).expect("secret is large enough");
let aes = Aes256::new(GenericArray::from_slice(&k_p));
let kv = KvStore::<Address<ADDRESS_LENGTH>, [u8; WORD_LENGTH]>::default();
let obf = MemoryEncryptionLayer::new(seed, kv);
let memory = InMemory::<Address<ADDRESS_LENGTH>, [u8; WORD_LENGTH]>::default();
let obf = MemoryEncryptionLayer::new(seed, memory);
let a = Address::<ADDRESS_LENGTH>::random(&mut rng);
let mut tok = obf.permute(a.clone());
assert_ne!(a, tok);
Expand All @@ -158,8 +158,8 @@ mod tests {
fn test_encrypt_decrypt() {
let mut rng = CsRng::from_entropy();
let seed = Secret::random(&mut rng);
let kv = KvStore::<Address<ADDRESS_LENGTH>, [u8; WORD_LENGTH]>::default();
let obf = MemoryEncryptionLayer::new(seed, kv);
let memory = InMemory::<Address<ADDRESS_LENGTH>, [u8; WORD_LENGTH]>::default();
let obf = MemoryEncryptionLayer::new(seed, memory);
let tok = Address::<ADDRESS_LENGTH>::random(&mut rng);
let ptx = [1; WORD_LENGTH];
let ctx = obf.encrypt(ptx, *tok);
Expand All @@ -168,15 +168,15 @@ mod tests {
assert_eq!(ptx, res);
}

/// Ensures a transaction can express an vector push operation:
/// Ensures a transaction can express a vector push operation:
/// - the counter is correctly incremented and all values are written;
/// - using the wrong value in the guard fails the operation and returns the current value.
#[test]
fn test_vector_push() {
let mut rng = CsRng::from_entropy();
let seed = Secret::random(&mut rng);
let kv = KvStore::<Address<ADDRESS_LENGTH>, [u8; WORD_LENGTH]>::default();
let obf = MemoryEncryptionLayer::new(seed, kv);
let memory = InMemory::<Address<ADDRESS_LENGTH>, [u8; WORD_LENGTH]>::default();
let obf = MemoryEncryptionLayer::new(seed, memory);

let header_addr = Address::<ADDRESS_LENGTH>::random(&mut rng);

Expand Down
6 changes: 3 additions & 3 deletions src/findex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ mod tests {
use crate::{
address::Address,
encoding::{dummy_decode, dummy_encode},
kv::KvStore,
in_memory_store::InMemory,
Findex, IndexADT, Value, ADDRESS_LENGTH,
};

Expand All @@ -238,8 +238,8 @@ mod tests {
fn test_insert_search_delete_search() {
let mut rng = CsRng::from_entropy();
let seed = Secret::random(&mut rng);
let kv = KvStore::<Address<ADDRESS_LENGTH>, [u8; WORD_LENGTH]>::default();
let findex = Findex::new(seed, kv, dummy_encode::<WORD_LENGTH, _>, dummy_decode);
let memory = InMemory::<Address<ADDRESS_LENGTH>, [u8; WORD_LENGTH]>::default();
let findex = Findex::new(seed, memory, dummy_encode::<WORD_LENGTH, _>, dummy_decode);
let bindings = HashMap::<&str, HashSet<Value>>::from_iter([
(
"cat",
Expand Down
24 changes: 12 additions & 12 deletions src/kv.rs → src/in_memory_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ impl Display for MemoryError {
impl std::error::Error for MemoryError {}

#[derive(Clone, Debug)]
pub struct KvStore<Address: Hash + Eq, Value> {
pub struct InMemory<Address: Hash + Eq, Value> {
inner: Arc<Mutex<HashMap<Address, Value>>>,
}

impl<Address: Hash + Eq + Debug, Value: Clone + Eq + Debug> Default for KvStore<Address, Value> {
impl<Address: Hash + Eq + Debug, Value: Clone + Eq + Debug> Default for InMemory<Address, Value> {
fn default() -> Self {
Self {
inner: Arc::new(Mutex::new(HashMap::new())),
}
}
}

impl<Address: Hash + Eq + Debug, Value: Clone + Eq + Debug> KvStore<Address, Value> {
impl<Address: Hash + Eq + Debug, Value: Clone + Eq + Debug> InMemory<Address, Value> {
#[cfg(feature = "bench")]
pub fn with_capacity(c: usize) -> Self {
Self {
Expand All @@ -45,7 +45,7 @@ impl<Address: Hash + Eq + Debug, Value: Clone + Eq + Debug> KvStore<Address, Val
}

impl<Address: Send + Sync + Hash + Eq + Debug, Value: Send + Sync + Clone + Eq + Debug> MemoryADT
for KvStore<Address, Value>
for InMemory<Address, Value>
{
type Address = Address;

Expand Down Expand Up @@ -77,7 +77,7 @@ impl<Address: Send + Sync + Hash + Eq + Debug, Value: Send + Sync + Clone + Eq +

#[cfg(feature = "bench")]
impl<Address: Hash + Eq + Debug + Clone, Value: Clone + Eq + Debug> IntoIterator
for KvStore<Address, Value>
for InMemory<Address, Value>
{
type Item = (Address, Value);

Expand All @@ -99,30 +99,30 @@ mod tests {

use crate::MemoryADT;

use super::KvStore;
use super::InMemory;

/// Ensures a transaction can express an vector push operation:
/// Ensures a transaction can express a vector push operation:
/// - the counter is correctly incremented and all values are written;
/// - using the wrong value in the guard fails the operation and returns the current value.
#[test]
fn test_vector_push() {
let kv = KvStore::<u8, u8>::default();
let memory = InMemory::<u8, u8>::default();

assert_eq!(
block_on(kv.guarded_write((0, None), vec![(0, 2), (1, 1), (2, 1)])).unwrap(),
block_on(memory.guarded_write((0, None), vec![(0, 2), (1, 1), (2, 1)])).unwrap(),
None
);
assert_eq!(
block_on(kv.guarded_write((0, None), vec![(0, 4), (3, 2), (4, 2)])).unwrap(),
block_on(memory.guarded_write((0, None), vec![(0, 4), (3, 2), (4, 2)])).unwrap(),
Some(2)
);
assert_eq!(
block_on(kv.guarded_write((0, Some(2)), vec![(0, 4), (3, 3), (4, 3)])).unwrap(),
block_on(memory.guarded_write((0, Some(2)), vec![(0, 4), (3, 3), (4, 3)])).unwrap(),
Some(2)
);
assert_eq!(
vec![Some(1), Some(1), Some(3), Some(3)],
block_on(kv.batch_read(vec![1, 2, 3, 4])).unwrap(),
block_on(memory.batch_read(vec![1, 2, 3, 4])).unwrap(),
)
}
}
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ pub use findex::Findex;
pub use value::Value;

#[cfg(any(test, feature = "bench"))]
mod kv;
mod in_memory_store;
#[cfg(feature = "bench")]
pub use encoding::{dummy_decode, dummy_encode, Op, WORD_LENGTH};
#[cfg(feature = "bench")]
pub use kv::KvStore;
pub use in_memory_store::InMemory;

/// 16-byte addresses ensure a high collision resistance that poses virtually no limitation on the
/// index.
Expand All @@ -35,6 +35,5 @@ pub const ADDRESS_LENGTH: usize = 16;
#[cfg(feature = "small")]
pub const ADDRESS_LENGTH: usize = 16;

/// Using 32-byte cryptographic keys allow achieving post-quantum resistance if the adequate
/// primitives are used (e.g. AES).
/// Using 32-byte cryptographic keys allows achieving post-quantum resistance with the AES primitive.
pub const KEY_LENGTH: usize = 64;
12 changes: 6 additions & 6 deletions src/ovec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ where
// ever terminate.
//
// TODO: this loop will arguably terminate if the index is not highly contended, but we
// need a stronger guarantee. Maybe a return with an error after a reaching a certain
// need a stronger guarantee. Maybe a return with an error after reaching a certain
// number of retries.
loop {
let (cur, new) = {
// Generates a new header which counter is incremented.
// Generates a new header with incremented counter.
let mut new = self.h.clone().unwrap_or_default();
new.cnt += vs.len() as u64;

Expand Down Expand Up @@ -214,7 +214,7 @@ mod tests {
address::Address,
adt::tests::{test_vector_concurrent, test_vector_sequential},
encryption_layer::MemoryEncryptionLayer,
kv::KvStore,
in_memory_store::InMemory,
ovec::IVec,
ADDRESS_LENGTH,
};
Expand All @@ -226,12 +226,12 @@ mod tests {
async fn test_ovec() {
let mut rng = CsRng::from_entropy();
let seed = Secret::random(&mut rng);
let kv = KvStore::<Address<ADDRESS_LENGTH>, [u8; WORD_LENGTH]>::default();
let obf = MemoryEncryptionLayer::new(seed, kv.clone());
let memory = InMemory::<Address<ADDRESS_LENGTH>, [u8; WORD_LENGTH]>::default();
let obf = MemoryEncryptionLayer::new(seed, memory.clone());
let address = Address::random(&mut rng);
let v = IVec::<WORD_LENGTH, _>::new(address.clone(), obf);
test_vector_sequential(&v).await;
kv.clear();
memory.clear();
test_vector_concurrent(&v).await;
}
}
Loading