Skip to content

Commit

Permalink
feat(mpz-ot): impl more OT traits on shared KOS
Browse files Browse the repository at this point in the history
  • Loading branch information
sinui0 committed Jun 6, 2024
1 parent a02393a commit 6aed645
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 10 deletions.
4 changes: 4 additions & 0 deletions crates/mpz-ot/src/kos/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ where
Ok(self.state.try_as_extension()?.remaining())
}

pub(crate) fn state(&self) -> &State {
&self.state
}

/// Returns the provided number of keys.
pub(crate) fn take_keys(&mut self, count: usize) -> Result<ReceiverKeys, ReceiverError> {
self.state
Expand Down
59 changes: 54 additions & 5 deletions crates/mpz-ot/src/kos/shared_receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ use std::sync::Arc;

use async_trait::async_trait;
use itybity::IntoBitIterator;
use mpz_common::{sync::Mutex, Context};
use mpz_common::{sync::AsyncMutex, Context};
use mpz_core::Block;
use mpz_ot_core::{kos::msgs::SenderPayload, OTReceiverOutput};
use mpz_ot_core::{kos::msgs::SenderPayload, OTReceiverOutput, ROTReceiverOutput, TransferId};
use serio::{stream::IoStreamExt, SinkExt};
use utils_aio::non_blocking_backend::{Backend, NonBlockingBackend};

use crate::{
kos::{Receiver, ReceiverError},
OTError, OTReceiver,
OTError, OTReceiver, RandomOTReceiver, VerifiableOTReceiver, VerifiableOTSender,
};

/// A shared KOS receiver.
#[derive(Debug, Clone)]
pub struct SharedReceiver<BaseOT> {
inner: Arc<Mutex<Receiver<BaseOT>>>,
inner: Arc<AsyncMutex<Receiver<BaseOT>>>,
}

impl<BaseOT> SharedReceiver<BaseOT> {
/// Creates a new shared receiver.
pub fn new(receiver: Receiver<BaseOT>) -> Self {
Self {
// KOS receiver is always the leader.
inner: Arc::new(Mutex::new_leader(receiver)),
inner: Arc::new(AsyncMutex::new_leader(receiver)),
}
}
}
Expand Down Expand Up @@ -59,3 +59,52 @@ where
Ok(OTReceiverOutput { id, msgs })
}
}

#[async_trait]
impl<Ctx, BaseOT> RandomOTReceiver<Ctx, bool, Block> for SharedReceiver<BaseOT>
where
Ctx: Context,
BaseOT: Send,
{
async fn receive_random(
&mut self,
ctx: &mut Ctx,
count: usize,
) -> Result<ROTReceiverOutput<bool, Block>, OTError> {
self.inner.lock(ctx).await?.receive_random(ctx, count).await
}
}

#[async_trait]
impl<Ctx, BaseOT> VerifiableOTReceiver<Ctx, bool, Block, [Block; 2]> for SharedReceiver<BaseOT>
where
Ctx: Context,
BaseOT: VerifiableOTSender<Ctx, bool, [Block; 2]> + Send,
{
async fn verify(
&mut self,
ctx: &mut Ctx,
id: TransferId,
msgs: &[[Block; 2]],
) -> Result<(), OTError> {
let record = {
let mut inner = self.inner.lock(ctx).await?;

// Verify delta if we haven't yet.
if inner.state().is_extension() {
inner.verify_delta(ctx).await?;
}

let receiver = inner.state().try_as_verify().map_err(ReceiverError::from)?;

receiver.remove_record(id).map_err(ReceiverError::from)?
};

let msgs = msgs.to_vec();
Backend::spawn(move || record.verify(&msgs))
.await
.map_err(ReceiverError::from)?;

Ok(())
}
}
41 changes: 36 additions & 5 deletions crates/mpz-ot/src/kos/shared_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ use std::sync::Arc;

use async_trait::async_trait;

use mpz_common::{sync::Mutex, Context};
use mpz_common::{sync::AsyncMutex, Context};
use mpz_core::Block;
use mpz_ot_core::OTSenderOutput;
use serio::{stream::IoStreamExt as _, SinkExt as _};

use crate::{
kos::{Sender, SenderError},
OTError, OTReceiver, OTSender,
CommittedOTReceiver, CommittedOTSender, OTError, OTReceiver, OTSender, OTSenderOutput,
ROTSenderOutput, RandomOTSender,
};

/// A shared KOS sender.
#[derive(Debug, Clone)]
pub struct SharedSender<BaseOT> {
inner: Arc<Mutex<Sender<BaseOT>>>,
inner: Arc<AsyncMutex<Sender<BaseOT>>>,
}

impl<BaseOT> SharedSender<BaseOT> {
/// Creates a new shared sender.
pub fn new(sender: Sender<BaseOT>) -> Self {
Self {
// KOS sender is always the follower.
inner: Arc::new(Mutex::new_follower(sender)),
inner: Arc::new(AsyncMutex::new_follower(sender)),
}
}
}
Expand Down Expand Up @@ -55,3 +55,34 @@ where
Ok(OTSenderOutput { id })
}
}

#[async_trait]
impl<Ctx, BaseOT> RandomOTSender<Ctx, [Block; 2]> for SharedSender<BaseOT>
where
Ctx: Context,
BaseOT: OTReceiver<Ctx, bool, Block> + Send + 'static,
{
async fn send_random(
&mut self,
ctx: &mut Ctx,
count: usize,
) -> Result<ROTSenderOutput<[Block; 2]>, OTError> {
self.inner.lock(ctx).await?.send_random(ctx, count).await
}
}

#[async_trait]
impl<Ctx, BaseOT> CommittedOTSender<Ctx, [Block; 2]> for SharedSender<BaseOT>
where
Ctx: Context,
BaseOT: CommittedOTReceiver<Ctx, bool, Block> + Send + 'static,
{
async fn reveal(&mut self, ctx: &mut Ctx) -> Result<(), OTError> {
self.inner
.lock(ctx)
.await?
.reveal(ctx)
.await
.map_err(OTError::from)
}
}

0 comments on commit 6aed645

Please sign in to comment.