From d74d75edc615c44e14554225e39925636a52d610 Mon Sep 17 00:00:00 2001 From: taskooh Date: Sat, 31 Aug 2024 17:38:07 +0900 Subject: [PATCH 1/5] Rename current methods --- Cargo.lock | 9 +++-- mpc-algebra/src/channel.rs | 4 +- .../groups/curves/twisted_edwards/mod.rs | 2 +- mpc-algebra/src/share/additive.rs | 20 +++++----- mpc-algebra/src/share/spdz.rs | 14 +++---- mpc-algebra/src/wire/edwards2.rs | 4 +- mpc-algebra/src/wire/field.rs | 30 ++++++++++++--- mpc-net/Cargo.lock | 36 +++++++++++++++++- mpc-net/Cargo.toml | 1 + mpc-net/src/lib.rs | 38 +++++++++++++++---- 10 files changed, 117 insertions(+), 41 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f695453d..4601e8cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -760,6 +760,7 @@ dependencies = [ "lazy_static", "log", "rayon", + "serde", ] [[package]] @@ -1121,18 +1122,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.200" +version = "1.0.209" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddc6f9cc94d67c0e21aaf7eda3a010fd3af78ebf6e096aa6e2e13c79749cce4f" +checksum = "99fce0ffe7310761ca6bf9faf5115afbc19688edd00171d81b1bb1b116c63e09" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.200" +version = "1.0.209" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "856f046b9400cee3c8c94ed572ecdb752444c24528c035cd35882aad6f492bcb" +checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170" dependencies = [ "proc-macro2", "quote", diff --git a/mpc-algebra/src/channel.rs b/mpc-algebra/src/channel.rs index 69de1683..a63fe965 100644 --- a/mpc-algebra/src/channel.rs +++ b/mpc-algebra/src/channel.rs @@ -20,7 +20,7 @@ pub trait MpcSerNet: MpcNet { fn send_to_king(out: &T) -> Option> { let mut bytes_out = Vec::new(); out.serialize(&mut bytes_out).unwrap(); - Self::send_bytes_to_king(&bytes_out).map(|bytes_in| { + Self::worker_send_or_leader_receive(&bytes_out).map(|bytes_in| { bytes_in .into_iter() .map(|b| T::deserialize(&b[..]).unwrap()) @@ -29,7 +29,7 @@ pub trait MpcSerNet: MpcNet { } fn receive_from_king(out: Option>) -> T { - let bytes_in = Self::recv_bytes_from_king(out.map(|outs| { + let bytes_in = Self::worker_receive_or_leader_send(out.map(|outs| { outs.iter() .map(|out| { let mut bytes_out = Vec::new(); diff --git a/mpc-algebra/src/r1cs_helper/groups/curves/twisted_edwards/mod.rs b/mpc-algebra/src/r1cs_helper/groups/curves/twisted_edwards/mod.rs index 5497c264..8d600d14 100644 --- a/mpc-algebra/src/r1cs_helper/groups/curves/twisted_edwards/mod.rs +++ b/mpc-algebra/src/r1cs_helper/groups/curves/twisted_edwards/mod.rs @@ -469,7 +469,7 @@ where let revealed_xr = xytzrsuv.reveal(); // step5: allocate share - let share = if Net::am_king() { + let share = if Net::is_leader() { MpcTEProjective::from_public(revealed_xr) - rsuv } else { -rsuv diff --git a/mpc-algebra/src/share/additive.rs b/mpc-algebra/src/share/additive.rs index c477536c..4bb72234 100644 --- a/mpc-algebra/src/share/additive.rs +++ b/mpc-algebra/src/share/additive.rs @@ -88,7 +88,7 @@ impl Reveal for AdditiveFieldShare { fn from_public(f: Self::Base) -> Self { Self { - val: if Net::am_king() { f } else { F::zero() }, + val: if Net::is_leader() { f } else { F::zero() }, } } @@ -99,7 +99,7 @@ impl Reveal for AdditiveFieldShare { let mut r: Vec = (0..(Net::n_parties() - 1)).map(|_| F::rand(rng)).collect(); let sum_r: F = r.iter().sum(); r.push(f - sum_r); - Self::from_add_shared(Net::receive_from_king(if Net::am_king() { + Self::from_add_shared(Net::receive_from_king(if Net::is_leader() { Some(r) } else { None @@ -113,7 +113,7 @@ impl Reveal for AdditiveFieldShare { .map(|i| f[i] - &rs.iter().map(|r| &r[i]).sum()) .collect(); rs.push(final_shares); - Net::receive_from_king(if Net::am_king() { Some(rs) } else { None }) + Net::receive_from_king(if Net::is_leader() { Some(rs) } else { None }) .into_iter() .map(Self::from_add_shared) .collect() @@ -144,7 +144,7 @@ impl FieldShare for AdditiveFieldShare { } fn shift(&mut self, other: &F) -> &mut Self { - if Net::am_king() { + if Net::is_leader() { self.val += other; } self @@ -262,7 +262,7 @@ impl Reveal for MulFieldShare { } fn from_public(f: F) -> Self { Self { - val: if Net::am_king() { f } else { F::one() }, + val: if Net::is_leader() { f } else { F::one() }, } } fn from_add_shared(f: F) -> Self { @@ -290,7 +290,7 @@ impl FieldShare for MulFieldShare { } fn scale(&mut self, other: &F) -> &mut Self { - if Net::am_king() { + if Net::is_leader() { self.val *= other; } self @@ -367,7 +367,7 @@ impl Reveal for AdditiveGroupShare { fn from_public(b: G) -> Self { Self { - val: if Net::am_king() { b } else { G::zero() }, + val: if Net::is_leader() { b } else { G::zero() }, _phants: PhantomData, } } @@ -379,7 +379,7 @@ impl Reveal for AdditiveGroupShare { let mut r: Vec = (0..(Net::n_parties() - 1)).map(|_| G::rand(rng)).collect(); let sum_r: G = r.iter().sum(); r.push(f - sum_r); - Self::from_add_shared(Net::receive_from_king(if Net::am_king() { + Self::from_add_shared(Net::receive_from_king(if Net::is_leader() { Some(r) } else { None @@ -393,7 +393,7 @@ impl Reveal for AdditiveGroupShare { .map(|i| f[i] - &rs.iter().map(|r| &r[i]).sum()) .collect(); rs.push(final_shares); - Net::receive_from_king(if Net::am_king() { Some(rs) } else { None }) + Net::receive_from_king(if Net::is_leader() { Some(rs) } else { None }) .into_iter() .map(Self::from_add_shared) .collect() @@ -483,7 +483,7 @@ impl> GroupShare for AdditiveGroupShare &mut Self { - if Net::am_king() { + if Net::is_leader() { self.val += other; } self diff --git a/mpc-algebra/src/share/spdz.rs b/mpc-algebra/src/share/spdz.rs index 0341b426..7edd8776 100644 --- a/mpc-algebra/src/share/spdz.rs +++ b/mpc-algebra/src/share/spdz.rs @@ -29,7 +29,7 @@ use crate::Reveal; #[inline] pub fn mac_share() -> F { - if Net::am_king() { + if Net::is_leader() { F::one() } else { F::zero() @@ -145,7 +145,7 @@ impl Reveal for SpdzFieldShare { let mut r: Vec = (0..(Net::n_parties() - 1)).map(|_| F::rand(rng)).collect(); let sum_r: F = r.iter().sum(); r.push(f - sum_r); - Self::from_add_shared(Net::receive_from_king(if Net::am_king() { + Self::from_add_shared(Net::receive_from_king(if Net::is_leader() { Some(r) } else { None @@ -159,7 +159,7 @@ impl Reveal for SpdzFieldShare { .map(|i| f[i] - &rs.iter().map(|r| &r[i]).sum()) .collect(); rs.push(final_shares); - Net::receive_from_king(if Net::am_king() { Some(rs) } else { None }) + Net::receive_from_king(if Net::is_leader() { Some(rs) } else { None }) .into_iter() .map(Self::from_add_shared) .collect() @@ -320,7 +320,7 @@ impl Reveal for SpdzGroupShare { let mut r: Vec = (0..(Net::n_parties() - 1)).map(|_| G::rand(rng)).collect(); let sum_r: G = r.iter().sum(); r.push(f - sum_r); - Self::from_add_shared(Net::receive_from_king(if Net::am_king() { + Self::from_add_shared(Net::receive_from_king(if Net::is_leader() { Some(r) } else { None @@ -334,7 +334,7 @@ impl Reveal for SpdzGroupShare { .map(|i| f[i] - &rs.iter().map(|r| &r[i]).sum()) .collect(); rs.push(final_shares); - Net::receive_from_king(if Net::am_king() { Some(rs) } else { None }) + Net::receive_from_king(if Net::is_leader() { Some(rs) } else { None }) .into_iter() .map(Self::from_add_shared) .collect() @@ -455,7 +455,7 @@ impl> GroupShare for SpdzGroupShare } fn shift(&mut self, other: &G) -> &mut Self { - if Net::am_king() { + if Net::is_leader() { self.sh.shift(other); } let mut other = other.clone(); @@ -531,7 +531,7 @@ impl FieldShare for SpdzMulFieldShare { } fn scale(&mut self, other: &F) -> &mut Self { - if Net::am_king() { + if Net::is_leader() { self.sh.scale(other); } self.mac.scale(&other.pow(&mac_share::().into_repr())); diff --git a/mpc-algebra/src/wire/edwards2.rs b/mpc-algebra/src/wire/edwards2.rs index 4f77d139..1530c7be 100644 --- a/mpc-algebra/src/wire/edwards2.rs +++ b/mpc-algebra/src/wire/edwards2.rs @@ -885,7 +885,7 @@ impl AffProjShare, GroupProjective mut a: Self::ProjectiveShare, o: &GroupAffine

, ) -> Self::ProjectiveShare { - if Net::am_king() { + if Net::is_leader() { a.val.add_assign_mixed(&o); } a @@ -928,7 +928,7 @@ impl AffProjShare, GroupProjective mut a: Self::ProjectiveShare, o: &GroupAffine

, ) -> Self::ProjectiveShare { - if Net::am_king() { + if Net::is_leader() { a.sh.val.add_assign_mixed(&o); } a diff --git a/mpc-algebra/src/wire/field.rs b/mpc-algebra/src/wire/field.rs index 235623c6..948dd90a 100644 --- a/mpc-algebra/src/wire/field.rs +++ b/mpc-algebra/src/wire/field.rs @@ -47,15 +47,35 @@ pub struct DummyFieldTripleSource { impl> BeaverSource for DummyFieldTripleSource { fn triple(&mut self) -> (S, S, S) { ( - S::from_add_shared(if Net::am_king() { T::one() } else { T::zero() }), - S::from_add_shared(if Net::am_king() { T::one() } else { T::zero() }), - S::from_add_shared(if Net::am_king() { T::one() } else { T::zero() }), + S::from_add_shared(if Net::is_leader() { + T::one() + } else { + T::zero() + }), + S::from_add_shared(if Net::is_leader() { + T::one() + } else { + T::zero() + }), + S::from_add_shared(if Net::is_leader() { + T::one() + } else { + T::zero() + }), ) } fn inv_pair(&mut self) -> (S, S) { ( - S::from_add_shared(if Net::am_king() { T::one() } else { T::zero() }), - S::from_add_shared(if Net::am_king() { T::one() } else { T::zero() }), + S::from_add_shared(if Net::is_leader() { + T::one() + } else { + T::zero() + }), + S::from_add_shared(if Net::is_leader() { + T::one() + } else { + T::zero() + }), ) } } diff --git a/mpc-net/Cargo.lock b/mpc-net/Cargo.lock index 38315171..523e3357 100644 --- a/mpc-net/Cargo.lock +++ b/mpc-net/Cargo.lock @@ -230,6 +230,7 @@ dependencies = [ "lazy_static", "log", "rayon", + "serde", "structopt", ] @@ -261,7 +262,7 @@ dependencies = [ "proc-macro-error-attr", "proc-macro2", "quote", - "syn", + "syn 1.0.109", "version_check", ] @@ -388,6 +389,26 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "serde" +version = "1.0.209" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99fce0ffe7310761ca6bf9faf5115afbc19688edd00171d81b1bb1b116c63e09" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.209" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.65", +] + [[package]] name = "strsim" version = "0.8.0" @@ -415,7 +436,7 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -429,6 +450,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syn" +version = "2.0.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2863d96a84c6439701d7a38f9de935ec562c8832cc55d1dde0f513b52fad106" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "termcolor" version = "1.3.0" diff --git a/mpc-net/Cargo.toml b/mpc-net/Cargo.toml index 1ae5b903..953ab36f 100644 --- a/mpc-net/Cargo.toml +++ b/mpc-net/Cargo.toml @@ -10,6 +10,7 @@ ark-std = { path = "../arkworks/std", version = "0.3.0", features = ["std"] } lazy_static = "1.4.0" log = "0.4.20" rayon = "1.8.0" +serde = { version = "1.0.209", features = ["derive"] } [dev-dependencies] env_logger = "0.10.0" diff --git a/mpc-net/src/lib.rs b/mpc-net/src/lib.rs index fce451ab..63db1d8d 100644 --- a/mpc-net/src/lib.rs +++ b/mpc-net/src/lib.rs @@ -9,6 +9,7 @@ use ark_std::{end_timer, perf_trace::TimerInfo, start_timer}; use lazy_static::lazy_static; use log::debug; use rayon::prelude::{IndexedParallelIterator, IntoParallelRefMutIterator, ParallelIterator}; +use serde::{Deserialize, Serialize}; lazy_static! { static ref CONNECTIONS: Mutex = Mutex::new(Connections::default()); @@ -266,10 +267,31 @@ impl Connections { } } +#[derive(Clone, Debug)] +pub enum MPCNetError { + Generic(String), + Protocol { err: String, party: u32 }, + NotConnected, + BadInput { err: &'static str }, +} + +impl From for MPCNetError { + fn from(e: T) -> Self { + MPCNetError::Generic(e.to_string()) + } +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash, Copy)] +pub enum MultiplexedStreamID { + Zero = 0, + One = 1, + Two = 2, +} + pub trait MpcNet { /// Am I the first party? #[inline] - fn am_king() -> bool { + fn is_leader() -> bool { Self::party_id() == 0 } /// How many parties are there? @@ -293,10 +315,10 @@ pub trait MpcNet { /// All parties send bytes to each other. fn broadcast_bytes(bytes: &[u8]) -> Vec>; /// All parties send bytes to the king. - fn send_bytes_to_king(bytes: &[u8]) -> Option>>; + fn worker_send_or_leader_receive(bytes: &[u8]) -> Option>>; /// All parties recv bytes from the king. /// Provide bytes iff you're the king! - fn recv_bytes_from_king(bytes: Option>>) -> Vec; + fn worker_receive_or_leader_send(bytes: Option>>) -> Vec; /// Everyone sends bytes to the king, who recieves those bytes, runs a computation on them, and /// redistributes the resulting bytes. @@ -304,9 +326,9 @@ pub trait MpcNet { /// The king's computation is given by a function, `f` /// proceeds. #[inline] - fn king_compute(bytes: &[u8], f: impl Fn(Vec>) -> Vec>) -> Vec { - let king_response = Self::send_bytes_to_king(bytes).map(f); - Self::recv_bytes_from_king(king_response) + fn leader_compute(bytes: &[u8], f: impl Fn(Vec>) -> Vec>) -> Vec { + let king_response = Self::worker_send_or_leader_receive(bytes).map(f); + Self::worker_receive_or_leader_send(king_response) } fn uninit(); @@ -362,12 +384,12 @@ impl MpcNet for MpcMultiNet { } #[inline] - fn send_bytes_to_king(bytes: &[u8]) -> Option>> { + fn worker_send_or_leader_receive(bytes: &[u8]) -> Option>> { get_ch!().send_to_king(bytes) } #[inline] - fn recv_bytes_from_king(bytes: Option>>) -> Vec { + fn worker_receive_or_leader_send(bytes: Option>>) -> Vec { get_ch!().recv_from_king(bytes) } From 3007e1702ce36b34ad500e123b3dab0fc03e13bb Mon Sep 17 00:00:00 2001 From: taskooh Date: Sat, 31 Aug 2024 17:40:53 +0900 Subject: [PATCH 2/5] bugfix of test --- mpc-algebra/examples/algebra.rs | 7 +++---- mpc-algebra/src/share/additive.rs | 2 +- mpc-algebra/src/share/spdz.rs | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/mpc-algebra/examples/algebra.rs b/mpc-algebra/examples/algebra.rs index 8ea7da20..aaca484a 100644 --- a/mpc-algebra/examples/algebra.rs +++ b/mpc-algebra/examples/algebra.rs @@ -8,12 +8,11 @@ use ark_poly::reveal; use ark_std::PubUniformRand; use ark_std::{end_timer, start_timer}; use log::debug; -use mpc_algebra::pedersen::Randomness; use mpc_algebra::boolean_field::MpcBooleanField; +use mpc_algebra::pedersen::Randomness; use mpc_algebra::{ edwards2, share, AdditiveFieldShare, BitAdd, BitDecomposition, BitwiseLessThan, BooleanWire, - CommitmentScheme as MpcCommitmentScheme, EqualityZero, LessThan, - LogicalOperations, + CommitmentScheme as MpcCommitmentScheme, EqualityZero, LessThan, LogicalOperations, MpcEdwardsProjective, MpcField, Reveal, UniformBitRand, }; use mpc_net::{MpcMultiNet as Net, MpcNet}; @@ -412,7 +411,7 @@ fn test_pedersen_commitment() { // mpc calculation let mpc_parameters = MpcPed::setup(rng).unwrap(); - let scalar_x_bytes = if Net::am_king() { + let scalar_x_bytes = if Net::is_leader() { x_bits .iter() .map(|b| { diff --git a/mpc-algebra/src/share/additive.rs b/mpc-algebra/src/share/additive.rs index 4bb72234..383c404e 100644 --- a/mpc-algebra/src/share/additive.rs +++ b/mpc-algebra/src/share/additive.rs @@ -524,7 +524,7 @@ macro_rules! groups_share { mut a: Self::ProjectiveShare, o: &E::$affine, ) -> Self::ProjectiveShare { - if Net::am_king() { + if Net::is_leader() { a.val.add_assign_mixed(&o); } a diff --git a/mpc-algebra/src/share/spdz.rs b/mpc-algebra/src/share/spdz.rs index 7edd8776..c61fb6e8 100644 --- a/mpc-algebra/src/share/spdz.rs +++ b/mpc-algebra/src/share/spdz.rs @@ -642,7 +642,7 @@ macro_rules! groups_share { mut a: Self::ProjectiveShare, o: &E::$affine, ) -> Self::ProjectiveShare { - if Net::am_king() { + if Net::is_leader() { a.sh.val.add_assign_mixed(&o); } a.mac.val += &o.scalar_mul(mac_share::()); From ec22f5168d6235b819b6660e2c5aae574b77be48 Mon Sep 17 00:00:00 2001 From: taskooh Date: Sat, 31 Aug 2024 17:57:48 +0900 Subject: [PATCH 3/5] Separate files --- mpc-net/examples/broadcast.rs | 9 +- mpc-net/src/lib.rs | 334 +--------------------------------- mpc-net/src/multi.rs | 334 ++++++++++++++++++++++++++++++++++ 3 files changed, 342 insertions(+), 335 deletions(-) create mode 100644 mpc-net/src/multi.rs diff --git a/mpc-net/examples/broadcast.rs b/mpc-net/examples/broadcast.rs index 4a3a40a5..6e656a78 100644 --- a/mpc-net/examples/broadcast.rs +++ b/mpc-net/examples/broadcast.rs @@ -16,15 +16,18 @@ struct Opt { } fn main() { - env_logger::builder().format_timestamp(None).format_module_path(false).init(); + env_logger::builder() + .format_timestamp(None) + .format_module_path(false) + .init(); debug!("Start"); let opt = Opt::from_args(); println!("{:?}", opt); MpcMultiNet::init_from_file(opt.input.to_str().unwrap(), opt.id); let all = MpcMultiNet::broadcast_bytes(&[opt.id as u8]); println!("{:?}", all); - let r = MpcMultiNet::send_bytes_to_king(&[opt.id as u8]); - let all = MpcMultiNet::recv_bytes_from_king(r); + let r = MpcMultiNet::worker_send_or_leader_receive(&[opt.id as u8]); + let all = MpcMultiNet::worker_receive_or_leader_send(r); println!("{:?}", all); // TODO MpcMultiNet::uninit(); diff --git a/mpc-net/src/lib.rs b/mpc-net/src/lib.rs index 63db1d8d..74cd1e80 100644 --- a/mpc-net/src/lib.rs +++ b/mpc-net/src/lib.rs @@ -1,272 +1,7 @@ -use std::{ - fs::File, - io::{BufRead, BufReader, Read, Write}, - net::{SocketAddr, TcpListener, TcpStream}, - sync::Mutex, -}; - -use ark_std::{end_timer, perf_trace::TimerInfo, start_timer}; -use lazy_static::lazy_static; -use log::debug; -use rayon::prelude::{IndexedParallelIterator, IntoParallelRefMutIterator, ParallelIterator}; +pub mod multi; +pub use multi::*; use serde::{Deserialize, Serialize}; -lazy_static! { - static ref CONNECTIONS: Mutex = Mutex::new(Connections::default()); -} - -/// Macro for locking the FieldChannel singleton in the current scope. -macro_rules! get_ch { - () => { - CONNECTIONS.lock().expect("Poisoned FieldChannel") - }; -} - -#[derive(Debug)] -struct Peer { - id: usize, - addr: SocketAddr, - stream: Option, -} - -impl Default for Peer { - fn default() -> Self { - Self { - id: 0, - addr: "127.0.0.1:8000".parse().unwrap(), - stream: None, - } - } -} - -#[derive(Default, Debug, Clone)] -pub struct Stats { - pub bytes_sent: usize, - pub bytes_recv: usize, - pub broadcasts: usize, - pub to_king: usize, - pub from_king: usize, -} - -#[derive(Default, Debug)] -struct Connections { - id: usize, - peers: Vec, - stats: Stats, -} - -impl Connections { - /// Given a path and the `id` of oneself, initialize the structure - fn init_from_path(&mut self, path: &str, id: usize) { - let f = BufReader::new(File::open(path).expect("host configuration path")); - let mut peer_id = 0; - for line in f.lines() { - let line = line.unwrap(); - let trimmed = line.trim(); - if !trimmed.is_empty() { - let addr: SocketAddr = trimmed - .parse() - .unwrap_or_else(|e| panic!("bad socket address: {}:\n{}", trimmed, e)); - let peer = Peer { - id: peer_id, - addr, - stream: None, - }; - self.peers.push(peer); - peer_id += 1; - } - } - assert!(id < self.peers.len()); - self.id = id; - } - fn connect_to_all(&mut self) { - let timer = start_timer!(|| "Connecting"); - let n = self.peers.len(); - for from_id in 0..n { - for to_id in (from_id + 1)..n { - debug!("{} to {}", from_id, to_id); - if self.id == from_id { - let to_addr = self.peers[to_id].addr; - debug!("Contacting {}", to_id); - let stream = loop { - let mut ms_waited = 0; - match TcpStream::connect(to_addr) { - Ok(s) => break s, - Err(e) => match e.kind() { - std::io::ErrorKind::ConnectionRefused - | std::io::ErrorKind::ConnectionReset => { - ms_waited += 10; - std::thread::sleep(std::time::Duration::from_millis(10)); - if ms_waited % 3_000 == 0 { - debug!("Still waiting"); - } else if ms_waited > 30_000 { - panic!("Could not find peer in 30s"); - } - } - _ => { - panic!("Error during FieldChannel::new: {}", e); - } - }, - } - }; - stream.set_nodelay(true).unwrap(); - self.peers[to_id].stream = Some(stream); - } else if self.id == to_id { - debug!("Awaiting {}", from_id); - let listener = TcpListener::bind(self.peers[self.id].addr).unwrap(); - let (stream, _addr) = listener.accept().unwrap(); - stream.set_nodelay(true).unwrap(); - self.peers[from_id].stream = Some(stream); - } - } - // Sender for next round waits for note from this sender to prevent race on receipt. - if from_id + 1 < n { - if self.id == from_id { - self.peers[self.id + 1] - .stream - .as_mut() - .unwrap() - .write_all(&[0u8]) - .unwrap(); - } else if self.id == from_id + 1 { - self.peers[self.id - 1] - .stream - .as_mut() - .unwrap() - .read_exact(&mut [0u8]) - .unwrap(); - } - } - } - // Do a round with the king, to be sure everyone is ready - let from_all = self.send_to_king(&[self.id as u8]); - self.recv_from_king(from_all); - for id in 0..n { - if id != self.id { - assert!(self.peers[id].stream.is_some()); - } - } - end_timer!(timer); - } - fn am_king(&self) -> bool { - self.id == 0 - } - fn broadcast(&mut self, bytes_out: &[u8]) -> Vec> { - let timer: TimerInfo; - #[cfg(feature = "log_broadcast")] - { - timer = start_timer!(|| format!("Broadcast {}", bytes_out.len())); - } - let m = bytes_out.len(); - let own_id = self.id; - self.stats.bytes_sent += (self.peers.len() - 1) * m; - self.stats.bytes_recv += (self.peers.len() - 1) * m; - self.stats.broadcasts += 1; - let r = self - .peers - .par_iter_mut() - .enumerate() - .map(|(id, peer)| { - let mut bytes_in = vec![0u8; m]; - match id.cmp(&own_id) { - std::cmp::Ordering::Less => { - let stream = peer.stream.as_mut().unwrap(); - stream.read_exact(&mut bytes_in[..]).unwrap(); - stream.write_all(bytes_out).unwrap(); - } - std::cmp::Ordering::Equal => { - bytes_in.copy_from_slice(bytes_out); - } - std::cmp::Ordering::Greater => { - let stream = peer.stream.as_mut().unwrap(); - stream.write_all(bytes_out).unwrap(); - stream.read_exact(&mut bytes_in[..]).unwrap(); - } - } - bytes_in - }) - .collect(); - #[cfg(feature = "log_broadcast")] - { - end_timer!(timer); - } - r - } - fn send_to_king(&mut self, bytes_out: &[u8]) -> Option>> { - let timer = start_timer!(|| format!("To king {}", bytes_out.len())); - let m = bytes_out.len(); - let own_id = self.id; - self.stats.to_king += 1; - let r = if self.am_king() { - self.stats.bytes_recv += (self.peers.len() - 1) * m; - Some( - self.peers - .par_iter_mut() - .enumerate() - .map(|(id, peer)| { - let mut bytes_in = vec![0u8; m]; - if id == own_id { - bytes_in.copy_from_slice(bytes_out); - } else { - let stream = peer.stream.as_mut().unwrap(); - stream.read_exact(&mut bytes_in[..]).unwrap(); - }; - bytes_in - }) - .collect(), - ) - } else { - self.stats.bytes_sent += m; - self.peers[0] - .stream - .as_mut() - .unwrap() - .write_all(bytes_out) - .unwrap(); - None - }; - end_timer!(timer); - r - } - fn recv_from_king(&mut self, bytes_out: Option>>) -> Vec { - let own_id = self.id; - self.stats.from_king += 1; - if self.am_king() { - let bytes_out = bytes_out.unwrap(); - let m = bytes_out[0].len(); - let timer = start_timer!(|| format!("From king {}", m)); - let bytes_size = (m as u64).to_le_bytes(); - self.stats.bytes_sent += (self.peers.len() - 1) * (m + 8); - self.peers - .par_iter_mut() - .enumerate() - .filter(|p| p.0 != own_id) - .for_each(|(id, peer)| { - let stream = peer.stream.as_mut().unwrap(); - assert_eq!(bytes_out[id].len(), m); - stream.write_all(&bytes_size).unwrap(); - stream.write_all(&bytes_out[id]).unwrap(); - }); - end_timer!(timer); - bytes_out[own_id].clone() - } else { - let stream = self.peers[0].stream.as_mut().unwrap(); - let mut bytes_size = [0u8; 8]; - stream.read_exact(&mut bytes_size).unwrap(); - let m = u64::from_le_bytes(bytes_size) as usize; - self.stats.bytes_recv += m; - let mut bytes_in = vec![0u8; m]; - stream.read_exact(&mut bytes_in).unwrap(); - bytes_in - } - } - fn uninit(&mut self) { - for p in &mut self.peers { - p.stream = None; - } - } -} - #[derive(Clone, Debug)] pub enum MPCNetError { Generic(String), @@ -333,68 +68,3 @@ pub trait MpcNet { fn uninit(); } - -pub struct MpcMultiNet; - -impl MpcNet for MpcMultiNet { - #[inline] - fn party_id() -> usize { - get_ch!().id - } - - #[inline] - fn n_parties() -> usize { - get_ch!().peers.len() - } - - #[inline] - fn init_from_file(path: &str, party_id: usize) { - let mut ch = get_ch!(); - ch.init_from_path(path, party_id); - ch.connect_to_all(); - } - - #[inline] - fn is_init() -> bool { - get_ch!() - .peers - .first() - .map(|p| p.stream.is_some()) - .unwrap_or(false) - } - - #[inline] - fn deinit() { - get_ch!().uninit() - } - - #[inline] - fn reset_stats() { - get_ch!().stats = Stats::default(); - } - - #[inline] - fn stats() -> crate::Stats { - get_ch!().stats.clone() - } - - #[inline] - fn broadcast_bytes(bytes: &[u8]) -> Vec> { - get_ch!().broadcast(bytes) - } - - #[inline] - fn worker_send_or_leader_receive(bytes: &[u8]) -> Option>> { - get_ch!().send_to_king(bytes) - } - - #[inline] - fn worker_receive_or_leader_send(bytes: Option>>) -> Vec { - get_ch!().recv_from_king(bytes) - } - - #[inline] - fn uninit() { - get_ch!().uninit() - } -} diff --git a/mpc-net/src/multi.rs b/mpc-net/src/multi.rs new file mode 100644 index 00000000..c7fdd893 --- /dev/null +++ b/mpc-net/src/multi.rs @@ -0,0 +1,334 @@ +use std::{ + fs::File, + io::{BufRead, BufReader, Read, Write}, + net::{SocketAddr, TcpListener, TcpStream}, + sync::Mutex, +}; + +use ark_std::{end_timer, perf_trace::TimerInfo, start_timer}; +use lazy_static::lazy_static; +use log::debug; +use rayon::prelude::{IndexedParallelIterator, IntoParallelRefMutIterator, ParallelIterator}; + +use crate::MpcNet; + +lazy_static! { + static ref CONNECTIONS: Mutex = Mutex::new(Connections::default()); +} + +/// Macro for locking the FieldChannel singleton in the current scope. +macro_rules! get_ch { + () => { + CONNECTIONS.lock().expect("Poisoned FieldChannel") + }; +} + +#[derive(Debug)] +struct Peer { + id: usize, + addr: SocketAddr, + stream: Option, +} + +impl Default for Peer { + fn default() -> Self { + Self { + id: 0, + addr: "127.0.0.1:8000".parse().unwrap(), + stream: None, + } + } +} + +#[derive(Default, Debug, Clone)] +pub struct Stats { + pub bytes_sent: usize, + pub bytes_recv: usize, + pub broadcasts: usize, + pub to_king: usize, + pub from_king: usize, +} + +#[derive(Default, Debug)] +struct Connections { + id: usize, + peers: Vec, + stats: Stats, +} + +impl Connections { + /// Given a path and the `id` of oneself, initialize the structure + fn init_from_path(&mut self, path: &str, id: usize) { + let f = BufReader::new(File::open(path).expect("host configuration path")); + let mut peer_id = 0; + for line in f.lines() { + let line = line.unwrap(); + let trimmed = line.trim(); + if !trimmed.is_empty() { + let addr: SocketAddr = trimmed + .parse() + .unwrap_or_else(|e| panic!("bad socket address: {}:\n{}", trimmed, e)); + let peer = Peer { + id: peer_id, + addr, + stream: None, + }; + self.peers.push(peer); + peer_id += 1; + } + } + assert!(id < self.peers.len()); + self.id = id; + } + fn connect_to_all(&mut self) { + let timer = start_timer!(|| "Connecting"); + let n = self.peers.len(); + for from_id in 0..n { + for to_id in (from_id + 1)..n { + debug!("{} to {}", from_id, to_id); + if self.id == from_id { + let to_addr = self.peers[to_id].addr; + debug!("Contacting {}", to_id); + let stream = loop { + let mut ms_waited = 0; + match TcpStream::connect(to_addr) { + Ok(s) => break s, + Err(e) => match e.kind() { + std::io::ErrorKind::ConnectionRefused + | std::io::ErrorKind::ConnectionReset => { + ms_waited += 10; + std::thread::sleep(std::time::Duration::from_millis(10)); + if ms_waited % 3_000 == 0 { + debug!("Still waiting"); + } else if ms_waited > 30_000 { + panic!("Could not find peer in 30s"); + } + } + _ => { + panic!("Error during FieldChannel::new: {}", e); + } + }, + } + }; + stream.set_nodelay(true).unwrap(); + self.peers[to_id].stream = Some(stream); + } else if self.id == to_id { + debug!("Awaiting {}", from_id); + let listener = TcpListener::bind(self.peers[self.id].addr).unwrap(); + let (stream, _addr) = listener.accept().unwrap(); + stream.set_nodelay(true).unwrap(); + self.peers[from_id].stream = Some(stream); + } + } + // Sender for next round waits for note from this sender to prevent race on receipt. + if from_id + 1 < n { + if self.id == from_id { + self.peers[self.id + 1] + .stream + .as_mut() + .unwrap() + .write_all(&[0u8]) + .unwrap(); + } else if self.id == from_id + 1 { + self.peers[self.id - 1] + .stream + .as_mut() + .unwrap() + .read_exact(&mut [0u8]) + .unwrap(); + } + } + } + // Do a round with the king, to be sure everyone is ready + let from_all = self.send_to_king(&[self.id as u8]); + self.recv_from_king(from_all); + for id in 0..n { + if id != self.id { + assert!(self.peers[id].stream.is_some()); + } + } + end_timer!(timer); + } + fn am_king(&self) -> bool { + self.id == 0 + } + fn broadcast(&mut self, bytes_out: &[u8]) -> Vec> { + let timer: TimerInfo; + #[cfg(feature = "log_broadcast")] + { + timer = start_timer!(|| format!("Broadcast {}", bytes_out.len())); + } + let m = bytes_out.len(); + let own_id = self.id; + self.stats.bytes_sent += (self.peers.len() - 1) * m; + self.stats.bytes_recv += (self.peers.len() - 1) * m; + self.stats.broadcasts += 1; + let r = self + .peers + .par_iter_mut() + .enumerate() + .map(|(id, peer)| { + let mut bytes_in = vec![0u8; m]; + match id.cmp(&own_id) { + std::cmp::Ordering::Less => { + let stream = peer.stream.as_mut().unwrap(); + stream.read_exact(&mut bytes_in[..]).unwrap(); + stream.write_all(bytes_out).unwrap(); + } + std::cmp::Ordering::Equal => { + bytes_in.copy_from_slice(bytes_out); + } + std::cmp::Ordering::Greater => { + let stream = peer.stream.as_mut().unwrap(); + stream.write_all(bytes_out).unwrap(); + stream.read_exact(&mut bytes_in[..]).unwrap(); + } + } + bytes_in + }) + .collect(); + #[cfg(feature = "log_broadcast")] + { + end_timer!(timer); + } + r + } + fn send_to_king(&mut self, bytes_out: &[u8]) -> Option>> { + let timer = start_timer!(|| format!("To king {}", bytes_out.len())); + let m = bytes_out.len(); + let own_id = self.id; + self.stats.to_king += 1; + let r = if self.am_king() { + self.stats.bytes_recv += (self.peers.len() - 1) * m; + Some( + self.peers + .par_iter_mut() + .enumerate() + .map(|(id, peer)| { + let mut bytes_in = vec![0u8; m]; + if id == own_id { + bytes_in.copy_from_slice(bytes_out); + } else { + let stream = peer.stream.as_mut().unwrap(); + stream.read_exact(&mut bytes_in[..]).unwrap(); + }; + bytes_in + }) + .collect(), + ) + } else { + self.stats.bytes_sent += m; + self.peers[0] + .stream + .as_mut() + .unwrap() + .write_all(bytes_out) + .unwrap(); + None + }; + end_timer!(timer); + r + } + fn recv_from_king(&mut self, bytes_out: Option>>) -> Vec { + let own_id = self.id; + self.stats.from_king += 1; + if self.am_king() { + let bytes_out = bytes_out.unwrap(); + let m = bytes_out[0].len(); + let timer = start_timer!(|| format!("From king {}", m)); + let bytes_size = (m as u64).to_le_bytes(); + self.stats.bytes_sent += (self.peers.len() - 1) * (m + 8); + self.peers + .par_iter_mut() + .enumerate() + .filter(|p| p.0 != own_id) + .for_each(|(id, peer)| { + let stream = peer.stream.as_mut().unwrap(); + assert_eq!(bytes_out[id].len(), m); + stream.write_all(&bytes_size).unwrap(); + stream.write_all(&bytes_out[id]).unwrap(); + }); + end_timer!(timer); + bytes_out[own_id].clone() + } else { + let stream = self.peers[0].stream.as_mut().unwrap(); + let mut bytes_size = [0u8; 8]; + stream.read_exact(&mut bytes_size).unwrap(); + let m = u64::from_le_bytes(bytes_size) as usize; + self.stats.bytes_recv += m; + let mut bytes_in = vec![0u8; m]; + stream.read_exact(&mut bytes_in).unwrap(); + bytes_in + } + } + fn uninit(&mut self) { + for p in &mut self.peers { + p.stream = None; + } + } +} + +pub struct MpcMultiNet; + +impl MpcNet for MpcMultiNet { + #[inline] + fn party_id() -> usize { + get_ch!().id + } + + #[inline] + fn n_parties() -> usize { + get_ch!().peers.len() + } + + #[inline] + fn init_from_file(path: &str, party_id: usize) { + let mut ch = get_ch!(); + ch.init_from_path(path, party_id); + ch.connect_to_all(); + } + + #[inline] + fn is_init() -> bool { + get_ch!() + .peers + .first() + .map(|p| p.stream.is_some()) + .unwrap_or(false) + } + + #[inline] + fn deinit() { + get_ch!().uninit() + } + + #[inline] + fn reset_stats() { + get_ch!().stats = Stats::default(); + } + + #[inline] + fn stats() -> crate::Stats { + get_ch!().stats.clone() + } + + #[inline] + fn broadcast_bytes(bytes: &[u8]) -> Vec> { + get_ch!().broadcast(bytes) + } + + #[inline] + fn worker_send_or_leader_receive(bytes: &[u8]) -> Option>> { + get_ch!().send_to_king(bytes) + } + + #[inline] + fn worker_receive_or_leader_send(bytes: Option>>) -> Vec { + get_ch!().recv_from_king(bytes) + } + + #[inline] + fn uninit() { + get_ch!().uninit() + } +} From 527c724983259efc22d9059831338491e0833858 Mon Sep 17 00:00:00 2001 From: taskooh Date: Thu, 3 Oct 2024 21:32:00 +0900 Subject: [PATCH 4/5] Add packages --- Cargo.lock | 443 +++++++++++++++++++++++++++++++-- Cargo.toml | 1 + mpc-algebra/Cargo.toml | 4 +- mpc-net/Cargo.lock | 540 +++++++++++++++++++++++++++++++++++------ mpc-net/Cargo.toml | 5 +- 5 files changed, 892 insertions(+), 101 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4601e8cb..59cb97d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,21 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "addr2line" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "ahash" version = "0.7.6" @@ -37,6 +52,55 @@ dependencies = [ "winapi", ] +[[package]] +name = "anstream" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" + +[[package]] +name = "anstyle-parse" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + [[package]] name = "approx" version = "0.5.1" @@ -284,13 +348,31 @@ dependencies = [ "rand", ] +[[package]] +name = "async_smux" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd0f8695d370a810623d18891f6d4d60149afd8499d7f1e608a7fb1cbf099531" +dependencies = [ + "bytes", + "futures", + "futures-sink", + "log", + "parking_lot", + "pin-project", + "rand", + "thiserror", + "tokio", + "tokio-util", +] + [[package]] name = "atty" version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ - "hermit-abi", + "hermit-abi 0.1.19", "libc", "winapi", ] @@ -301,12 +383,33 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "backtrace" +version = "0.3.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + [[package]] name = "blake2" version = "0.9.2" @@ -339,12 +442,27 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94bbb0ad554ad961ddc5da507a12a29b14e4ae5bda06b19f575a3e6079d2e2ae" +[[package]] +name = "bytes" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50" + [[package]] name = "cast" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" +[[package]] +name = "cc" +version = "1.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b62ac837cdb5cb22e10a256099b4fc502b1dfe560cb282963a974d7abd80e476" +dependencies = [ + "shlex", +] + [[package]] name = "cfg-if" version = "1.0.0" @@ -386,7 +504,7 @@ checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" dependencies = [ "ansi_term", "atty", - "bitflags", + "bitflags 1.3.2", "strsim", "textwrap 0.11.0", "unicode-width", @@ -399,7 +517,7 @@ version = "3.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" dependencies = [ - "bitflags", + "bitflags 1.3.2", "clap_lex", "indexmap", "textwrap 0.16.1", @@ -414,6 +532,12 @@ dependencies = [ "os_str_bytes", ] +[[package]] +name = "colorchoice" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" + [[package]] name = "colored" version = "2.1.0" @@ -544,17 +668,116 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" +[[package]] +name = "env_filter" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f2c92ceda6ceec50f43169f9ee8424fe2db276791afde7b2cd8bc084cb376ab" +dependencies = [ + "log", + "regex", +] + [[package]] name = "env_logger" -version = "0.8.4" +version = "0.11.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3" +checksum = "e13fa619b91fb2381732789fc5de83b45675e882f66623b7d8cb4f643017018d" dependencies = [ - "atty", + "anstream", + "anstyle", + "env_filter", "humantime", "log", - "regex", - "termcolor", +] + +[[package]] +name = "futures" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" + +[[package]] +name = "futures-executor" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" + +[[package]] +name = "futures-macro" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.60", +] + +[[package]] +name = "futures-sink" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" + +[[package]] +name = "futures-task" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" + +[[package]] +name = "futures-util" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", ] [[package]] @@ -578,6 +801,12 @@ dependencies = [ "wasi", ] +[[package]] +name = "gimli" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" + [[package]] name = "half" version = "2.4.1" @@ -621,6 +850,12 @@ dependencies = [ "libc", ] +[[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + [[package]] name = "hex" version = "0.4.3" @@ -643,6 +878,12 @@ dependencies = [ "hashbrown 0.12.3", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + [[package]] name = "itertools" version = "0.10.5" @@ -694,11 +935,21 @@ version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7012b1bbb0719e1097c47611d3898568c546d597c2e74d66f6087edd5233ff4" +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "log" -version = "0.4.20" +version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" [[package]] name = "matrixmultiply" @@ -725,6 +976,27 @@ dependencies = [ "autocfg", ] +[[package]] +name = "miniz_oxide" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" +dependencies = [ + "adler", +] + +[[package]] +name = "mio" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" +dependencies = [ + "hermit-abi 0.3.9", + "libc", + "wasi", + "windows-sys 0.52.0", +] + [[package]] name = "mpc-algebra" version = "0.1.0" @@ -757,10 +1029,13 @@ name = "mpc-net" version = "0.1.0" dependencies = [ "ark-std", + "async_smux", "lazy_static", "log", "rayon", "serde", + "tokio", + "tokio-util", ] [[package]] @@ -846,6 +1121,15 @@ dependencies = [ "libm", ] +[[package]] +name = "object" +version = "0.36.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "084f1a5821ac4c651660a94a7153d27ac9d8a53736203f58b31945ded098070a" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.18.0" @@ -870,6 +1154,29 @@ version = "6.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2355d85b9a3786f481747ced0e0ff2ba35213a1f9bd406ed906554d7af805a1" +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets 0.52.5", +] + [[package]] name = "paste" version = "1.0.14" @@ -886,12 +1193,38 @@ dependencies = [ "ucd-trie", ] +[[package]] +name = "pin-project" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.60", +] + [[package]] name = "pin-project-lite" version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "12cc1b0bf1727a77a54b6654e7b5f1af8604923edc8b81885f8ec92f9e3f0a05" +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "plotters" version = "0.3.5" @@ -1034,6 +1367,15 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "redox_syscall" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0884ad60e090bf1345b93da0a5de8923c93884cd03f40dfcfddd3b4bee661853" +dependencies = [ + "bitflags 2.6.0", +] + [[package]] name = "regex" version = "1.10.4" @@ -1063,6 +1405,12 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + [[package]] name = "rustc_version" version = "0.3.3" @@ -1164,6 +1512,12 @@ dependencies = [ "opaque-debug", ] +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + [[package]] name = "simba" version = "0.9.0" @@ -1177,6 +1531,31 @@ dependencies = [ "wide", ] +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + +[[package]] +name = "socket2" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "strsim" version = "0.8.0" @@ -1235,15 +1614,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "termcolor" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" -dependencies = [ - "winapi-util", -] - [[package]] name = "textwrap" version = "0.11.0" @@ -1289,6 +1659,34 @@ dependencies = [ "serde_json", ] +[[package]] +name = "tokio" +version = "1.40.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998" +dependencies = [ + "backtrace", + "bytes", + "libc", + "mio", + "pin-project-lite", + "socket2", + "windows-sys 0.52.0", +] + +[[package]] +name = "tokio-util" +version = "0.7.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61e7c3654c13bcd040d4a03abee2c75b1d14a37b423cf5a813ceae1cc903ec6a" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + [[package]] name = "tracing" version = "0.1.37" @@ -1361,6 +1759,12 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "valuable" version = "0.1.0" @@ -1695,5 +2099,6 @@ dependencies = [ "serde", "serde_json", "structopt", + "tokio", "zeroize", ] diff --git a/Cargo.toml b/Cargo.toml index a058c575..3898dc2c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,6 +48,7 @@ mpc-net = { path = "mpc-net", version = "0.1.0" } mpc-trait = { path = "mpc-trait", version = "0.1.0" } itertools = "0.13.0" nalgebra = "0.33.0" +tokio = { version = "1.40.0", features = ["sync", "net"] } [dev-dependencies] criterion = { version = "0.4", features = ["html_reports"] } diff --git a/mpc-algebra/Cargo.toml b/mpc-algebra/Cargo.toml index 4a25ee9c..4dcd3bb7 100644 --- a/mpc-algebra/Cargo.toml +++ b/mpc-algebra/Cargo.toml @@ -29,12 +29,12 @@ mpc-net = { path = "../mpc-net" } mpc-trait = { path = "../mpc-trait" } structopt = "0.3" -env_logger = "0.8" sha2 = "0.9" +env_logger = "0.11.5" [features] default = ["ark-r1cs-std"] [[example]] name = "algebra" -path = "examples/algebra.rs" \ No newline at end of file +path = "examples/algebra.rs" diff --git a/mpc-net/Cargo.lock b/mpc-net/Cargo.lock index 523e3357..8f3bb806 100644 --- a/mpc-net/Cargo.lock +++ b/mpc-net/Cargo.lock @@ -2,11 +2,26 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "addr2line" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5fb1d8e4442bd405fdfd1dacb42792696b0cf9cb15882e5d097b742a676d375" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" + [[package]] name = "aho-corasick" -version = "1.1.2" +version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" dependencies = [ "memchr", ] @@ -20,6 +35,55 @@ dependencies = [ "winapi", ] +[[package]] +name = "anstream" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" + +[[package]] +name = "anstyle-parse" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" +dependencies = [ + "anstyle", + "windows-sys", +] + [[package]] name = "ark-std" version = "0.3.0" @@ -29,6 +93,24 @@ dependencies = [ "rand", ] +[[package]] +name = "async_smux" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd0f8695d370a810623d18891f6d4d60149afd8499d7f1e608a7fb1cbf099531" +dependencies = [ + "bytes", + "futures", + "futures-sink", + "log", + "parking_lot", + "pin-project", + "rand", + "thiserror", + "tokio", + "tokio-util", +] + [[package]] name = "atty" version = "0.2.14" @@ -46,6 +128,21 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "backtrace" +version = "0.3.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-targets", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -58,6 +155,12 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" +[[package]] +name = "bytes" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50" + [[package]] name = "cfg-if" version = "1.0.0" @@ -79,6 +182,12 @@ dependencies = [ "vec_map", ] +[[package]] +name = "colorchoice" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" + [[package]] name = "crossbeam-deque" version = "0.8.3" @@ -118,29 +227,135 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" +[[package]] +name = "env_filter" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f2c92ceda6ceec50f43169f9ee8424fe2db276791afde7b2cd8bc084cb376ab" +dependencies = [ + "log", + "regex", +] + [[package]] name = "env_logger" -version = "0.10.0" +version = "0.11.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85cdab6a89accf66733ad5a1693a4dcced6aeff64602b634530dd73c1f3ee9f0" +checksum = "e13fa619b91fb2381732789fc5de83b45675e882f66623b7d8cb4f643017018d" dependencies = [ + "anstream", + "anstyle", + "env_filter", "humantime", - "is-terminal", "log", - "regex", - "termcolor", ] [[package]] -name = "errno" -version = "0.3.5" +name = "futures" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" + +[[package]] +name = "futures-executor" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" + +[[package]] +name = "futures-macro" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.65", +] + +[[package]] +name = "futures-sink" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" + +[[package]] +name = "futures-task" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" + +[[package]] +name = "futures-util" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "getrandom" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3e13f66a2f95e32a39eaa81f6b95d42878ca0e1db0c7543723dfe12557e860" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" dependencies = [ + "cfg-if", "libc", - "windows-sys", + "wasi", ] +[[package]] +name = "gimli" +version = "0.31.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32085ea23f3234fc7846555e85283ba4de91e21016dc0455a16286d87a292d64" + [[package]] name = "heck" version = "0.3.3" @@ -161,9 +376,9 @@ dependencies = [ [[package]] name = "hermit-abi" -version = "0.3.3" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" [[package]] name = "humantime" @@ -172,15 +387,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] -name = "is-terminal" -version = "0.4.9" +name = "is_terminal_polyfill" +version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" -dependencies = [ - "hermit-abi 0.3.3", - "rustix", - "windows-sys", -] +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" [[package]] name = "lazy_static" @@ -190,21 +400,25 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.155" +version = "0.2.158" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" [[package]] -name = "linux-raw-sys" -version = "0.4.10" +name = "lock_api" +version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da2479e8c062e40bf0066ffa0bc823de0a9368974af99c9f6df941d2c231e03f" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] [[package]] name = "log" -version = "0.4.20" +version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" [[package]] name = "memchr" @@ -221,17 +435,41 @@ dependencies = [ "autocfg", ] +[[package]] +name = "miniz_oxide" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" +dependencies = [ + "adler2", +] + +[[package]] +name = "mio" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" +dependencies = [ + "hermit-abi 0.3.9", + "libc", + "wasi", + "windows-sys", +] + [[package]] name = "mpc-net" version = "0.1.0" dependencies = [ "ark-std", + "async_smux", "env_logger", "lazy_static", "log", "rayon", "serde", "structopt", + "tokio", + "tokio-util", ] [[package]] @@ -247,6 +485,70 @@ dependencies = [ "autocfg", ] +[[package]] +name = "object" +version = "0.36.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "084f1a5821ac4c651660a94a7153d27ac9d8a53736203f58b31945ded098070a" +dependencies = [ + "memchr", +] + +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + +[[package]] +name = "pin-project" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.65", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -301,6 +603,7 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ + "libc", "rand_chacha", "rand_core", ] @@ -320,6 +623,9 @@ name = "rand_core" version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] [[package]] name = "rayon" @@ -341,11 +647,20 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "redox_syscall" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0884ad60e090bf1345b93da0a5de8923c93884cd03f40dfcfddd3b4bee661853" +dependencies = [ + "bitflags 2.4.1", +] + [[package]] name = "regex" -version = "1.10.2" +version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +checksum = "38200e5ee88914975b69f657f0801b6f6dccafd44fd9326302a4aaeecfacb1d8" dependencies = [ "aho-corasick", "memchr", @@ -355,9 +670,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.3" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" +checksum = "368758f23274712b504848e9d5a6f010445cc8b87a7cdb4d7cbee666c1288da3" dependencies = [ "aho-corasick", "memchr", @@ -366,22 +681,15 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.8.2" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] -name = "rustix" -version = "0.38.20" +name = "rustc-demangle" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67ce50cb2e16c2903e30d1cbccfd8387a74b9d4c938b6a4c5ec6cc7556f7a8a0" -dependencies = [ - "bitflags 2.4.1", - "errno", - "libc", - "linux-raw-sys", - "windows-sys", -] +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" [[package]] name = "scopeguard" @@ -409,6 +717,31 @@ dependencies = [ "syn 2.0.65", ] +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + +[[package]] +name = "socket2" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" +dependencies = [ + "libc", + "windows-sys", +] + [[package]] name = "strsim" version = "0.8.0" @@ -462,21 +795,60 @@ dependencies = [ ] [[package]] -name = "termcolor" -version = "1.3.0" +name = "textwrap" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6093bad37da69aab9d123a8091e4be0aa4a03e4d601ec641c327398315f62b64" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" dependencies = [ - "winapi-util", + "unicode-width", ] [[package]] -name = "textwrap" -version = "0.11.0" +name = "thiserror" +version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" dependencies = [ - "unicode-width", + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.65", +] + +[[package]] +name = "tokio" +version = "1.40.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998" +dependencies = [ + "backtrace", + "bytes", + "libc", + "mio", + "pin-project-lite", + "socket2", + "windows-sys", +] + +[[package]] +name = "tokio-util" +version = "0.7.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61e7c3654c13bcd040d4a03abee2c75b1d14a37b423cf5a813ceae1cc903ec6a" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", ] [[package]] @@ -497,6 +869,12 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "vec_map" version = "0.8.2" @@ -509,6 +887,12 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "winapi" version = "0.3.9" @@ -525,15 +909,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" -[[package]] -name = "winapi-util" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" -dependencies = [ - "winapi", -] - [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" @@ -542,22 +917,23 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows-sys" -version = "0.48.0" +version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ "windows-targets", ] [[package]] name = "windows-targets" -version = "0.48.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", + "windows_i686_gnullvm", "windows_i686_msvc", "windows_x86_64_gnu", "windows_x86_64_gnullvm", @@ -566,42 +942,48 @@ dependencies = [ [[package]] name = "windows_aarch64_gnullvm" -version = "0.48.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" -version = "0.48.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" -version = "0.48.5" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" -version = "0.48.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" -version = "0.48.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" -version = "0.48.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" -version = "0.48.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/mpc-net/Cargo.toml b/mpc-net/Cargo.toml index 953ab36f..0086b484 100644 --- a/mpc-net/Cargo.toml +++ b/mpc-net/Cargo.toml @@ -7,11 +7,14 @@ edition = "2021" [dependencies] ark-std = { path = "../arkworks/std", version = "0.3.0", features = ["std"] } +async_smux = "0.3.3" lazy_static = "1.4.0" log = "0.4.20" rayon = "1.8.0" serde = { version = "1.0.209", features = ["derive"] } +tokio = { version = "1.40.0", features = ["sync", "net"] } +tokio-util = { version = "0.7.12", features = ["codec"] } [dev-dependencies] -env_logger = "0.10.0" +env_logger = "0.11.5" structopt = "0.3.26" From 048653ea188b14ea3e702064a4a54ff247e080bb Mon Sep 17 00:00:00 2001 From: taskooh Date: Tue, 8 Oct 2024 15:21:07 +0900 Subject: [PATCH 5/5] WIP --- mpc-net/src/lib.rs | 25 +++--- mpc-net/src/multi.rs | 199 ++++++++++++++++++------------------------- 2 files changed, 93 insertions(+), 131 deletions(-) diff --git a/mpc-net/src/lib.rs b/mpc-net/src/lib.rs index 74cd1e80..d46899ec 100644 --- a/mpc-net/src/lib.rs +++ b/mpc-net/src/lib.rs @@ -23,16 +23,15 @@ pub enum MultiplexedStreamID { Two = 2, } -pub trait MpcNet { +pub trait MpcNet: Send + Sync { /// Am I the first party? - #[inline] - fn is_leader() -> bool { - Self::party_id() == 0 + fn is_leader(&self) -> bool { + self.party_id() == 0 } /// How many parties are there? - fn n_parties() -> usize; + fn n_parties(&self) -> usize; /// What is my party number (0 to n-1)? - fn party_id() -> usize; + fn party_id(&self) -> usize; /// Initialize the network layer from a file. /// The file should contain one HOST:PORT setting per line, corresponding to the addresses of /// the parties in increasing order. @@ -40,7 +39,7 @@ pub trait MpcNet { /// Parties are zero-indexed. fn init_from_file(path: &str, party_id: usize); /// Is the network layer initalized? - fn is_init() -> bool; + fn is_init(&self) -> bool; /// Uninitialize the network layer, closing all connections. fn deinit(); /// Set statistics to zero. @@ -48,12 +47,12 @@ pub trait MpcNet { /// Get statistics. fn stats() -> Stats; /// All parties send bytes to each other. - fn broadcast_bytes(bytes: &[u8]) -> Vec>; + fn broadcast_bytes(&self, bytes: &[u8]) -> Vec>; /// All parties send bytes to the king. - fn worker_send_or_leader_receive(bytes: &[u8]) -> Option>>; + fn worker_send_or_leader_receive(&self, bytes: &[u8]) -> Option>>; /// All parties recv bytes from the king. /// Provide bytes iff you're the king! - fn worker_receive_or_leader_send(bytes: Option>>) -> Vec; + fn worker_receive_or_leader_send(&self, bytes: Option>>) -> Vec; /// Everyone sends bytes to the king, who recieves those bytes, runs a computation on them, and /// redistributes the resulting bytes. @@ -61,9 +60,9 @@ pub trait MpcNet { /// The king's computation is given by a function, `f` /// proceeds. #[inline] - fn leader_compute(bytes: &[u8], f: impl Fn(Vec>) -> Vec>) -> Vec { - let king_response = Self::worker_send_or_leader_receive(bytes).map(f); - Self::worker_receive_or_leader_send(king_response) + fn leader_compute(&self, bytes: &[u8], f: impl Fn(Vec>) -> Vec>) -> Vec { + let king_response = self.worker_send_or_leader_receive(bytes).map(f); + self.worker_receive_or_leader_send(king_response) } fn uninit(); diff --git a/mpc-net/src/multi.rs b/mpc-net/src/multi.rs index c7fdd893..801ffad9 100644 --- a/mpc-net/src/multi.rs +++ b/mpc-net/src/multi.rs @@ -1,41 +1,42 @@ -use std::{ - fs::File, - io::{BufRead, BufReader, Read, Write}, - net::{SocketAddr, TcpListener, TcpStream}, - sync::Mutex, -}; - use ark_std::{end_timer, perf_trace::TimerInfo, start_timer}; -use lazy_static::lazy_static; -use log::debug; -use rayon::prelude::{IndexedParallelIterator, IntoParallelRefMutIterator, ParallelIterator}; +use async_smux::MuxStream; +use std::collections::HashMap; +use std::fmt::{Debug, Formatter}; +use std::fs::File; +use std::io::{BufRead, BufReader}; +use std::net::SocketAddr; +use std::sync::atomic::AtomicUsize; +use tokio::io::{AsyncRead, AsyncWrite}; +use tokio::net::{TcpListener, TcpStream}; +use tokio::sync::Mutex as TokioMutex; +use tokio_util::codec::{Framed, LengthDelimitedCodec}; -use crate::MpcNet; +use crate::{MPCNetError, MpcNet}; -lazy_static! { - static ref CONNECTIONS: Mutex = Mutex::new(Connections::default()); -} +pub type WrappedMuxStream = Framed, LengthDelimitedCodec>; -/// Macro for locking the FieldChannel singleton in the current scope. -macro_rules! get_ch { - () => { - CONNECTIONS.lock().expect("Poisoned FieldChannel") - }; +struct Peer { + id: usize, + listen_addr: SocketAddr, + streams: Option>>>, } -#[derive(Debug)] -struct Peer { - id: usize, - addr: SocketAddr, - stream: Option, +impl Debug for Peer { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let mut f = f.debug_struct("Peer"); + f.field("id", &self.id); + f.field("listen_addr", &self.listen_addr); + f.field("streams", &self.streams.is_some()); + f.finish() + } } -impl Default for Peer { - fn default() -> Self { +impl Clone for Peer { + fn clone(&self) -> Self { Self { - id: 0, - addr: "127.0.0.1:8000".parse().unwrap(), - stream: None, + id: self.id, + listen_addr: self.listen_addr, + streams: None, } } } @@ -50,15 +51,26 @@ pub struct Stats { } #[derive(Default, Debug)] -struct Connections { - id: usize, - peers: Vec, - stats: Stats, +struct MPCNetConnection { + pub id: usize, + pub listener: Option, + pub peers: HashMap>, + pub n_parties: usize, + pub upload: AtomicUsize, + pub download: AtomicUsize, } -impl Connections { +impl MPCNetConnection { /// Given a path and the `id` of oneself, initialize the structure - fn init_from_path(&mut self, path: &str, id: usize) { + fn init_from_path(path: &str, id: usize) -> Self { + let mut this = MPCNetConnection { + id: 0, + listener: None, + peers: Default::default(), + n_parties: 0, + upload: AtomicUsize::new(0), + download: AtomicUsize::new(0), + }; let f = BufReader::new(File::open(path).expect("host configuration path")); let mut peer_id = 0; for line in f.lines() { @@ -70,85 +82,36 @@ impl Connections { .unwrap_or_else(|e| panic!("bad socket address: {}:\n{}", trimmed, e)); let peer = Peer { id: peer_id, - addr, - stream: None, + listen_addr: addr, + streams: None, }; - self.peers.push(peer); + this.peers.insert(peer_id, peer); peer_id += 1; } } - assert!(id < self.peers.len()); - self.id = id; + assert!(id < this.peers.len()); + this.id = id; + this.n_parties = this.peers.len(); + this } - fn connect_to_all(&mut self) { - let timer = start_timer!(|| "Connecting"); - let n = self.peers.len(); - for from_id in 0..n { - for to_id in (from_id + 1)..n { - debug!("{} to {}", from_id, to_id); - if self.id == from_id { - let to_addr = self.peers[to_id].addr; - debug!("Contacting {}", to_id); - let stream = loop { - let mut ms_waited = 0; - match TcpStream::connect(to_addr) { - Ok(s) => break s, - Err(e) => match e.kind() { - std::io::ErrorKind::ConnectionRefused - | std::io::ErrorKind::ConnectionReset => { - ms_waited += 10; - std::thread::sleep(std::time::Duration::from_millis(10)); - if ms_waited % 3_000 == 0 { - debug!("Still waiting"); - } else if ms_waited > 30_000 { - panic!("Could not find peer in 30s"); - } - } - _ => { - panic!("Error during FieldChannel::new: {}", e); - } - }, - } - }; - stream.set_nodelay(true).unwrap(); - self.peers[to_id].stream = Some(stream); - } else if self.id == to_id { - debug!("Awaiting {}", from_id); - let listener = TcpListener::bind(self.peers[self.id].addr).unwrap(); - let (stream, _addr) = listener.accept().unwrap(); - stream.set_nodelay(true).unwrap(); - self.peers[from_id].stream = Some(stream); - } - } - // Sender for next round waits for note from this sender to prevent race on receipt. - if from_id + 1 < n { - if self.id == from_id { - self.peers[self.id + 1] - .stream - .as_mut() - .unwrap() - .write_all(&[0u8]) - .unwrap(); - } else if self.id == from_id + 1 { - self.peers[self.id - 1] - .stream - .as_mut() - .unwrap() - .read_exact(&mut [0u8]) - .unwrap(); - } - } - } - // Do a round with the king, to be sure everyone is ready - let from_all = self.send_to_king(&[self.id as u8]); - self.recv_from_king(from_all); - for id in 0..n { - if id != self.id { - assert!(self.peers[id].stream.is_some()); - } - } - end_timer!(timer); + + pub async fn listen(&mut self) -> Result<(), MPCNetError> { + let listen_addr = self.peers.get(&self.id).unwrap().listen_addr; + let listener = TcpListener::bind(listen_addr).await.unwrap(); + self.listener = Some(listener); + Ok(()) } + + async fn connect_to_all(&mut self) { + let n_minus_1 = self.n_parties - 1; + let self_id = self.id; + let peer_addrs = self + .peers + .iter() + .map(|(_, p)| p.listen_addr) + .collect::>(); + } + fn am_king(&self) -> bool { self.id == 0 } @@ -219,7 +182,7 @@ impl Connections { } else { self.stats.bytes_sent += m; self.peers[0] - .stream + .streams .as_mut() .unwrap() .write_all(bytes_out) @@ -251,7 +214,7 @@ impl Connections { end_timer!(timer); bytes_out[own_id].clone() } else { - let stream = self.peers[0].stream.as_mut().unwrap(); + let stream = self.peers[0].streams.as_mut().unwrap(); let mut bytes_size = [0u8; 8]; stream.read_exact(&mut bytes_size).unwrap(); let m = u64::from_le_bytes(bytes_size) as usize; @@ -263,7 +226,7 @@ impl Connections { } fn uninit(&mut self) { for p in &mut self.peers { - p.stream = None; + p.streams = None; } } } @@ -272,24 +235,24 @@ pub struct MpcMultiNet; impl MpcNet for MpcMultiNet { #[inline] - fn party_id() -> usize { + fn party_id(&self) -> usize { get_ch!().id } #[inline] - fn n_parties() -> usize { + fn n_parties(&self) -> usize { get_ch!().peers.len() } #[inline] fn init_from_file(path: &str, party_id: usize) { let mut ch = get_ch!(); - ch.init_from_path(path, party_id); + MPCNetConnection::init_from_path(path, party_id); ch.connect_to_all(); } #[inline] - fn is_init() -> bool { + fn is_init(&self) -> bool { get_ch!() .peers .first() @@ -313,17 +276,17 @@ impl MpcNet for MpcMultiNet { } #[inline] - fn broadcast_bytes(bytes: &[u8]) -> Vec> { + fn broadcast_bytes(&self, bytes: &[u8]) -> Vec> { get_ch!().broadcast(bytes) } #[inline] - fn worker_send_or_leader_receive(bytes: &[u8]) -> Option>> { + fn worker_send_or_leader_receive(&self, bytes: &[u8]) -> Option>> { get_ch!().send_to_king(bytes) } #[inline] - fn worker_receive_or_leader_send(bytes: Option>>) -> Vec { + fn worker_receive_or_leader_send(&self, bytes: Option>>) -> Vec { get_ch!().recv_from_king(bytes) }