diff --git a/src/marker/mod.rs b/src/marker/mod.rs index f1761fc..bca9dea 100644 --- a/src/marker/mod.rs +++ b/src/marker/mod.rs @@ -41,7 +41,7 @@ impl Unverified { } } -impl<'ctx, S: InteractiveShared<'ctx>> Verified { +impl Verified { pub async fn open( self, ctx: &mut S::Context, @@ -61,7 +61,7 @@ impl<'ctx, S: InteractiveShared<'ctx>> Verified { } } -impl<'ctx, S: InteractiveShared<'ctx>> Unverified { +impl Unverified { pub async fn share_symmetric( val: S::Value, ctx: &mut S::Context, diff --git a/src/protocols/beaver.rs b/src/protocols/beaver.rs index c50ec5d..f4795d1 100644 --- a/src/protocols/beaver.rs +++ b/src/protocols/beaver.rs @@ -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 { +pub struct BeaverTriple { pub shares: (S, S, S), } @@ -17,7 +21,7 @@ pub struct BeaverPower { powers: Vec, } -impl> BeaverTriple { +impl> BeaverTriple { /// Fake a set of beaver triples. /// /// This produces `n` shares corresponding to a shared beaver triple, @@ -82,29 +86,24 @@ impl> BeaverTriple { /// * `triple`: beaver triple /// * `network`: unicasting network pub async fn beaver_multiply< - C, F: Field, - S: Shared + Copy + std::ops::Mul, + S: InteractiveShared + std::ops::Mul, >( - ctx: &C, + ctx: &mut S::Context, x: S, y: S, triple: BeaverTriple, - agent: &mut impl Broadcast, -) -> Option { - // TODO: Better error handling. + mut coms: impl Communicate, +) -> Result { 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< @@ -243,14 +242,14 @@ mod test { async fn do_mpc( triple: BeaverTriple>, network: InMemoryNetwork, - ctx: ShamirParams, + mut ctx: ShamirParams, ) { 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(); diff --git a/src/schemes/mod.rs b/src/schemes/mod.rs index 07b9259..df14a6c 100644 --- a/src/schemes/mod.rs +++ b/src/schemes/mod.rs @@ -159,11 +159,11 @@ pub mod interactive { } use super::*; - impl<'ctx, S, V, Ctx> InteractiveShared<'ctx> for S + impl InteractiveShared for S where S: Shared + Send, V: Send + Clone, - Ctx: Send + Sync + Clone + 'ctx, + Ctx: Send + Sync + Clone, { type Context = S::Context; type Value = V; @@ -220,7 +220,7 @@ pub mod interactive { } } - pub trait InteractiveShared<'ctx>: + pub trait InteractiveShared: Sized + Add + Sub @@ -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; @@ -264,7 +264,7 @@ pub mod interactive { ) -> impl std::future::Future> + Send; } - pub trait InteractiveSharedMany<'ctx>: InteractiveShared<'ctx> { + pub trait InteractiveSharedMany: InteractiveShared { type VectorShare; fn share_many( @@ -295,9 +295,9 @@ pub mod interactive { } // TODO: Consider using specialized SharedMany instead. - impl<'ctx, S, V, Ctx> InteractiveSharedMany<'ctx> for S + impl InteractiveSharedMany for S where - S: InteractiveShared<'ctx, Error = CommunicationError, Value = V, Context = Ctx> + S: InteractiveShared + Shared + Send, V: Send + Clone, diff --git a/src/schemes/spdz/mod.rs b/src/schemes/spdz/mod.rs index d02e879..bbec8a2 100644 --- a/src/schemes/spdz/mod.rs +++ b/src/schemes/spdz/mod.rs @@ -126,7 +126,7 @@ where Ok(res) } -impl<'ctx, F> InteractiveShared<'ctx> for Share +impl InteractiveShared for Share where F: PrimeField + serde::Serialize + serde::de::DeserializeOwned, { @@ -206,7 +206,7 @@ where } } -impl<'ctx, F> InteractiveSharedMany<'ctx> for Share +impl InteractiveSharedMany for Share where F: PrimeField + serde::Serialize + serde::de::DeserializeOwned, {