Skip to content

Commit

Permalink
wip: feat: add compact
Browse files Browse the repository at this point in the history
  • Loading branch information
tbrezot committed Jun 21, 2023
1 parent 6c10bcd commit d16bc79
Show file tree
Hide file tree
Showing 6 changed files with 449 additions and 78 deletions.
42 changes: 19 additions & 23 deletions refactored/src/edx/chain_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,25 +117,20 @@ impl<
&self,
rng: &mut impl CryptoRngCore,
key: &Self::Key,
values: HashMap<Self::Token, [u8; VALUE_LENGTH]>,
) -> Result<HashMap<Self::Token, Self::EncryptedValue>, Self::Error> {
values
.into_iter()
.map(|(token, mut bytes)| {
let mut nonce = [0; NONCE_LENGTH];
rng.fill_bytes(&mut nonce);
let tag = self
.enc
.encrypt(&key.value, &nonce, &mut bytes, None)
.map_err(|e| Self::Error::Crypto(e.to_string()))?;
let encrypted_value = Self::EncryptedValue {
ciphertext: bytes,
nonce,
tag,
};
Ok((token, encrypted_value))
})
.collect()
mut value: [u8; VALUE_LENGTH],
) -> Result<Self::EncryptedValue, Self::Error> {
let mut nonce = [0; NONCE_LENGTH];
rng.fill_bytes(&mut nonce);
let tag = self
.enc
.encrypt(&key.value, &nonce, &mut value, None)
.map_err(|e| Self::Error::Crypto(e.to_string()))?;
let encrypted_value = Self::EncryptedValue {
ciphertext: value,
nonce,
tag,
};
Ok(encrypted_value)
}
}

Expand Down Expand Up @@ -166,11 +161,12 @@ mod tests {
let mut value = [0; VALUE_LENGTH];
rng.fill_bytes(&mut value);

let items = table
.prepare(&mut rng, &key, HashMap::from_iter([(token, value)]))
.unwrap();
let encrypted_value = table.prepare(&mut rng, &key, value).unwrap();

table.insert(items).await.unwrap();
table
.insert(HashMap::from_iter([(token, encrypted_value)]))
.await
.unwrap();

let res = table.get(HashSet::from_iter([token])).await.unwrap();

Expand Down
58 changes: 36 additions & 22 deletions refactored/src/edx/entry_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use cosmian_crypto_core::{
use super::{
aead::AeadInPlace,
structs::{EdxKey, Seed},
TokenDump,
};
use crate::{
edx::{DxEnc, EdxStore},
Expand Down Expand Up @@ -118,25 +119,34 @@ impl<
&self,
rng: &mut impl CryptoRngCore,
key: &Self::Key,
values: HashMap<Self::Token, [u8; VALUE_LENGTH]>,
) -> Result<HashMap<Self::Token, Self::EncryptedValue>, Self::Error> {
values
.into_iter()
.map(|(token, mut bytes)| {
let mut nonce = [0; NONCE_LENGTH];
rng.fill_bytes(&mut nonce);
let tag = self
.enc
.encrypt(&key.value, &nonce, &mut bytes, None)
.map_err(|e| Self::Error::Crypto(e.to_string()))?;
let encrypted_value = Self::EncryptedValue {
ciphertext: bytes,
nonce,
tag,
};
Ok((token, encrypted_value))
})
.collect()
mut value: [u8; VALUE_LENGTH],
) -> Result<Self::EncryptedValue, Self::Error> {
let mut nonce = [0; NONCE_LENGTH];
rng.fill_bytes(&mut nonce);
let tag = self
.enc
.encrypt(&key.value, &nonce, &mut value, None)
.map_err(|e| Self::Error::Crypto(e.to_string()))?;
let encrypted_value = Self::EncryptedValue {
ciphertext: value,
nonce,
tag,
};
Ok(encrypted_value)
}
}

impl<
const VALUE_LENGTH: usize,
Edx: EdxStore<VALUE_LENGTH, EncryptedValue = EncryptedValue<VALUE_LENGTH>>,
EncryptionScheme: AeadInPlace<KEY_LENGTH, MAC_LENGTH, NONCE_LENGTH>,
> TokenDump for EntryTable<VALUE_LENGTH, Edx, EncryptionScheme>
{
type Error = <Self as DxEnc<VALUE_LENGTH>>::Error;
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)
}
}

Expand Down Expand Up @@ -166,10 +176,14 @@ mod tests {
let mut value = [0; VALUE_LENGTH];
rng.fill_bytes(&mut value);

let items = table
.prepare(&mut rng, &key, HashMap::from_iter([(token, value)]))
let encrypted_value = table.prepare(&mut rng, &key, value).unwrap();
table
.upsert(
&HashMap::new(),
HashMap::from_iter([(token, encrypted_value)]),
)
.await
.unwrap();
table.upsert(&HashMap::new(), items).await.unwrap();

let res = table.get(HashSet::from_iter([token])).await.unwrap();

Expand Down
28 changes: 24 additions & 4 deletions refactored/src/edx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ use crate::{
CallbackErrorTrait, TOKEN_LENGTH,
};

pub trait TokenDump {
type Token;

type Error;

async fn dump_tokens(&self) -> Result<HashSet<Self::Token>, Self::Error>;
}

pub trait DxEnc<const VALUE_LENGTH: usize>: Sync + Send {
/// Seed used to derive the key.
type Seed: Sized + ZeroizeOnDrop + AsRef<[u8]> + From<[u8; SEED_LENGTH]> + Send + Sync;
Expand Down Expand Up @@ -78,14 +86,13 @@ pub trait DxEnc<const VALUE_LENGTH: usize>: Sync + Send {
encrypted_value: &Self::EncryptedValue,
) -> Result<[u8; VALUE_LENGTH], Self::Error>;

/// Transforms the given map of (tag, value) into a map of (token,
/// ciphertex) using the given key.
/// Encrypts the given values using the given key.
fn prepare(
&self,
rng: &mut impl CryptoRngCore,
key: &Self::Key,
values: HashMap<Self::Token, [u8; VALUE_LENGTH]>,
) -> Result<HashMap<Self::Token, Self::EncryptedValue>, Self::Error>;
values: [u8; VALUE_LENGTH],
) -> Result<Self::EncryptedValue, Self::Error>;

/// Conditionally upsert the given items into the EDX.
///
Expand Down Expand Up @@ -142,6 +149,9 @@ pub trait EdxStore<const VALUE_LENGTH: usize>: Sync + Send {
/// Instantiates a new EDX.
fn setup() -> Self;

/// Queries the EDX for all tokens stored.
async fn dump_tokens(&self) -> Result<HashSet<Self::Token>, Self::Error>;

/// Queries an Edx for the given tokens. Only returns a value for the tokens
/// that are present in the store.
async fn fetch(
Expand Down Expand Up @@ -242,6 +252,16 @@ pub mod in_memory {
Self(Arc::new(Mutex::new(HashMap::new())))
}

async fn dump_tokens(&self) -> Result<HashSet<Self::Token>, Self::Error> {
Ok(self
.0
.lock()
.expect("could not lock table")
.keys()
.cloned()
.collect())
}

async fn fetch(
&self,
tokens: HashSet<Self::Token>,
Expand Down
63 changes: 37 additions & 26 deletions refactored/src/findex_mm/mm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl<
///
/// Tags simply are the values between the initialization value and the
/// counter.
fn unroll(
pub(crate) fn unroll(
&self,
key: &ChainTable::Key,
seed: &[u8; HASH_LENGTH],
Expand Down Expand Up @@ -102,7 +102,7 @@ impl<
}

/// Decomposes the given Findex index modifications into Chain Table values.
fn decompose<const BLOCK_LENGTH: usize, const LINE_LENGTH: usize>(
pub(crate) fn decompose<const BLOCK_LENGTH: usize, const LINE_LENGTH: usize>(
&self,
modifications: &[(Operation, <Self as MmEnc<SEED_LENGTH, EdxError>>::Item)],
) -> Result<Vec<ChainTableValue>, Error<EdxError>> {
Expand Down Expand Up @@ -160,7 +160,7 @@ impl<
/// - merges the data from the stacked block and fill the stack;
/// - if this value was an addition, adds it to the set, otherwise removes
/// any matching value from the set.
fn recompose<const BLOCK_LENGTH: usize, const LINE_LENGTH: usize>(
pub(crate) fn recompose<const BLOCK_LENGTH: usize, const LINE_LENGTH: usize>(
&self,
chain: &[ChainTableValue],
) -> Result<HashSet<<Self as MmEnc<SEED_LENGTH, EdxError>>::Item>, Error<EdxError>> {
Expand Down Expand Up @@ -345,17 +345,21 @@ impl<
et_value.last_token = Some(new_tokens[ct_values.len() - 1]);

chain_tokens.insert(*findex_token, (chain_key, new_tokens));
et_values_new.insert(*et_token, et_value.into());
et_values_new.insert(
*et_token,
self.entry_table.prepare(
&mut *rng.lock().expect("could not lock mutex"),
&key.entry_table,
et_value.into(),
)?,
);
}

// 2 - Upsert modifications to the Entry Table.
let et_items = self.entry_table.prepare(
&mut *rng.lock().expect("could not lock mutex"),
&key.entry_table,
et_values_new,
)?;

let mut et_values_old = self.entry_table.upsert(&et_values_old, et_items).await?;
let mut et_values_old = self
.entry_table
.upsert(&et_values_old, et_values_new)
.await?;

// Retry until all modifications are upserted.
while !et_values_old.is_empty() {
Expand Down Expand Up @@ -389,16 +393,18 @@ impl<
CoreError::Crypto(
"`et_tokens` was added all tokens from `et_values_old`".to_string(),
)
})? = et_value.into();
})? = self.entry_table.prepare(
&mut *rng.lock().expect("could not lock mutex"),
&key.entry_table,
et_value.into(),
)?;
}
}

let items = self.entry_table.prepare(
&mut *rng.lock().expect("could not lock mutex"),
&key.entry_table,
et_values_new,
)?;
et_values_old = self.entry_table.upsert(&et_values_old, items).await?;
et_values_old = self
.entry_table
.upsert(&et_values_old, et_values_new)
.await?;
}

let mut ct_lines =
Expand All @@ -419,16 +425,21 @@ impl<
.into());
}

let ct_values: HashMap<ChainTable::Token, [u8; CHAIN_TABLE_VALUE_LENGTH]> = new_tokens
let ct_values: HashMap<_, ChainTable::EncryptedValue> = new_tokens
.into_iter()
.zip(ct_values.into_iter())
.map(|(token, value)| (token, value.0))
.collect();
ct_lines.extend(self.chain_table.prepare(
&mut *rng.lock().expect("could not lock mutex"),
&chain_key,
ct_values,
)?);
.map(|(token, value)| {
Ok((
token,
self.chain_table.prepare(
&mut *rng.lock().expect("could not lock mutex"),
&chain_key,
value.0,
)?,
))
})
.collect::<Result<_, Error<EdxError>>>()?;
ct_lines.extend(ct_values);
}

self.chain_table.insert(ct_lines).await
Expand Down
1 change: 1 addition & 0 deletions refactored/src/findex_mm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use crate::{edx::DxEnc, CallbackErrorTrait};
mod mm;
mod structs;

pub(crate) use structs::{ChainTableValue, EntryTableValue};
pub use structs::{FindexKey, Operation, CHAIN_TABLE_VALUE_LENGTH, ENTRY_TABLE_VALUE_LENGTH};

pub trait MmEnc<const SEED_LENGTH: usize, EdxError: std::error::Error + CallbackErrorTrait>:
Expand Down
Loading

0 comments on commit d16bc79

Please sign in to comment.