From 506468522460a60680c53605ac48059a3106f348 Mon Sep 17 00:00:00 2001 From: Nanak Nihal Khalsa Date: Wed, 27 Dec 2023 14:58:33 -0500 Subject: [PATCH] wasm blake hash compatability (but circom compatability broken in wasm still) --- Cargo.toml | 17 ++++++++++++----- src/lib.rs | 29 +++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d4a594a..e6d1362 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,8 +16,6 @@ rand = "0.4.6" num = "0.4" num-bigint = {version = "0.4", features = ["rand", "serde"]} num-traits = "0.2.8" -blake-hash = {version="0.4.0", optional=true} -blake = {version="2.0.1", optional=true} generic-array = "0.14" poseidon-rs = "0.0.8" arrayref = "0.3.5" @@ -34,6 +32,15 @@ hex = "0.4" name = "bench_babyjubjub" harness = false -[features] -default = ["blake-hash"] -aarch64 = ["blake"] +[target.'cfg(not(any( target_arch = "aarch64", target_arch = "wasm32" )))'.dependencies] +blake-hash = {version="0.4.1" } + +[target.'cfg(target_arch = "aarch64")'.dependencies] +blake = { version = "2.0.1" } + +[target.'cfg(target_arch = "wasm32")'.dependencies] +blake2 = { version = "0.10.6" } + +# [features] +# default = ["blake-hash"] +# aarch64 = ["blake"] diff --git a/src/lib.rs b/src/lib.rs index bcd705a..7c7f113 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,12 +21,17 @@ pub struct Fl(FpRepr); use arrayref::array_ref; -#[cfg(not(feature = "aarch64"))] +#[cfg(not(any( target_arch = "aarch64", target_arch = "wasm32" )))] use blake_hash::Digest; // compatible version with Blake used at circomlib -#[cfg(feature = "aarch64")] +#[cfg( target_arch = "aarch64" )] extern crate blake; // compatible version with Blake used at circomlib + +#[cfg( target_arch = "wasm32" )] +use blake2::{Blake2b512, Blake2s256, Digest}; // NOT compatible with circomlib but it works on WASM + + use std::{cmp::min, str::FromStr}; use num_bigint::{BigInt, RandBigInt, Sign, ToBigInt}; @@ -465,19 +470,29 @@ pub fn decompress_point(bb: [u8; 32]) -> Result { Ok(Point { x: x_fr, y: y_fr }) } -#[cfg(not(feature = "aarch64"))] -fn blh(b: &[u8]) -> Vec { +#[cfg(not(any( target_arch = "aarch64", target_arch = "wasm32" )))] +pub fn blh(b: &[u8]) -> Vec { let hash = blake_hash::Blake512::digest(b); hash.to_vec() } -#[cfg(feature = "aarch64")] -fn blh(b: &[u8]) -> Vec { +#[cfg(target_arch = "aarch64")] +pub fn blh(b: &[u8]) -> Vec { let mut hash = [0; 64]; blake::hash(512, b, &mut hash).unwrap(); hash.to_vec() } +#[cfg(target_arch = "wasm32")] +/// This is incompatible with the circom version +/// TODO: find a BLAKE-512 that works on WASM +pub fn blh(b: &[u8]) -> Vec { + let mut hasher = Blake2b512::new(); + hasher.update(b); + hasher.finalize().to_vec() +} +// #[cfg(target_arch = "wasm32")] + #[derive(Debug, Clone, Serialize)] pub struct Signature { pub r_b8: Point, @@ -549,6 +564,8 @@ impl PrivateKey { } pub fn public(&self) -> Point { + println!("calling public"); + println!("scalar key {}", &self.scalar_key()); B8.mul_scalar(&self.scalar_key()) }