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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//! Insecure Benchmarking API - Do not use in Production!
//!
//! The [`BenchParty`] API provides an easy way of benchmarking an MPC application
//! or protocol implemented in SEEC. An example of its usage is e.g. located in the
//! `crates/seec/examples/bristol.rs` binary.

use crate::circuit::{ExecutableCircuit, GateIdx};
use crate::executor::{Executor, Input, Message};
use crate::mul_triple::storage::MTStorage;
use crate::mul_triple::{boolean, MTProvider};
use crate::protocols::boolean_gmw::BooleanGmw;
use crate::protocols::mixed_gmw::{Mixed, MixedGmw};
use crate::protocols::{mixed_gmw, Protocol, Ring, Share, ShareStorage};
use crate::utils::{BoxError, ErasedError};
use crate::CircuitBuilder;
use anyhow::{anyhow, Context};
use bitvec::view::BitViewSized;
use rand::distributions::{Distribution, Standard};
use rand::rngs::OsRng;
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha8Rng;
use seec_channel::util::{Phase, RunResult, Statistics};
use seec_channel::{sub_channels_for, Channel, Sender};
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use std::fs::File;
use std::future::Future;
use std::io::BufReader;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::time::Duration;
use zappot::ot_ext::ExtOTMsg;

type DynMTP<P> =
    Box<dyn MTProvider<Output = <P as Protocol>::SetupStorage, Error = BoxError> + Send + 'static>;

pub trait BenchProtocol: Protocol + Default + Debug {
    fn insecure_setup() -> DynMTP<Self>;
    fn ot_setup(ch: Channel<Sender<ExtOTMsg>>) -> DynMTP<Self>;
    fn stored(path: &Path) -> DynMTP<Self>;
}

impl BenchProtocol for BooleanGmw {
    fn insecure_setup() -> DynMTP<Self> {
        Box::new(ErasedError(boolean::InsecureMTProvider::default()))
    }

    fn ot_setup(ch: Channel<Sender<ExtOTMsg>>) -> DynMTP<Self> {
        let ot_sender = zappot::ot_ext::Sender::default();
        let ot_recv = zappot::ot_ext::Receiver::default();
        let mtp = boolean::OtMTProvider::new(OsRng, ot_sender, ot_recv, ch.0, ch.1);
        Box::new(ErasedError(mtp))
    }

    fn stored(path: &Path) -> DynMTP<Self> {
        let file = BufReader::new(File::open(path).expect("opening MT file"));
        MTStorage::new(file).insecure_loop_file(true).into_dyn()
    }
}

impl<R> BenchProtocol for MixedGmw<R>
where
    R: Ring,
    Standard: Distribution<R>,
    [R; 1]: BitViewSized,
{
    fn insecure_setup() -> DynMTP<Self> {
        mixed_gmw::InsecureMixedSetup::default().into_dyn()
    }

    fn ot_setup(_ch: Channel<Sender<ExtOTMsg>>) -> DynMTP<Self> {
        todo!()
    }

    fn stored(_path: &Path) -> DynMTP<Self> {
        todo!()
    }
}

// TODO this is wrong to just always generate arith shares, so it lives here in the bench API
impl<R> Distribution<Mixed<R>> for Standard
where
    Standard: Distribution<R>,
{
    fn sample<RNG: Rng + ?Sized>(&self, rng: &mut RNG) -> Mixed<R> {
        Mixed::Arith(rng.sample(Standard))
    }
}

pub struct BenchParty<P: Protocol, Idx> {
    id: usize,
    circ: Option<ExecutableCircuit<P::Plain, P::Gate, Idx>>,
    server: Option<SocketAddr>,
    meta: String,
    insecure_setup: bool,
    stored_mts: Option<PathBuf>,
    sleep_after_phase: Duration,
    precompute_layers: bool,
    interleave_setup: bool,
    repeat: usize,
    tls_config: Option<ServerTlsConfig>,
    tls_domain: Option<String>,
}

pub struct ServerTlsConfig {
    pub private_key_file: PathBuf,
    pub certificate_chain_file: PathBuf,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchResult {
    pub protocol: String,
    pub metadata: String,
    pub data: Vec<RunResult>,
}

impl<P, Idx> BenchParty<P, Idx>
where
    P: BenchProtocol,
    Standard: Distribution<P::Share>,
    Idx: GateIdx,
    P::Share: Share<SimdShare = P::ShareStorage>,
{
    pub fn new(id: usize) -> Self {
        Self {
            id,
            circ: None,
            server: None,
            meta: String::new(),
            insecure_setup: false,
            stored_mts: None,
            sleep_after_phase: Duration::from_millis(200),
            precompute_layers: true,
            interleave_setup: false,
            repeat: 1,
            tls_config: None,
            tls_domain: None,
        }
    }

    pub fn server(mut self, server: SocketAddr) -> Self {
        self.server = Some(server);
        self
    }

    pub fn explicit_circuit(mut self, circuit: ExecutableCircuit<P::Plain, P::Gate, Idx>) -> Self {
        self.circ = Some(circuit);
        self
    }

    pub fn insecure_setup(mut self, insecure: bool) -> Self {
        assert_eq!(None, self.stored_mts);
        self.insecure_setup = insecure;
        self
    }

    pub fn interleave_setup(mut self, interleave_setup: bool) -> Self {
        self.interleave_setup = interleave_setup;
        self
    }

    /// Default is true
    pub fn precompute_layers(mut self, precompute_layers: bool) -> Self {
        self.precompute_layers = precompute_layers;
        self
    }

    /// Sets the metadata of the `BenchResult` that is returned by `bench()`
    pub fn metadata(mut self, meta: String) -> Self {
        self.meta = meta;
        self
    }

    pub fn sleep_after_phase(mut self, sleep: Duration) -> Self {
        self.sleep_after_phase = sleep;
        self
    }

    pub fn repeat(mut self, repeat: usize) -> Self {
        self.repeat = repeat;
        self
    }

    pub fn stored_mts(mut self, path: &Path) -> Self {
        assert!(!self.insecure_setup);
        self.stored_mts = Some(path.to_path_buf());
        self
    }

    pub fn tls_config(mut self, tls_config: ServerTlsConfig) -> Self {
        self.tls_config = Some(tls_config);
        self
    }

    pub fn tls_domain(mut self, tls_domain: String) -> Self {
        self.tls_domain = Some(tls_domain);
        self
    }

    #[tracing::instrument(level = "debug", skip(self))]
    #[allow(clippy::manual_async_fn)] // I want ot force the Send bound here
    pub fn bench(self) -> impl Future<Output = anyhow::Result<BenchResult>> + Send {
        async move {
            let server = self.server.unwrap_or("127.0.0.1:7744".parse().unwrap());
            let (mut sender, bytes_written, mut receiver, bytes_read) = match self.id {
                0 => {
                    if let Some(tls_config) = self.tls_config {
                        seec_channel::tls::listen(
                            &server,
                            tls_config.private_key_file,
                            tls_config.certificate_chain_file,
                        )
                        .await?
                    } else {
                        seec_channel::tcp::listen(&server).await?
                    }
                }
                1 => {
                    if let Some(domain) = self.tls_domain {
                        seec_channel::tls::connect(&domain, &server).await?
                    } else {
                        seec_channel::tcp::connect_with_timeout(&server, Duration::from_secs(120))
                            .await?
                    }
                }
                illegal => anyhow::bail!("Illegal party id {illegal}. Must be 0 or 1."),
            };

            let mut res = vec![];
            let mut owned_circ;
            for run in 0..self.repeat {
                tracing::debug!(run, "Performing bench run");
                let mut statistics = Statistics::new(bytes_written.clone(), bytes_read.clone())
                    .with_sleep(self.sleep_after_phase)
                    .without_unaccounted(true);

                let (ot_ch, mut exec_ch) = sub_channels_for!(
                    &mut sender,
                    &mut receiver,
                    128,
                    Sender<ExtOTMsg>,
                    Message<P>
                )
                .await
                .context("Establishing sub channels")?;

                let circ = match &self.circ {
                    Some(circ) => circ,
                    None => {
                        let circ = CircuitBuilder::<P::Plain, P::Gate, Idx>::global_into_circuit();
                        if self.precompute_layers {
                            owned_circ = ExecutableCircuit::DynLayers(circ).precompute_layers();
                            &owned_circ
                        } else {
                            owned_circ = ExecutableCircuit::DynLayers(circ);
                            &owned_circ
                        }
                    }
                };

                let mut mtp = match (self.insecure_setup, &self.stored_mts) {
                    (false, None) => P::ot_setup(ot_ch),
                    (true, None) => P::insecure_setup(),
                    (false, Some(path)) => P::stored(path),
                    (true, Some(_)) => unreachable!("ensure via setters"),
                };
                let mts_needed = circ.interactive_count_times_simd();
                if !self.interleave_setup {
                    statistics
                        .record(Phase::Mts, mtp.precompute_mts(mts_needed))
                        .await
                        .map_err(|err| anyhow!(err))
                        .context("MT precomputation failed")?;
                }

                let mut executor = statistics
                    .record(
                        Phase::FunctionDependentSetup,
                        Executor::<P, Idx>::new(circ, self.id, mtp),
                    )
                    .await
                    .context("Failed to create executor")?;

                let mut rng = ChaCha8Rng::seed_from_u64(42 * self.id as u64);
                let fake_inp = match circ.simd_size(0) {
                    None => Input::Scalar(P::ShareStorage::random(circ.input_count(), &mut rng)),
                    Some(size) => Input::Simd(vec![
                        P::ShareStorage::random(size.get(), &mut rng);
                        circ.input_count()
                    ]),
                };

                let output = statistics
                    .record(
                        Phase::Online,
                        executor.execute(fake_inp, &mut exec_ch.0, &mut exec_ch.1),
                    )
                    .await
                    .context("Failed to execute circuit")?;

                tracing::debug!(id = self.id, ?output);

                res.push(statistics.into_run_result());
            }

            Ok(BenchResult {
                protocol: format!("{:?}", P::default()),
                metadata: self.meta,
                data: res,
            })
        }
    }
}