Skip to content

Commit

Permalink
Added feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
th4s committed May 28, 2024
1 parent 3844d15 commit 3a43828
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 22 deletions.
5 changes: 5 additions & 0 deletions crates/mpz-ole-core/src/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ impl<F: Field> OLEReceiver<F> {

Some((receiver_adjust, adjustments))
}

/// Returns the number of preprocessed OLEs that are available.
pub fn cache_size(&self) -> usize {
self.cache.len()
}
}

/// Receiver adjustments waiting for [`BatchAdjust`] from the sender.
Expand Down
5 changes: 5 additions & 0 deletions crates/mpz-ole-core/src/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ impl<F: Field> OLESender<F> {

Some((sender_adjust, adjustments))
}

/// Returns the number of preprocessed OLEs that are available.
pub fn cache_size(&self) -> usize {
self.cache.len()
}
}

/// Sender adjustments waiting for [`BatchAdjust`] from the receiver.
Expand Down
46 changes: 27 additions & 19 deletions crates/mpz-ole/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ pub trait OLESender<Ctx: Context, F: Field> {
///
/// # Arguments
///
/// * `ctx` - The context, which provides IO channels.
/// * `a_k` - The sender's OLE inputs.
/// * `ctx` - The context.
/// * `inputs` - The sender's OLE inputs.
///
/// # Returns
///
/// * The sender's OLE outputs `x_k`.
async fn send(&mut self, ctx: &mut Ctx, a_k: Vec<F>) -> Result<Vec<F>, OLEError>;
async fn send(&mut self, ctx: &mut Ctx, inputs: Vec<F>) -> Result<Vec<F>, OLEError>;
}

/// Batch OLE Receiver.
Expand All @@ -49,8 +49,8 @@ pub trait OLEReceiver<Ctx: Context, F: Field> {
///
/// # Arguments
///
/// * `ctx` - The context, which provides IO channels.
/// * `b_k` - The receiver's OLE inputs.
/// * `ctx` - The context.
/// * `inputs` - The receiver's OLE inputs.
///
/// # Returns
///
Expand All @@ -60,43 +60,51 @@ pub trait OLEReceiver<Ctx: Context, F: Field> {

/// An OLE error.
#[derive(Debug, thiserror::Error)]
#[error("OLE error: {kind}")]
pub struct OLEError {
kind: OLEErrorKind,
#[source]
source: Option<Box<dyn Error + Send + Sync>>,
}

impl OLEError {
fn new(kind: OLEErrorKind, source: Box<dyn Error + Send + Sync + 'static>) -> Self {
fn new<E>(kind: OLEErrorKind, source: E) -> Self
where
E: Into<Box<dyn Error + Send + Sync>>,
{
Self {
kind,
source: Some(source),
source: Some(source.into()),
}
}
}

#[derive(Debug)]
pub(crate) enum OLEErrorKind {
OT,
IO,
Core,
Field,
InsufficientOLEs,
}

impl Display for OLEErrorKind {
impl Display for OLEError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
match self.kind {
OLEErrorKind::OT => write!(f, "OT Error"),
OLEErrorKind::IO => write!(f, "IO Error"),
OLEErrorKind::Core => write!(f, "OLE Core Error"),
OLEErrorKind::Field => write!(f, "FieldError"),
OLEErrorKind::InsufficientOLEs => write!(f, "Insufficient OLEs"),
}?;

if let Some(source) = self.source.as_ref() {
write!(f, " caused by: {source}")?;
}

Ok(())
}
}

#[derive(Debug)]
pub(crate) enum OLEErrorKind {
OT,
IO,
Core,
Field,
InsufficientOLEs,
}

impl From<OTError> for OLEError {
fn from(value: OTError) -> Self {
Self {
Expand Down
4 changes: 3 additions & 1 deletion crates/mpz-ole/src/rot/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ where
F: Field + Serialize + Deserialize,
{
async fn receive(&mut self, ctx: &mut Ctx, b_k: Vec<F>) -> Result<Vec<F>, OLEError> {
let len_requested = b_k.len();

let (receiver_adjust, adjust) = self.core.adjust(b_k).ok_or_else(|| {
OLEError::new(
OLEErrorKind::InsufficientOLEs,
"Not enough OLEs available".into(),
format!("{} < {}", self.core.cache_size(), len_requested),
)
})?;

Expand Down
5 changes: 3 additions & 2 deletions crates/mpz-ole/src/rot/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,14 @@ where
F: Field + Serialize + Deserialize,
{
async fn send(&mut self, ctx: &mut Ctx, a_k: Vec<F>) -> Result<Vec<F>, OLEError> {
let len_requested = a_k.len();

let (sender_adjust, adjust) = self.core.adjust(a_k).ok_or_else(|| {
OLEError::new(
OLEErrorKind::InsufficientOLEs,
"Not enough OLEs available".into(),
format!("{} < {}", self.core.cache_size(), len_requested),
)
})?;

let channel = ctx.io_mut();
channel.send(adjust).await?;
let adjust = channel.expect_next::<BatchAdjust<F>>().await?;
Expand Down

0 comments on commit 3a43828

Please sign in to comment.