Skip to content

Commit

Permalink
Updating criterion and num-bigint.
Browse files Browse the repository at this point in the history
  • Loading branch information
armfazh committed Feb 18, 2022
1 parent 6e0569d commit 5a8c09c
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 41 deletions.
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "redox-ecc"
version = "0.2.2"
version = "0.2.3"
authors = ["Armando Faz Hernandez"]
edition = "2018"
edition = "2021"
description = "elliptic curve arithmetic"
keywords = ["elliptic curve", "math", "crypto", "ecc", "elliptic", "weierstrass"]
categories = ["cryptography", "math"]
Expand All @@ -19,11 +19,11 @@ harness = false

[dependencies]
impl_ops = "0.1.1"
num-bigint = "0.2.6"
num-integer = "0.1.42"
num-traits = "0.2.11"
num-bigint = "0.4.3"
num-integer = "0.1.44"
num-traits = "0.2.14"
doc-comment = "0.3.3"
atomic_refcell = "0.1.6"
atomic_refcell = "0.1.8"

[dev-dependencies]
criterion = "0.3.2"
criterion = "0.3.5"
13 changes: 6 additions & 7 deletions benches/curve.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate num_bigint;
use crate::num_bigint::BigInt;

use criterion::{criterion_group, criterion_main, Benchmark, Criterion};
use criterion::{criterion_group, criterion_main, Criterion};

use redox_ecc::ellipticcurve::EllipticCurve;
use redox_ecc::instances::{GetCurve, P256, P384, P521};
Expand All @@ -12,12 +12,11 @@ fn arith(c: &mut Criterion) {
let mut g0 = ec.get_generator();
let mut g1 = g0.clone();
let k = ec.new_scalar(BigInt::from(-1));
c.bench(
format!("{}/ec", id).as_str(),
Benchmark::new("add", move |b| b.iter(|| g0 = &g0 + &g0))
.with_function("mul", move |b| b.iter(|| g1 = &k * &g1))
.sample_size(10),
);
let mut group = c.benchmark_group(format!("{}/ec", id).as_str());
group.sample_size(10);
group.bench_function("add", move |b| b.iter(|| g0 = &g0 + &g0));
group.bench_function("mul", move |b| b.iter(|| g1 = &k * &g1));
group.finish();
}
}

Expand Down
16 changes: 7 additions & 9 deletions benches/field.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use criterion::{criterion_group, criterion_main, Benchmark, Criterion};
use criterion::{criterion_group, criterion_main, Criterion};

use redox_ecc::ellipticcurve::EllipticCurve;
use redox_ecc::instances::{GetCurve, P256, P384, P521};
Expand All @@ -13,14 +13,12 @@ fn arith(c: &mut Criterion) {
let mut x2 = f.from(-1i64);
let y0 = f.from(15i64);
let y1 = f.from(15i64);

c.bench(
format!("{}/fp", id).as_str(),
Benchmark::new("add", move |b| b.iter(|| x0 = &x0 + &y0))
.with_function("mul", move |b| b.iter(|| x1 = &x1 * &y1))
.with_function("inv", move |b| b.iter(|| x2 = 1u32 / &x2))
.sample_size(10),
);
let mut group = c.benchmark_group(format!("{}/fp", id).as_str());
group.sample_size(10);
group.bench_function("add", move |b| b.iter(|| x0 = &x0 + &y0));
group.bench_function("mul", move |b| b.iter(|| x1 = &x1 * &y1));
group.bench_function("inv", move |b| b.iter(|| x2 = 1u32 / &x2));
group.finish();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/edwards/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Decode for Curve {
// based on https://tools.ietf.org/html/rfc8032#section-5.2.3
fn decode(&self, buf: &[u8]) -> Result<Self::Deser, Error> {
let modulus = self.get_field().get_modulus();
let size = (modulus.bits() + 1 + 7) / 8;
let size = (modulus.bits() as usize + 1 + 7) / 8;
// step 1
if buf.len() != size {
return Err(Error::new(ErrorKind::Other, "Wrong input buffer size."));
Expand Down Expand Up @@ -199,7 +199,7 @@ mod tests {
let modulus = ec.get_field().get_modulus();
let gen = ec.get_generator();
let ser = gen.encode(false); // compression does not exist
assert_eq!(ser.len(), (modulus.bits() + 1 + 7) / 8);
assert_eq!(ser.len(), (modulus.bits() as usize + 1 + 7) / 8);
let deser = ec.decode(&ser).unwrap();
assert!(
ec.is_on_curve(&deser),
Expand Down
2 changes: 1 addition & 1 deletion src/edwards/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Encode for Point {
let x_0 = (((x.sgn0() >> 1) & 0x01) << 7) as u8;
let mut enc = y.to_bytes_le();
let p = self.e.f.get_modulus();
let size = (p.bits() + 1 + 7) / 8;
let size = (p.bits() as usize + 1 + 7) / 8;
enc.resize(size, 0u8);
let last = enc.len() - 1;
enc[last] |= x_0;
Expand Down
6 changes: 3 additions & 3 deletions src/edwards/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl EcScalar for Scalar {}
impl Serialize for Scalar {
/// serializes the field element into big-endian bytes
fn to_bytes_be(&self) -> Vec<u8> {
let field_len = (self.r.bits() + 7) / 8;
let field_len = (self.r.bits() as usize + 7) / 8;
let mut bytes = self.k.to_biguint().unwrap().to_bytes_be();
let mut out = vec![0; field_len - bytes.len()];
if !out.is_empty() {
Expand Down Expand Up @@ -143,14 +143,14 @@ impl std::iter::Iterator for Iterino {

impl Scalar {
pub fn iter_lr(&self) -> impl std::iter::Iterator<Item = bool> {
let l = self.k.bits();
let l = self.k.bits() as usize;
let i = l - 1usize;
let (_, v) = self.k.to_u32_digits();
let is_lr = true;
Iterino { l, i, v, is_lr }
}
pub fn iter_rl(&self) -> impl std::iter::Iterator<Item = bool> {
let l = self.k.bits();
let l = self.k.bits() as usize;
let i = 0usize;
let (_, v) = self.k.to_u32_digits();
let is_lr = false;
Expand Down
4 changes: 2 additions & 2 deletions src/instances/edw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ impl std::fmt::Display for EdCurveID {
}

/// EDWARDS25519 is the edwards25519 elliptic curve as specified in RFC-7748.
pub static EDWARDS25519: EdCurveID = EdCurveID(&EDWARDS25519_PARAMS);
pub static EDWARDS25519: EdCurveID = EdCurveID(EDWARDS25519_PARAMS);
/// EDWARDS448 is the edwards448 elliptic curve as specified in RFC-7748.
pub static EDWARDS448: EdCurveID = EdCurveID(&EDWARDS448_PARAMS);
pub static EDWARDS448: EdCurveID = EdCurveID(EDWARDS448_PARAMS);

static EDWARDS25519_PARAMS: &Params = &Params {
name: "edwards25519",
Expand Down
2 changes: 1 addition & 1 deletion src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ macro_rules! do_if_eq {
if $cond {
$body
} else {
panic!($error)
panic!("{}", $error)
}
};
}
6 changes: 3 additions & 3 deletions src/montgomery/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl EcScalar for Scalar {}
impl Serialize for Scalar {
/// serializes the field element into big-endian bytes
fn to_bytes_be(&self) -> Vec<u8> {
let field_len = (self.r.bits() + 7) / 8;
let field_len = (self.r.bits() as usize + 7) / 8;
let mut bytes = self.k.to_biguint().unwrap().to_bytes_be();
let mut out = vec![0; field_len - bytes.len()];
if !out.is_empty() {
Expand Down Expand Up @@ -137,14 +137,14 @@ impl std::iter::Iterator for Iterino {

impl Scalar {
pub fn iter_lr(&self) -> impl std::iter::Iterator<Item = bool> {
let l = self.k.bits();
let l = self.k.bits() as usize;
let i = l - 1usize;
let (_, v) = self.k.to_u32_digits();
let is_lr = true;
Iterino { l, i, v, is_lr }
}
pub fn iter_rl(&self) -> impl std::iter::Iterator<Item = bool> {
let l = self.k.bits();
let l = self.k.bits() as usize;
let i = 0usize;
let (_, v) = self.k.to_u32_digits();
let is_lr = false;
Expand Down
2 changes: 1 addition & 1 deletion src/primefield/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Field for Fp {
self.0.p.clone()
}
fn size_bytes(&self) -> usize {
(self.0.p.bits() + 7) / 8
(self.0.p.bits() as usize + 7) / 8
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ use super::{private_version, version};

#[test]
fn uint_testing() {
assert_eq!(version(), "0.2.2");
assert_eq!(version(), "0.2.3");
assert_eq!(version(), private_version());
}
6 changes: 3 additions & 3 deletions src/weierstrass/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl EcScalar for Scalar {}
impl Serialize for Scalar {
/// serializes the field element into big-endian bytes
fn to_bytes_be(&self) -> Vec<u8> {
let field_len = (self.r.bits() + 7) / 8;
let field_len = (self.r.bits() as usize + 7) / 8;
let mut bytes = self.k.to_biguint().unwrap().to_bytes_be();
let mut out = vec![0; field_len - bytes.len()];
if !out.is_empty() {
Expand Down Expand Up @@ -137,14 +137,14 @@ impl std::iter::Iterator for Iterino {

impl Scalar {
pub fn iter_lr(&self) -> impl std::iter::Iterator<Item = bool> {
let l = self.k.bits();
let l = self.k.bits() as usize;
let i = l - 1usize;
let (_, v) = self.k.to_u32_digits();
let is_lr = true;
Iterino { l, i, v, is_lr }
}
pub fn iter_rl(&self) -> impl std::iter::Iterator<Item = bool> {
let l = self.k.bits();
let l = self.k.bits() as usize;
let i = 0usize;
let (_, v) = self.k.to_u32_digits();
let is_lr = false;
Expand Down
2 changes: 1 addition & 1 deletion tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ use redox_ecc::version;

#[test]
fn integration_testing() {
assert_eq!(version(), "0.2.2");
assert_eq!(version(), "0.2.3");
}

0 comments on commit 5a8c09c

Please sign in to comment.