mpc_ristretto/
beaver.rs

1//! Defines the Beaver value generation interface
2//! as well as a dummy beaver interface for testing
3
4#[cfg(test)]
5use curve25519_dalek::scalar::Scalar;
6use itertools::Itertools;
7
8/// SharedValueSource implements both the functionality for:
9///     1. Single additively shared values [x] where party 1 holds
10///        x_1 and party 2 holds x_2 such that x_1 + x_2 = x
11///     2. Beaver triplets; additively shared values [a], [b], [c] such
12///        that a * b = c
13pub trait SharedValueSource<T> {
14    /// Fetch the next shared single bit
15    fn next_shared_bit(&mut self) -> T;
16    /// Fetch the next shared batch of bits
17    fn next_shared_bit_batch(&mut self, num_values: usize) -> Vec<T> {
18        (0..num_values)
19            .map(|_| self.next_shared_bit())
20            .collect_vec()
21    }
22    /// Fetch the next shared single value
23    fn next_shared_value(&mut self) -> T;
24    /// Fetch a batch of shared single values
25    fn next_shared_value_batch(&mut self, num_values: usize) -> Vec<T> {
26        (0..num_values)
27            .map(|_| self.next_shared_value())
28            .collect_vec()
29    }
30    /// Fetch the next pair of values that are multiplicative inverses of one another
31    fn next_shared_inverse_pair(&mut self) -> (T, T);
32    /// Fetch the next batch of multiplicative inverse pairs
33    fn next_shared_inverse_pair_batch(&mut self, num_pairs: usize) -> Vec<(T, T)> {
34        (0..num_pairs)
35            .map(|_| self.next_shared_inverse_pair())
36            .collect_vec()
37    }
38    /// Fetch the next beaver triplet
39    fn next_triplet(&mut self) -> (T, T, T);
40    /// Fetch a batch of beaver triplets
41    fn next_triplet_batch(&mut self, num_triplets: usize) -> Vec<(T, T, T)> {
42        (0..num_triplets).map(|_| self.next_triplet()).collect_vec()
43    }
44}
45
46/// A dummy value source that outputs only ones
47/// Used for testing
48#[cfg(test)]
49#[derive(Debug, Default)]
50pub struct DummySharedScalarSource;
51
52#[cfg(test)]
53#[allow(dead_code)]
54impl DummySharedScalarSource {
55    pub fn new() -> Self {
56        Self
57    }
58}
59
60#[cfg(test)]
61impl SharedValueSource<Scalar> for DummySharedScalarSource {
62    fn next_shared_bit(&mut self) -> Scalar {
63        Scalar::one()
64    }
65
66    fn next_shared_value(&mut self) -> Scalar {
67        Scalar::one()
68    }
69
70    fn next_shared_inverse_pair(&mut self) -> (Scalar, Scalar) {
71        (Scalar::one(), Scalar::one())
72    }
73
74    fn next_triplet(&mut self) -> (Scalar, Scalar, Scalar) {
75        (Scalar::one(), Scalar::one(), Scalar::one())
76    }
77}