Skip to content

Commit

Permalink
chore: drop ring in favor of sha2 rust impl
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Jul 26, 2018
1 parent aded904 commit 8d0ef89
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ authors = ["dignifiedquire <[email protected]>"]
[dependencies]
sapling-crypto = { git = "https://github.com/zcash-hackworks/sapling-crypto", branch = "master" }
rand = "0.4"
ring = "0.12"
libc = "0.2"
merkle_light = { git = "https://github.com/dignifiedquire/merkle_light", branch = "master" }
failure = "0.1"
Expand All @@ -23,6 +22,7 @@ indicatif = "0.9"
colored = "1.6"
aes = "0.1"
block-modes = "0.1"
sha2 = "0.7"

[dependencies.pairing]
version = "0.14.2"
Expand Down
10 changes: 2 additions & 8 deletions src/crypto/feistel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ring::digest::{Context, SHA256};
use sha2::{Digest, Sha256};

pub fn permute(num_elements: u32, index: u32, keys: &[u32]) -> u32 {
let mut u = encode(num_elements, index, keys);
Expand Down Expand Up @@ -74,7 +74,7 @@ fn feistel(right: u32, key: u32, right_mask: u32) -> u32 {
data[6] = (key >> 8) as u8;
data[7] = key as u8;

let hash = sha256_digest(&data);
let hash = Sha256::digest(&data);

let r = u32::from(hash[0]) << 24
| u32::from(hash[1]) << 16
Expand All @@ -84,12 +84,6 @@ fn feistel(right: u32, key: u32, right_mask: u32) -> u32 {
r & right_mask
}

fn sha256_digest(data: &[u8]) -> Vec<u8> {
let mut context = Context::new(&SHA256);
context.update(data);
context.finish().as_ref().into()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
36 changes: 15 additions & 21 deletions src/hasher/sha256.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use merkle_light::hash::{Algorithm, Hashable};
use ring::digest::{Context, SHA256};
use sha2::{Digest, Sha256};
use std::fmt;
use std::hash::Hasher;

#[derive(Clone)]
pub struct SHA256Algorithm(Context);
pub struct SHA256Algorithm(Sha256);

impl SHA256Algorithm {
fn new() -> SHA256Algorithm {
SHA256Algorithm(Context::new(&SHA256))
SHA256Algorithm(Sha256::new())
}
}

Expand All @@ -27,7 +27,7 @@ impl fmt::Debug for SHA256Algorithm {
impl Hasher for SHA256Algorithm {
#[inline]
fn write(&mut self, msg: &[u8]) {
self.0.update(msg)
self.0.input(msg)
}

#[inline]
Expand All @@ -36,31 +36,26 @@ impl Hasher for SHA256Algorithm {
}
}

pub type RingSHA256Hash = [u8; 32];
pub type SHA256Hash = [u8; 32];

impl Algorithm<RingSHA256Hash> for SHA256Algorithm {
impl Algorithm<SHA256Hash> for SHA256Algorithm {
#[inline]
fn hash(&mut self) -> RingSHA256Hash {
fn hash(&mut self) -> SHA256Hash {
let mut h = [0u8; 32];
h.copy_from_slice(self.0.clone().finish().as_ref());
h.copy_from_slice(self.0.clone().result().as_ref());
h
}

#[inline]
fn reset(&mut self) {
self.0 = Context::new(&SHA256);
self.0 = Sha256::new();
}

fn leaf(&mut self, leaf: RingSHA256Hash) -> RingSHA256Hash {
fn leaf(&mut self, leaf: SHA256Hash) -> SHA256Hash {
leaf
}

fn node(
&mut self,
left: RingSHA256Hash,
right: RingSHA256Hash,
_height: usize,
) -> RingSHA256Hash {
fn node(&mut self, left: SHA256Hash, right: SHA256Hash, _height: usize) -> SHA256Hash {
// TODO: second preimage attack fix
left.hash(self);
right.hash(self);
Expand Down Expand Up @@ -98,7 +93,7 @@ mod tests {
}

#[test]
fn test_ring_256_hash() {
fn test_sha256_hash() {
let mut a = SHA256Algorithm::new();
"hello".hash(&mut a);
let h1 = a.hash();
Expand All @@ -109,7 +104,7 @@ mod tests {
}

#[test]
fn test_ring_sha256_node() {
fn test_sha256_node() {
let mut h1 = [0u8; 32];
let mut h2 = [0u8; 32];
let mut h3 = [0u8; 32];
Expand Down Expand Up @@ -159,9 +154,8 @@ mod tests {
// "e6a6b12f6147ce9ce87c9f2a7f41ddd9587f6ea59ccbfb33fba08e3740d96200"
// );

let t: MerkleTree<RingSHA256Hash, SHA256Algorithm> =
MerkleTree::from_iter(vec![h1, h2, h3]);
let t2: MerkleTree<RingSHA256Hash, SHA256Algorithm> = MerkleTree::from_iter(vec![h1, h2]);
let t: MerkleTree<SHA256Hash, SHA256Algorithm> = MerkleTree::from_iter(vec![h1, h2, h3]);
let t2: MerkleTree<SHA256Hash, SHA256Algorithm> = MerkleTree::from_iter(vec![h1, h2]);

assert_eq!(t2.as_slice()[0], l1.as_ref());
assert_eq!(t2.as_slice()[1], l2.as_ref());
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ extern crate num_bigint;
extern crate num_traits;
extern crate pairing;
extern crate rand;
extern crate ring;
extern crate sapling_crypto;
extern crate sha2;

#[cfg(test)]
extern crate tempfile;
Expand Down

0 comments on commit 8d0ef89

Please sign in to comment.