Skip to content

Commit

Permalink
fix: implement deref for ET/CT
Browse files Browse the repository at this point in the history
  • Loading branch information
tbrezot committed Aug 7, 2023
1 parent 3ce3f62 commit dcfa66c
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 52 deletions.
12 changes: 6 additions & 6 deletions refactored/benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ fn bench_search(c: &mut Criterion) {

println!(
"Entry Table length: {}",
findex.findex_graph.findex_mm.entry_table.edx.len()
findex.findex_graph.findex_mm.entry_table.len()
);
println!(
"Entry Table length: {}",
findex.findex_graph.findex_mm.entry_table.edx.size()
findex.findex_graph.findex_mm.entry_table.size()
);
println!(
"Chain Table length: {}",
findex.findex_graph.findex_mm.chain_table.edx.len()
findex.findex_graph.findex_mm.chain_table.len()
);
println!(
"Chain Table length: {}",
findex.findex_graph.findex_mm.chain_table.edx.size()
findex.findex_graph.findex_mm.chain_table.size()
);

for power in 0..=3 {
Expand Down Expand Up @@ -108,8 +108,8 @@ fn bench_upsert(c: &mut Criterion) {
b.iter(|| {
block_on(findex.add(&master_key, &label, locations_and_words.clone()))
.expect("upsert failed");
findex.findex_graph.findex_mm.entry_table.edx.flush();
findex.findex_graph.findex_mm.chain_table.edx.flush();
findex.findex_graph.findex_mm.entry_table.0.flush();
findex.findex_graph.findex_mm.chain_table.0.flush();
});
});
}
Expand Down
22 changes: 15 additions & 7 deletions refactored/src/edx/chain_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use std::{
collections::{HashMap, HashSet},
ops::DerefMut,
ops::{Deref, DerefMut},
};

use cosmian_crypto_core::{kdf256, reexport::rand_core::CryptoRngCore, SymmetricKey};
Expand All @@ -23,8 +23,16 @@ use crate::{
};

/// Chain Table representation.
pub struct ChainTable<const VALUE_LENGTH: usize, Edx: EdxStore<VALUE_LENGTH>> {
pub edx: Edx,
pub struct ChainTable<const VALUE_LENGTH: usize, Edx: EdxStore<VALUE_LENGTH>>(pub Edx);

impl<const VALUE_LENGTH: usize, Edx: EdxStore<VALUE_LENGTH>> Deref
for ChainTable<VALUE_LENGTH, Edx>
{
type Target = Edx;

fn deref(&self) -> &Self::Target {
&self.0
}
}

const CHAIN_TABLE_KEY_DERIVATION_INFO: &[u8] = b"Chain Table key derivation info.";
Expand All @@ -42,7 +50,7 @@ impl<
type Token = EdxScheme::Token;

fn setup(edx: Self::EdxScheme) -> Self {
Self { edx }
Self(edx)
}

fn gen_seed(&self, rng: &mut impl CryptoRngCore) -> Self::Seed {
Expand Down Expand Up @@ -89,7 +97,7 @@ impl<
&self,
tokens: HashSet<Self::Token>,
) -> Result<HashMap<Self::Token, Self::EncryptedValue>, Self::Error> {
self.edx.fetch(tokens).await.map_err(Error::Callback)
self.0.fetch(tokens).await.map_err(Error::Callback)
}

fn resolve(
Expand Down Expand Up @@ -121,11 +129,11 @@ impl<
&mut self,
items: HashMap<Self::Token, Self::EncryptedValue>,
) -> Result<(), Self::Error> {
self.edx.insert(items).await.map_err(Error::Callback)
self.0.insert(items).await.map_err(Error::Callback)
}

async fn delete(&mut self, items: HashSet<Self::Token>) -> Result<(), Self::Error> {
self.edx.delete(items).await.map_err(Error::Callback)
self.0.delete(items).await.map_err(Error::Callback)
}
}

Expand Down
24 changes: 16 additions & 8 deletions refactored/src/edx/entry_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use std::{
collections::{HashMap, HashSet},
ops::DerefMut,
ops::{Deref, DerefMut},
};

use cosmian_crypto_core::{kdf256, reexport::rand_core::CryptoRngCore, SymmetricKey};
Expand All @@ -22,8 +22,16 @@ use crate::{
};

/// Implementation of the Entry Table EDX.
pub struct EntryTable<const VALUE_LENGTH: usize, Edx: EdxStore<VALUE_LENGTH>> {
pub edx: Edx,
pub struct EntryTable<const VALUE_LENGTH: usize, Edx: EdxStore<VALUE_LENGTH>>(pub Edx);

impl<const VALUE_LENGTH: usize, Edx: EdxStore<VALUE_LENGTH>> Deref
for EntryTable<VALUE_LENGTH, Edx>
{
type Target = Edx;

fn deref(&self) -> &Self::Target {
&self.0
}
}

const ENTRY_TABLE_KEY_DERIVATION_INFO: &[u8] = b"Entry Table key derivation info.";
Expand All @@ -41,7 +49,7 @@ impl<
type Token = Edx::Token;

fn setup(edx: Self::EdxScheme) -> Self {
Self { edx }
Self(edx)
}

fn gen_seed(&self, rng: &mut impl CryptoRngCore) -> Self::Seed {
Expand Down Expand Up @@ -82,7 +90,7 @@ impl<
&self,
tokens: HashSet<Self::Token>,
) -> Result<HashMap<Self::Token, Self::EncryptedValue>, Self::Error> {
self.edx.fetch(tokens).await.map_err(Self::Error::from)
self.0.fetch(tokens).await.map_err(Self::Error::from)
}

fn resolve(
Expand All @@ -98,7 +106,7 @@ impl<
old_values: &HashMap<Self::Token, Self::EncryptedValue>,
new_values: HashMap<Self::Token, Self::EncryptedValue>,
) -> Result<HashMap<Self::Token, Self::EncryptedValue>, Self::Error> {
self.edx
self.0
.upsert(old_values, new_values)
.await
.map_err(Self::Error::from)
Expand All @@ -121,7 +129,7 @@ impl<
}

async fn delete(&mut self, items: HashSet<Self::Token>) -> Result<(), Self::Error> {
self.edx.delete(items).await.map_err(Self::Error::Callback)
self.0.delete(items).await.map_err(Self::Error::Callback)
}
}

Expand All @@ -134,7 +142,7 @@ impl<
type Token = <Self as DxEnc<VALUE_LENGTH>>::Token;

async fn dump_tokens(&self) -> Result<HashSet<Self::Token>, Self::Error> {
self.edx.dump_tokens().await.map_err(Error::Callback)
self.0.dump_tokens().await.map_err(Error::Callback)
}
}

Expand Down
12 changes: 6 additions & 6 deletions refactored/src/findex_graph/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Findex Graph Encryption Scheme (GX-Enc).
//!
//! Uses the Findex MM-Enc scheme in order to securely store a graph. This graph
//! stores `IndexedValue`s for `Tag`s. An `IndexedValue` can be either a
//! Uses the Findex MM-Enc scheme in order to sec0ly store a graph. This graph
//! stores `IndexedValue`s for `Tag`s. An `IndexedValue` can be ei0r a
//! `Pointer` to another `Tag` or a `Data` containing a `Value`.

use std::{
Expand Down Expand Up @@ -265,14 +265,14 @@ mod tests {

println!(
"ET length ({} lines), size ({}B)",
findex.findex_mm.entry_table.edx.len(),
findex.findex_mm.entry_table.edx.size()
findex.findex_mm.entry_table.0.len(),
findex.findex_mm.entry_table.0.size()
);

println!(
"CT length ({} lines), size ({}B)",
findex.findex_mm.chain_table.edx.len(),
findex.findex_mm.chain_table.edx.size()
findex.findex_mm.chain_table.0.len(),
findex.findex_mm.chain_table.0.size()
);
}
}
9 changes: 4 additions & 5 deletions refactored/src/findex_mm/mm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ use std::{
use cosmian_crypto_core::reexport::rand_core::CryptoRngCore;
use tiny_keccak::{Hasher, Sha3};

use super::{
structs::{Entry, Link, Operation},
MmEnc,
};
use crate::{
edx::DxEnc,
error::{CoreError, Error},
findex_mm::{FindexMultiMap, ENTRY_LENGTH, LINK_LENGTH},
findex_mm::{
structs::{Entry, Link, Operation},
FindexMultiMap, MmEnc, ENTRY_LENGTH, LINK_LENGTH,
},
parameters::{BLOCK_LENGTH, HASH_LENGTH, LINE_LENGTH, SEED_LENGTH},
CallbackErrorTrait, Label,
};
Expand Down
16 changes: 8 additions & 8 deletions refactored/tests/non_regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ async fn write_index() -> Result<(), Error<KvStoreError>> {
}

let mut ser = Serializer::new();
ser.write(&findex.findex_graph.findex_mm.entry_table.edx)?;
ser.write(&findex.findex_graph.findex_mm.chain_table.edx)?;
ser.write(&findex.findex_graph.findex_mm.entry_table.0)?;
ser.write(&findex.findex_graph.findex_mm.chain_table.0)?;

std::fs::write("datasets/serialized_index", ser.finalize()).unwrap();
std::fs::write("datasets/key", master_key.as_bytes()).unwrap();
Expand Down Expand Up @@ -104,24 +104,24 @@ async fn test_non_regression() -> Result<(), Error<KvStoreError>> {

let serialized_index = std::fs::read("datasets/serialized_index").unwrap();
let mut de = Deserializer::new(&serialized_index);
findex.findex_graph.findex_mm.entry_table.edx = de.read::<InMemoryEdx<ENTRY_LENGTH>>()?;
findex.findex_graph.findex_mm.chain_table.edx = de.read::<InMemoryEdx<LINK_LENGTH>>()?;
findex.findex_graph.findex_mm.entry_table.0 = de.read::<InMemoryEdx<ENTRY_LENGTH>>()?;
findex.findex_graph.findex_mm.chain_table.0 = de.read::<InMemoryEdx<LINK_LENGTH>>()?;

println!(
"Entry Table length: {}",
findex.findex_graph.findex_mm.entry_table.edx.len()
findex.findex_graph.findex_mm.entry_table.len()
);
println!(
"Entry Table size: {}",
findex.findex_graph.findex_mm.entry_table.edx.size()
findex.findex_graph.findex_mm.entry_table.size()
);
println!(
"Chain Table length: {}",
findex.findex_graph.findex_mm.chain_table.edx.len()
findex.findex_graph.findex_mm.chain_table.len()
);
println!(
"Chain Table size: {}",
findex.findex_graph.findex_mm.chain_table.edx.size()
findex.findex_graph.findex_mm.chain_table.size()
);

let master_key = SymmetricKey::try_from_slice(&std::fs::read("datasets/key").unwrap())?;
Expand Down
24 changes: 12 additions & 12 deletions refactored/tests/test_in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,17 +674,17 @@ async fn test_first_names() -> Result<(), Error<KvStoreError>> {
println!("Built a list of {} search keywords", searches.len());
println!(
"Graphs: table sizes: entry -> {} records, {} kbytes, chain -> {} records, {} kbytes",
graph_findex.findex_graph.findex_mm.entry_table.edx.len(),
graph_findex.findex_graph.findex_mm.entry_table.edx.size() / 1024,
graph_findex.findex_graph.findex_mm.chain_table.edx.len(),
graph_findex.findex_graph.findex_mm.chain_table.edx.size() / 1024
graph_findex.findex_graph.findex_mm.entry_table.len(),
graph_findex.findex_graph.findex_mm.entry_table.size() / 1024,
graph_findex.findex_graph.findex_mm.chain_table.len(),
graph_findex.findex_graph.findex_mm.chain_table.size() / 1024
);
println!(
"Naive: table sizes: entry -> {} records, {} kbytes, chain -> {} records, {} kbytes",
naive_findex.findex_graph.findex_mm.entry_table.edx.len(),
naive_findex.findex_graph.findex_mm.entry_table.edx.size() / 1024,
naive_findex.findex_graph.findex_mm.chain_table.edx.len(),
naive_findex.findex_graph.findex_mm.chain_table.edx.size() / 1024
naive_findex.findex_graph.findex_mm.entry_table.len(),
naive_findex.findex_graph.findex_mm.entry_table.size() / 1024,
naive_findex.findex_graph.findex_mm.chain_table.len(),
naive_findex.findex_graph.findex_mm.chain_table.size() / 1024
);

let mut total_results = 0_usize;
Expand Down Expand Up @@ -777,11 +777,11 @@ async fn test_graph_compacting() {

println!(
"Length of the Entry Table: {}",
findex.findex_graph.findex_mm.entry_table.edx.len()
findex.findex_graph.findex_mm.entry_table.len()
);
println!(
"Length of the Chain Table: {}",
findex.findex_graph.findex_mm.chain_table.edx.len()
findex.findex_graph.findex_mm.chain_table.len()
);

// Compact then search
Expand All @@ -798,11 +798,11 @@ async fn test_graph_compacting() {

println!(
"Length of the Entry Table: {}",
findex.findex_graph.findex_mm.entry_table.edx.len()
findex.findex_graph.findex_mm.entry_table.len()
);
println!(
"Length of the Chain Table: {}",
findex.findex_graph.findex_mm.chain_table.edx.len()
findex.findex_graph.findex_mm.chain_table.len()
);

// Search for "rob"
Expand Down

0 comments on commit dcfa66c

Please sign in to comment.