Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CO15 mismatched length issue #199

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions crates/mpz-ot-core/src/chou_orlandi/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
use crate::TransferId;
use serde::{Deserialize, Serialize};

/// A Chou-Orlandi abort error.
#[derive(Debug, Clone, Copy, thiserror::Error, Serialize, Deserialize)]
#[allow(missing_docs)]
pub enum AbortError {
#[error("mismatched OT batch lengths: expected {expected}, actual {actual}")]
MismatchedLengths { expected: usize, actual: usize },
}

/// Errors that can occur when using the CO15 sender.
#[derive(Debug, thiserror::Error)]
Expand Down
2 changes: 1 addition & 1 deletion crates/mpz-ot-core/src/chou_orlandi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub use config::{
ReceiverConfig, ReceiverConfigBuilder, ReceiverConfigBuilderError, SenderConfig,
SenderConfigBuilder, SenderConfigBuilderError,
};
pub use error::{ReceiverError, SenderError, SenderVerifyError};
pub use error::{AbortError, ReceiverError, SenderError, SenderVerifyError};
pub use receiver::{state as receiver_state, Receiver};
pub use sender::{state as sender_state, Sender};

Expand Down
6 changes: 6 additions & 0 deletions crates/mpz-ot/src/chou_orlandi/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use mpz_ot_core::chou_orlandi::AbortError;

use crate::OTError;

/// A Chou-Orlandi sender error.
Expand All @@ -14,6 +16,8 @@ pub enum SenderError {
CointossError(#[from] mpz_cointoss::CointossError),
#[error("invalid configuration: {0}")]
InvalidConfig(String),
#[error(transparent)]
AbortError(#[from] AbortError),
}

impl From<SenderError> for OTError {
Expand Down Expand Up @@ -45,6 +49,8 @@ pub enum ReceiverError {
CointossError(#[from] mpz_cointoss::CointossError),
#[error("invalid configuration: {0}")]
InvalidConfig(String),
#[error(transparent)]
AbortError(#[from] AbortError),
}

impl From<ReceiverError> for OTError {
Expand Down
32 changes: 32 additions & 0 deletions crates/mpz-ot/src/chou_orlandi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ mod tests {
use mpz_common::executor::test_st_executor;
use mpz_common::Context;
use mpz_core::Block;
use mpz_ot_core::chou_orlandi::AbortError;
use rand::Rng;
use rand_chacha::ChaCha12Rng;
use rand_core::SeedableRng;
Expand Down Expand Up @@ -125,6 +126,37 @@ mod tests {
assert_eq!(output_receiver.msgs, expected);
}

#[rstest]
#[tokio::test]
async fn test_chou_orlandi_mismatched_lengths(mut data: Vec<[Block; 2]>, choices: Vec<bool>) {
let (mut sender_ctx, mut receiver_ctx) = test_st_executor(8);
let (mut sender, mut receiver) = setup(
SenderConfig::default(),
ReceiverConfig::default(),
&mut sender_ctx,
&mut receiver_ctx,
)
.await;

// Remove an element from the array of messages.
data.pop();

let result = tokio::try_join!(
sender.send(&mut sender_ctx, &data).map_err(OTError::from),
receiver
.receive(&mut receiver_ctx, &choices)
.map_err(OTError::from)
);

let _expected_error = OTError::SenderError(Box::new(SenderError::AbortError(
AbortError::MismatchedLengths {
actual: data.len(),
expected: choices.len(),
},
)));
assert!(matches!(result, Err(_expected_error)));
}

#[rstest]
#[tokio::test]
async fn test_chou_orlandi_committed_receiver(data: Vec<[Block; 2]>, choices: Vec<bool>) {
Expand Down
8 changes: 6 additions & 2 deletions crates/mpz-ot/src/chou_orlandi/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use mpz_common::Context;
use mpz_core::Block;
use mpz_ot_core::chou_orlandi::msgs::SenderPayload;
use mpz_ot_core::chou_orlandi::{
receiver_state as state, Receiver as ReceiverCore, ReceiverConfig,
receiver_state as state, AbortError, Receiver as ReceiverCore, ReceiverConfig,
};

use enum_try_as_inner::EnumTryAsInner;
Expand Down Expand Up @@ -155,7 +155,11 @@ where

ctx.io_mut().send(receiver_payload).await?;

let sender_payload: SenderPayload = ctx.io_mut().expect_next().await?;
let sender_payload = ctx
.io_mut()
.expect_next::<Result<SenderPayload, AbortError>>()
.await?
.map_err(ReceiverError::from)?;
let id = sender_payload.id;

let (receiver, msgs) = Backend::spawn(move || {
Expand Down
23 changes: 20 additions & 3 deletions crates/mpz-ot/src/chou_orlandi/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use async_trait::async_trait;
use mpz_cointoss as cointoss;
use mpz_common::Context;
use mpz_core::Block;
use mpz_ot_core::chou_orlandi::{sender_state as state, Sender as SenderCore, SenderConfig};
use mpz_ot_core::chou_orlandi::{
msgs::{ReceiverPayload, SenderPayload},
sender_state as state, AbortError, Sender as SenderCore, SenderConfig,
};
use rand::{thread_rng, Rng};
use serio::{stream::IoStreamExt, SinkExt as _};
use utils_aio::non_blocking_backend::{Backend, NonBlockingBackend};
Expand Down Expand Up @@ -110,7 +113,19 @@ impl<Ctx: Context> OTSender<Ctx, [Block; 2]> for Sender {
.try_into_setup()
.map_err(SenderError::from)?;

let receiver_payload = ctx.io_mut().expect_next().await?;
let receiver_payload: ReceiverPayload = ctx.io_mut().expect_next().await?;

if receiver_payload.blinded_choices.len() != input.len() {
let abort = AbortError::MismatchedLengths {
actual: receiver_payload.blinded_choices.len(),
expected: input.len(),
};
ctx.io_mut()
.send(Result::<SenderPayload, _>::Err(abort))
.await?;

Err(SenderError::AbortError(abort))?;
}

let input = input.to_vec();
let (sender, payload) = Backend::spawn(move || {
Expand All @@ -123,7 +138,9 @@ impl<Ctx: Context> OTSender<Ctx, [Block; 2]> for Sender {

let id = payload.id;

ctx.io_mut().send(payload).await?;
ctx.io_mut()
.send(Result::<_, AbortError>::Ok(payload))
.await?;

self.state = State::Setup(sender);

Expand Down