From bf617de79d53e1a76f8a6146bbe74aaac8967cc4 Mon Sep 17 00:00:00 2001 From: arnaucube Date: Mon, 15 May 2023 20:43:00 +0200 Subject: [PATCH] Add WASM compatibility throguh feature --- Cargo.toml | 7 +++++-- src/lib.rs | 29 +++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 23050e7..4252216 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ poseidon-ark = { git = "https://github.com/arnaucube/poseidon-ark" } # 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"] diff --git a/src/lib.rs b/src/lib.rs index a175d48..fc94f5e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,11 +15,25 @@ use ark_std::{rand::Rng, UniformRand}; 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; use ark_ff::fields::{Fp256, MontBackend, MontConfig}; @@ -229,11 +243,13 @@ pub fn test_bit(b: &[u8], i: usize) -> bool { // } #[cfg(not(feature = "aarch64"))] +#[cfg(not(feature = "wasm"))] fn blh(b: &[u8]) -> Vec { let hash = blake_hash::Blake512::digest(b); hash.to_vec() } +#[cfg(not(feature = "wasm"))] #[cfg(feature = "aarch64")] fn blh(b: &[u8]) -> Vec { let mut hash = [0; 64]; @@ -241,6 +257,15 @@ fn blh(b: &[u8]) -> Vec { hash.to_vec() } +#[cfg(not(feature = "aarch64"))] +#[cfg(feature = "wasm")] +fn blh(b: &[u8]) -> Vec { + // not-compatible with circomlib implementation, but using Blake2b + let mut hasher = Blake2b512::new(); + hasher.update(b); + hasher.finalize().to_vec() +} + #[derive(Debug, Clone)] pub struct Signature { pub r_b8: Point, @@ -347,8 +372,8 @@ impl PrivateKey { 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();