1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//! Needs either "silent-ot" or "silent-ot-quasi-cyclic" feature.
use crate::common::BitVec;
use crate::mul_triple::boolean::MulTriples;
use crate::mul_triple::MTProvider;
use crate::protocols::SetupStorage;
use async_trait::async_trait;
use rand::rngs::OsRng;
use rand::SeedableRng;
use rand_chacha::rand_core::{CryptoRng, RngCore};
use rand_chacha::ChaChaRng;
use std::thread::available_parallelism;
use zappot::silent_ot;
use zappot::silent_ot::MultType;

pub type Msg = silent_ot::Msg;

pub struct SilentMtProvider<Rng> {
    rng: Rng,
    configured_ots: usize,
    stored_mts: Option<MulTriples>,
    silent_sender: Option<silent_ot::Sender>,
    silent_receiver: Option<silent_ot::Receiver>,
    ch1: Option<seec_channel::Channel<silent_ot::Msg>>,
    ch2: Option<seec_channel::Channel<silent_ot::Msg>>,
}

impl<Rng: RngCore + CryptoRng + Send> SilentMtProvider<Rng> {
    /// Executes base OTs for silent OT but not num_ots silentOT itself. `Rng` is used to seed
    /// ChaChaRng's.
    /// When the `silent-ot` feature is enabled, the [`MultType::ExConv7x24`] code from libOTe is used by default.
    /// If only `silent-ot-quasi-cyclic` is enabled, ZappOT wil not depend on libOTe and the [`MultType::QuasiCyclic`]
    /// code is used. Note that this code depends on AVX2 support, and therefore can't be used on ARM.
    pub async fn new(
        num_ots: usize,
        rng: Rng,
        ch1: seec_channel::Channel<silent_ot::Msg>,
        ch2: seec_channel::Channel<silent_ot::Msg>,
    ) -> Self {
        #[cfg(feature = "silent-ot")]
        {
            Self::new_with_mult_type(num_ots, MultType::ExConv7x24, rng, ch1, ch2).await
        }
        // not silent_ot implies silent-ot-quasi-cyclic
        #[cfg(not(feature = "silent-ot"))]
        {
            Self::new_with_mult_type(num_ots, MultType::QuasiCyclic { scaler: 2 }, rng, ch1, ch2)
                .await
        }
    }

    pub async fn new_with_mult_type(
        num_ots: usize,
        mul_type: MultType,
        mut rng: Rng,
        mut ch1: seec_channel::Channel<silent_ot::Msg>,
        mut ch2: seec_channel::Channel<silent_ot::Msg>,
    ) -> Self {
        let mut rng1 = ChaChaRng::from_rng(&mut rng).expect("Seeding Rng in SilentMtProvider::new");
        let mut rng2 = ChaChaRng::from_rng(&mut rng).expect("Seeding Rng in SilentMtProvider::new");
        let threads_per_ot = available_parallelism().map(usize::from).unwrap_or(2) / 2;
        let (silent_sender, silent_receiver) = tokio::join!(
            silent_ot::Sender::new_with_base_ot_sender(
                zappot::base_ot::Sender::new(),
                &mut rng1,
                num_ots,
                mul_type,
                threads_per_ot,
                &mut ch1.0,
                &mut ch1.1
            ),
            silent_ot::Receiver::new_with_base_ot_receiver(
                zappot::base_ot::Receiver::new(),
                &mut rng2,
                num_ots,
                mul_type,
                threads_per_ot,
                &mut ch2.0,
                &mut ch2.1
            ),
        );
        Self {
            rng,
            configured_ots: num_ots,
            stored_mts: None,
            silent_sender: Some(silent_sender),
            silent_receiver: Some(silent_receiver),
            ch1: Some(ch1),
            ch2: Some(ch2),
        }
    }

    pub async fn precompute_mts(&mut self) {
        let silent_sender = self
            .silent_sender
            .take()
            .expect("precompute_mts can only be called once");
        let silent_receiver = self.silent_receiver.take().unwrap();
        let ch1 = self.ch1.take().unwrap();
        let ch2 = self.ch2.take().unwrap();
        let send = silent_sender.random_silent_send(&mut self.rng, ch1.0, ch1.1);

        let receive = silent_receiver.random_silent_receive(ch2.0, ch2.1);

        let (send_ots, (recv_ots, a_i)) = tokio::join!(send, receive);
        eprintln!("{send_ots:?}");
        eprintln!("{recv_ots:?}");
        eprintln!("{a_i:?}");

        let mut b_i = BitVec::with_capacity(self.configured_ots);
        let mut v_i: BitVec<usize> = BitVec::with_capacity(self.configured_ots);

        send_ots
            .into_iter()
            .map(|arr| arr.map(|b| b.lsb()))
            .for_each(|[m0, m1]| {
                b_i.push(m0 ^ m1);
                v_i.push(m0);
            });
        let u_i = recv_ots.into_iter().map(|b| b.lsb());
        let c_i = a_i
            .iter()
            .by_vals()
            .zip(b_i.iter().by_vals())
            .zip(u_i)
            .zip(v_i)
            .map(|(((a, b), u), v)| a & b ^ u ^ v)
            .collect();

        self.stored_mts = Some(MulTriples::from_raw(a_i, b_i, c_i));
    }

    pub fn mts_available(&self) -> usize {
        self.stored_mts.as_ref().map(|mts| mts.len()).unwrap_or(0)
    }
}

impl SilentMtProvider<OsRng> {
    pub fn from_raw_mts(mts: MulTriples) -> Self {
        Self {
            rng: OsRng,
            configured_ots: mts.len(),
            stored_mts: Some(mts),
            silent_sender: None,
            silent_receiver: None,
            ch1: None,
            ch2: None,
        }
    }
}

#[async_trait]
impl<Rng: RngCore + CryptoRng + Send> MTProvider for SilentMtProvider<Rng> {
    type Output = MulTriples;
    type Error = ();

    async fn precompute_mts(&mut self, amount: usize) -> Result<(), Self::Error> {
        assert_eq!(
            amount, self.configured_ots,
            "Requested OTs must be equal to configured OTs of SilentMtProvider"
        );
        self.precompute_mts().await;
        Ok(())
    }

    async fn request_mts(&mut self, amount: usize) -> Result<Self::Output, Self::Error> {
        if let Some(stored_mts) = &mut self.stored_mts {
            return Ok(stored_mts.split_off_last(amount));
        }
        self.precompute_mts().await;
        self.request_mts(amount).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn silent_mts() {
        let (ch11, ch21) = seec_channel::in_memory::new_pair(128);
        let (ch12, ch22) = seec_channel::in_memory::new_pair(128);
        let (mut mtp1, mut mtp2) = tokio::join!(
            SilentMtProvider::new(100, OsRng, ch11, ch22),
            SilentMtProvider::new(100, OsRng, ch12, ch21)
        );

        let (mts1, mts2) = tokio::try_join!(mtp1.request_mts(100), mtp2.request_mts(100),).unwrap();
        let left = mts1.c ^ mts2.c;
        let right = (mts1.a ^ mts2.a) & (mts1.b ^ mts2.b);
        assert_eq!(left, right);
    }
}