From a66949d24a7352a3522a842d546eca4bafdb2040 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Mon, 19 Sep 2016 10:35:32 -0600 Subject: [PATCH] Add BLAKE2. --- Cargo.lock | 14 ++++++++++++++ Cargo.toml | 1 + src/coordinator.rs | 14 ++++++++++---- src/player.rs | 1 + src/protocol/secrets.rs | 11 ++++++++--- src/simulator.rs | 2 ++ src/verifier.rs | 6 +++++- 7 files changed, 41 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 35c3209..bdd3ff9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,6 +4,7 @@ version = "0.0.1" dependencies = [ "ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "bincode 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "blake2-rfc 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)", "bn 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "crossbeam 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -37,6 +38,14 @@ dependencies = [ "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "blake2-rfc" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "constant_time_eq 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "bn" version = "0.4.0" @@ -52,6 +61,11 @@ name = "byteorder" version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "constant_time_eq" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "crossbeam" version = "0.2.9" diff --git a/Cargo.toml b/Cargo.toml index 58c8e51..d890793 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,7 @@ bn = "0.4.0" crossbeam = "0.2.9" rand = "0.3.14" rustc-serialize = "~0.3.19" +blake2-rfc = "0.2.17" log = "*" env_logger = "*" time = "*" diff --git a/src/coordinator.rs b/src/coordinator.rs index 80745ab..64819af 100644 --- a/src/coordinator.rs +++ b/src/coordinator.rs @@ -5,6 +5,7 @@ extern crate rand; extern crate snark; extern crate crossbeam; extern crate rustc_serialize; +extern crate blake2_rfc; extern crate bincode; #[macro_use] @@ -123,17 +124,22 @@ impl ConnectionHandler { let mut peers = vec![]; let mut pubkeys = vec![]; - let mut commitments: Vec<[u8; 32]> = 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 = self.read(&peerid); - commitments.push(comm); + let comm: PublicKeyHash = self.read(&peerid); + if comm.len() != 64 { + error!("Peer sent invalid length commitment (peerid={})", peerid.to_hex()); + panic!("cannot recover."); + } info!("PublicKey Commitment received (peerid={})", peerid.to_hex()); - peers.push(peerid); info!("Writing commitment to transcript"); encode_into(&comm, &mut transcript, Infinite).unwrap(); + + commitments.push(comm); + peers.push(peerid); } // The remote end should never hang up, so this should always be `PLAYERS`. diff --git a/src/player.rs b/src/player.rs index 2f1e3dc..58cdb96 100644 --- a/src/player.rs +++ b/src/player.rs @@ -6,6 +6,7 @@ extern crate rand; extern crate snark; extern crate crossbeam; extern crate rustc_serialize; +extern crate blake2_rfc; extern crate bincode; mod protocol; diff --git a/src/protocol/secrets.rs b/src/protocol/secrets.rs index 205f0d0..144c592 100644 --- a/src/protocol/secrets.rs +++ b/src/protocol/secrets.rs @@ -5,7 +5,7 @@ use super::spair::{Spair, same_power}; use snark::*; use rustc_serialize::{Encodable, Encoder, Decodable, Decoder}; -pub type PublicKeyHash = [u8; 32]; +pub type PublicKeyHash = Vec; #[derive(Clone, PartialEq, Eq)] pub struct PublicKey { @@ -45,8 +45,13 @@ impl PublicKey { } pub fn hash(&self) -> PublicKeyHash { - // TODO - [0xff; 32] + 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 tau_g2(&self) -> Spair { diff --git a/src/simulator.rs b/src/simulator.rs index 72ac280..432601b 100644 --- a/src/simulator.rs +++ b/src/simulator.rs @@ -3,6 +3,8 @@ extern crate rand; extern crate snark; extern crate crossbeam; extern crate rustc_serialize; +extern crate blake2_rfc; +extern crate bincode; mod protocol; use protocol::*; diff --git a/src/verifier.rs b/src/verifier.rs index 2150b5b..aed7fd2 100644 --- a/src/verifier.rs +++ b/src/verifier.rs @@ -3,6 +3,7 @@ extern crate rand; extern crate snark; extern crate crossbeam; extern crate rustc_serialize; +extern crate blake2_rfc; extern crate bincode; mod protocol; @@ -35,7 +36,10 @@ fn main() { let mut commitments = vec![]; let mut pubkeys = vec![]; for i in 0..num_players { - let comm: [u8; 32] = decode_from(&mut f, Infinite).unwrap(); + let comm: PublicKeyHash = decode_from(&mut f, Infinite).unwrap(); + if comm.len() != 64 { + panic!("Commitment length invalid."); + } commitments.push(comm); }