arcium-core-utils 0.4.3

Arcium core utils
Documentation
pub mod global;
pub mod mock;

use primitives::{izip_eq, utils::IntoExactSizeIterator};

use crate::preprocessing::{iterator::NextElement, Preprocessing};

/// Hardcoded batch size for testing. Can be tweaked if needed.
pub type TestBatchSize = typenum::U128;

/// Generator of a queue of preprocessing elements for all peers by batches.
pub trait TrustedGenerator<T: Preprocessing + 'static> {
    /// Generates a fixed-size batch of preprocessing elements for each of the peers.
    fn generate_batch_for_each(
        &mut self,
        associated_data: T::AssociatedData,
    ) -> impl ExactSizeIterator<Item = impl IntoExactSizeIterator<Item = T>>;

    /// Generates n_parties x n_elements preprocessing elements, one vec per peer.
    fn generate_n_for_each(
        &mut self,
        n_elements: usize,
        associated_data: T::AssociatedData,
    ) -> Vec<Vec<T>> {
        let mut all_preprocessing: Vec<_> = self
            .generate_batch_for_each(associated_data.clone())
            .map(|batch_i| batch_i.into_iter().collect::<Vec<T>>())
            .collect();
        while all_preprocessing[0].len() < n_elements {
            izip_eq!(
                all_preprocessing.iter_mut(),
                self.generate_batch_for_each(associated_data.clone())
            )
            .for_each(|(preprocessing_i, new_elements)| {
                preprocessing_i.extend(new_elements);
            });
        }
        for preprocessing in &mut all_preprocessing {
            preprocessing.truncate(n_elements);
        }
        all_preprocessing
    }

    /// Mocks the delivery of n preprocessing elements to each peer. Acts as a
    /// PreprocessingSource for all parties at once.
    fn mock_deal_n_for_each(
        &mut self,
        n_elements: usize,
        associated_data: T::AssociatedData,
    ) -> Vec<Vec<NextElement<T>>> {
        self.generate_n_for_each(n_elements, associated_data)
            .into_iter()
            .map(|vec| {
                vec.into_iter()
                    .map(|t| Box::pin(std::future::ready(Ok(t))) as NextElement<_>)
                    .collect()
            })
            .collect()
    }
}