Function seec_channel::tcp::listen

source ·
pub async fn listen<T: RemoteSend>(
    addr: impl ToSocketAddrs + Debug
) -> Result<TrackingChannel<T>, Error>
Examples found in repository?
crates/zappot/examples/co_base_ot.rs (line 33)
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
async fn sender(args: Args) -> Vec<[Block; 2]> {
    // Create a secure RNG to use in the protocol
    let mut rng = OsRng;
    let mut sender = Sender::new();
    // Create a channel by listening on a socket address. Once another party connect, this
    // returns the channel
    let (mut base_sender, _, mut base_receiver, _) =
        seec_channel::tcp::listen::<seec_channel::Sender<_>>(("127.0.0.1", args.port))
            .await
            .expect("Error listening for channel connection");
    let (ch_sender, mut ch_receiver) = sub_channel(&mut base_sender, &mut base_receiver, 8)
        .await
        .expect("Establishing sub channel");
    // Perform the random ots
    sender
        .send_random(args.num_ots, &mut rng, &ch_sender, &mut ch_receiver)
        .await
        .expect("Failed to generate ROTs")
}
More examples
Hide additional examples
crates/zappot/examples/alsz_ot_extension.rs (line 38)
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
async fn sender(args: Args) -> (Vec<[Block; 2]>, usize, usize) {
    // Create a secure RNG to use in the protocol
    let mut rng = OsRng;
    // Create the ot extension sender. A base OT **receiver** is passed as an argument and used
    // to create the base_ots
    let mut sender = Sender::new(base_ot::Receiver);
    // Create a channel by listening on a socket address. Once another party connect, this
    // returns the channel
    let (mut base_sender, send_cnt, mut base_receiver, recv_cnt) =
        seec_channel::tcp::listen::<seec_channel::Sender<_>>(("127.0.0.1", args.port))
            .await
            .expect("Error listening for channel connection");
    let (ch_sender, mut ch_receiver) = sub_channel(&mut base_sender, &mut base_receiver, 128)
        .await
        .expect("Establishing sub channel");

    // Perform the random ots
    let ots = sender
        .send_random(args.num_ots, &mut rng, &ch_sender, &mut ch_receiver)
        .await
        .expect("Failed to generate ROTs");
    (ots, send_cnt.get(), recv_cnt.get())
}
crates/seec/examples/precompute_mts.rs (line 38)
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
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(())
}
crates/seec/examples/privmail.rs (line 228)
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
async fn main() -> anyhow::Result<()> {
    let args: Args = Args::parse();
    let search_query: SearchQuery =
        serde_yaml::from_reader(File::open(args.query_file_path).expect("Opening query file"))
            .expect("Deserializing query file");
    let mails: Vec<Mail> = fs::read_dir(args.mail_dir_path)
        .expect("Reading mail dir")
        .map(|entry| {
            let entry = entry.expect("Mail dir iteration");
            serde_yaml::from_reader(File::open(entry.path()).expect("Opening mail file"))
                .expect("Deserializing mail file")
        })
        .collect();

    tracing_subscriber::fmt()
        .with_env_filter(EnvFilter::from_default_env())
        .init();

    let (input, _) = priv_mail_search(
        &search_query.keywords,
        &search_query.modifier_chain_share,
        &mails,
        args.duplication_factor,
    );

    let circuit: ExecutableCircuit<bool, _, _> =
        ExecutableCircuit::DynLayers(CircuitBuilder::global_into_circuit());
    // if args.save_circuit {
    //     circuit.save_dot("privmail.dot")?;
    // }

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

    let mut executor = {
        let mt_provider = InsecureMTProvider::default();
        BoolGmwExecutor::new(&circuit, args.my_id, mt_provider).await?
    };

    let (mut sender, mut receiver) =
        sub_channels_for!(&mut sender, &mut receiver, 16, Message<BooleanGmw>).await?;

    let output = executor
        .execute(Input::Scalar(input), &mut sender, &mut receiver)
        .await?;
    info!(
        my_id = %args.my_id,
        output = ?output,
        bytes_written = bytes_written.get(),
        bytes_read = bytes_read.get(),
        gate_count = circuit.gate_count()
    );
    Ok(())
}
crates/seec/examples/privmail_sc.rs (line 219)
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
async fn main() -> anyhow::Result<()> {
    let args: Args = Args::parse();
    let search_query: SearchQuery =
        serde_yaml::from_reader(File::open(args.query_file_path).expect("Opening query file"))
            .expect("Deserializing query file");
    let mails: Vec<Mail> = fs::read_dir(args.mail_dir_path)
        .expect("Reading mail dir")
        .map(|entry| {
            let entry = entry.expect("Mail dir iteration");
            serde_yaml::from_reader(File::open(entry.path()).expect("Opening mail file"))
                .expect("Deserializing mail file")
        })
        .collect();

    tracing_subscriber::fmt()
        .with_env_filter(EnvFilter::from_default_env())
        .init();

    let now = Instant::now();
    let (input, _) = priv_mail_search(
        &search_query.keywords,
        &search_query.modifier_chain_share,
        &mails,
        args.duplication_factor,
    );
    let circuit = ExecutableCircuit::DynLayers(CircuitBuilder::global_into_circuit());
    info!("Building circuit took: {}", now.elapsed().as_secs_f32());
    // circuit = circuit.clone().into_base_circuit().into();
    // if args.save_circuit {
    //     bc.save_dot("privmail.dot")?;
    // }
    // dbg!(&circuit);
    let (mut sender, bytes_written, mut receiver, bytes_read) = match args.my_id {
        0 => seec_channel::tcp::listen(args.server).await?,
        1 => seec_channel::tcp::connect(args.server).await?,
        illegal => anyhow::bail!("Illegal party id {illegal}. Must be 0 or 1."),
    };

    let (ch1, mut ch2) = sub_channels_for!(
        &mut sender,
        &mut receiver,
        64,
        seec_channel::Sender<ot_ext::ExtOTMsg>,
        Message<BooleanGmw>
    )
    .await?;

    let mut executor = {
        let mt_provider = OtMTProvider::new(
            OsRng,
            ot_ext::Sender::default(),
            ot_ext::Receiver::default(),
            ch1.0,
            ch1.1,
        );
        BoolGmwExecutor::new(&circuit, args.my_id, mt_provider).await?
    };

    let output = executor
        .execute(Input::Scalar(input), &mut ch2.0, &mut ch2.1)
        .await?;
    info!(
        my_id = %args.my_id,
        output = ?output,
        bytes_written = bytes_written.get(),
        bytes_read = bytes_read.get(),
        gate_count = circuit.gate_count(),
    );
    Ok(())
}
crates/seec/examples/sha256.rs (line 61)
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
async fn main() -> Result<()> {
    let _guard = init_tracing()?;
    let args = Args::parse();
    let circuit: ExecutableCircuit<bool, BooleanGate, u32> = ExecutableCircuit::DynLayers(
        BaseCircuit::load_bristol(args.circuit, Load::Circuit)?.into(),
    );

    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(args.server).await?,
        illegal => anyhow::bail!("Illegal party id {illegal}. Must be 0 or 1."),
    };

    // Initialize the communication statistics tracker with the counters for the main channel
    let mut comm_stats = Statistics::new(bytes_written, bytes_read).without_unaccounted(true);

    let (mut sender, mut receiver) =
        sub_channels_for!(&mut sender, &mut receiver, 8, Message<BooleanGmw>).await?;

    let mut executor: Executor<BooleanGmw, _> = if let Some(addr) = args.mt_provider {
        let (mt_sender, bytes_written, mt_receiver, bytes_read) =
            seec_channel::tcp::connect(addr).await?;
        // Set the counters for the helper channel
        comm_stats.set_helper(bytes_written, bytes_read);
        let mt_provider = TrustedMTProviderClient::new("unique-id".into(), mt_sender, mt_receiver);
        // As the MTs are generated when the Executor is created, we record the communication
        // with the `record_helper` method and a custom category
        comm_stats
            .record_helper(
                Phase::Custom("Helper-Mts".into()),
                Executor::new(&circuit, args.id, mt_provider),
            )
            .await?
    } else {
        let mt_provider = InsecureMTProvider::default();
        comm_stats
            .record(
                Phase::FunctionDependentSetup,
                Executor::new(&circuit, args.id, mt_provider),
            )
            .await?
    };
    let input = BitVec::repeat(false, 768);
    let _out = comm_stats
        .record(
            Phase::Online,
            executor.execute(Input::Scalar(input), &mut sender, &mut receiver),
        )
        .await?;

    // Depending on whether a --stats file is set, create a file writer or stdout
    let mut writer: Box<dyn Write> = match args.stats {
        Some(path) => {
            let file = File::create(path)?;
            Box::new(file)
        }
        None => Box::new(stdout()),
    };
    // serde_json is used to write the statistics in json format. `.csv` is currently not
    // supported.
    let mut res = comm_stats.into_run_result();
    res.add_metadata("circuit", "sha256.rs");
    serde_json::to_writer_pretty(&mut writer, &res)?;
    writeln!(writer)?;

    Ok(())
}