diff --git a/iso/mpc_compute/mpc_compute.rs b/iso/mpc_compute/mpc_compute.rs deleted file mode 100755 index 3f4c191..0000000 Binary files a/iso/mpc_compute/mpc_compute.rs and /dev/null differ diff --git a/src/coordinator.rs b/src/coordinator.rs index bd09217..f1648a3 100644 --- a/src/coordinator.rs +++ b/src/coordinator.rs @@ -126,11 +126,11 @@ impl ConnectionHandler { let mut peers = vec![]; let mut pubkeys = vec![]; - let mut commitments: Vec = vec![]; + let mut commitments: Vec = vec![]; for peerid in new_peers.into_iter().take(PLAYERS) { info!("Initializing new player (peerid={})", peerid.to_hex()); info!("Asking for commitment to PublicKey (peerid={})", peerid.to_hex()); - let comm: Digest = self.read(&peerid); + let comm: Digest512 = self.read(&peerid); info!("PublicKey Commitment received (peerid={})", peerid.to_hex()); info!("Writing commitment to transcript"); diff --git a/src/protocol/digest.rs b/src/protocol/digest.rs index d862e08..b7474bb 100644 --- a/src/protocol/digest.rs +++ b/src/protocol/digest.rs @@ -4,18 +4,77 @@ use rustc_serialize::{Encodable, Encoder, Decodable, Decoder}; use bincode::SizeLimit::Infinite; use bincode::rustc_serialize::encode; use blake2_rfc::blake2b::blake2b; +use blake2_rfc::blake2s::blake2s; -/// 512-bit BLAKE2b hash digest -pub struct Digest([u8; 64]); +macro_rules! digest_impl { + ($name:ident, $bytes:expr, $hash:ident) => { + pub struct $name([u8; $bytes]); -impl Digest { + impl $name { + pub fn from(obj: &E) -> Option { + let serialized = encode(obj, Infinite); + match serialized { + Ok(ref serialized) => { + let mut buf: [u8; $bytes] = [0; $bytes]; + buf.copy_from_slice(&$hash($bytes, &[], serialized).as_bytes()); + + Some($name(buf)) + }, + Err(_) => None + } + } + } + + impl PartialEq for $name { + fn eq(&self, other: &$name) -> bool { + (&self.0[..]).eq(&other.0[..]) + } + } + + impl Eq for $name { } + + impl Copy for $name { } + impl Clone for $name { + fn clone(&self) -> $name { + *self + } + } + + impl Encodable for $name { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { + for i in 0..$bytes { + try!(s.emit_u8(self.0[i])); + } + + Ok(()) + } + } + + impl Decodable for $name { + fn decode(s: &mut S) -> Result<$name, S::Error> { + let mut buf = [0; $bytes]; + + for i in 0..$bytes { + buf[i] = try!(s.read_u8()); + } + + Ok($name(buf)) + } + } + } +} + +digest_impl!(Digest512, 64, blake2b); +digest_impl!(Digest256, 32, blake2s); + +impl Digest512 { pub fn to_string(&self) -> String { use rustc_serialize::hex::{ToHex}; self.0.to_hex() } - pub fn from_string(s: &str) -> Option { + pub fn from_string(s: &str) -> Option { use rustc_serialize::hex::{FromHex}; match s.from_hex() { @@ -23,7 +82,7 @@ impl Digest { if decoded.len() == 64 { let mut decoded_bytes: [u8; 64] = [0; 64]; decoded_bytes.copy_from_slice(&decoded); - Some(Digest(decoded_bytes)) + Some(Digest512(decoded_bytes)) } else { None } @@ -34,57 +93,7 @@ impl Digest { } } - pub fn from(obj: &E) -> Option { - let serialized = encode(obj, Infinite); - match serialized { - Ok(ref serialized) => { - let mut buf: [u8; 64] = [0; 64]; - buf.copy_from_slice(&blake2b(64, &[], serialized).as_bytes()); - - Some(Digest(buf)) - }, - Err(_) => None - } - } - pub fn interpret(&self) -> Fr { Fr::interpret(&self.0) } } - -impl PartialEq for Digest { - fn eq(&self, other: &Digest) -> bool { - (&self.0[..]).eq(&other.0[..]) - } -} - -impl Eq for Digest { } - -impl Copy for Digest { } -impl Clone for Digest { - fn clone(&self) -> Digest { - *self - } -} - -impl Encodable for Digest { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - for i in 0..64 { - try!(s.emit_u8(self.0[i])); - } - - Ok(()) - } -} - -impl Decodable for Digest { - fn decode(s: &mut S) -> Result { - let mut buf = [0; 64]; - - for i in 0..64 { - buf[i] = try!(s.read_u8()); - } - - Ok(Digest(buf)) - } -} diff --git a/src/protocol/nizk.rs b/src/protocol/nizk.rs index a0e87f7..6931461 100644 --- a/src/protocol/nizk.rs +++ b/src/protocol/nizk.rs @@ -1,6 +1,6 @@ use bn::*; use rand::Rng; -use super::digest::Digest; +use super::digest::Digest512; #[derive(PartialEq, Eq, Clone, RustcEncodable, RustcDecodable)] pub struct Nizk { @@ -14,7 +14,7 @@ impl Nizk { pub fn new(rng: &mut R, f: G, s: Fr) -> Nizk { let a = Fr::random(rng); let r = f * a; - let c = Digest::from(&r).expect("group element should never fail to encode").interpret(); + let c = Digest512::from(&r).expect("group element should never fail to encode").interpret(); Nizk { r: r, u: a + c * s @@ -23,7 +23,7 @@ impl Nizk { /// Verify the Nizk pub fn verify(&self, f: G, fs: G) -> bool { - let c = Digest::from(&self.r).expect("group element should never fail to encode").interpret(); + let c = Digest512::from(&self.r).expect("group element should never fail to encode").interpret(); (f * self.u) == (self.r + fs * c) } diff --git a/src/protocol/secrets.rs b/src/protocol/secrets.rs index ce846c4..488a455 100644 --- a/src/protocol/secrets.rs +++ b/src/protocol/secrets.rs @@ -2,7 +2,7 @@ use bn::*; use rand::Rng; use super::spair::{Spair, same_power}; use super::nizk::Nizk; -use super::digest::Digest; +use super::digest::Digest512; #[cfg(feature = "snark")] use snark::*; use rustc_serialize::{Encodable, Encoder, Decodable, Decoder}; @@ -66,8 +66,8 @@ impl PublicKey { same_power(&self.f8_gamma, &Spair::new(self.f2_beta, self.f2_beta_gamma).unwrap()) } - pub fn hash(&self) -> Digest { - Digest::from(self).expect("PublicKey should never fail to encode") + pub fn hash(&self) -> Digest512 { + Digest512::from(self).expect("PublicKey should never fail to encode") } pub fn tau_g2(&self) -> Spair {