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

Commit

Permalink
towards using new Channel interface
Browse files Browse the repository at this point in the history
  • Loading branch information
amaloz committed Jun 4, 2019
1 parent 3c782cf commit 823ae59
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 40 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ base_conversion = { path = "base_conversion" }
itertools = "0.8.0"
rand = "0.6.5"
regex = "1.1.6"
scuttlebutt = { git = "https://github.com/amaloz/scuttlebutt", features = ["serde"], branch = "dev" }
# scuttlebutt = { git = "https://github.com/amaloz/scuttlebutt", features = ["serde"], branch = "dev" }
scuttlebutt = { path = "../scuttlebutt", features = ["serde"] }
serde = { version = "1.0.90", features = ["derive"] }

[dev-dependencies]
Expand Down
35 changes: 23 additions & 12 deletions src/garble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,18 @@ mod classic {
mod streaming {
use crate::dummy::{Dummy, DummyVal};
use crate::util::RngExt;
use crate::{Fancy, FancyInput};
use crate::{Evaluator, Garbler, Wire};
use crate::{Fancy, FancyInput};
use itertools::Itertools;
use rand::thread_rng;
use scuttlebutt::AesRng;
use scuttlebutt::{AesRng, Channel};
use std::cell::RefCell;
use std::io::{BufReader, BufWriter};
use std::os::unix::net::UnixStream;
use std::rc::Rc;

type MyChannel = Channel<BufReader<UnixStream>, BufWriter<UnixStream>>;

// helper - checks that Streaming evaluation of a fancy function equals Dummy
// evaluation of the same function
fn streaming_test<FGB, FEV, FDU>(
Expand All @@ -315,7 +318,7 @@ mod streaming {
mut f_du: FDU,
input_mods: &[u16],
) where
FGB: FnMut(&mut Garbler<UnixStream, AesRng>, &[Wire]) + Send + Sync + Copy + 'static,
FGB: FnMut(&mut Garbler<MyChannel, AesRng>, &[Wire]) + Send + Sync + Copy + 'static,
FEV: FnMut(&mut Evaluator<UnixStream>, &[Wire]) + Send + Sync + Copy + 'static,
FDU: FnMut(&mut Dummy, &[DummyVal]) + Send + Sync + Copy + 'static,
{
Expand All @@ -333,8 +336,10 @@ mod streaming {

let input_mods_ = input_mods.to_vec();
std::thread::spawn(move || {
let sender = Rc::new(RefCell::new(sender));
let mut gb = Garbler::new(sender, rng, &[]);
let reader = BufReader::new(sender.try_clone().unwrap());
let writer = BufWriter::new(sender);
let channel = Channel::new(reader, writer);
let mut gb = Garbler::new(channel, rng, &[]);
let (gb_inp, ev_inp) = gb.encode_many_wires(&inputs, &input_mods_).unwrap();
for w in ev_inp.iter() {
gb.send_wire(w).unwrap();
Expand Down Expand Up @@ -460,8 +465,9 @@ mod complex {
use crate::{CrtBundle, CrtGadgets, Evaluator, Fancy, FancyInput, Garbler};
use itertools::Itertools;
use rand::thread_rng;
use scuttlebutt::AesRng;
use scuttlebutt::{AesRng, Channel};
use std::cell::RefCell;
use std::io::{BufReader, BufWriter};
use std::os::unix::net::UnixStream;
use std::rc::Rc;

Expand Down Expand Up @@ -503,8 +509,10 @@ mod complex {

let input_ = input.clone();
std::thread::spawn(move || {
let sender = Rc::new(RefCell::new(sender));
let mut garbler = Garbler::new(sender, AesRng::new(), &[]);
let reader = BufReader::new(sender.try_clone().unwrap());
let writer = BufWriter::new(sender);
let channel = Channel::new(reader, writer);
let mut garbler = Garbler::new(channel, AesRng::new(), &[]);

// encode input and send it to the evaluator
let mut gb_inp = Vec::with_capacity(N);
Expand Down Expand Up @@ -545,8 +553,9 @@ mod reuse {
use crate::*;
use itertools::Itertools;
use rand::random;
use scuttlebutt::AesRng;
use scuttlebutt::{AbstractChannel, AesRng, Channel};
use std::cell::RefCell;
use std::io::{BufReader, BufWriter};
use std::os::unix::net::UnixStream;
use std::rc::Rc;

Expand All @@ -571,8 +580,10 @@ mod reuse {
let inps_ = inps.clone();
let mods_ = mods.clone();
std::thread::spawn(move || {
let sender = Rc::new(RefCell::new(sender));
let mut gb1 = Garbler::new(sender.clone(), AesRng::new(), &[]);
let reader = BufReader::new(sender.try_clone().unwrap());
let writer = BufWriter::new(sender);
let channel = Channel::new(reader, writer);
let mut gb1 = Garbler::new(channel.clone(), AesRng::new(), &[]);

// get the input wirelabels
let (gb_inps, ev_inps) = gb1.encode_many_wires(&inps_, &mods_).unwrap();
Expand All @@ -584,7 +595,7 @@ mod reuse {
// get deltas for input wires
let ds = mods_.into_iter().map(|q| gb1.delta(q)).collect_vec();

let mut gb2 = Garbler::new(sender, AesRng::new(), &ds);
let mut gb2 = Garbler::new(channel, AesRng::new(), &ds);

// output the input wires from the previous garbler
gb2.outputs(&gb_inps).unwrap();
Expand Down
36 changes: 14 additions & 22 deletions src/garble/garbler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,24 @@ use crate::fancy::{BinaryBundle, CrtBundle, Fancy, HasModulus};
use crate::util::{output_tweak, tweak, tweak2, RngExt};
use crate::wire::Wire;
use rand::{CryptoRng, RngCore};
use scuttlebutt::Block;
use std::cell::RefCell;
use scuttlebutt::{AbstractChannel, Block};
use std::collections::HashMap;
use std::fmt::Debug;
use std::io::Write;
use std::rc::Rc;

/// Streams garbled circuit ciphertexts through a callback. Parallelizable.
pub struct Garbler<W, RNG> {
writer: Rc<RefCell<W>>,
pub struct Garbler<C, RNG> {
channel: C,
deltas: HashMap<u16, Wire>, // map from modulus to associated delta wire-label.
current_output: usize,
current_gate: usize,
rng: RNG,
}

impl<W: Write + Debug, RNG: CryptoRng + RngCore> Garbler<W, RNG> {
impl<C: AbstractChannel, RNG: CryptoRng + RngCore> Garbler<C, RNG> {
/// Create a new garbler.
#[inline]
pub fn new(writer: Rc<RefCell<W>>, rng: RNG, reused_deltas: &[Wire]) -> Self {
pub fn new(channel: C, rng: RNG, reused_deltas: &[Wire]) -> Self {
Garbler {
writer,
channel,
deltas: reused_deltas
.iter()
.map(|w| (w.modulus(), w.clone()))
Expand Down Expand Up @@ -74,8 +70,7 @@ impl<W: Write + Debug, RNG: CryptoRng + RngCore> Garbler<W, RNG> {
/// Send a wire using the Sender.
#[inline]
pub fn send_wire(&mut self, wire: &Wire) -> Result<(), GarblerError> {
let mut writer = self.writer.borrow_mut();
writer.write_all(wire.as_block().as_ref())?;
self.channel.write_block(&wire.as_block())?;
Ok(())
}

Expand Down Expand Up @@ -136,7 +131,7 @@ impl<W: Write + Debug, RNG: CryptoRng + RngCore> Garbler<W, RNG> {
}
}

impl<W: Write + Debug, RNG: CryptoRng + RngCore> Fancy for Garbler<W, RNG> {
impl<C: AbstractChannel, RNG: CryptoRng + RngCore> Fancy for Garbler<C, RNG> {
type Item = Wire;
type Error = GarblerError;

Expand Down Expand Up @@ -279,9 +274,8 @@ impl<W: Write + Debug, RNG: CryptoRng + RngCore> Fancy for Garbler<W, RNG> {
}
}

let mut writer = self.writer.borrow_mut();
for block in gate.into_iter() {
writer.write_all(block.as_ref())?;
for block in gate.iter() {
self.channel.write_block(block)?;
}
Ok(X.plus_mov(&Y))
}
Expand Down Expand Up @@ -334,9 +328,8 @@ impl<W: Write + Debug, RNG: CryptoRng + RngCore> Fancy for Garbler<W, RNG> {
gate[ix - 1] = ct;
}

let mut writer = self.writer.borrow_mut();
for block in gate.into_iter() {
writer.write_all(block.as_ref())?;
for block in gate.iter() {
self.channel.write_block(block)?;
}
Ok(C)
}
Expand All @@ -351,9 +344,8 @@ impl<W: Write + Debug, RNG: CryptoRng + RngCore> Fancy for Garbler<W, RNG> {
let t = output_tweak(i, k);
cts.push(X.plus(&D.cmul(k)).hash(t));
}
let mut writer = self.writer.borrow_mut();
for block in cts.into_iter() {
writer.write_all(block.as_ref())?;
for block in cts.iter() {
self.channel.write_block(block)?;
}
Ok(())
}
Expand Down
18 changes: 13 additions & 5 deletions src/static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::fancy::HasModulus;
use crate::garble::{Evaluator, Garbler};
use crate::wire::Wire;
use itertools::Itertools;
use scuttlebutt::{AesRng, Block};
use scuttlebutt::{AbstractChannel, AesRng, Block, Channel};
use serde::{Deserialize, Serialize};
use std::cell::RefCell;
use std::collections::HashMap;
Expand Down Expand Up @@ -56,11 +56,14 @@ impl GarbledCircuit {

/// Garble a circuit without streaming.
pub fn garble(c: &mut Circuit) -> Result<(Encoder, GarbledCircuit), GarblerError> {
let writer = Rc::new(RefCell::new(GarbledWriter::new(Some(c.num_nonfree_gates))));
let writer_ = writer.clone();
let channel = Channel::new(
GarbledReader::new(&[]),
GarbledWriter::new(Some(c.num_nonfree_gates)),
);
let channel_ = channel.clone();

let rng = AesRng::new();
let mut garbler = Garbler::new(writer_, rng, &[]);
let mut garbler = Garbler::new(channel_, rng, &[]);

// get input wires, ignoring encoded values
let gb_inps = (0..c.num_garbler_inputs())
Expand All @@ -85,7 +88,12 @@ pub fn garble(c: &mut Circuit) -> Result<(Encoder, GarbledCircuit), GarblerError

let en = Encoder::new(gb_inps, ev_inps, garbler.get_deltas());

let gc = GarbledCircuit::new(Rc::try_unwrap(writer).unwrap().into_inner().blocks);
let gc = GarbledCircuit::new(
Rc::try_unwrap(channel.writer())
.unwrap()
.into_inner()
.blocks,
);

Ok((en, gc))
}
Expand Down

0 comments on commit 823ae59

Please sign in to comment.