From 68d921912d51b6f515eb3572d741434afac6c9dd Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Tue, 20 Sep 2016 12:33:06 -0600 Subject: [PATCH] Split `Digest` into its own module. --- src/coordinator.rs | 8 ++--- src/protocol/digest.rs | 65 +++++++++++++++++++++++++++++++++++++++++ src/protocol/mod.rs | 2 ++ src/protocol/nizk.rs | 20 ++----------- src/protocol/secrets.rs | 13 ++------- src/verifier.rs | 5 +--- 6 files changed, 76 insertions(+), 37 deletions(-) create mode 100644 src/protocol/digest.rs diff --git a/src/coordinator.rs b/src/coordinator.rs index 2327105..46e0bde 100644 --- a/src/coordinator.rs +++ b/src/coordinator.rs @@ -124,15 +124,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: PublicKeyHash = self.read(&peerid); - if comm.len() != 64 { - error!("Peer sent invalid length commitment (peerid={})", peerid.to_hex()); - panic!("cannot recover."); - } + let comm: Digest = 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 new file mode 100644 index 0000000..a21ba3a --- /dev/null +++ b/src/protocol/digest.rs @@ -0,0 +1,65 @@ +use bn::Fr; + +use rustc_serialize::{Encodable, Encoder, Decodable, Decoder}; +use bincode::SizeLimit::Infinite; +use bincode::rustc_serialize::encode; +use blake2_rfc::blake2b::blake2b; + +/// 512-bit BLAKE2b hash digest +pub struct Digest([u8; 64]); + +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/mod.rs b/src/protocol/mod.rs index fd284a4..d434215 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -38,7 +38,9 @@ mod secrets; mod spair; mod nizk; mod multicore; +mod digest; pub use self::secrets::*; +pub use self::digest::*; use self::spair::*; use self::multicore::*; diff --git a/src/protocol/nizk.rs b/src/protocol/nizk.rs index b8d8f2d..a0e87f7 100644 --- a/src/protocol/nizk.rs +++ b/src/protocol/nizk.rs @@ -1,20 +1,6 @@ use bn::*; use rand::Rng; - -/// Hash a group element with BLAKE2b and interpret it as an -/// element of Fr. -fn hash_group_to_fr(r: &G) -> Fr { - use bincode::SizeLimit::Infinite; - use bincode::rustc_serialize::encode; - use blake2_rfc::blake2b::blake2b; - - let serialized = encode(r, Infinite).unwrap(); - - let mut hash = [0; 64]; - hash.copy_from_slice(blake2b(64, &[], &serialized).as_bytes()); - - Fr::interpret(&hash) -} +use super::digest::Digest; #[derive(PartialEq, Eq, Clone, RustcEncodable, RustcDecodable)] pub struct Nizk { @@ -28,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 = hash_group_to_fr(&r); + let c = Digest::from(&r).expect("group element should never fail to encode").interpret(); Nizk { r: r, u: a + c * s @@ -37,7 +23,7 @@ impl Nizk { /// Verify the Nizk pub fn verify(&self, f: G, fs: G) -> bool { - let c = hash_group_to_fr(&self.r); + let c = Digest::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 a628a6f..ce846c4 100644 --- a/src/protocol/secrets.rs +++ b/src/protocol/secrets.rs @@ -2,12 +2,11 @@ use bn::*; use rand::Rng; use super::spair::{Spair, same_power}; use super::nizk::Nizk; +use super::digest::Digest; #[cfg(feature = "snark")] use snark::*; use rustc_serialize::{Encodable, Encoder, Decodable, Decoder}; -pub type PublicKeyHash = Vec; - #[derive(Clone, PartialEq, Eq)] pub struct PublicKey { f1: G2, // f1 @@ -67,14 +66,8 @@ impl PublicKey { same_power(&self.f8_gamma, &Spair::new(self.f2_beta, self.f2_beta_gamma).unwrap()) } - pub fn hash(&self) -> PublicKeyHash { - use bincode::SizeLimit::Infinite; - use bincode::rustc_serialize::encode; - use blake2_rfc::blake2b::blake2b; - - let serialized = encode(self, Infinite).unwrap(); - - blake2b(64, &[], &serialized).as_bytes().to_vec() + pub fn hash(&self) -> Digest { + Digest::from(self).expect("PublicKey should never fail to encode") } pub fn tau_g2(&self) -> Spair { diff --git a/src/verifier.rs b/src/verifier.rs index aed7fd2..8d72d29 100644 --- a/src/verifier.rs +++ b/src/verifier.rs @@ -36,10 +36,7 @@ fn main() { let mut commitments = vec![]; let mut pubkeys = vec![]; for i in 0..num_players { - let comm: PublicKeyHash = decode_from(&mut f, Infinite).unwrap(); - if comm.len() != 64 { - panic!("Commitment length invalid."); - } + let comm: Digest = decode_from(&mut f, Infinite).unwrap(); commitments.push(comm); }