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
use anyhow::Context;
use clap::Parser;
use rand::rngs::OsRng;
use seec::mul_triple;
use seec::mul_triple::storage::MTStorage;
use seec_channel::sub_channels_for;
use std::net::SocketAddr;
use std::path::PathBuf;
use std::time::Duration;
use tokio::time::Instant;
use tracing::info;
use tracing_subscriber::EnvFilter;

#[derive(Parser, Clone, Debug)]
struct Args {
    #[clap(short, long)]
    num: usize,
    #[clap(short, long)]
    batch_size: usize,
    #[clap(short, long)]
    output: PathBuf,
    #[clap(long, default_value = "127.0.0.1:7742")]
    server: SocketAddr,
    #[clap(long)]
    id: usize,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Initialize logging, see top of file for instructions on how to get output.
    tracing_subscriber::fmt()
        .with_env_filter(EnvFilter::from_default_env())
        .init();

    let args = Args::parse();

    let (mut sender, bytes_written, mut receiver, bytes_read) = match args.id {
        0 => seec_channel::tcp::listen(&args.server).await?,
        1 => {
            seec_channel::tcp::connect_with_timeout(&args.server, Duration::from_secs(120)).await?
        }
        illegal => anyhow::bail!("Illegal party id {illegal}. Must be 0 or 1."),
    };

    let (mt_ch, mut sync_ch) = sub_channels_for!(
        &mut sender,
        &mut receiver,
        128,
        mul_triple::boolean::ot_ext::DefaultMsg,
        seec_channel::SyncMsg
    )
    .await
    .context("sub-channel establishment")?;

    let mtp =
        mul_triple::boolean::ot_ext::OtMTProvider::new_with_default_ot_ext(OsRng, mt_ch.0, mt_ch.1);

    let mut mt_storage = MTStorage::create(&args.output).context("create mt storage")?;
    mt_storage.set_batch_size(args.batch_size);
    let now = Instant::now();
    mt_storage
        .store_mts(args.num, mtp)
        .await
        .context("unable to precompute and store MTs")?;
    info!(
        elapsed_ms = now.elapsed().as_millis(),
        bytes_written = bytes_written.get(),
        bytes_received = bytes_read.get(),
        "Finished precomputing MTs"
    );

    seec_channel::sync(&mut sync_ch.0, &mut sync_ch.1)
        .await
        .context("unable to sync")?;

    Ok(())
}