Skip to main content

primitives/correlated_randomness/singlets/
random.rs

1use crate::{
2    algebra::field::FieldExtension,
3    correlated_randomness::singlets::types::{Singlet, Singlets},
4    random::{CryptoRngCore, Random, RandomWith},
5    sharing::{FieldShare, FieldShares, GlobalFieldKey},
6    types::Positive,
7    utils::IntoExactSizeIterator,
8};
9// -------------------------
10// |   Random Generation   |
11// -------------------------
12
13impl<F: FieldExtension> Random for Singlet<F> {
14    /// UNIMPLEMENTED: Singlets are correlated randomness, use `random_n`/`random_n_with`` to
15    /// establish correlation for `n_parties`.
16    fn random(_rng: impl CryptoRngCore) -> Self {
17        unimplemented!("Singlets are correlated randomness. Use `random_n`/`random_n_with`` to establish correlation for n_parties")
18    }
19
20    /// Generates a vector of random singlets, one for each party.
21    fn random_n<Container: FromIterator<Self>>(
22        source: impl CryptoRngCore,
23        size: usize,
24    ) -> Container {
25        FieldShare::random_n::<Vec<_>>(source, size)
26            .into_iter()
27            .map(Singlet)
28            .collect()
29    }
30}
31
32impl<F: FieldExtension, M: Positive> Random for Singlets<F, M> {
33    /// UNIMPLEMENTED: Singlets are correlated randomness, use `random_n`/`random_n_with`` to
34    /// establish correlation for `n_parties`.
35    fn random(_rng: impl CryptoRngCore) -> Self {
36        unimplemented!("Singlets are correlated randomness. Use `random_n`/`random_n_with`` to establish correlation for n_parties")
37    }
38
39    /// Generates a vector of random singlets, one for each party.
40    fn random_n<Container: FromIterator<Self>>(
41        source: impl CryptoRngCore,
42        size: usize,
43    ) -> Container {
44        FieldShares::<F, M>::random_n::<Vec<_>>(source, size)
45            .into_iter()
46            .map(Singlets)
47            .collect()
48    }
49}
50
51impl<F: FieldExtension, M: Positive> RandomWith<usize> for Singlets<F, M> {
52    fn random_with(mut rng: impl CryptoRngCore, n_parties: usize) -> Self {
53        Singlets(FieldShares::<F, M>::random_with(&mut rng, n_parties))
54    }
55}
56
57impl<F: FieldExtension, M: Positive> RandomWith<Vec<GlobalFieldKey<F>>> for Singlets<F, M> {
58    fn random_with(_rng: impl CryptoRngCore, _alphas: Vec<GlobalFieldKey<F>>) -> Self {
59        unimplemented!("Singlets are correlated randomness. Use `random_vec_with`/`random_box_with` to establish correlation for n_parties")
60    }
61
62    /// Generates a vector of random singlets, one for each party, using the provided alphas.
63    fn random_n_with_each<Container: FromIterator<Self>>(
64        rng: impl CryptoRngCore,
65        all_alphas: impl IntoExactSizeIterator<Item = Vec<GlobalFieldKey<F>>>,
66    ) -> Container {
67        FieldShares::<F, M>::random_n_with_each::<Vec<_>>(rng, all_alphas)
68            .into_iter()
69            .map(Singlets)
70            .collect()
71    }
72}