Skip to content

Commit

Permalink
Merge #1140
Browse files Browse the repository at this point in the history
1140: Use blake3 instead of blake2_simd r=syrusakbary a=syrusakbary

<!-- 
Prior to submitting a PR, review the CONTRIBUTING.md document for recommendations on how to test:
https://github.com/wasmerio/wasmer/blob/master/CONTRIBUTING.md#pull-requests

-->

# Description

Blake3 has a much faster hashing rate than Blake2/Blake2b_simd. This PR updates wasmer to use it as the default hashing algorithm.

More info here: https://www.infoq.com/news/2020/01/blake3-fast-crypto-hash/

<!-- 
Provide details regarding the change including motivation,
links to related issues, and the context of the PR.
-->

# Review

- [x] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Syrus <[email protected]>
Co-authored-by: Syrus Akbary <[email protected]>
  • Loading branch information
bors[bot] and syrusakbary authored Jan 20, 2020
2 parents d73d837 + 0d2d436 commit caa651c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## **[Unreleased]**

- [#1129](https://github.com/wasmerio/wasmer/pull/1129) Standard exception types for singlepass backend.
- [#1140](https://github.com/wasmerio/wasmer/pull/1140) Use [`blake3`](https://github.com/BLAKE3-team/BLAKE3) as default hashing algorithm for caching.

## 0.13.1 - 2020-01-16
- Fix bug in wapm related to the `package.wasmer_extra_flags` entry in the manifest
Expand Down
14 changes: 8 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions lib/runtime-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ edition = "2018"
nix = "0.15"
page_size = "0.4"
wasmparser = "0.45.0"
parking_lot = "0.9"
parking_lot = "0.10.0"
lazy_static = "1.4"
errno = "0.2"
libc = "0.2.60"
hex = "0.3"
hex = "0.4"
smallvec = "0.6"
bincode = "1.1"

Expand All @@ -36,16 +36,16 @@ version = "1.0"
version = "0.11"
[dependencies.serde-bench]
version = "0.0.7"
[dependencies.blake2b_simd]
version = "0.5"
[dependencies.blake3]
version = "0.1.0"
[dependencies.digest]
version = "0.8"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["memoryapi"] }

[build-dependencies]
blake2b_simd = "0.5"
blake3 = "0.1.0"
rustc_version = "0.2"
cc = "1.0"

Expand Down
7 changes: 3 additions & 4 deletions lib/runtime-core/build.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use blake2b_simd::blake2bp;
use std::{env, fs, io::Write, path::PathBuf};

const WASMER_VERSION: &'static str = env!("CARGO_PKG_VERSION");

fn main() {
let mut state = blake2bp::State::new();
state.update(WASMER_VERSION.as_bytes());
let mut hasher = blake3::Hasher::new();
hasher.update(WASMER_VERSION.as_bytes());

let hasher = state.finalize();
let hasher = hasher.finalize();
let hash_string = hasher.to_hex().as_str().to_owned();

let crate_dir = env::var("OUT_DIR").unwrap();
Expand Down
41 changes: 11 additions & 30 deletions lib/runtime-core/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//! and loaded to allow skipping compilation and fast startup.
use crate::{module::ModuleInfo, sys::Memory};
use blake2b_simd::blake2bp;
use std::{io, mem, slice};

/// Indicates the invalid type of invalid cache file
Expand Down Expand Up @@ -46,10 +45,8 @@ impl From<io::Error> for Error {
///
/// [`Cache`]: trait.Cache.html
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
// WasmHash is made up of two 32 byte arrays instead of a 64 byte array
// because derive only works on fixed sized arrays size 32 or below
// TODO: fix this when this gets fixed by improved const generics
pub struct WasmHash([u8; 32], [u8; 32]);
// WasmHash is made up of a 32 byte array
pub struct WasmHash([u8; 32]);

impl WasmHash {
/// Hash a wasm module.
Expand All @@ -58,18 +55,8 @@ impl WasmHash {
/// This does no verification that the supplied data
/// is, in fact, a wasm module.
pub fn generate(wasm: &[u8]) -> Self {
let mut first_part = [0u8; 32];
let mut second_part = [0u8; 32];

let mut state = blake2bp::State::new();
state.update(wasm);

let hasher = state.finalize();
let generic_array = hasher.as_bytes();

first_part.copy_from_slice(&generic_array[0..32]);
second_part.copy_from_slice(&generic_array[32..64]);
WasmHash(first_part, second_part)
let hash = blake3::hash(wasm);
WasmHash(hash.into())
}

/// Create the hexadecimal representation of the
Expand All @@ -86,26 +73,20 @@ impl WasmHash {
e
))
})?;
if bytes.len() != 64 {
if bytes.len() != 32 {
return Err(Error::DeserializeError(
"Prehashed keys must deserialze into exactly 64 bytes".to_string(),
"Prehashed keys must deserialze into exactly 32 bytes".to_string(),
));
}
use std::convert::TryInto;
Ok(WasmHash(
bytes[0..32].try_into().map_err(|e| {
Error::DeserializeError(format!("Could not get first 32 bytes: {}", e))
})?,
bytes[32..64].try_into().map_err(|e| {
Error::DeserializeError(format!("Could not get last 32 bytes: {}", e))
})?,
))
Ok(WasmHash(bytes[0..32].try_into().map_err(|e| {
Error::DeserializeError(format!("Could not get first 32 bytes: {}", e))
})?))
}

pub(crate) fn into_array(self) -> [u8; 64] {
let mut total = [0u8; 64];
pub(crate) fn into_array(self) -> [u8; 32] {
let mut total = [0u8; 32];
total[0..32].copy_from_slice(&self.0);
total[32..64].copy_from_slice(&self.1);
total
}
}
Expand Down

0 comments on commit caa651c

Please sign in to comment.