mirror of
https://github.com/arnaucube/babyjubjub-ark.git
synced 2026-01-13 17:21:29 +01:00
Add WASM compatibility throguh feature
This commit is contained in:
@@ -22,6 +22,7 @@ poseidon-ark = { git = "https://github.com/arnaucube/poseidon-ark" }
|
|||||||
# num-traits = "0.2.8"
|
# num-traits = "0.2.8"
|
||||||
blake-hash = {version="0.4.0", optional=true}
|
blake-hash = {version="0.4.0", optional=true}
|
||||||
blake = {version="2.0.1", optional=true}
|
blake = {version="2.0.1", optional=true}
|
||||||
|
blake2 = { version = "0.10", optional=true}
|
||||||
generic-array = "0.14"
|
generic-array = "0.14"
|
||||||
arrayref = "0.3.5"
|
arrayref = "0.3.5"
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
@@ -36,5 +37,7 @@ name = "bench_babyjubjub"
|
|||||||
harness = false
|
harness = false
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["blake-hash"]
|
default = ["blake-hash"] # compatible with circomlib
|
||||||
aarch64 = ["blake"]
|
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"]
|
||||||
|
|||||||
29
src/lib.rs
29
src/lib.rs
@@ -15,11 +15,25 @@ use ark_std::{rand::Rng, UniformRand};
|
|||||||
use poseidon_ark::Poseidon;
|
use poseidon_ark::Poseidon;
|
||||||
|
|
||||||
#[cfg(not(feature = "aarch64"))]
|
#[cfg(not(feature = "aarch64"))]
|
||||||
|
#[cfg(not(feature = "wasm"))]
|
||||||
use blake_hash::Digest; // compatible version with Blake used at circomlib
|
use blake_hash::Digest; // compatible version with Blake used at circomlib
|
||||||
|
|
||||||
|
#[cfg(not(feature = "wasm"))]
|
||||||
#[cfg(feature = "aarch64")]
|
#[cfg(feature = "aarch64")]
|
||||||
extern crate blake; // compatible version with Blake used at circomlib
|
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 generic_array::GenericArray;
|
||||||
|
|
||||||
use ark_ff::fields::{Fp256, MontBackend, MontConfig};
|
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 = "aarch64"))]
|
||||||
|
#[cfg(not(feature = "wasm"))]
|
||||||
fn blh(b: &[u8]) -> Vec<u8> {
|
fn blh(b: &[u8]) -> Vec<u8> {
|
||||||
let hash = blake_hash::Blake512::digest(b);
|
let hash = blake_hash::Blake512::digest(b);
|
||||||
hash.to_vec()
|
hash.to_vec()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "wasm"))]
|
||||||
#[cfg(feature = "aarch64")]
|
#[cfg(feature = "aarch64")]
|
||||||
fn blh(b: &[u8]) -> Vec<u8> {
|
fn blh(b: &[u8]) -> Vec<u8> {
|
||||||
let mut hash = [0; 64];
|
let mut hash = [0; 64];
|
||||||
@@ -241,6 +257,15 @@ fn blh(b: &[u8]) -> Vec<u8> {
|
|||||||
hash.to_vec()
|
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)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Signature {
|
pub struct Signature {
|
||||||
pub r_b8: Point,
|
pub r_b8: Point,
|
||||||
@@ -347,8 +372,8 @@ impl PrivateKey {
|
|||||||
let mut s = self.scalar_key() * Fr::from(8_u8);
|
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(to_hex(&hm).as_bytes(), 16).unwrap();
|
||||||
// let hm_b = BigInt::parse_bytes(&hm.into_bigint().to_bytes_be(), 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());
|
let hm_Fr = Fr::from_le_bytes_mod_order(&hm.into_bigint().to_bytes_le());
|
||||||
s = hm_b * s;
|
s = hm_Fr * s;
|
||||||
s = r + s;
|
s = r + s;
|
||||||
// s %= &SUBORDER.clone();
|
// s %= &SUBORDER.clone();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user