Update aarch64 feature related code & minor update - Update aarch64 feature related code - Update benchmarks to last lib updates - Apply Clippy fixes
arnaucube root@arnaucube.com
Fri, 09 Sep 2022 15:13:05 +0200
5 files changed,
35 insertions(+),
26 deletions(-)
M
.github/workflows/test.yml
→
.github/workflows/test.yml
@@ -12,4 +12,4 @@ run: cargo build --verbose
- name: Run tests run: | cargo test --verbose - cargo test --verbose --no-default-features --features=aarch64 + cargo test --verbose --features=aarch64
M
Cargo.toml
→
Cargo.toml
@@ -1,6 +1,6 @@
[package] name = "babyjubjub-rs" -version = "0.0.9" +version = "0.0.10" authors = ["arnaucube <root@arnaucube.com>"] edition = "2021" license = "GPL-3.0"
M
benches/bench_babyjubjub.rs
→
benches/bench_babyjubjub.rs
@@ -9,7 +9,7 @@ extern crate num;
extern crate num_bigint; use num_bigint::{BigInt, ToBigInt}; -use babyjubjub_rs::{utils, Point}; +use babyjubjub_rs::Point; fn criterion_benchmark(c: &mut Criterion) { let p: Point = Point {@@ -44,7 +44,7 @@ b.iter(|| babyjubjub_rs::decompress_point(p_comp))
}); let sk = babyjubjub_rs::new_key(); - let pk = sk.public().unwrap(); + let pk = sk.public(); let msg = 5.to_bigint().unwrap(); c.bench_function("sign", |b| b.iter(|| sk.sign(msg.clone()))); let sig = sk.sign(msg.clone()).unwrap();
M
src/lib.rs
→
src/lib.rs
@@ -8,8 +8,11 @@ pub type Fr = poseidon_rs::Fr; // alias
use arrayref::array_ref; -#[cfg(feature = "default")] -use blake_hash::Digest; +#[cfg(not(feature = "aarch64"))] +use blake_hash::Digest; // compatible version with Blake used at circomlib + +#[cfg(feature = "aarch64")] +extern crate blake; // compatible version with Blake used at circomlib use std::cmp::min;@@ -220,14 +223,14 @@ let y_fr: Fr = Fr::from_str(&y.to_string()).unwrap();
Ok(Point { x: x_fr, y: y_fr }) } -#[cfg(feature = "default")] +#[cfg(not(feature = "aarch64"))] fn blh(b: &[u8]) -> Vec<u8> { - let hash = blake_hash::Blake512::digest(&b); + let hash = blake_hash::Blake512::digest(b); hash.to_vec() } #[cfg(feature = "aarch64")] -fn blh(b: &Vec<u8>) -> Vec<u8> { +fn blh(b: &[u8]) -> Vec<u8> { let mut hash = [0; 64]; blake::hash(512, b, &mut hash).unwrap(); hash.to_vec()@@ -285,9 +288,11 @@ // hasher.update(sk_raw_bytes);
// let mut h = hasher.finalize(); // compatible with circomlib implementation - let hash: Vec<u8> = blh(&self.key.to_vec()); + let hash: Vec<u8> = blh(&self.key); let mut h: Vec<u8> = hash[..32].to_vec(); + // prune buffer following RFC 8032 + // https://tools.ietf.org/html/rfc8032#page-13 h[0] &= 0xF8; h[31] &= 0x7F; h[31] |= 0x40;@@ -308,7 +313,7 @@ // let (_, sk_bytes) = self.key.to_bytes_le();
// let mut hasher = Blake2b::new(); // hasher.update(sk_bytes); // let mut h = hasher.finalize(); // h: hash(sk), s: h[32:64] - let mut h: Vec<u8> = blh(&self.key.to_vec()); + let mut h: Vec<u8> = blh(&self.key); let (_, msg_bytes) = msg.to_bytes_le(); let mut msg32: [u8; 32] = [0; 32];@@ -346,7 +351,7 @@ // r = k·G
let r = B8.mul_scalar(&k); // h = H(x, r, m) - let pk = &self.public(); + let pk = self.public(); let h = schnorr_hash(&pk, m, &r)?; // s= k+x·h@@ -409,8 +414,8 @@
#[cfg(test)] mod tests { use super::*; + use ::hex; use rand::Rng; - use ::hex; #[test] fn test_add_same_point() {@@ -590,14 +595,16 @@ }
#[test] fn test_point_decompress0() { - let y_bytes_raw = hex::decode("b5328f8791d48f20bec6e481d91c7ada235f1facf22547901c18656b6c3e042f") - .unwrap(); + let y_bytes_raw = + hex::decode("b5328f8791d48f20bec6e481d91c7ada235f1facf22547901c18656b6c3e042f") + .unwrap(); let mut y_bytes: [u8; 32] = [0; 32]; y_bytes.copy_from_slice(&y_bytes_raw); let p = decompress_point(y_bytes).unwrap(); - let expected_px_raw = hex::decode("b86cc8d9c97daef0afe1a4753c54fb2d8a530dc74c7eee4e72b3fdf2496d2113") - .unwrap(); + let expected_px_raw = + hex::decode("b86cc8d9c97daef0afe1a4753c54fb2d8a530dc74c7eee4e72b3fdf2496d2113") + .unwrap(); let mut e_px_bytes: [u8; 32] = [0; 32]; e_px_bytes.copy_from_slice(&expected_px_raw); let expected_px: Fr =@@ -607,14 +614,16 @@ }
#[test] fn test_point_decompress1() { - let y_bytes_raw = hex::decode("70552d3ff548e09266ded29b33ce75139672b062b02aa66bb0d9247ffecf1d0b") - .unwrap(); + let y_bytes_raw = + hex::decode("70552d3ff548e09266ded29b33ce75139672b062b02aa66bb0d9247ffecf1d0b") + .unwrap(); let mut y_bytes: [u8; 32] = [0; 32]; y_bytes.copy_from_slice(&y_bytes_raw); let p = decompress_point(y_bytes).unwrap(); - let expected_px_raw = hex::decode("30f1635ba7d56f9cb32c3ffbe6dca508a68c7f43936af11a23c785ce98cb3404") - .unwrap(); + let expected_px_raw = + hex::decode("30f1635ba7d56f9cb32c3ffbe6dca508a68c7f43936af11a23c785ce98cb3404") + .unwrap(); let mut e_px_bytes: [u8; 32] = [0; 32]; e_px_bytes.copy_from_slice(&expected_px_raw); let expected_px: Fr =
M
src/utils.rs
→
src/utils.rs
@@ -115,10 +115,10 @@ // -> section 6
let zero: BigInt = Zero::zero(); let one: BigInt = One::one(); - if legendre_symbol(&a, q) != 1 || a == &zero || q == &2.to_bigint().unwrap() { + if legendre_symbol(a, q) != 1 || a == &zero || q == &2.to_bigint().unwrap() { return Err("not a mod p square".to_string()); } else if q % 4.to_bigint().unwrap() == 3.to_bigint().unwrap() { - let r = a.modpow(&((q + one) / 4), &q); + let r = a.modpow(&((q + one) / 4), q); return Ok(r); }@@ -168,10 +168,10 @@ // This implementation is following this Python implementation by Dusk https://github.com/dusk-network/dusk-zerocaf/blob/master/tools/tonelli.py
let zero: BigInt = Zero::zero(); let one: BigInt = One::one(); - if legendre_symbol(&a, q) != 1 || a == &zero || q == &2.to_bigint().unwrap() { + if legendre_symbol(a, q) != 1 || a == &zero || q == &2.to_bigint().unwrap() { return Err("not a mod p square".to_string()); } else if q % 4.to_bigint().unwrap() == 3.to_bigint().unwrap() { - let r = a.modpow(&((q + one) / 4), &q); + let r = a.modpow(&((q + one) / 4), q); return Ok(r); }@@ -215,7 +215,7 @@
pub fn legendre_symbol(a: &BigInt, q: &BigInt) -> i32 { // returns 1 if has a square root modulo q let one: BigInt = One::one(); - let ls: BigInt = a.modpow(&((q - &one) >> 1), &q); + let ls: BigInt = a.modpow(&((q - &one) >> 1), q); if ls == q - one { return -1; }