|
@ -1,3 +1,5 @@ |
|
|
|
|
|
// For LICENSE check https://github.com/arnaucube/babyjubjub-rs
|
|
|
|
|
|
|
|
|
extern crate rand;
|
|
|
extern crate rand;
|
|
|
#[macro_use]
|
|
|
#[macro_use]
|
|
|
extern crate ff;
|
|
|
extern crate ff;
|
|
@ -17,10 +19,16 @@ extern crate rand6; |
|
|
use rand6::Rng;
|
|
|
use rand6::Rng;
|
|
|
|
|
|
|
|
|
// use blake2::{Blake2b, Digest};
|
|
|
// use blake2::{Blake2b, Digest};
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "default")]
|
|
|
extern crate blake_hash; // compatible version with Blake used at circomlib
|
|
|
extern crate blake_hash; // compatible version with Blake used at circomlib
|
|
|
|
|
|
#[cfg(feature = "default")]
|
|
|
#[macro_use]
|
|
|
#[macro_use]
|
|
|
use blake_hash::Digest;
|
|
|
use blake_hash::Digest;
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "aarch64")]
|
|
|
|
|
|
extern crate blake; // compatible version with Blake used at circomlib
|
|
|
|
|
|
|
|
|
use std::cmp::min;
|
|
|
use std::cmp::min;
|
|
|
|
|
|
|
|
|
use num_bigint::{BigInt, RandBigInt, Sign, ToBigInt};
|
|
|
use num_bigint::{BigInt, RandBigInt, Sign, ToBigInt};
|
|
@ -232,6 +240,19 @@ pub fn decompress_point(bb: [u8; 32]) -> Result { |
|
|
Ok(Point { x: x_fr, y: y_fr })
|
|
|
Ok(Point { x: x_fr, y: y_fr })
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "default")]
|
|
|
|
|
|
fn blh(b: &Vec<u8>) -> Vec<u8> {
|
|
|
|
|
|
let hash = blake_hash::Blake512::digest(&b);
|
|
|
|
|
|
hash.to_vec()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "aarch64")]
|
|
|
|
|
|
fn blh(b: &Vec<u8>) -> Vec<u8> {
|
|
|
|
|
|
let mut hash = [0; 64];
|
|
|
|
|
|
blake::hash(512, b, &mut hash).unwrap();
|
|
|
|
|
|
hash.to_vec()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
#[derive(Debug, Clone)]
|
|
|
pub struct Signature {
|
|
|
pub struct Signature {
|
|
|
r_b8: Point,
|
|
|
r_b8: Point,
|
|
@ -287,7 +308,7 @@ impl PrivateKey { |
|
|
// let mut h = hasher.finalize();
|
|
|
// let mut h = hasher.finalize();
|
|
|
|
|
|
|
|
|
// compatible with circomlib implementation
|
|
|
// compatible with circomlib implementation
|
|
|
let hash = blake_hash::Blake512::digest(&self.key.to_vec());
|
|
|
|
|
|
|
|
|
let mut hash: Vec<u8> = blh(&self.key.to_vec());
|
|
|
let mut h: Vec<u8> = hash[..32].to_vec();
|
|
|
let mut h: Vec<u8> = hash[..32].to_vec();
|
|
|
|
|
|
|
|
|
h[0] = h[0] & 0xF8;
|
|
|
h[0] = h[0] & 0xF8;
|
|
@ -312,7 +333,7 @@ impl PrivateKey { |
|
|
// let mut hasher = Blake2b::new();
|
|
|
// let mut hasher = Blake2b::new();
|
|
|
// hasher.update(sk_bytes);
|
|
|
// hasher.update(sk_bytes);
|
|
|
// let mut h = hasher.finalize(); // h: hash(sk), s: h[32:64]
|
|
|
// let mut h = hasher.finalize(); // h: hash(sk), s: h[32:64]
|
|
|
let mut h = blake_hash::Blake512::digest(&self.key);
|
|
|
|
|
|
|
|
|
let mut h: Vec<u8> = blh(&self.key.to_vec());
|
|
|
|
|
|
|
|
|
let (_, msg_bytes) = msg.to_bytes_le();
|
|
|
let (_, msg_bytes) = msg.to_bytes_le();
|
|
|
let mut msg32: [u8; 32] = [0; 32];
|
|
|
let mut msg32: [u8; 32] = [0; 32];
|
|
@ -322,7 +343,7 @@ impl PrivateKey { |
|
|
// https://tools.ietf.org/html/rfc8032#section-5.1.6
|
|
|
// https://tools.ietf.org/html/rfc8032#section-5.1.6
|
|
|
let s = GenericArray::<u8, generic_array::typenum::U32>::from_mut_slice(&mut h[32..64]);
|
|
|
let s = GenericArray::<u8, generic_array::typenum::U32>::from_mut_slice(&mut h[32..64]);
|
|
|
let r_bytes = utils::concatenate_arrays(s, &msg32);
|
|
|
let r_bytes = utils::concatenate_arrays(s, &msg32);
|
|
|
let r_hashed = blake_hash::Blake512::digest(&r_bytes);
|
|
|
|
|
|
|
|
|
let r_hashed: Vec<u8> = blh(&r_bytes);
|
|
|
let mut r = BigInt::from_bytes_le(Sign::Plus, &r_hashed[..]);
|
|
|
let mut r = BigInt::from_bytes_le(Sign::Plus, &r_hashed[..]);
|
|
|
r = utils::modulus(&r, &SUBORDER);
|
|
|
r = utils::modulus(&r, &SUBORDER);
|
|
|
let r8: Point = B8.mul_scalar(&r);
|
|
|
let r8: Point = B8.mul_scalar(&r);
|
|
@ -644,7 +665,7 @@ mod tests { |
|
|
let random_bytes = rand6::thread_rng().gen::<[u8; 32]>();
|
|
|
let random_bytes = rand6::thread_rng().gen::<[u8; 32]>();
|
|
|
let sk_raw: BigInt = BigInt::from_bytes_le(Sign::Plus, &random_bytes[..]);
|
|
|
let sk_raw: BigInt = BigInt::from_bytes_le(Sign::Plus, &random_bytes[..]);
|
|
|
let (_, sk_raw_bytes) = sk_raw.to_bytes_be();
|
|
|
let (_, sk_raw_bytes) = sk_raw.to_bytes_be();
|
|
|
let mut h = blake_hash::Blake512::digest(&sk_raw_bytes);
|
|
|
|
|
|
|
|
|
let mut h: Vec<u8> = blh(&sk_raw_bytes);
|
|
|
|
|
|
|
|
|
h[0] = h[0] & 0xF8;
|
|
|
h[0] = h[0] & 0xF8;
|
|
|
h[31] = h[31] & 0x7F;
|
|
|
h[31] = h[31] & 0x7F;
|
|
@ -699,7 +720,7 @@ mod tests { |
|
|
.unwrap();
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
// test blake compatible with circomlib implementation
|
|
|
// test blake compatible with circomlib implementation
|
|
|
let h = blake_hash::Blake512::digest(&sk_raw_bytes);
|
|
|
|
|
|
|
|
|
let mut h: Vec<u8> = blh(&sk_raw_bytes);
|
|
|
assert_eq!(h.to_hex(), "c992db23d6290c70ffcc02f7abeb00b9d00fa8b43e55d7949c28ba6be7545d3253882a61bd004a236ef1cdba01b27ba0aedfb08eefdbfb7c19657c880b43ddf1");
|
|
|
assert_eq!(h.to_hex(), "c992db23d6290c70ffcc02f7abeb00b9d00fa8b43e55d7949c28ba6be7545d3253882a61bd004a236ef1cdba01b27ba0aedfb08eefdbfb7c19657c880b43ddf1");
|
|
|
|
|
|
|
|
|
// test private key
|
|
|
// test private key
|
|
|