arcium-primitives 0.6.0

Arcium primitives
Documentation
use std::{
    fmt::Debug,
    ops::{Add, AddAssign, Sub, SubAssign},
};

use crate::correlated_randomness::bundler::errors::BundlerError;

pub mod errors;
/// Bundles together multiple correlation generators/streams to fetch all correlations
///  required by a given consumer, e.g., a circuit, a protocol, a layer...
pub trait Bundler {
    /// A type to hold the futures to all requested correlations.
    type Iterator: BundleIterator;

    /// Fetches the requested correlations and returns a ready-to-use iterator.
    fn fetch(
        &mut self,
        size: &<Self::Iterator as BundleIterator>::Size,
    ) -> Result<Self::Iterator, BundlerError>;

    /// Fetches the requested correlations and returns a ready-to-use iterator.
    fn fetch_for<Consumer: BundleConsumer<Iterator = Self::Iterator>>(
        &mut self,
        consumer: &Consumer,
    ) -> Result<Self::Iterator, BundlerError> {
        let size = consumer.required_preprocessing();
        self.fetch(&size)
    }
}

impl<B: Bundler> Bundler for &mut B {
    type Iterator = B::Iterator;

    fn fetch(
        &mut self,
        size: &<Self::Iterator as BundleIterator>::Size,
    ) -> Result<Self::Iterator, BundlerError> {
        (**self).fetch(size)
    }
}

/// A consumer of a bundle of correlations, e.g., a circuit, a protocol, a layer...
pub trait BundleConsumer {
    /// An iterator holding futures to all correlations required by the consumer
    ///  (e.g., F_q triples, binary singlets, ...)
    type Iterator: BundleIterator;

    /// Returns the size of the preprocessing required by the consumer.
    fn required_preprocessing(&self) -> <Self::Iterator as BundleIterator>::Size;

    /// Fetches all preprocessing required by `consumer` and returns a ready-to-use iterator.
    fn fetch_preprocessing_from<PB: Bundler<Iterator = Self::Iterator>>(
        &self,
        bundler: &mut PB,
    ) -> Result<Self::Iterator, BundlerError> {
        let size = self.required_preprocessing();
        bundler.fetch(&size)
    }
}

/// An iterator that holds futures to all correlations required by a consumer.
pub trait BundleIterator {
    /// The size of the iterator, i.e., the number of correlations per type that it holds.
    type Size: Debug + Clone + Default + Eq + Add + Sub + AddAssign + SubAssign;

    /// Returns the size of the iterator, i.e., the number of correlations per type that it holds.
    fn len(&self) -> Self::Size;

    /// Returns `true` if the iterator is empty, i.e., it holds no correlations.
    fn is_empty(&self) -> bool {
        self.len() == Self::Size::default()
    }
}