make hashing
crate wasm compatible
This commit is contained in:
parent
20b7bdda4a
commit
2a50550b87
2
eth2/utils/hashing/.cargo/config
Normal file
2
eth2/utils/hashing/.cargo/config
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
[target.wasm32-unknown-unknown]
|
||||||
|
runner = 'wasm-bindgen-test-runner'
|
@ -4,5 +4,14 @@ version = "0.1.0"
|
|||||||
authors = ["Paul Hauner <paul@paulhauner.com>"]
|
authors = ["Paul Hauner <paul@paulhauner.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||||
ring = "0.14.6"
|
ring = "0.14.6"
|
||||||
|
|
||||||
|
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||||
|
sha2 = "0.8.0"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
rustc-hex = "2.0.1"
|
||||||
|
|
||||||
|
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
|
||||||
|
wasm-bindgen-test = "0.2.47"
|
||||||
|
@ -1,7 +1,17 @@
|
|||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
use ring::digest::{digest, SHA256};
|
use ring::digest::{digest, SHA256};
|
||||||
|
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
pub fn hash(input: &[u8]) -> Vec<u8> {
|
pub fn hash(input: &[u8]) -> Vec<u8> {
|
||||||
digest(&SHA256, input).as_ref().into()
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
let h = digest(&SHA256, input).as_ref().into();
|
||||||
|
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
let h = Sha256::digest(input).as_ref().into();
|
||||||
|
|
||||||
|
h
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get merkle root of some hashed values - the input leaf nodes is expected to already be hashed
|
/// Get merkle root of some hashed values - the input leaf nodes is expected to already be hashed
|
||||||
@ -37,19 +47,24 @@ pub fn merkle_root(values: &[Vec<u8>]) -> Option<Vec<u8>> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use ring::test;
|
use rustc_hex::FromHex;
|
||||||
|
|
||||||
#[test]
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
use wasm_bindgen_test::*;
|
||||||
|
|
||||||
|
#[cfg_attr(not(target_arch = "wasm32"), test)]
|
||||||
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
|
||||||
fn test_hashing() {
|
fn test_hashing() {
|
||||||
let input: Vec<u8> = b"hello world".as_ref().into();
|
let input: Vec<u8> = b"hello world".as_ref().into();
|
||||||
|
|
||||||
let output = hash(input.as_ref());
|
let output = hash(input.as_ref());
|
||||||
let expected_hex = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9";
|
let expected_hex = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9";
|
||||||
let expected: Vec<u8> = test::from_hex(expected_hex).unwrap();
|
let expected: Vec<u8> = expected_hex.from_hex().unwrap();
|
||||||
assert_eq!(expected, output);
|
assert_eq!(expected, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[cfg_attr(not(target_arch = "wasm32"), test)]
|
||||||
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
|
||||||
fn test_merkle_root() {
|
fn test_merkle_root() {
|
||||||
// hash the leaf nodes
|
// hash the leaf nodes
|
||||||
let mut input = vec![
|
let mut input = vec![
|
||||||
@ -79,13 +94,17 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(&expected[..], output.unwrap().as_slice());
|
assert_eq!(&expected[..], output.unwrap().as_slice());
|
||||||
}
|
}
|
||||||
#[test]
|
|
||||||
|
#[cfg_attr(not(target_arch = "wasm32"), test)]
|
||||||
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
|
||||||
fn test_empty_input_merkle_root() {
|
fn test_empty_input_merkle_root() {
|
||||||
let input = vec![];
|
let input = vec![];
|
||||||
let output = merkle_root(&input[..]);
|
let output = merkle_root(&input[..]);
|
||||||
assert_eq!(None, output);
|
assert_eq!(None, output);
|
||||||
}
|
}
|
||||||
#[test]
|
|
||||||
|
#[cfg_attr(not(target_arch = "wasm32"), test)]
|
||||||
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
|
||||||
fn test_odd_leaf_merkle_root() {
|
fn test_odd_leaf_merkle_root() {
|
||||||
let input = vec![
|
let input = vec![
|
||||||
hash("a".as_bytes()),
|
hash("a".as_bytes()),
|
||||||
|
Loading…
Reference in New Issue
Block a user