Add WASM compatibility throguh feature
arnaucube root@arnaucube.com
Mon, 15 May 2023 20:43:00 +0200
2 files changed,
32 insertions(+),
4 deletions(-)
M
Cargo.toml
→
Cargo.toml
@@ -22,6 +22,7 @@ # num-bigint = {version = "0.4", features = ["rand"]}
# num-traits = "0.2.8" blake-hash = {version="0.4.0", optional=true} blake = {version="2.0.1", optional=true} +blake2 = { version = "0.10", optional=true} generic-array = "0.14" arrayref = "0.3.5" lazy_static = "1.4.0"@@ -36,5 +37,7 @@ name = "bench_babyjubjub"
harness = false [features] -default = ["blake-hash"] -aarch64 = ["blake"] +default = ["blake-hash"] # compatible with circomlib +aarch64 = ["blake"] # compatible with circomlib +wasm = ["blake2"] # Warning: this feature is not compatible with the circomlib key generation (meaning that same secret keys will lead to different public keys. But the signatures are compatible with circomlib & circomlibjs. +# wasm = ["blake-hash"]
M
src/lib.rs
→
src/lib.rs
@@ -15,10 +15,24 @@
use poseidon_ark::Poseidon; #[cfg(not(feature = "aarch64"))] +#[cfg(not(feature = "wasm"))] use blake_hash::Digest; // compatible version with Blake used at circomlib +#[cfg(not(feature = "wasm"))] #[cfg(feature = "aarch64")] extern crate blake; // compatible version with Blake used at circomlib + +#[cfg(not(feature = "aarch64"))] +#[cfg(feature = "wasm")] +extern crate blake2; // non-compatible version with Blake used at circomlib + +#[cfg(not(feature = "aarch64"))] +#[cfg(feature = "wasm")] +use blake2::digest::Digest; + +#[cfg(not(feature = "aarch64"))] +#[cfg(feature = "wasm")] +use blake2::Blake2b512; use generic_array::GenericArray;@@ -229,16 +243,27 @@ // Ok(Point { x: x_fr, y: y_fr })
// } #[cfg(not(feature = "aarch64"))] +#[cfg(not(feature = "wasm"))] fn blh(b: &[u8]) -> Vec<u8> { let hash = blake_hash::Blake512::digest(b); hash.to_vec() } +#[cfg(not(feature = "wasm"))] #[cfg(feature = "aarch64")] fn blh(b: &[u8]) -> Vec<u8> { let mut hash = [0; 64]; blake::hash(512, b, &mut hash).unwrap(); hash.to_vec() +} + +#[cfg(not(feature = "aarch64"))] +#[cfg(feature = "wasm")] +fn blh(b: &[u8]) -> Vec<u8> { + // not-compatible with circomlib implementation, but using Blake2b + let mut hasher = Blake2b512::new(); + hasher.update(b); + hasher.finalize().to_vec() } #[derive(Debug, Clone)]@@ -347,8 +372,8 @@ // let mut s = &self.scalar_key() << 3;
let mut s = self.scalar_key() * Fr::from(8_u8); // let hm_b = BigInt::parse_bytes(to_hex(&hm).as_bytes(), 16).unwrap(); // let hm_b = BigInt::parse_bytes(&hm.into_bigint().to_bytes_be(), 16).unwrap(); - let hm_b = Fr::from_le_bytes_mod_order(&hm.into_bigint().to_bytes_le()); - s = hm_b * s; + let hm_Fr = Fr::from_le_bytes_mod_order(&hm.into_bigint().to_bytes_le()); + s = hm_Fr * s; s = r + s; // s %= &SUBORDER.clone();