Browse Source

Merge pull request #3 from nicolas-geniteau/update-deps

Update rust to 2021 and deps
pull/4/head
arnaucube 2 years ago
committed by GitHub
parent
commit
4793de7d24
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 50 deletions
  1. +9
    -10
      Cargo.toml
  2. +14
    -36
      src/lib.rs
  3. +0
    -4
      src/utils.rs

+ 9
- 10
Cargo.toml

@ -1,31 +1,30 @@
[package] [package]
name = "babyjubjub-rs" name = "babyjubjub-rs"
version = "0.0.8"
version = "0.0.9"
authors = ["arnaucube <root@arnaucube.com>"] authors = ["arnaucube <root@arnaucube.com>"]
edition = "2018"
edition = "2021"
license = "GPL-3.0" license = "GPL-3.0"
description = "BabyJubJub elliptic curve implementation" description = "BabyJubJub elliptic curve implementation"
repository = "https://github.com/arnaucube/babyjubjub-rs" repository = "https://github.com/arnaucube/babyjubjub-rs"
readme = "README.md" readme = "README.md"
[dependencies] [dependencies]
ff = {package="ff_ce" , version="0.11", features = ["derive"]}
rand = "0.4"
rand6 = {package="rand", version="0.6.5"}
num = "0.2.0"
num-bigint = {version = "0.2.2", features = ["rand"]}
ff = {package="ff_ce" , version= "0.11", features = ["derive"]}
rand = "0.8"
num = "0.4"
num-bigint = {version = "0.4", features = ["rand"]}
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}
generic-array = "0.13.2"
tiny-keccak = "1.5"
rustc-hex = "1.0.0"
generic-array = "0.14"
poseidon-rs = "0.0.8" poseidon-rs = "0.0.8"
arrayref = "0.3.5" arrayref = "0.3.5"
lazy_static = "1.4.0" lazy_static = "1.4.0"
[dev-dependencies] [dev-dependencies]
criterion = "0.3" criterion = "0.3"
hex = "0.4"
[[bench]] [[bench]]
name = "bench_babyjubjub" name = "bench_babyjubjub"

+ 14
- 36
src/lib.rs

@ -1,32 +1,16 @@
// BabyJubJub elliptic curve implementation in Rust. // BabyJubJub elliptic curve implementation in Rust.
// For LICENSE check https://github.com/arnaucube/babyjubjub-rs // For LICENSE check https://github.com/arnaucube/babyjubjub-rs
extern crate ff;
extern crate rand;
use ff::*; use ff::*;
use poseidon_rs::Poseidon; use poseidon_rs::Poseidon;
pub type Fr = poseidon_rs::Fr; // alias pub type Fr = poseidon_rs::Fr; // alias
#[macro_use]
extern crate arrayref;
extern crate generic_array;
extern crate num;
extern crate num_bigint;
extern crate num_traits;
use arrayref::array_ref;
extern crate rand6;
// use blake2::{Blake2b, Digest};
#[cfg(feature = "default")]
extern crate blake_hash; // compatible version with Blake used at circomlib
#[cfg(feature = "default")] #[cfg(feature = "default")]
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};
@ -36,8 +20,7 @@ use generic_array::GenericArray;
pub mod utils; pub mod utils;
#[macro_use]
extern crate lazy_static;
use lazy_static::lazy_static;
lazy_static! { lazy_static! {
static ref D: Fr = Fr::from_str("168696").unwrap(); static ref D: Fr = Fr::from_str("168696").unwrap();
@ -169,7 +152,7 @@ impl Point {
let mut exp: PointProjective = self.projective(); let mut exp: PointProjective = self.projective();
let (_, b) = n.to_bytes_le(); let (_, b) = n.to_bytes_le();
for i in 0..n.bits() { for i in 0..n.bits() {
if test_bit(&b, i) {
if test_bit(&b, i.try_into().unwrap()) {
r = r.add(&exp); r = r.add(&exp);
} }
exp = exp.add(&exp); exp = exp.add(&exp);
@ -356,7 +339,7 @@ impl PrivateKey {
#[allow(clippy::many_single_char_names)] #[allow(clippy::many_single_char_names)]
pub fn sign_schnorr(&self, m: BigInt) -> Result<(Point, BigInt), String> { pub fn sign_schnorr(&self, m: BigInt) -> Result<(Point, BigInt), String> {
// random r // random r
let mut rng = rand6::thread_rng();
let mut rng = rand::thread_rng();
let k = rng.gen_biguint(1024).to_bigint().unwrap(); let k = rng.gen_biguint(1024).to_bigint().unwrap();
// r = k·G // r = k·G
@ -398,7 +381,7 @@ pub fn verify_schnorr(pk: Point, m: BigInt, r: Point, s: BigInt) -> Result
pub fn new_key() -> PrivateKey { pub fn new_key() -> PrivateKey {
// https://tools.ietf.org/html/rfc8032#section-5.1.5 // https://tools.ietf.org/html/rfc8032#section-5.1.5
let mut rng = rand6::thread_rng();
let mut rng = rand::thread_rng();
let sk_raw = rng.gen_biguint(1024).to_bigint().unwrap(); let sk_raw = rng.gen_biguint(1024).to_bigint().unwrap();
let (_, sk_raw_bytes) = sk_raw.to_bytes_be(); let (_, sk_raw_bytes) = sk_raw.to_bytes_be();
PrivateKey::import(sk_raw_bytes[..32].to_vec()).unwrap() PrivateKey::import(sk_raw_bytes[..32].to_vec()).unwrap()
@ -426,9 +409,8 @@ pub fn verify(pk: Point, sig: Signature, msg: BigInt) -> bool {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
extern crate rustc_hex;
use rand6::Rng;
use rustc_hex::{FromHex, ToHex};
use rand::Rng;
use ::hex;
#[test] #[test]
fn test_add_same_point() { fn test_add_same_point() {
@ -598,7 +580,7 @@ mod tests {
}; };
let p_comp = p.compress(); let p_comp = p.compress();
assert_eq!( assert_eq!(
p_comp[..].to_hex(),
hex::encode(p_comp),
"53b81ed5bffe9545b54016234682e7b2f699bd42a5e9eae27ff4051bc698ce85" "53b81ed5bffe9545b54016234682e7b2f699bd42a5e9eae27ff4051bc698ce85"
); );
let p2 = decompress_point(p_comp).unwrap(); let p2 = decompress_point(p_comp).unwrap();
@ -608,15 +590,13 @@ mod tests {
#[test] #[test]
fn test_point_decompress0() { fn test_point_decompress0() {
let y_bytes_raw = "b5328f8791d48f20bec6e481d91c7ada235f1facf22547901c18656b6c3e042f"
.from_hex()
let y_bytes_raw = hex::decode("b5328f8791d48f20bec6e481d91c7ada235f1facf22547901c18656b6c3e042f")
.unwrap(); .unwrap();
let mut y_bytes: [u8; 32] = [0; 32]; let mut y_bytes: [u8; 32] = [0; 32];
y_bytes.copy_from_slice(&y_bytes_raw); y_bytes.copy_from_slice(&y_bytes_raw);
let p = decompress_point(y_bytes).unwrap(); let p = decompress_point(y_bytes).unwrap();
let expected_px_raw = "b86cc8d9c97daef0afe1a4753c54fb2d8a530dc74c7eee4e72b3fdf2496d2113"
.from_hex()
let expected_px_raw = hex::decode("b86cc8d9c97daef0afe1a4753c54fb2d8a530dc74c7eee4e72b3fdf2496d2113")
.unwrap(); .unwrap();
let mut e_px_bytes: [u8; 32] = [0; 32]; let mut e_px_bytes: [u8; 32] = [0; 32];
e_px_bytes.copy_from_slice(&expected_px_raw); e_px_bytes.copy_from_slice(&expected_px_raw);
@ -627,15 +607,13 @@ mod tests {
#[test] #[test]
fn test_point_decompress1() { fn test_point_decompress1() {
let y_bytes_raw = "70552d3ff548e09266ded29b33ce75139672b062b02aa66bb0d9247ffecf1d0b"
.from_hex()
let y_bytes_raw = hex::decode("70552d3ff548e09266ded29b33ce75139672b062b02aa66bb0d9247ffecf1d0b")
.unwrap(); .unwrap();
let mut y_bytes: [u8; 32] = [0; 32]; let mut y_bytes: [u8; 32] = [0; 32];
y_bytes.copy_from_slice(&y_bytes_raw); y_bytes.copy_from_slice(&y_bytes_raw);
let p = decompress_point(y_bytes).unwrap(); let p = decompress_point(y_bytes).unwrap();
let expected_px_raw = "30f1635ba7d56f9cb32c3ffbe6dca508a68c7f43936af11a23c785ce98cb3404"
.from_hex()
let expected_px_raw = hex::decode("30f1635ba7d56f9cb32c3ffbe6dca508a68c7f43936af11a23c785ce98cb3404")
.unwrap(); .unwrap();
let mut e_px_bytes: [u8; 32] = [0; 32]; let mut e_px_bytes: [u8; 32] = [0; 32];
e_px_bytes.copy_from_slice(&expected_px_raw); e_px_bytes.copy_from_slice(&expected_px_raw);
@ -647,7 +625,7 @@ mod tests {
#[test] #[test]
fn test_point_decompress_loop() { fn test_point_decompress_loop() {
for _ in 0..5 { for _ in 0..5 {
let random_bytes = rand6::thread_rng().gen::<[u8; 32]>();
let random_bytes = rand::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: Vec<u8> = blh(&sk_raw_bytes); let mut h: Vec<u8> = blh(&sk_raw_bytes);
@ -706,7 +684,7 @@ mod tests {
// test blake compatible with circomlib implementation // test blake compatible with circomlib implementation
let h: Vec<u8> = blh(&sk_raw_bytes); let h: Vec<u8> = blh(&sk_raw_bytes);
assert_eq!(h.to_hex(), "c992db23d6290c70ffcc02f7abeb00b9d00fa8b43e55d7949c28ba6be7545d3253882a61bd004a236ef1cdba01b27ba0aedfb08eefdbfb7c19657c880b43ddf1");
assert_eq!(hex::encode(h), "c992db23d6290c70ffcc02f7abeb00b9d00fa8b43e55d7949c28ba6be7545d3253882a61bd004a236ef1cdba01b27ba0aedfb08eefdbfb7c19657c880b43ddf1");
// test private key // test private key
let sk = PrivateKey::import( let sk = PrivateKey::import(

+ 0
- 4
src/utils.rs

@ -1,10 +1,6 @@
// BabyJubJub elliptic curve implementation in Rust. // BabyJubJub elliptic curve implementation in Rust.
// For LICENSE check https://github.com/arnaucube/babyjubjub-rs // For LICENSE check https://github.com/arnaucube/babyjubjub-rs
extern crate num;
extern crate num_bigint;
extern crate num_traits;
use num_bigint::{BigInt, ToBigInt}; use num_bigint::{BigInt, ToBigInt};
use num_traits::{One, Zero}; use num_traits::{One, Zero};

Loading…
Cancel
Save