Skip to content

Commit

Permalink
Remove lifetime from InteractiveShared
Browse files Browse the repository at this point in the history
and use it in beaver_mult
  • Loading branch information
quackzar committed Jun 19, 2024
1 parent 79c66a3 commit 498fabc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/marker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<S> Unverified<S> {
}
}

impl<'ctx, S: InteractiveShared<'ctx>> Verified<S> {
impl<S: InteractiveShared> Verified<S> {
pub async fn open(
self,
ctx: &mut S::Context,
Expand All @@ -61,7 +61,7 @@ impl<'ctx, S: InteractiveShared<'ctx>> Verified<S> {
}
}

impl<'ctx, S: InteractiveShared<'ctx>> Unverified<S> {
impl<S: InteractiveShared> Unverified<S> {
pub async fn share_symmetric(
val: S::Value,
ctx: &mut S::Context,
Expand Down
39 changes: 19 additions & 20 deletions src/protocols/beaver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ use std::iter;
use itertools::{izip, multiunzip};
use rand::RngCore;

use crate::{algebra::field::Field, net::agency::Broadcast, schemes::Shared};
use crate::{
algebra::field::Field,
net::{agency::Broadcast, Communicate},
schemes::{interactive::InteractiveShared, Shared},
};

/// Beaver (Multiplication) Triple
#[derive(Clone)]
pub struct BeaverTriple<S: Shared> {
pub struct BeaverTriple<S> {
pub shares: (S, S, S),
}

Expand All @@ -17,7 +21,7 @@ pub struct BeaverPower<S: Shared> {
powers: Vec<S>,
}

impl<F: Field, C, S: Shared<Value = F, Context = C>> BeaverTriple<S> {
impl<F: Field, C: Clone, S: Shared<Value = F, Context = C>> BeaverTriple<S> {
/// Fake a set of beaver triples.
///
/// This produces `n` shares corresponding to a shared beaver triple,
Expand Down Expand Up @@ -82,29 +86,24 @@ impl<F: Field, C, S: Shared<Value = F, Context = C>> BeaverTriple<S> {
/// * `triple`: beaver triple
/// * `network`: unicasting network
pub async fn beaver_multiply<
C,
F: Field,
S: Shared<Value = F, Context = C> + Copy + std::ops::Mul<S::Value, Output = S>,
S: InteractiveShared<Value = F> + std::ops::Mul<S::Value, Output = S>,
>(
ctx: &C,
ctx: &mut S::Context,
x: S,
y: S,
triple: BeaverTriple<S>,
agent: &mut impl Broadcast,
) -> Option<S> {
// TODO: Better error handling.
mut coms: impl Communicate,
) -> Result<S, S::Error> {
let BeaverTriple { shares: (a, b, c) } = triple;
let ax: S = a + x;
let by: S = b + y;

// Sending both at once it more efficient.
let resp = agent.symmetric_broadcast::<_>((ax, by)).await.ok()?;
let (ax, by): (Vec<_>, Vec<_>) = itertools::multiunzip(resp);
let ax: S = x + a.clone();
let by: S = y.clone() + b;

let ax = S::recombine(ctx, &ax)?;
let by = S::recombine(ctx, &by)?;
// TODO: concurrency
let ax = S::recombine(ctx, ax, &mut coms).await?;
let by = S::recombine(ctx, by, &mut coms).await?;

Some(y * ax + a * (-by) + c)
Ok(y * ax + a * (-by) + c)
}

pub async fn beaver_multiply_many<
Expand Down Expand Up @@ -243,14 +242,14 @@ mod test {
async fn do_mpc(
triple: BeaverTriple<shamir::Share<Element32>>,
network: InMemoryNetwork,
ctx: ShamirParams<Element32>,
mut ctx: ShamirParams<Element32>,
) {
let mut rng = rand::rngs::mock::StepRng::new(1, 7);
let mut network = network;
let v = Element32::from(5u32);
let shares = shamir::share(v, &ctx.ids, ctx.threshold, &mut rng);
let shares = network.symmetric_unicast(shares).await.unwrap();
let res = beaver_multiply(&ctx, shares[0], shares[1], triple, &mut network)
let res = beaver_multiply(&mut ctx, shares[0], shares[1], triple, &mut network)
.await
.unwrap();
let res = network.symmetric_broadcast(res).await.unwrap();
Expand Down
14 changes: 7 additions & 7 deletions src/schemes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ pub mod interactive {
}

use super::*;
impl<'ctx, S, V, Ctx> InteractiveShared<'ctx> for S
impl<S, V, Ctx> InteractiveShared for S
where
S: Shared<Value = V, Context = Ctx> + Send,
V: Send + Clone,
Ctx: Send + Sync + Clone + 'ctx,
Ctx: Send + Sync + Clone,
{
type Context = S::Context;
type Value = V;
Expand Down Expand Up @@ -220,7 +220,7 @@ pub mod interactive {
}
}

pub trait InteractiveShared<'ctx>:
pub trait InteractiveShared:
Sized
+ Add<Output = Self>
+ Sub<Output = Self>
Expand All @@ -229,7 +229,7 @@ pub mod interactive {
+ Clone
+ Sync
{
type Context: Sync + Send + 'ctx;
type Context: Sync + Send;
type Value: Clone + Send;
type Error: Send + Sized + Error + 'static;

Expand Down Expand Up @@ -264,7 +264,7 @@ pub mod interactive {
) -> impl std::future::Future<Output = Result<Self::Value, Self::Error>> + Send;
}

pub trait InteractiveSharedMany<'ctx>: InteractiveShared<'ctx> {
pub trait InteractiveSharedMany: InteractiveShared {
type VectorShare;

fn share_many(
Expand Down Expand Up @@ -295,9 +295,9 @@ pub mod interactive {
}

// TODO: Consider using specialized SharedMany instead.
impl<'ctx, S, V, Ctx> InteractiveSharedMany<'ctx> for S
impl<S, V, Ctx> InteractiveSharedMany for S
where
S: InteractiveShared<'ctx, Error = CommunicationError, Value = V, Context = Ctx>
S: InteractiveShared<Error = CommunicationError, Value = V, Context = Ctx>
+ Shared<Value = V, Context = Ctx>
+ Send,
V: Send + Clone,
Expand Down
4 changes: 2 additions & 2 deletions src/schemes/spdz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ where
Ok(res)
}

impl<'ctx, F> InteractiveShared<'ctx> for Share<F>
impl<F> InteractiveShared for Share<F>
where
F: PrimeField + serde::Serialize + serde::de::DeserializeOwned,
{
Expand Down Expand Up @@ -206,7 +206,7 @@ where
}
}

impl<'ctx, F> InteractiveSharedMany<'ctx> for Share<F>
impl<F> InteractiveSharedMany for Share<F>
where
F: PrimeField + serde::Serialize + serde::de::DeserializeOwned,
{
Expand Down

0 comments on commit 498fabc

Please sign in to comment.