Skip to content

Commit

Permalink
test: impl Display trait on structs used in callbacks for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuthor committed Jun 23, 2023
1 parent 8e0899a commit 546ba72
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 58 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ in_memory = []
live_compact = []

[dependencies]
base64 = "0.21.2"
cosmian_crypto_core = "7.0.0"
futures = "0.3"
rand = "0.8"
Expand Down
17 changes: 9 additions & 8 deletions src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use cosmian_crypto_core::symmetric_crypto::Dem;

use crate::{
chain_table::{ChainTableValue, KwiChainUids},
structs::EncryptedMultiTable,
CallbackError, EncryptedTable, Error, IndexedValue, KeyingMaterial, Keyword, Location, Uid,
UpsertData, CHAIN_TABLE_KEY_DERIVATION_INFO,
Uids, UpsertData, CHAIN_TABLE_KEY_DERIVATION_INFO,
};

/// Trait implementing all callbacks needed by Findex.
Expand All @@ -21,7 +22,7 @@ pub trait FindexCallbacks<Error: std::error::Error + CallbackError, const UID_LE
) -> Result<bool, Error>;

/// Fetch all the UIDs from the entry table
async fn fetch_all_entry_table_uids(&self) -> Result<HashSet<Uid<UID_LENGTH>>, Error>;
async fn fetch_all_entry_table_uids(&self) -> Result<Uids<UID_LENGTH>, Error>;

/// Fetch the lines with the given UIDs from the Entry Table. The returned
/// values are encrypted since they are stored that way. The decryption
Expand All @@ -39,8 +40,8 @@ pub trait FindexCallbacks<Error: std::error::Error + CallbackError, const UID_LE
/// Table
async fn fetch_entry_table(
&self,
entry_table_uids: HashSet<Uid<UID_LENGTH>>,
) -> Result<Vec<(Uid<UID_LENGTH>, Vec<u8>)>, Error>;
entry_table_uids: Uids<UID_LENGTH>,
) -> Result<EncryptedMultiTable<UID_LENGTH>, Error>;

/// Fetch the lines with the given UIDs from the Chain Table. The returned
/// values are encrypted since they are stored that way. The decryption is
Expand All @@ -58,7 +59,7 @@ pub trait FindexCallbacks<Error: std::error::Error + CallbackError, const UID_LE
/// Table
async fn fetch_chain_table(
&self,
chain_table_uids: HashSet<Uid<UID_LENGTH>>,
chain_table_uids: Uids<UID_LENGTH>,
) -> Result<EncryptedTable<UID_LENGTH>, Error>;

/// Upserts lines in the Entry Table. The input data maps each Entry Table
Expand Down Expand Up @@ -146,7 +147,7 @@ pub trait FindexCallbacks<Error: std::error::Error + CallbackError, const UID_LE
/// - `new_chain_table_items` : items to insert into the Chain Table
fn update_lines(
&mut self,
chain_table_uids_to_remove: HashSet<Uid<UID_LENGTH>>,
chain_table_uids_to_remove: Uids<UID_LENGTH>,
new_entry_table_items: EncryptedTable<UID_LENGTH>,
new_chain_table_items: EncryptedTable<UID_LENGTH>,
) -> Result<(), Error>;
Expand Down Expand Up @@ -174,7 +175,7 @@ pub trait FindexCallbacks<Error: std::error::Error + CallbackError, const UID_LE

#[cfg(feature = "live_compact")]
/// Delete the Chain Table lines with the given UIDs.
async fn delete_chain(&mut self, uids: HashSet<Uid<UID_LENGTH>>) -> Result<(), Error>;
async fn delete_chain(&mut self, uids: Uids<UID_LENGTH>) -> Result<(), Error>;
}

pub trait FetchChains<
Expand Down Expand Up @@ -206,7 +207,7 @@ pub trait FetchChains<
Error<CustomError>,
> {
// Collect to a `HashSet` to mix UIDs between chains.
let chain_table_uids = kwi_chain_table_uids.values().flatten().cloned().collect();
let chain_table_uids = Uids(kwi_chain_table_uids.values().flatten().cloned().collect());

let encrypted_items = self.fetch_chain_table(chain_table_uids).await?;

Expand Down
16 changes: 9 additions & 7 deletions src/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
error::CallbackError,
parameters::check_parameter_constraints,
structs::{BlockType, EncryptedTable, IndexedValue, Label},
Error, FindexCallbacks, KeyingMaterial, CHAIN_TABLE_KEY_DERIVATION_INFO,
Error, FindexCallbacks, KeyingMaterial, Uids, CHAIN_TABLE_KEY_DERIVATION_INFO,
ENTRY_TABLE_KEY_DERIVATION_INFO,
};

Expand Down Expand Up @@ -48,7 +48,7 @@ pub trait FindexCompact<
{
/// Replaces all the Index Entry Table UIDs and values. New UIDs are derived
/// using the given label and the KMAC key derived from the new master key.
/// The values are dectypted using the DEM key derived from the master key
/// The values are decrypted using the DEM key derived from the master key
/// and re-encrypted using the DEM key derived from the new master key.
///
/// Randomly selects index entries and recompact their associated chains.
Expand Down Expand Up @@ -152,11 +152,13 @@ pub trait FindexCompact<
//
// Remove all reindexed Chain Table items. Chains are recomputed entirely.
//
let chain_table_uids_to_remove = chains_to_reindex
.values()
.flat_map(|chain| chain.iter().map(|(k, _)| k))
.cloned()
.collect();
let chain_table_uids_to_remove = Uids(
chains_to_reindex
.values()
.flat_map(|chain| chain.iter().map(|(k, _)| k))
.cloned()
.collect(),
);

// Get the values stored in the reindexed chains.
let mut reindexed_chain_values = HashMap::with_capacity(chains_to_reindex.len());
Expand Down
34 changes: 17 additions & 17 deletions src/compact_live.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
entry_table::{EntryTable, EntryTableValue},
structs::{BlockType, ChainData},
CallbackError, EncryptedTable, Error, FindexCallbacks, FindexCompact, IndexedValue,
KeyingMaterial, Location, Uid, UpsertData, CHAIN_TABLE_KEY_DERIVATION_INFO,
KeyingMaterial, Location, Uid, Uids, UpsertData, CHAIN_TABLE_KEY_DERIVATION_INFO,
ENTRY_TABLE_KEY_DERIVATION_INFO,
};

Expand Down Expand Up @@ -116,7 +116,7 @@ pub trait FindexLiveCompact<
&self,
rng: &mut impl CryptoRngCore,
num_reindexing_before_full_set: u32
) -> Result<(Vec<Uid<UID_LENGTH>>, HashSet<Uid<UID_LENGTH>>), Error<CustomError>> {
) -> Result<(Vec<Uid<UID_LENGTH>>, Uids<UID_LENGTH>), Error<CustomError>> {

let entry_table_uids = self.fetch_all_entry_table_uids().await?;

Expand All @@ -137,16 +137,16 @@ pub trait FindexLiveCompact<
// and `gamma` is the Euler's constant.
//
// See the [coupon collector's problem](https://wikipedia.org/wiki/Coupon_collector's_problem).
let entry_table_length = entry_table_uids.len() as f64;
let entry_table_length = entry_table_uids.0.len() as f64;
let n_compact = ((entry_table_length * (entry_table_length.log2() + GAMMA))
/ f64::from(num_reindexing_before_full_set))
.ceil() as usize;

// The number of compacted UIDs should leave enough unused UIDs for the noise.
if (n_compact as f64 * (1f64 + Self::NOISE_RATIO)) > entry_table_uids.len() as f64 {
if (n_compact as f64 * (1f64 + Self::NOISE_RATIO)) > entry_table_uids.0.len() as f64 {
return Err(Error::CryptoError(format!(
"Number of Entry Table UIDs to compact ({n_compact}) should not be greater than {}",
entry_table_uids.len() as f64 / (1f64 + Self::NOISE_RATIO)
entry_table_uids.0.len() as f64 / (1f64 + Self::NOISE_RATIO)
)));
}

Expand All @@ -157,11 +157,11 @@ pub trait FindexLiveCompact<
let mut mixed_uids = Vec::with_capacity(2 * n_compact);

// Needed because `uids` is moved in the loop condition.
let n_uids = entry_table_uids.len();
let n_uids = entry_table_uids.0.len();
let n_noise_candidates = n_uids - n_compact;
let n_noise = (Self::NOISE_RATIO * n_compact as f64) as usize;

for uid in entry_table_uids {
for uid in entry_table_uids.0 {
let tmp = rng.next_u32() as usize;
if tmp % n_uids < n_compact {
// Randomly select ~ `n_compact` target UIDs.
Expand All @@ -173,7 +173,7 @@ pub trait FindexLiveCompact<
}
}

Ok((mixed_uids, noise_uids))
Ok((mixed_uids, Uids(noise_uids)))
}

/// Fetch all useful information for the compact from the Chain Table:
Expand Down Expand Up @@ -269,7 +269,7 @@ pub trait FindexLiveCompact<
&self,
rng: &mut impl CryptoRngCore,
k_value: &DemScheme::Key,
noise: &HashSet<Uid<UID_LENGTH>>,
noise: &Uids<UID_LENGTH>,
noisy_remaining_locations: &HashSet<Location>,
noisy_encrypted_entry_table: &EncryptedTable<UID_LENGTH>,
noisy_chain_values: &HashMap<Uid<UID_LENGTH>, HashSet<IndexedValue>>,
Expand All @@ -281,15 +281,15 @@ pub trait FindexLiveCompact<
Error<CustomError>,
> {
let mut noisy_entry_table = EntryTable::with_capacity(noisy_chain_values.len());
let mut compacted_chains = HashMap::with_capacity(noisy_chain_values.len() - noise.len());
let mut cache = HashMap::with_capacity(noisy_chain_values.len() - noise.len());
let mut compacted_chains = HashMap::with_capacity(noisy_chain_values.len() - noise.0.len());
let mut cache = HashMap::with_capacity(noisy_chain_values.len() - noise.0.len());

for (uid, encrypted_value) in noisy_encrypted_entry_table.iter() {
let entry_table_value = EntryTableValue::decrypt::<DEM_KEY_LENGTH, DemScheme>(
k_value,
encrypted_value
)?;
if noise.contains(uid) {
if noise.0.contains(uid) {
// Noise entries are simply re-encrypted.
noisy_entry_table.insert(*uid, entry_table_value);
} else {
Expand Down Expand Up @@ -343,8 +343,8 @@ pub trait FindexLiveCompact<
&mut self,
rng: &mut impl CryptoRngCore,
k_value: &DemScheme::Key,
mixed_uids: HashSet<Uid<UID_LENGTH>>,
noise_uids: &HashSet<Uid<UID_LENGTH>>,
mixed_uids: Uids<UID_LENGTH>,
noise_uids: &Uids<UID_LENGTH>,
) -> Result<(), Error<CustomError>> {
// Fetch both target and noise values from the Entry Table.
let mut encrypted_entry_table = self
Expand Down Expand Up @@ -408,7 +408,7 @@ pub trait FindexLiveCompact<
// which leads to the same number of clone operations, or less)
let mut old_chains_to_remove = Vec::with_capacity(chains.chain_uids.len());
for uid in chains.chain_uids.keys() {
if !encrypted_entry_table.contains_key(uid) && !noise_uids.contains(uid) {
if !encrypted_entry_table.contains_key(uid) && !noise_uids.0.contains(uid) {
old_chains_to_remove.push(*uid);
}
}
Expand All @@ -419,7 +419,7 @@ pub trait FindexLiveCompact<
chains_to_delete.extend(chain);
}

self.delete_chain(chains_to_delete).await?;
self.delete_chain(Uids(chains_to_delete)).await?;
}

Ok(())
Expand Down Expand Up @@ -451,7 +451,7 @@ pub trait FindexLiveCompact<
self.live_compact_uids(
&mut rng,
&k_value,
batch.iter().cloned().collect(),
Uids(batch.iter().cloned().collect()),
&noise_uids,
).await?;
}
Expand Down
33 changes: 18 additions & 15 deletions src/in_memory_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use cosmian_crypto_core::{reexport::rand_core::SeedableRng, CsRng};
use rand::Rng;

use crate::{
callbacks::FetchChains, parameters::UID_LENGTH, EncryptedTable, FindexCallbacks, FindexCompact,
FindexSearch, FindexUpsert, IndexedValue, Keyword, Location, Uid, UpsertData,
callbacks::FetchChains, parameters::UID_LENGTH, structs::EncryptedMultiTable, EncryptedTable,
FindexCallbacks, FindexCompact, FindexSearch, FindexUpsert, IndexedValue, Keyword, Location,
Uids, UpsertData,
};
#[cfg(feature = "live_compact")]
use crate::{compact_live::FindexLiveCompact, parameters::*};
Expand Down Expand Up @@ -97,32 +98,34 @@ impl<const UID_LENGTH: usize> FindexCallbacks<ExampleError, UID_LENGTH>
Ok(!self.progress_callback_cancel)
}

async fn fetch_all_entry_table_uids(&self) -> Result<HashSet<Uid<UID_LENGTH>>, ExampleError> {
let uids = self.entry_table.keys().cloned().collect();
async fn fetch_all_entry_table_uids(&self) -> Result<Uids<UID_LENGTH>, ExampleError> {
let uids = Uids(self.entry_table.keys().cloned().collect());
Ok(uids)
}

async fn fetch_entry_table(
&self,
entry_table_uids: HashSet<Uid<UID_LENGTH>>,
) -> Result<Vec<(Uid<UID_LENGTH>, Vec<u8>)>, ExampleError> {
Ok(entry_table_uids
entry_table_uids: Uids<UID_LENGTH>,
) -> Result<EncryptedMultiTable<UID_LENGTH>, ExampleError> {
let items = entry_table_uids
.0
.into_iter()
.filter_map(|uid| {
self.entry_table
.get(&uid)
.cloned()
.map(|value| (uid, value))
})
.collect())
.collect::<Vec<_>>();
Ok(EncryptedMultiTable(items))
}

async fn fetch_chain_table(
&self,
chain_table_uids: HashSet<Uid<UID_LENGTH>>,
chain_table_uids: Uids<UID_LENGTH>,
) -> Result<EncryptedTable<UID_LENGTH>, ExampleError> {
let mut items = EncryptedTable::with_capacity(chain_table_uids.len());
for uid in chain_table_uids {
let mut items = EncryptedTable::with_capacity(chain_table_uids.0.len());
for uid in chain_table_uids.0 {
if let Some(value) = self.chain_table.get(&uid) {
items.insert(uid, value.clone());
}
Expand Down Expand Up @@ -165,7 +168,7 @@ impl<const UID_LENGTH: usize> FindexCallbacks<ExampleError, UID_LENGTH>

fn update_lines(
&mut self,
chain_table_uids_to_remove: HashSet<Uid<UID_LENGTH>>,
chain_table_uids_to_remove: Uids<UID_LENGTH>,
new_encrypted_entry_table_items: EncryptedTable<UID_LENGTH>,
new_encrypted_chain_table_items: EncryptedTable<UID_LENGTH>,
) -> Result<(), ExampleError> {
Expand All @@ -178,7 +181,7 @@ impl<const UID_LENGTH: usize> FindexCallbacks<ExampleError, UID_LENGTH>
);
}

for removed_chain_table_uid in chain_table_uids_to_remove {
for removed_chain_table_uid in chain_table_uids_to_remove.0 {
self.chain_table.remove(&removed_chain_table_uid);
}

Expand All @@ -204,8 +207,8 @@ impl<const UID_LENGTH: usize> FindexCallbacks<ExampleError, UID_LENGTH>
}

#[cfg(feature = "live_compact")]
async fn delete_chain(&mut self, uids: HashSet<Uid<UID_LENGTH>>) -> Result<(), ExampleError> {
self.chain_table.retain(|uid, _| !uids.contains(uid));
async fn delete_chain(&mut self, uids: Uids<UID_LENGTH>) -> Result<(), ExampleError> {
self.chain_table.retain(|uid, _| !uids.0.contains(uid));
Ok(())
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,8 @@ pub use compact_live::FindexLiveCompact;
pub use error::{CallbackError, CoreError, Error};
pub use keys::KeyingMaterial;
pub use search::FindexSearch;
pub use structs::{EncryptedTable, IndexedValue, Keyword, Label, Location, Uid, UpsertData};
pub use structs::{
EncryptedMultiTable, EncryptedTable, IndexedValue, Keyword, Label, Location, Uid, Uids,
UpsertData,
};
pub use upsert::FindexUpsert;
10 changes: 5 additions & 5 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
error::CallbackError,
parameters::check_parameter_constraints,
structs::{IndexedValue, Keyword, Label, Location},
Error, KeyingMaterial, CHAIN_TABLE_KEY_DERIVATION_INFO, ENTRY_TABLE_KEY_DERIVATION_INFO,
Error, KeyingMaterial, Uids, CHAIN_TABLE_KEY_DERIVATION_INFO, ENTRY_TABLE_KEY_DERIVATION_INFO,
};

/// Trait implementing the search functionality of Findex.
Expand Down Expand Up @@ -72,13 +72,13 @@ pub trait FindexSearch<

// Query the Entry Table for these UIDs.
let entry_table = self
.fetch_entry_table(entry_table_uid_map.keys().copied().collect())
.fetch_entry_table(Uids(entry_table_uid_map.keys().copied().collect()))
.await?;

// Unchain all Entry Table values.
let mut kwi_chain_table_uids = KwiChainUids::with_capacity(entry_table.len());
let mut kwi_to_keyword = HashMap::with_capacity(entry_table.len());
for (uid, encrypted_value) in entry_table.into_iter() {
let mut kwi_chain_table_uids = KwiChainUids::with_capacity(entry_table.0.len());
let mut kwi_to_keyword = HashMap::with_capacity(entry_table.0.len());
for (uid, encrypted_value) in entry_table.0.into_iter() {
let keyword = entry_table_uid_map.get(&uid).ok_or_else(|| {
Error::<CustomError>::CryptoError(format!(
"Could not find keyword associated to UID {uid:?}."
Expand Down
Loading

0 comments on commit 546ba72

Please sign in to comment.