From bb2bd53708851fd258a01edd844eca5d9f10eacb Mon Sep 17 00:00:00 2001 From: Hanyun Xu Date: Wed, 27 Apr 2022 12:06:18 -0700 Subject: [PATCH] import blake2b_simd and shorten personalization msg --- Cargo.lock | 12 ++++++++++++ crates/threshold-bls-ffi/Cargo.toml | 2 ++ crates/threshold-bls-ffi/src/ffi.rs | 12 +++++++++--- crates/threshold-bls-ffi/src/wasm.rs | 2 +- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 61bf2301..819c07f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -303,6 +303,17 @@ dependencies = [ "digest 0.10.3", ] +[[package]] +name = "blake2b_simd" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72936ee4afc7f8f736d1c38383b56480b5497b4617b4a77bdbf1d2ababc76127" +dependencies = [ + "arrayref", + "arrayvec", + "constant_time_eq", +] + [[package]] name = "blake2s_simd" version = "1.0.0" @@ -1313,6 +1324,7 @@ version = "0.2.0" dependencies = [ "bincode", "blake2 0.10.4", + "blake2b_simd", "bls-crypto", "cfg-if 0.1.10", "console_error_panic_hook", diff --git a/crates/threshold-bls-ffi/Cargo.toml b/crates/threshold-bls-ffi/Cargo.toml index 548e57da..1c112a2c 100644 --- a/crates/threshold-bls-ffi/Cargo.toml +++ b/crates/threshold-bls-ffi/Cargo.toml @@ -17,6 +17,8 @@ rand_chacha = { version = "0.3.1", default-features = false } bincode = { version = "1.2.1", default-features = false } serde = { version = "1.0.106", default-features = false } +blake2b_simd = { version = "1.0.0", default-features = false } + # Required for WASM interface blake2 = { version = "0.10", default-features = false, optional = true } diff --git a/crates/threshold-bls-ffi/src/ffi.rs b/crates/threshold-bls-ffi/src/ffi.rs index b28fcaee..ffa49cc6 100644 --- a/crates/threshold-bls-ffi/src/ffi.rs +++ b/crates/threshold-bls-ffi/src/ffi.rs @@ -2,6 +2,7 @@ use rand_chacha::ChaChaRng; use rand_core::{RngCore, SeedableRng}; +use blake2b_simd::Params; use serde::{de::DeserializeOwned, Serialize}; use threshold_bls::{ poly::{Idx as Index, Poly}, @@ -581,7 +582,12 @@ pub unsafe extern "C" fn destroy_sig(signature: *mut Signature) { /// /// The seed MUST be at least 32 bytes long #[no_mangle] -pub unsafe extern "C" fn threshold_keygen(n: usize, t: usize, seed: *const Buffer, keys: *mut *mut Keys) { +pub unsafe extern "C" fn threshold_keygen( + n: usize, + t: usize, + seed: *const Buffer, + keys: *mut *mut Keys, +) { let seed = <&[u8]>::from(unsafe { &*seed }); let mut rng = get_rng(seed); let private = Poly::::new_from(t - 1, &mut rng); @@ -713,13 +719,13 @@ fn from_slice(bytes: &[u8]) -> [u8; 32] { let mut array = [0; 32]; let hash_result = Params::new() .hash_length(32) - .personal(b"THRESHOLD BLS_rng") // personalization + .personal(b"BLS_rng") // personalization .to_state() .update(bytes) // digest .finalize() .as_ref() .to_vec(); - array.copy_from_slice(hash_result); + array.copy_from_slice(&hash_result); array } diff --git a/crates/threshold-bls-ffi/src/wasm.rs b/crates/threshold-bls-ffi/src/wasm.rs index bbcbf31a..821d357f 100644 --- a/crates/threshold-bls-ffi/src/wasm.rs +++ b/crates/threshold-bls-ffi/src/wasm.rs @@ -1,9 +1,9 @@ //! # BLS12-377 WASM Bindings for Blind Threshold Signatures. use wasm_bindgen::prelude::*; +use blake2::{Blake2s256, Digest}; use rand_chacha::ChaChaRng; use rand_core::{RngCore, SeedableRng}; -use blake2::{Blake2s256, Digest}; use threshold_bls::{ poly::{Idx as Index, Poly},