Skip to content
This repository has been archived by the owner on Dec 20, 2019. It is now read-only.

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Brent Carmer committed Jun 21, 2019
2 parents 31d9c35 + 9b83ae2 commit 4b1f49b
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 80 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ nightly = ["rand/nightly", "scuttlebutt/nightly"]
base_conversion = { path = "base_conversion" }
itertools = "0.8.0"
rand = "0.6.5"
regex = "1.1.6"
scuttlebutt = { git = "https://github.com/amaloz/scuttlebutt", tag = "0.3.1" }
regex = "1.1.7"
scuttlebutt = { git = "https://github.com/GaloisInc/scuttlebutt", tag = "0.3.1" }

[dev-dependencies]
criterion = "0.2.11"
crossbeam = "0.7.1"

[profile.release]
opt-level = 3
debug = false
debug = true
debug-assertions = false
overflow-checks = false

Expand All @@ -42,7 +42,7 @@ overflow-checks = true

[profile.bench]
opt-level = 3
debug = false
debug = true
debug-assertions = false
overflow-checks = false

Expand Down
6 changes: 3 additions & 3 deletions benches/circuits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use fancy_garbling::garble;
use std::time::Duration;

fn circuit(fname: &str) -> Circuit {
let mut circ = Circuit::parse(fname).unwrap();
println!("{}", fname);
circ.print_info().unwrap();
let circ = Circuit::parse(fname).unwrap();
// println!("{}", fname);
// circ.print_info().unwrap();
circ
}

Expand Down
10 changes: 8 additions & 2 deletions examples/mixed_radix_addition_cost.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use fancy_garbling::{error::InformerError, informer::{Informer, InformerVal}};
use fancy_garbling::*;
use fancy_garbling::{
error::InformerError,
informer::{Informer, InformerVal},
};

fn main() {
let n = 5;
let ps = vec![3, 4, 7, 83];

let mut b = Informer::new();
let xs = (0..n).map(|_| b.receive_bundle(&ps)).collect::<Result<Vec<Bundle<InformerVal>>, InformerError>>().unwrap();
let xs = (0..n)
.map(|_| b.receive_bundle(&ps))
.collect::<Result<Vec<Bundle<InformerVal>>, InformerError>>()
.unwrap();
let z = b.mixed_radix_addition_msb_only(&xs).unwrap();
b.output(&z).unwrap();
b.print_info();
Expand Down
2 changes: 1 addition & 1 deletion src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ impl CircuitBuilder {
}

#[cfg(test)]
mod basic {
mod plaintext {
use super::*;
use crate::util::RngExt;
use itertools::Itertools;
Expand Down
2 changes: 1 addition & 1 deletion src/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! creating any circuits.

use crate::error::{DummyError, FancyError};
use crate::fancy::{FancyInput, Fancy, HasModulus};
use crate::fancy::{Fancy, FancyInput, HasModulus};

/// Simple struct that performs the fancy computation over `u16`.
pub struct Dummy {
Expand Down
170 changes: 108 additions & 62 deletions src/fancy/input.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use super::*;
use itertools::Itertools;
use crate::util;
use itertools::Itertools;

/// Convenience functions for encoding input to Fancy objects.
pub trait FancyInput: Fancy {
////////////////////////////////////////////////////////////////////////////////
// required methods

/// Encode many values where the actual input is known.
fn encode_many(&mut self, values: &[u16], moduli: &[u16]) -> Result<Vec<Self::Item>, Self::Error>;
fn encode_many(
&mut self,
values: &[u16],
moduli: &[u16],
) -> Result<Vec<Self::Item>, Self::Error>;

/// Receive many values where the input is not known.
fn receive_many(&mut self, moduli: &[u16]) -> Result<Vec<Self::Item>, Self::Error>;
Expand All @@ -29,9 +33,11 @@ pub trait FancyInput: Fancy {
}

/// Encode a bundle.
fn encode_bundle(&mut self, values: &[u16], moduli: &[u16])
-> Result<Bundle<Self::Item>, Self::Error>
{
fn encode_bundle(
&mut self,
values: &[u16],
moduli: &[u16],
) -> Result<Bundle<Self::Item>, Self::Error> {
self.encode_many(values, moduli).map(Bundle::new)
}

Expand All @@ -41,39 +47,52 @@ pub trait FancyInput: Fancy {
}

/// Encode many input bundles.
fn encode_bundles(&mut self, values: &[Vec<u16>], moduli: &[Vec<u16>])
-> Result<Vec<Bundle<Self::Item>>, Self::Error>
{
fn encode_bundles(
&mut self,
values: &[Vec<u16>],
moduli: &[Vec<u16>],
) -> Result<Vec<Bundle<Self::Item>>, Self::Error> {
let qs = moduli.iter().flatten().cloned().collect_vec();
let xs = values.iter().flatten().cloned().collect_vec();
if xs.len() != qs.len() {
Err(FancyError::InvalidArg("unequal number of values and moduli".to_string()))?;
Err(FancyError::InvalidArg(
"unequal number of values and moduli".to_string(),
))?;
}
let mut wires = self.encode_many(&xs, &qs)?;
let buns = moduli.iter().map(|qs| {
let ws = wires.drain(0..qs.len()).collect_vec();
Bundle::new(ws)
}).collect_vec();
let buns = moduli
.iter()
.map(|qs| {
let ws = wires.drain(0..qs.len()).collect_vec();
Bundle::new(ws)
})
.collect_vec();
Ok(buns)
}

/// Receive many input bundles.
fn receive_many_bundles(&mut self, moduli: &[Vec<u16>])
-> Result<Vec<Bundle<Self::Item>>, Self::Error>
{
fn receive_many_bundles(
&mut self,
moduli: &[Vec<u16>],
) -> Result<Vec<Bundle<Self::Item>>, Self::Error> {
let qs = moduli.iter().flatten().cloned().collect_vec();
let mut wires = self.receive_many(&qs)?;
let buns = moduli.iter().map(|qs| {
let ws = wires.drain(0..qs.len()).collect_vec();
Bundle::new(ws)
}).collect_vec();
let buns = moduli
.iter()
.map(|qs| {
let ws = wires.drain(0..qs.len()).collect_vec();
Bundle::new(ws)
})
.collect_vec();
Ok(buns)
}

/// Encode a CRT input bundle.
fn crt_encode(&mut self, value: u128, modulus: u128)
-> Result<CrtBundle<Self::Item>, Self::Error>
{
fn crt_encode(
&mut self,
value: u128,
modulus: u128,
) -> Result<CrtBundle<Self::Item>, Self::Error> {
let qs = util::factor(modulus);
let xs = util::crt(value, &qs);
self.encode_bundle(&xs, &qs).map(CrtBundle::from)
Expand All @@ -86,73 +105,100 @@ pub trait FancyInput: Fancy {
}

/// Encode many CRT input bundles.
fn crt_encode_many(&mut self, values: &[u128], modulus: u128)
-> Result<Vec<CrtBundle<Self::Item>>, Self::Error>
{
fn crt_encode_many(
&mut self,
values: &[u128],
modulus: u128,
) -> Result<Vec<CrtBundle<Self::Item>>, Self::Error> {
let mods = util::factor(modulus);
let nmods = mods.len();
let xs = values.iter().map(|x| util::crt(*x,&mods)).flatten().collect_vec();
let qs = itertools::repeat_n(mods, values.len()).flatten().collect_vec();
let xs = values
.iter()
.map(|x| util::crt(*x, &mods))
.flatten()
.collect_vec();
let qs = itertools::repeat_n(mods, values.len())
.flatten()
.collect_vec();
let mut wires = self.encode_many(&xs, &qs)?;
let buns = (0..values.len()).map(|_| {
let ws = wires.drain(0..nmods).collect_vec();
CrtBundle::new(ws)
}).collect_vec();
let buns = (0..values.len())
.map(|_| {
let ws = wires.drain(0..nmods).collect_vec();
CrtBundle::new(ws)
})
.collect_vec();
Ok(buns)
}

/// Receive many CRT input bundles.
fn crt_receive_many(&mut self, n: usize, modulus: u128)
-> Result<Vec<CrtBundle<Self::Item>>, Self::Error>
{
fn crt_receive_many(
&mut self,
n: usize,
modulus: u128,
) -> Result<Vec<CrtBundle<Self::Item>>, Self::Error> {
let mods = util::factor(modulus);
let nmods = mods.len();
let qs = itertools::repeat_n(mods, n).flatten().collect_vec();
let mut wires = self.receive_many(&qs)?;
let buns = (0..n).map(|_| {
let ws = wires.drain(0..nmods).collect_vec();
CrtBundle::new(ws)
}).collect_vec();
let buns = (0..n)
.map(|_| {
let ws = wires.drain(0..nmods).collect_vec();
CrtBundle::new(ws)
})
.collect_vec();
Ok(buns)
}

/// Encode a binary input bundle.
fn bin_encode(&mut self, value: u128, nbits: usize)
-> Result<BinaryBundle<Self::Item>, Self::Error>
{
fn bin_encode(
&mut self,
value: u128,
nbits: usize,
) -> Result<BinaryBundle<Self::Item>, Self::Error> {
let xs = util::u128_to_bits(value, nbits);
self.encode_bundle(&xs, &vec![2;nbits]).map(BinaryBundle::from)
self.encode_bundle(&xs, &vec![2; nbits])
.map(BinaryBundle::from)
}

/// Receive an binary input bundle.
fn bin_receive(&mut self, nbits: usize)
-> Result<BinaryBundle<Self::Item>, Self::Error>
{
self.receive_bundle(&vec![2;nbits]).map(BinaryBundle::from)
fn bin_receive(&mut self, nbits: usize) -> Result<BinaryBundle<Self::Item>, Self::Error> {
self.receive_bundle(&vec![2; nbits]).map(BinaryBundle::from)
}

/// Encode many binary input bundles.
fn bin_encode_many(&mut self, values: &[u128], nbits: usize)
-> Result<Vec<BinaryBundle<Self::Item>>, Self::Error>
{
let xs = values.iter().map(|x| util::u128_to_bits(*x,nbits)).flatten().collect_vec();
fn bin_encode_many(
&mut self,
values: &[u128],
nbits: usize,
) -> Result<Vec<BinaryBundle<Self::Item>>, Self::Error> {
let xs = values
.iter()
.map(|x| util::u128_to_bits(*x, nbits))
.flatten()
.collect_vec();
let mut wires = self.encode_many(&xs, &vec![2; values.len() * nbits])?;
let buns = (0..values.len()).map(|_| {
let ws = wires.drain(0..nbits).collect_vec();
BinaryBundle::new(ws)
}).collect_vec();
let buns = (0..values.len())
.map(|_| {
let ws = wires.drain(0..nbits).collect_vec();
BinaryBundle::new(ws)
})
.collect_vec();
Ok(buns)
}

/// Receive many binary input bundles.
fn bin_receive_many(&mut self, ninputs: usize, nbits: usize)
-> Result<Vec<BinaryBundle<Self::Item>>, Self::Error>
{
fn bin_receive_many(
&mut self,
ninputs: usize,
nbits: usize,
) -> Result<Vec<BinaryBundle<Self::Item>>, Self::Error> {
let mut wires = self.receive_many(&vec![2; ninputs * nbits])?;
let buns = (0..ninputs).map(|_| {
let ws = wires.drain(0..nbits).collect_vec();
BinaryBundle::new(ws)
}).collect_vec();
let buns = (0..ninputs)
.map(|_| {
let ws = wires.drain(0..nbits).collect_vec();
BinaryBundle::new(ws)
})
.collect_vec();
Ok(buns)
}
}
2 changes: 1 addition & 1 deletion src/garble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub use crate::garble::garbler::Garbler;
// tests

#[cfg(test)]
mod classic {
mod nonstreaming {
use crate::circuit::{Circuit, CircuitBuilder};
use crate::dummy::Dummy;
use crate::dummy::DummyVal;
Expand Down
6 changes: 5 additions & 1 deletion src/informer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,11 @@ impl FancyInput for Informer {
moduli.iter().map(|q| self.receive(*q)).collect()
}

fn encode_many(&mut self, _values: &[u16], moduli: &[u16]) -> Result<Vec<Self::Item>, Self::Error> {
fn encode_many(
&mut self,
_values: &[u16],
moduli: &[u16],
) -> Result<Vec<Self::Item>, Self::Error> {
moduli.iter().map(|q| self.receive(*q)).collect()
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,8 @@ pub fn base_q_add_eq(xs: &mut [u16], ys: &[u16], q: u16) {

while i < ys.len() {
xs[i] += ys[i] + c;
c = 0;
if xs[i] >= q {
xs[i] -= q;
c = 1;
}
c = (xs[i] >= q) as u16;
xs[i] -= c * q;
i += 1;
}

Expand Down
6 changes: 6 additions & 0 deletions src/wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ impl Wire {
for i in 0..15 {
let cs = base_conversion::lookup_digits_mod_at_position(bytes[i], q, i);
util::base_q_add_eq(&mut ds, &cs, q);
// for (x,y) in ds.iter_mut().zip(cs.into_iter()) {
// *x += y;
// if *x >= q {
// *x -= q;
// }
// }
}
// Drop the digits we won't be able to pack back in again, especially if
// they get multiplied.
Expand Down

0 comments on commit 4b1f49b

Please sign in to comment.