Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
robinhundt committed May 26, 2024
1 parent bfa0e86 commit 7f3fc3d
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 313 deletions.
144 changes: 71 additions & 73 deletions crates/seec/src/private_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ use crate::common::BitVec;
use crate::executor::{Executor, Input};
use crate::gate::base::BaseGate;
use crate::mul_triple::{arithmetic, boolean};
// use crate::protocols::arithmetic_gmw::{AdditiveSharing, ArithmeticGmw};
use crate::protocols::arithmetic_gmw::{AdditiveSharing, ArithmeticGmw};
use crate::protocols::boolean_gmw::{BooleanGmw, XorSharing};
// use crate::protocols::mixed_gmw::{MixedGmw, MixedShareStorage, MixedSharing};
use crate::protocols::{
FunctionDependentSetup, Gate, Plain, Protocol, Ring, ScalarDim, Share, Sharing,
};
use crate::protocols::mixed_gmw::{self, MixedGmw, MixedShareStorage, MixedSharing};
use crate::protocols::{FunctionDependentSetup, Protocol, Ring, ScalarDim, Share, Sharing};

pub trait ProtocolTestExt: Protocol + Default {
type InsecureSetup<Idx: GateIdx>: FunctionDependentSetup<Self, Idx, Error = Infallible>
Expand All @@ -54,18 +52,18 @@ impl ProtocolTestExt for BooleanGmw {
type InsecureSetup<Idx: GateIdx> = boolean::insecure_provider::InsecureMTProvider;
}

// impl<R: Ring> ProtocolTestExt for ArithmeticGmw<R> {
// type InsecureSetup<Idx: GateIdx> = arithmetic::insecure_provider::InsecureMTProvider<R>;
// }
//
// impl<R> ProtocolTestExt for MixedGmw<R>
// where
// R: Ring,
// Standard: Distribution<R>,
// [R; 1]: BitViewSized,
// {
// type InsecureSetup<Idx: GateIdx> = mixed_gmw::InsecureMixedSetup<R>;
// }
impl<R: Ring> ProtocolTestExt for ArithmeticGmw<R> {
type InsecureSetup<Idx: GateIdx> = arithmetic::insecure_provider::InsecureMTProvider<R>;
}

impl<R> ProtocolTestExt for MixedGmw<R>
where
R: Ring,
Standard: Distribution<R>,
[R; 1]: BitViewSized,
{
type InsecureSetup<Idx: GateIdx> = mixed_gmw::InsecureMixedSetup<R>;
}

pub fn create_and_tree(depth: u32) -> BaseCircuit<bool> {
let total_nodes = 2_u32.pow(depth);
Expand Down Expand Up @@ -137,48 +135,48 @@ macro_rules! impl_into_shares {
}
}

// impl IntoShares<AdditiveSharing<$typ, ThreadRng>> for $typ {
// fn into_shares(self) -> (Vec<$typ>, Vec<$typ>) {
// let [a, b] = AdditiveSharing::new(thread_rng()).share(vec![self]);
// (a, b)
// }
// }
//
// impl IntoShares<MixedSharing<XorSharing<ThreadRng>, AdditiveSharing<$typ, ThreadRng>, $typ>>
// for $typ
// {
// fn into_shares(self) -> (MixedShareStorage<$typ>, MixedShareStorage<$typ>) {
// static RNG: Lazy<Mutex<ChaCha8Rng>> = Lazy::new(|| {
// let seed = match env::var("RNG_SEED") {
// Ok(seed) => seed.parse().expect("failed to parse RNG_SEED env var as u64"),
// Err(_) => thread_rng().gen()
// };
// debug!(seed, "Input sharing rng seed");
// Mutex::new(ChaCha8Rng::seed_from_u64(seed))
// });
// let mut rng = RNG.lock();
// // let [a, b] = AdditiveSharing::new(ChaCha8Rng::seed_from_u64(65432)).share(vec![self]);
// let [a, b] = AdditiveSharing::new(&mut *rng).share(vec![self]);
// (MixedShareStorage::Arith(a), MixedShareStorage::Arith(b))
// }
// }
//
// impl IntoShares<MixedSharing<XorSharing<ThreadRng>, AdditiveSharing<$typ, ThreadRng>, $typ>> for ToBool<$typ> {
// fn into_shares(self) -> (MixedShareStorage<$typ>, MixedShareStorage<$typ>) {
// // use xor bool sharing
// let (a, b) = IntoShares::<XorSharing<ThreadRng>>::into_shares(self.0);
// (MixedShareStorage::Bool(a), MixedShareStorage::Bool(b))
// }
// }
//
//
// impl<T: IntoShares<AdditiveSharing<$typ, ThreadRng>>> IntoInput<AdditiveSharing<$typ, ThreadRng>>
// for T
// {
// fn into_input(self) -> (Vec<$typ>, Vec<$typ>) {
// self.into_shares()
// }
// }
impl IntoShares<AdditiveSharing<$typ, ThreadRng>> for $typ {
fn into_shares(self) -> (Vec<$typ>, Vec<$typ>) {
let [a, b] = AdditiveSharing::new(thread_rng()).share(vec![self]);
(a, b)
}
}

impl IntoShares<MixedSharing<XorSharing<ThreadRng>, AdditiveSharing<$typ, ThreadRng>, $typ>>
for $typ
{
fn into_shares(self) -> (MixedShareStorage<$typ>, MixedShareStorage<$typ>) {
static RNG: Lazy<Mutex<ChaCha8Rng>> = Lazy::new(|| {
let seed = match env::var("RNG_SEED") {
Ok(seed) => seed.parse().expect("failed to parse RNG_SEED env var as u64"),
Err(_) => thread_rng().gen()
};
debug!(seed, "Input sharing rng seed");
Mutex::new(ChaCha8Rng::seed_from_u64(seed))
});
let mut rng = RNG.lock();
// let [a, b] = AdditiveSharing::new(ChaCha8Rng::seed_from_u64(65432)).share(vec![self]);
let [a, b] = AdditiveSharing::new(&mut *rng).share(vec![self]);
(MixedShareStorage::Arith(a), MixedShareStorage::Arith(b))
}
}

impl IntoShares<MixedSharing<XorSharing<ThreadRng>, AdditiveSharing<$typ, ThreadRng>, $typ>> for ToBool<$typ> {
fn into_shares(self) -> (MixedShareStorage<$typ>, MixedShareStorage<$typ>) {
// use xor bool sharing
let (a, b) = IntoShares::<XorSharing<ThreadRng>>::into_shares(self.0);
(MixedShareStorage::Bool(a), MixedShareStorage::Bool(b))
}
}


impl<T: IntoShares<AdditiveSharing<$typ, ThreadRng>>> IntoInput<AdditiveSharing<$typ, ThreadRng>>
for T
{
fn into_input(self) -> (Vec<$typ>, Vec<$typ>) {
self.into_shares()
}
}
)*
};
}
Expand All @@ -196,20 +194,20 @@ impl IntoShares<XorSharing<ThreadRng>> for bool {
}
}

// impl<R> IntoShares<MixedSharing<XorSharing<ThreadRng>, AdditiveSharing<R, ThreadRng>, R>> for bool
// where
// R: Ring,
// Standard: Distribution<R>,
// {
// fn into_shares(self) -> (MixedShareStorage<R>, MixedShareStorage<R>)
// where
// BitSlice<u8, Lsb0>: BitField,
// {
// let a = BitVec::repeat(false, 1);
// let b = BitVec::repeat(self, 1);
// (MixedShareStorage::Bool(a), MixedShareStorage::Bool(b))
// }
// }
impl<R> IntoShares<MixedSharing<XorSharing<ThreadRng>, AdditiveSharing<R, ThreadRng>, R>> for bool
where
R: Ring,
Standard: Distribution<R>,
{
fn into_shares(self) -> (MixedShareStorage<R>, MixedShareStorage<R>)
where
BitSlice<u8, Lsb0>: BitField,
{
let a = BitVec::repeat(false, 1);
let b = BitVec::repeat(self, 1);
(MixedShareStorage::Bool(a), MixedShareStorage::Bool(b))
}
}

impl<T: IntoShares<XorSharing<ThreadRng>>> IntoInput<XorSharing<ThreadRng>> for T {
fn into_input(self) -> (BitVec<usize>, BitVec<usize>) {
Expand Down
Loading

0 comments on commit 7f3fc3d

Please sign in to comment.