arcium-primitives 0.4.5

Arcium primitives
Documentation
use crate::{
    algebra::field::FieldExtension,
    correlated_randomness::singlets::types::{Singlet, Singlets},
    random::{CryptoRngCore, Random, RandomWith},
    sharing::{FieldShare, FieldShares, GlobalFieldKey},
    types::Positive,
    utils::IntoExactSizeIterator,
};
// -------------------------
// |   Random Generation   |
// -------------------------

impl<F: FieldExtension> Random for Singlet<F> {
    /// UNIMPLEMENTED: Singlets are correlated randomness, use `random_n`/`random_n_with`` to
    /// establish correlation for `n_parties`.
    fn random(_rng: impl CryptoRngCore) -> Self {
        unimplemented!("Singlets are correlated randomness. Use `random_n`/`random_n_with`` to establish correlation for n_parties")
    }

    /// Generates a vector of random singlets, one for each party.
    fn random_n<Container: FromIterator<Self>>(
        source: impl CryptoRngCore,
        size: usize,
    ) -> Container {
        FieldShare::random_n::<Vec<_>>(source, size)
            .into_iter()
            .map(Singlet)
            .collect()
    }
}

impl<F: FieldExtension, M: Positive> Random for Singlets<F, M> {
    /// UNIMPLEMENTED: Singlets are correlated randomness, use `random_n`/`random_n_with`` to
    /// establish correlation for `n_parties`.
    fn random(_rng: impl CryptoRngCore) -> Self {
        unimplemented!("Singlets are correlated randomness. Use `random_n`/`random_n_with`` to establish correlation for n_parties")
    }

    /// Generates a vector of random singlets, one for each party.
    fn random_n<Container: FromIterator<Self>>(
        source: impl CryptoRngCore,
        size: usize,
    ) -> Container {
        FieldShares::<F, M>::random_n::<Vec<_>>(source, size)
            .into_iter()
            .map(Singlets)
            .collect()
    }
}

impl<F: FieldExtension, M: Positive> RandomWith<usize> for Singlets<F, M> {
    fn random_with(mut rng: impl CryptoRngCore, n_parties: usize) -> Self {
        Singlets(FieldShares::<F, M>::random_with(&mut rng, n_parties))
    }
}

impl<F: FieldExtension, M: Positive> RandomWith<Vec<GlobalFieldKey<F>>> for Singlets<F, M> {
    fn random_with(_rng: impl CryptoRngCore, _alphas: Vec<GlobalFieldKey<F>>) -> Self {
        unimplemented!("Singlets are correlated randomness. Use `random_vec_with`/`random_box_with` to establish correlation for n_parties")
    }

    /// Generates a vector of random singlets, one for each party, using the provided alphas.
    fn random_n_with_each<Container: FromIterator<Self>>(
        rng: impl CryptoRngCore,
        all_alphas: impl IntoExactSizeIterator<Item = Vec<GlobalFieldKey<F>>>,
    ) -> Container {
        FieldShares::<F, M>::random_n_with_each::<Vec<_>>(rng, all_alphas)
            .into_iter()
            .map(Singlets)
            .collect()
    }
}