Replace ring with tiny-keccak.

This commit is contained in:
Andreas Fackler 2018-08-09 11:51:31 +02:00 committed by Andreas Fackler
parent b90a7bf450
commit e4ddd6a079
5 changed files with 28 additions and 28 deletions

View File

@ -28,16 +28,16 @@ pairing = { version = "0.14.2", features = ["u128-support"] }
rand = "0.4.2"
rand_derive = "0.3.1"
reed-solomon-erasure = "3.1.0"
ring = "^0.12"
serde = "1.0.55"
serde_derive = "1.0.55"
threshold_crypto = { git = "https://github.com/poanetwork/threshold_crypto" }
tiny-keccak = "1.4"
[dev-dependencies]
colored = "1.6"
crossbeam = "0.3.2"
crossbeam-channel = "0.1"
docopt = "0.8"
docopt = "1.0"
serde_derive = "1.0.55"
signifix = "0.9"

View File

@ -8,7 +8,7 @@ use rand;
use reed_solomon_erasure as rse;
use reed_solomon_erasure::ReedSolomon;
use super::merkle::{MerkleTree, Proof};
use super::merkle::{Digest, MerkleTree, Proof};
use super::{Error, Result};
use fault_log::{Fault, FaultKind};
use fmt::{HexBytes, HexList, HexProof};
@ -21,7 +21,7 @@ use traits::NodeUidT;
pub enum Message {
Value(Proof<Vec<u8>>),
Echo(Proof<Vec<u8>>),
Ready(Vec<u8>),
Ready(Digest),
}
// A random generation impl is provided for test cases. Unfortunately `#[cfg(test)]` does not work
@ -41,7 +41,7 @@ impl rand::Rand for Message {
match message_type {
"value" => Message::Value(proof),
"echo" => Message::Echo(proof),
"ready" => Message::Ready(b"dummy-ready".to_vec()),
"ready" => Message::Ready([b'r'; 32]),
_ => unreachable!(),
}
}
@ -260,7 +260,7 @@ impl<N: NodeUidT> Broadcast<N> {
return Ok(Fault::new(sender_id.clone(), FaultKind::InvalidProof).into());
}
let hash = p.root_hash().to_vec();
let hash = *p.root_hash();
// Save the proof for reconstructing the tree later.
self.echos.insert(sender_id.clone(), p);
@ -274,7 +274,7 @@ impl<N: NodeUidT> Broadcast<N> {
}
/// Handles a received `Ready` message.
fn handle_ready(&mut self, sender_id: &N, hash: &[u8]) -> Result<Step<N>> {
fn handle_ready(&mut self, sender_id: &N, hash: &Digest) -> Result<Step<N>> {
// If the sender has already sent a `Ready` before, ignore.
if self.readys.contains_key(sender_id) {
info!(
@ -312,12 +312,12 @@ impl<N: NodeUidT> Broadcast<N> {
}
/// Sends a `Ready` message and handles it. Does nothing if we are only an observer.
fn send_ready(&mut self, hash: &[u8]) -> Result<Step<N>> {
fn send_ready(&mut self, hash: &Digest) -> Result<Step<N>> {
self.ready_sent = true;
if !self.netinfo.is_validator() {
return Ok(Step::default());
}
let ready_msg = Message::Ready(hash.to_vec());
let ready_msg = Message::Ready(*hash);
let mut step: Step<_> = Target::All.message(ready_msg).into();
let our_uid = &self.netinfo.our_uid().clone();
step.extend(self.handle_ready(our_uid, hash)?);
@ -326,7 +326,7 @@ impl<N: NodeUidT> Broadcast<N> {
/// Checks whether the conditions for output are met for this hash, and if so, sets the output
/// value.
fn compute_output(&mut self, hash: &[u8]) -> Result<Step<N>> {
fn compute_output(&mut self, hash: &Digest) -> Result<Step<N>> {
if self.decided
|| self.count_readys(hash) <= 2 * self.netinfo.num_faulty()
|| self.count_echos(hash) < self.coding.data_shard_count()
@ -381,7 +381,7 @@ impl<N: NodeUidT> Broadcast<N> {
}
/// Returns the number of nodes that have sent us an `Echo` message with this hash.
fn count_echos(&self, hash: &[u8]) -> usize {
fn count_echos(&self, hash: &Digest) -> usize {
self.echos
.values()
.filter(|p| p.root_hash() == hash)
@ -389,7 +389,7 @@ impl<N: NodeUidT> Broadcast<N> {
}
/// Returns the number of nodes that have sent us a `Ready` message with this hash.
fn count_readys(&self, hash: &[u8]) -> usize {
fn count_readys(&self, hash: &Digest) -> usize {
self.readys
.values()
.filter(|h| h.as_slice() == hash)
@ -467,7 +467,7 @@ fn decode_from_shards(
leaf_values: &mut [Option<Box<[u8]>>],
coding: &Coding,
data_shard_num: usize,
root_hash: &[u8],
root_hash: &Digest,
) -> Option<Vec<u8>> {
// Try to interpolate the Merkle tree using the Reed-Solomon erasure coding scheme.
if let Err(err) = coding.reconstruct_shards(leaf_values) {

View File

@ -1,8 +1,8 @@
use std::mem;
use ring::digest::{self, Digest, SHA256};
use tiny_keccak::sha3_256;
type DigestBytes = Vec<u8>;
pub type Digest = [u8; 32];
/// A Merkle tree: The leaves are values and their hashes. Each level consists of the hashes of
/// pairs of values on the previous level. The root is the value in the first level with only one
@ -39,7 +39,7 @@ impl<T: AsRef<[u8]> + Clone> MerkleTree<T> {
for level in &self.levels {
// Insert the sibling hash if there is one.
if let Some(digest) = level.get(lvl_i ^ 1) {
digests.push(digest.as_ref().to_vec());
digests.push(*digest);
}
lvl_i /= 2;
}
@ -47,13 +47,13 @@ impl<T: AsRef<[u8]> + Clone> MerkleTree<T> {
index,
digests,
value,
root_hash: self.root_hash.as_ref().to_vec(),
root_hash: self.root_hash,
})
}
/// Returns the root hash of the tree.
pub fn root_hash(&self) -> &[u8] {
self.root_hash.as_ref()
pub fn root_hash(&self) -> &Digest {
&self.root_hash
}
/// Returns a the slice containing all leaf values.
@ -72,8 +72,8 @@ impl<T: AsRef<[u8]> + Clone> MerkleTree<T> {
pub struct Proof<T> {
value: T,
index: usize,
digests: Vec<DigestBytes>,
root_hash: DigestBytes,
digests: Vec<Digest>,
root_hash: Digest,
}
impl<T: AsRef<[u8]>> Proof<T> {
@ -98,7 +98,7 @@ impl<T: AsRef<[u8]>> Proof<T> {
if digest_itr.next().is_some() {
return false; // Too many levels in the proof.
}
digest.as_ref() == &self.root_hash[..]
digest == self.root_hash
}
/// Returns the index of this proof's value in the tree.
@ -107,8 +107,8 @@ impl<T: AsRef<[u8]>> Proof<T> {
}
/// Returns the tree's root hash.
pub fn root_hash(&self) -> &[u8] {
self.root_hash.as_ref()
pub fn root_hash(&self) -> &Digest {
&self.root_hash
}
/// Returns the leaf value.
@ -140,7 +140,7 @@ fn hash_pair<T0: AsRef<[u8]>, T1: AsRef<[u8]>>(v0: &T0, v1: &T1) -> Digest {
/// Returns the SHA-256 hash of the value's `[u8]` representation.
fn hash<T: AsRef<[u8]>>(value: T) -> Digest {
digest::digest(&SHA256, value.as_ref())
sha3_256(value.as_ref())
}
#[cfg(test)]

View File

@ -42,8 +42,8 @@ impl<'a, T: AsRef<[u8]>> fmt::Debug for HexProof<'a, T> {
f,
"Proof {{ #{}, root_hash: {:?}, value: {:?}, .. }}",
&self.0.index(),
HexBytes(&self.0.root_hash()),
HexBytes(&self.0.value().as_ref())
HexBytes(self.0.root_hash().as_ref()),
HexBytes(self.0.value().as_ref())
)
}
}

View File

@ -116,11 +116,11 @@ extern crate rand;
#[macro_use]
extern crate rand_derive;
extern crate reed_solomon_erasure;
extern crate ring;
extern crate serde;
#[macro_use]
extern crate serde_derive;
pub extern crate threshold_crypto as crypto;
extern crate tiny_keccak;
pub mod agreement;
pub mod broadcast;