Skip to content

Commit

Permalink
Blanket sharing for (Un)Ver
Browse files Browse the repository at this point in the history
  • Loading branch information
quackzar committed May 13, 2024
1 parent 72c4009 commit fd514d2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pycare/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ name = "caring"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.21", features = ["abi3-py37", "generate-import-lib"]}
pyo3 = { version = "0.21", features = ["abi3-py37", "generate-import-lib", "extension-module"]}
wecare = { path = "../wecare" }
1 change: 1 addition & 0 deletions src/marker/exptree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@ impl<T: Clone> ExpTree<T> {
Some(res)

}

}
57 changes: 47 additions & 10 deletions src/marker/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
mod exptree;

use rand::RngCore;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::{net::Communicate, schemes::{Shared, Verify}};
use crate::{net::Communicate, schemes::{interactive::InteractiveShared, Shared, Verify}};


#[repr(transparent)]
Expand Down Expand Up @@ -31,12 +32,39 @@ impl<S: Verify> Unverified<S> {
None
}
}
}

impl<S> Unverified<S> {
pub fn assume_verified(self) -> Verified<S> {
Verified(self.0)
}
}


impl<S: InteractiveShared> Verified<S> {
pub async fn open(self, ctx: &S::Context, coms: impl Communicate) -> Result<S::Value, S::Error> {
S::recombine(ctx, self.0, coms).await
}

pub async fn share(val: S::Value, ctx: &S::Context, rng: impl RngCore + Send, coms: impl Communicate) -> Result<Self, S::Error> {
let s = S::share(ctx, val, rng, coms).await?;
Ok(Self(s))
}

}

impl<S: InteractiveShared> Unverified<S> {
pub async fn share_symmetric(val: S::Value, ctx: &S::Context, rng: impl RngCore + Send, coms: impl Communicate) -> Result<Vec<Self>, S::Error> {
let s = S::symmetric_share(ctx, val, rng, coms).await?;
Ok(s.into_iter().map(Self).collect())
}

pub async fn receive_share(ctx: &S::Context, coms: impl Communicate, from: usize) -> Result<Self, S::Error> {
let s = S::receive_share(ctx, coms, from).await?;
Ok(Self(s))
}
}

impl<T> From<Unverified<Vec<T>>> for Vec<Unverified<T>> {
fn from(value: Unverified<Vec<T>>) -> Self {
value.0.into_iter().map(|t| Unverified(t)).collect()
Expand All @@ -58,18 +86,12 @@ impl<S: Verify> Unverified<Vec<S>> {
}).collect();
Verified(res)
}


pub fn assume_verified(self) -> Verified<S> {
todo!()
}
}

// Pure boring manual operator implementations
// Could be done with some macros instead.
mod ops {
use std::ops::{Add, Sub};

use std::ops::{Add, Mul, Sub};
use crate::schemes::Shared;

use super::*;
Expand Down Expand Up @@ -121,8 +143,23 @@ mod ops {
Self(self.0 - rhs.0)
}
}
}

impl<S: Shared> Mul<S::Value> for Verified<S> where S: Mul<S::Value, Output=S> {
type Output = Self;

fn mul(self, rhs: S::Value) -> Self::Output {
Self(self.0 * rhs)
}
}

impl<S: Shared> Mul<S::Value> for Unverified<S> where S: Mul<S::Value, Output=S> {
type Output = Self;

fn mul(self, rhs: S::Value) -> Self::Output {
Self(self.0 * rhs)
}
}
}

#[cfg(test)]
mod test {
Expand All @@ -142,7 +179,7 @@ mod test {
me: 0,
};
let mut rng = rngs::mock::StepRng::new(0, 0);
let s = mock::Share::share(&ctx, Mod11(3), &mut rng);
let s = <mock::Share<Mod11> as Shared>::share(&ctx, Mod11(3), &mut rng);
let s = Verified(s[0]);
let s0 = s.clone();
let s = s0 + s;
Expand Down
48 changes: 37 additions & 11 deletions src/schemes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ use std::{
};

use rand::RngCore;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::net::Communicate;

/// Currently unused trait, but might be a better way to represent that a share
/// can be multiplied by a const, however, it could also just be baked into 'Shared' directly.
trait MulByConst<A>:
pub trait MulByConst<A>:
Shared<Value = A> + std::ops::Mul<A, Output = Self> + std::ops::MulAssign<A>
{
}
Expand Down Expand Up @@ -143,12 +142,27 @@ pub trait InteractiveMult: Shared {
) -> impl Future<Output = Result<Self, Box<dyn Error>>>;
}

mod interactive {
pub mod interactive {
use thiserror::Error;

use crate::net::Tuneable;


#[derive(Debug, Error)]
#[error("Communication failure: {0}")]
pub struct CommunicationError(Box<dyn Error + Send>);
impl CommunicationError {
fn new(e: impl Error + Send + 'static) -> Self {
Self(Box::new(e))
}

}

use super::*;
impl<S, V, C> InteractiveShared for S where S: Shared<Value = V, Context = C> + Send, V: Send + Clone, C: Send + Sync + Clone {
type Context = S::Context;
type Value = V;
type Error = Box<dyn Error + Send>;
type Error = CommunicationError;

async fn share(
ctx: &Self::Context,
Expand All @@ -158,11 +172,7 @@ mod interactive {
) -> Result<Self, Self::Error> {
let shares = S::share(ctx, secret, rng);
let my_share = shares[coms.id()].clone();
coms.unicast(&shares).await.map_err(|e| {
let e : Box<dyn Error + Send> = Box::new(e);
e
}
)?;
coms.unicast(&shares).await.map_err(CommunicationError::new)?;
Ok(my_share)
}

Expand All @@ -171,7 +181,7 @@ mod interactive {
secret: Self,
mut coms: impl Communicate,
) -> Result<V, Self::Error> {
let shares = coms.symmetric_broadcast(secret).await.unwrap();
let shares = coms.symmetric_broadcast(secret).await.map_err(CommunicationError::new)?;
Ok(Shared::recombine(ctx, &shares).unwrap())
}

Expand All @@ -182,9 +192,19 @@ mod interactive {
mut coms: impl Communicate,
) -> Result<Vec<Self>, Self::Error> {
let shares = S::share(ctx, secret, rng);
let shared = coms.symmetric_unicast(shares).await.unwrap();
let shared = coms.symmetric_unicast(shares).await.map_err(CommunicationError::new)?;
Ok(shared)
}

async fn receive_share(
_ctx: &Self::Context,
mut coms: impl Communicate,
from: usize,
) -> Result<Self, Self::Error> {
let s = Tuneable::recv_from(&mut coms, from).await;
let s = s.map_err(CommunicationError::new)?;
Ok(s)
}
}


Expand Down Expand Up @@ -215,6 +235,12 @@ mod interactive {
coms: impl Communicate,
) -> impl std::future::Future<Output = Result<Vec<Self>, Self::Error>>;

fn receive_share(
ctx: &Self::Context,
coms: impl Communicate,
from: usize,
) -> impl std::future::Future<Output = Result<Self, Self::Error>>;

fn recombine(
ctx: &Self::Context,
secrets: Self,
Expand Down

0 comments on commit fd514d2

Please sign in to comment.