diff --git a/examples/http-multi-server-channels/src/main.rs b/examples/http-multi-server-channels/src/main.rs index 9a572e5..7eb9c55 100644 --- a/examples/http-multi-server-channels/src/main.rs +++ b/examples/http-multi-server-channels/src/main.rs @@ -81,7 +81,7 @@ async fn main() -> Result<(), Error> { let fpre = Preprocessor::TrustedDealer(urls.len() - 1); let p_out: Vec<_> = (0..(urls.len() - 1)).collect(); let channel = HttpChannel::new(urls, party).await?; - let output = mpc(channel, &prg.circuit, &input, fpre, 0, party, &p_out).await?; + let output = mpc(channel, &prg.circuit, &input, fpre, 0, party, &p_out, true).await?; if !output.is_empty() { println!("\nThe result is {}", prg.parse_output(&output)?); } diff --git a/examples/http-single-server-channels/src/main.rs b/examples/http-single-server-channels/src/main.rs index 679a67d..9b481d6 100644 --- a/examples/http-single-server-channels/src/main.rs +++ b/examples/http-single-server-channels/src/main.rs @@ -112,7 +112,7 @@ async fn main() { } let fpre = Preprocessor::TrustedDealer(P_DEALER); let p_out: Vec<_> = (0..parties).collect(); - let output = mpc(channel, &prg.circuit, &input, fpre, p_eval, party, &p_out) + let output = mpc(channel, &prg.circuit, &input, fpre, p_eval, party, &p_out, true) .await .unwrap(); if !output.is_empty() { diff --git a/examples/iroh-p2p-channels/src/main.rs b/examples/iroh-p2p-channels/src/main.rs index db13741..2f7d07c 100644 --- a/examples/iroh-p2p-channels/src/main.rs +++ b/examples/iroh-p2p-channels/src/main.rs @@ -242,7 +242,7 @@ async fn main() -> Result<()> { let fpre = Preprocessor::TrustedDealer(conns.len() - 1); let channel = IrohChannel::new(conns, MAX_MSG_BYTES); let p_out: Vec<_> = (0..parties).collect(); - let output = mpc(channel, &prg.circuit, &input, fpre, p_eval, party, &p_out) + let output = mpc(channel, &prg.circuit, &input, fpre, p_eval, party, &p_out, true) .await .unwrap(); if !output.is_empty() { diff --git a/examples/sql-integration/src/main.rs b/examples/sql-integration/src/main.rs index d331b27..8cb8a0b 100644 --- a/examples/sql-integration/src/main.rs +++ b/examples/sql-integration/src/main.rs @@ -654,7 +654,7 @@ async fn execute_mpc( recv_count: 0, } }; - let output = mpc(channel, &prg.circuit, &input, fpre, 0, *party, &p_out).await?; + let output = mpc(channel, &prg.circuit, &input, fpre, 0, *party, &p_out, true).await?; state.lock().await.senders.clear(); let elapsed = now.elapsed(); info!( diff --git a/src/faand.rs b/src/faand.rs index 95de30c..1567a2f 100644 --- a/src/faand.rs +++ b/src/faand.rs @@ -32,6 +32,10 @@ pub enum Error { AANDWrongEFMAC, /// No Mac or Key MissingMacKey, + /// Conversion error + ConversionError, + /// Empty bucket + EmptyBucketError, } impl From for Error { @@ -383,8 +387,12 @@ pub(crate) async fn fashare( dm_entry.push(shares[length + r].0 as u8); for p in 0..p_max { if p != p_own { - d0[r] ^= shares[length + r].1 .0[p].unwrap().1 .0; - dm_entry.extend(&shares[length + r].1 .0[p].unwrap().0 .0.to_be_bytes()); + if let Some((mac, key)) = shares[length + r].1 .0[p] { + d0[r] ^= key.0; + dm_entry.extend(&mac.0.to_be_bytes()); + } else { + return Err(Error::MissingMacKey); + } } else { dm_entry.extend(&[0; 16]); } @@ -443,10 +451,11 @@ pub(crate) async fn fashare( for (p, pitem) in dmp.iter().enumerate().take(p_max) { for pp in (0..p_max).filter(|pp| *pp != p) { if !pitem[r].is_empty() { - let b: u128 = u128::from_be_bytes( - pitem[r][(1 + pp * 16)..(17 + pp * 16)].try_into().unwrap(), - ); - xormacs[pp][r] ^= b; + if let Ok(b) = pitem[r][(1 + pp * 16)..(17 + pp * 16)].try_into().map(u128::from_be_bytes) { + xormacs[pp][r] ^= b; + } else { + return Err(Error::ConversionError); + } } } } @@ -967,10 +976,17 @@ pub(crate) async fn combine_bucket( bucket: Vec<(Share, Share, Share)>, ) -> Result<(Share, Share, Share), Error> { let mut bucketcopy = bucket.clone(); - let mut result = bucketcopy.pop().unwrap(); + + let mut result = match bucketcopy.pop() { + Some(first_triple) => first_triple, + None => { + return Err(Error::EmptyBucketError); + } + }; while let Some(triple) = bucketcopy.pop() { result = combine_two_leaky_ands(channel, p_own, p_max, delta, &result, &triple).await?; } + Ok(result) } diff --git a/src/ot.rs b/src/ot.rs index 0b8030c..eebdccc 100644 --- a/src/ot.rs +++ b/src/ot.rs @@ -44,31 +44,27 @@ pub(crate) async fn mpz_ot_sender( sender .setup(ctx_sender) .map_err(OTError::from) - .await - .unwrap(); + .await?; - // extend once. + // Extend once. let num = LPN_PARAMETERS_TEST.k; sender .extend(ctx_sender, num) .map_err(OTError::from) - .await - .unwrap(); + .await?; - // extend twice + // Extend twice. sender .extend(ctx_sender, count) .map_err(OTError::from) - .await - .unwrap(); + .await?; let RCOTSenderOutput { id: sender_id, msgs: u, } = sender .send_random_correlated(ctx_sender, count) - .await - .unwrap(); + .await?; Ok((sender_id, u, block_to_u128(sender.delta()))) } @@ -87,23 +83,20 @@ pub(crate) async fn mpz_ot_receiver( receiver .setup(ctx_receiver) .map_err(OTError::from) - .await - .unwrap(); + .await?; // extend once. let num = LPN_PARAMETERS_TEST.k; receiver .extend(ctx_receiver, num) .map_err(OTError::from) - .await - .unwrap(); + .await?; // extend twice receiver .extend(ctx_receiver, count) .map_err(OTError::from) - .await - .unwrap(); + .await?; let RCOTReceiverOutput { id: receiver_id, @@ -111,67 +104,11 @@ pub(crate) async fn mpz_ot_receiver( msgs: w, } = receiver .receive_random_correlated(ctx_receiver, count) - .await - .unwrap(); + .await?; Ok((receiver_id, b, w)) } -pub(crate) async fn _mpz_ot(count: usize) -> Result { - let lpn_type: LpnType = LpnType::Regular; - let (mut ctx_sender, mut ctx_receiver) = test_st_executor(8); - - let (rcot_sender, rcot_receiver) = ideal_rcot(); - - let config = FerretConfig::new(LPN_PARAMETERS_TEST, lpn_type); - - let mut sender = Sender::new(config.clone(), rcot_sender); - let mut receiver = Receiver::new(config, rcot_receiver); - - tokio::try_join!( - sender.setup(&mut ctx_sender).map_err(OTError::from), - receiver.setup(&mut ctx_receiver).map_err(OTError::from) - ) - .unwrap(); - - // extend once. - let num = LPN_PARAMETERS_TEST.k; - tokio::try_join!( - sender.extend(&mut ctx_sender, num).map_err(OTError::from), - receiver - .extend(&mut ctx_receiver, num) - .map_err(OTError::from) - ) - .unwrap(); - - // extend twice - tokio::try_join!( - sender.extend(&mut ctx_sender, count).map_err(OTError::from), - receiver - .extend(&mut ctx_receiver, count) - .map_err(OTError::from) - ) - .unwrap(); - - let ( - RCOTSenderOutput { - id: _sender_id, - msgs: _u, - }, - RCOTReceiverOutput { - id: _receiver_id, - choices: _b, - msgs: _w, - }, - ) = tokio::try_join!( - sender.send_random_correlated(&mut ctx_sender, count), - receiver.receive_random_correlated(&mut ctx_receiver, count) - ) - .unwrap(); - - Ok(true) -} - pub(crate) async fn generate_ots( count: usize, ) -> Result<(u128, Vec, Vec, Vec), OTError> { diff --git a/src/protocol.rs b/src/protocol.rs index d02b198..478e198 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -22,8 +22,6 @@ pub(crate) struct GarbledGate(pub(crate) [Vec; 4]); #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] pub(crate) struct Label(pub(crate) u128); -const TRUSTEDDEALER: bool = false; - impl BitXor for Label { type Output = Self; @@ -178,9 +176,10 @@ pub fn simulate_mpc( circuit: &Circuit, inputs: &[&[bool]], output_parties: &[usize], + trusted: bool, ) -> Result, Error> { let tokio = Runtime::new().expect("Could not start tokio runtime"); - tokio.block_on(simulate_mpc_async(circuit, inputs, output_parties)) + tokio.block_on(simulate_mpc_async(circuit, inputs, output_parties, trusted)) } /// Simulates the multi party computation with the given inputs and party 0 as the evaluator. @@ -188,12 +187,13 @@ pub async fn simulate_mpc_async( circuit: &Circuit, inputs: &[&[bool]], output_parties: &[usize], + trusted: bool, ) -> Result, Error> { let p_eval = 0; let p_pre = inputs.len(); let mut channels: Vec; - if TRUSTEDDEALER { + if trusted { channels = SimpleChannel::channels(inputs.len() + 1); tokio::spawn(fpre(channels.pop().unwrap(), inputs.len())); } else { @@ -219,6 +219,7 @@ pub async fn simulate_mpc_async( p_eval, p_own, &output_parties, + trusted, ) .await; let mut res_party: Vec = Vec::new(); @@ -242,6 +243,7 @@ pub async fn simulate_mpc_async( p_eval, p_eval, output_parties, + trusted, ) .await; match eval_result { @@ -282,6 +284,7 @@ pub async fn mpc( p_eval: usize, p_own: usize, p_out: &[usize], + trusted: bool, ) -> Result, Error> { let p_max = circuit.input_gates.len(); let is_contrib = p_own != p_eval; @@ -334,7 +337,7 @@ pub async fn mpc( ]; let mut delta: Delta = Delta(0); - if TRUSTEDDEALER { + if trusted { channel.send_to(p_fpre, "delta", &()).await?; delta = channel.recv_from(p_fpre, "delta").await?; } else { @@ -368,7 +371,7 @@ pub async fn mpc( let mut labels: Vec