Skip to content

Commit

Permalink
bign256: digital signature algorithm (#935)
Browse files Browse the repository at this point in the history
  • Loading branch information
makavity authored Dec 14, 2023
1 parent 0ed00f7 commit b21dd4f
Show file tree
Hide file tree
Showing 13 changed files with 764 additions and 41 deletions.
32 changes: 30 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions bign256/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,25 @@ elliptic-curve = { version = "0.13.8", features = ["hazmat", "sec1"] }

# optional dependencies
primeorder = { version = "0.13.5", optional = true, path = "../primeorder" }
signature = { version = "2", optional = true }
belt-hash = { version = "0.1.0", optional = true, default-features = false }
crypto-bigint = { version = "0.5.3", optional = true }
rfc6979 = { version = "0.4", optional = true }

[dev-dependencies]
criterion = "0.5"
hex-literal = "0.4"
proptest = "1"
rand_core = { version = "0.6", features = ["getrandom"] }
hex = {version = "*" }


[features]
default = ["arithmetic", "pkcs8", "std"]
default = ["arithmetic", "pkcs8", "std", "dsa"]
alloc = ["elliptic-curve/alloc", "primeorder?/alloc"]
std = ["alloc", "elliptic-curve/std"]
std = ["alloc", "elliptic-curve/std", "signature?/std"]

dsa = ["arithmetic", "dep:rfc6979", "dep:signature", "dep:belt-hash", "dep:crypto-bigint"]
arithmetic = ["dep:primeorder", "elliptic-curve/arithmetic"]
pkcs8 = ["elliptic-curve/pkcs8"]

Expand Down
17 changes: 7 additions & 10 deletions bign256/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ USE AT YOUR OWN RISK!

## About BIGN P-256

BIGN P-256 is a Weierstrass curve specified in [STB 34.101.45-2013]:
Recommendations for Discrete Logarithm-based Cryptography:
Elliptic Curve Domain Parameters.

BIGN P-256 is a Weierstrass curve specified in [STB 34.101.45-2013].
Also known as bign-curve256v1.

## Minimum Supported Rust Version
Expand Down Expand Up @@ -65,12 +62,12 @@ dual licensed as above, without any additional terms or conditions.

[//]: # (badges)

[crate-image]: https://buildstats.info/crate/p224
[crate-link]: https://crates.io/crates/p224
[docs-image]: https://docs.rs/p224/badge.svg
[docs-link]: https://docs.rs/p224/
[build-image]: https://github.com/RustCrypto/elliptic-curves/actions/workflows/p224.yml/badge.svg
[build-link]: https://github.com/RustCrypto/elliptic-curves/actions/workflows/p224.yml
[crate-image]: https://buildstats.info/crate/bign256
[crate-link]: https://crates.io/crates/bign256
[docs-image]: https://docs.rs/bign256/badge.svg
[docs-link]: https://docs.rs/bign256/
[build-image]: https://github.com/RustCrypto/elliptic-curves/actions/workflows/bign256.yml/badge.svg
[build-link]: https://github.com/RustCrypto/elliptic-curves/actions/workflows/bign256.yml
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.65+-blue.svg
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg
Expand Down
13 changes: 6 additions & 7 deletions bign256/benches/field.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! bign-curve256v1 field element benchmarks
use bign256::elliptic_curve::Field;
use bign256::FieldElement;
use bign256::arithmetic::FieldElement;
use criterion::{
criterion_group, criterion_main, measurement::Measurement, BenchmarkGroup, Criterion,
};
Expand All @@ -21,23 +20,23 @@ fn test_field_element_y() -> FieldElement {
.unwrap()
}

fn bench_field_element_mul<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
fn bench_field_element_mul<M: Measurement>(group: &mut BenchmarkGroup<M>) {
let x = test_field_element_x();
let y = test_field_element_y();
group.bench_function("mul", |b| b.iter(|| &x * &y));
group.bench_function("mul", |b| b.iter(|| x * y));
}

fn bench_field_element_square<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
fn bench_field_element_square<M: Measurement>(group: &mut BenchmarkGroup<M>) {
let x = test_field_element_x();
group.bench_function("square", |b| b.iter(|| x.square()));
}

fn bench_field_element_sqrt<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
fn bench_field_element_sqrt<M: Measurement>(group: &mut BenchmarkGroup<M>) {
let x = test_field_element_x();
group.bench_function("sqrt", |b| b.iter(|| x.sqrt()));
}

fn bench_field_element_invert<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
fn bench_field_element_invert<M: Measurement>(group: &mut BenchmarkGroup<M>) {
let x = test_field_element_x();
group.bench_function("invert", |b| b.iter(|| x.invert()));
}
Expand Down
20 changes: 10 additions & 10 deletions bign256/benches/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,37 @@ fn test_scalar_y() -> Scalar {
.unwrap()
}

fn bench_point_mul<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
fn bench_point_mul<M: Measurement>(group: &mut BenchmarkGroup<M>) {
let p = ProjectivePoint::GENERATOR;
let m = test_scalar_x();
let s = Scalar::from_repr(m.into()).unwrap();
group.bench_function("point-scalar mul", |b| b.iter(|| &p * &s));
group.bench_function("point-scalar mul", |b| b.iter(|| p * s));
}

fn bench_scalar_sub<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
fn bench_scalar_sub<M: Measurement>(group: &mut BenchmarkGroup<M>) {
let x = test_scalar_x();
let y = test_scalar_y();
group.bench_function("sub", |b| b.iter(|| &x - &y));
group.bench_function("sub", |b| b.iter(|| x - y));
}

fn bench_scalar_add<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
fn bench_scalar_add<M: Measurement>(group: &mut BenchmarkGroup<M>) {
let x = test_scalar_x();
let y = test_scalar_y();
group.bench_function("add", |b| b.iter(|| &x + &y));
group.bench_function("add", |b| b.iter(|| x + y));
}

fn bench_scalar_mul<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
fn bench_scalar_mul<M: Measurement>(group: &mut BenchmarkGroup<M>) {
let x = test_scalar_x();
let y = test_scalar_y();
group.bench_function("mul", |b| b.iter(|| &x * &y));
group.bench_function("mul", |b| b.iter(|| x * y));
}

fn bench_scalar_negate<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
fn bench_scalar_negate<M: Measurement>(group: &mut BenchmarkGroup<M>) {
let x = test_scalar_x();
group.bench_function("negate", |b| b.iter(|| -x));
}

fn bench_scalar_invert<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
fn bench_scalar_invert<M: Measurement>(group: &mut BenchmarkGroup<M>) {
let x = test_scalar_x();
group.bench_function("invert", |b| b.iter(|| x.invert()));
}
Expand Down
6 changes: 3 additions & 3 deletions bign256/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ pub(crate) mod scalar;

pub use self::scalar::Scalar;

use self::field::FieldElement;
pub use self::field::FieldElement;
use crate::BignP256;
use elliptic_curve::{CurveArithmetic, PrimeCurveArithmetic};
use primeorder::{point_arithmetic, PrimeCurveParams};
pub use elliptic_curve::{CurveArithmetic, PrimeCurveArithmetic};
pub use primeorder::{point_arithmetic, PrimeCurveParams};

/// Elliptic curve point in affine coordinates.
pub type AffinePoint = primeorder::AffinePoint<BignP256>;
Expand Down
7 changes: 4 additions & 3 deletions bign256/src/arithmetic/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
clippy::cast_possible_wrap,
clippy::cast_sign_loss,
clippy::cast_possible_truncation,
clippy::integer_arithmetic,
clippy::arithmetic_side_effects,
clippy::should_implement_trait,
clippy::suspicious_op_assign_impl,
clippy::unused_unit,
Expand All @@ -35,10 +35,11 @@ use core::{
iter::{Product, Sum},
ops::{AddAssign, MulAssign, Neg, SubAssign},
};
use elliptic_curve::bigint::Limb;
use elliptic_curve::ops::Invert;

use elliptic_curve::{
bigint::Limb,
ff::PrimeField,
ops::Invert,
subtle::{Choice, ConstantTimeEq, CtOption},
};
use primeorder::impl_bernstein_yang_invert;
Expand Down
4 changes: 2 additions & 2 deletions bign256/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
clippy::cast_sign_loss,
clippy::cast_possible_truncation,
clippy::identity_op,
clippy::integer_arithmetic,
clippy::arithmetic_side_effects,
clippy::too_many_arguments,
clippy::unnecessary_cast
)]
Expand Down Expand Up @@ -63,7 +63,7 @@ use core::ops::{Add, Mul, Sub};
///
/// Please see the documentation for the relevant traits for more information.
#[derive(Clone, Copy, Debug, PartialOrd, Ord)]
pub struct Scalar(U256);
pub struct Scalar(pub U256);

primeorder::impl_mont_field_element!(
BignP256,
Expand Down
Loading

0 comments on commit b21dd4f

Please sign in to comment.