Browse Source

wasm blake hash compatability (but circom compatability broken in wasm still)

pull/8/head
Nanak Nihal Khalsa 4 months ago
parent
commit
5064685224
2 changed files with 35 additions and 11 deletions
  1. +12
    -5
      Cargo.toml
  2. +23
    -6
      src/lib.rs

+ 12
- 5
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"]

+ 23
- 6
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<u8> {
#[cfg(not(any( target_arch = "aarch64", target_arch = "wasm32" )))]
pub fn blh(b: &[u8]) -> Vec<u8> {
let hash = blake_hash::Blake512::digest(b);
hash.to_vec()
}
#[cfg(feature = "aarch64")]
fn blh(b: &[u8]) -> Vec<u8> {
#[cfg(target_arch = "aarch64")]
pub fn blh(b: &[u8]) -> Vec<u8> {
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<u8> {
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())
}

Loading…
Cancel
Save