Skip to content

Commit

Permalink
Migrate BLAKE3 to BLAKE2 for easier cross-compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Oct 7, 2024
1 parent 234e93e commit 96113df
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 39 deletions.
36 changes: 7 additions & 29 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion packages/vm-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license = "Apache-2.0"
proc-macro = true

[dependencies]
blake3 = "1.5"
blake2 = "0.10.6"
proc-macro2 = "1.0.86"
quote = "1.0.37"
syn = { version = "2.0.77", features = ["extra-traits", "full"] }
11 changes: 6 additions & 5 deletions packages/vm-derive/src/hash_function.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::hash::{Hash, Hasher};

use blake2::{Blake2b512, Digest};
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::{punctuated::Punctuated, Token};
Expand Down Expand Up @@ -36,18 +37,18 @@ impl syn::parse::Parse for Options {
}

struct Blake3Hasher {
hasher: blake3::Hasher,
hasher: Blake2b512,
}

impl Blake3Hasher {
fn new() -> Self {
Self {
hasher: blake3::Hasher::new(),
hasher: Blake2b512::new(),
}
}

fn consume(self) -> blake3::Hash {
self.hasher.finalize()
fn consume(self) -> [u8; 64] {
self.hasher.finalize().into()
}
}

Expand All @@ -72,7 +73,7 @@ pub fn hash_function_impl(attr: TokenStream, input: TokenStream) -> TokenStream
let hash = hasher.consume();

let hash_variable_name = &options.const_name;
let hash_bytes = hash.as_bytes();
let hash_bytes = hash.as_slice();

quote! {
pub const #hash_variable_name: &[u8] = &[#(#hash_bytes),*];
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ bytes = "1.4.0" # need a higher version than the one required by Wasmer for
clru = "0.6.1"
crc32fast = "1.3.2"
bech32 = "0.11.0"
blake3 = "1.5"
blake2 = "0.10.6"
# Uses the path when built locally; uses the given version from crates.io when published
cosmwasm-core = { version = "2.2.0-rc.1", path = "../core" }
cosmwasm-std = { version = "2.2.0-rc.1", path = "../std", default-features = false, features = [
Expand Down
7 changes: 4 additions & 3 deletions packages/vm/src/modules/file_system_cache.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use blake2::{digest::consts::U5, Blake2b, Digest};
use std::fs;
use std::hash::Hash;
use std::io;
Expand Down Expand Up @@ -75,7 +76,7 @@ const MODULE_SERIALIZATION_VERSION: &str = "v20";
fn raw_module_version_discriminator() -> String {
let hashes = [COST_FUNCTION_HASH];

let mut hasher = blake3::Hasher::new();
let mut hasher = Blake2b::<U5>::new();

hasher.update(MODULE_SERIALIZATION_VERSION.as_bytes());
hasher.update(wasmer::VERSION.as_bytes());
Expand All @@ -84,7 +85,7 @@ fn raw_module_version_discriminator() -> String {
hasher.update(hash);
}

hasher.finalize().to_hex().to_string()
hex::encode(hasher.finalize())
}

/// This version __MUST__ change whenever the module system changes in a way
Expand Down Expand Up @@ -481,7 +482,7 @@ mod tests {
let version = raw_module_version_discriminator();
assert_eq!(
version,
"b2a230627e6fd9c14c45aabcf781b58d873dd251fcb004d30e081c8407cad5af"
"5b35f8ce52"
);
}
}

0 comments on commit 96113df

Please sign in to comment.