Skip to content

Commit

Permalink
[WIP] weierstrass: initial crate
Browse files Browse the repository at this point in the history
Implements `AffinePoint` and `ProjectivePoint` generically
  • Loading branch information
tarcieri committed Aug 2, 2022
1 parent c48ae66 commit ee4b565
Show file tree
Hide file tree
Showing 19 changed files with 1,784 additions and 1,118 deletions.
10 changes: 9 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ members = [
"k256",
"p256",
"p384",
"p521"
"p521",
"weierstrass"
]

[profile.dev]
Expand Down
3 changes: 2 additions & 1 deletion p384/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "p384"
version = "0.11.1"
version = "0.12.0-pre"
description = """
Pure Rust implementation of the NIST P-384 (a.k.a. secp384r1) elliptic curve
with support for ECDH, ECDSA signing/verification, and general purpose curve
Expand All @@ -18,6 +18,7 @@ rust-version = "1.57"

[dependencies]
elliptic-curve = { version = "0.12.2", default-features = false, features = ["hazmat", "sec1"] }
weierstrass = { version = "0", path = "../weierstrass" }

# optional dependencies
ecdsa-core = { version = "0.14", package = "ecdsa", optional = true, default-features = false, features = ["der"] }
Expand Down
76 changes: 57 additions & 19 deletions p384/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,63 @@
#[macro_use]
mod macros;

pub(crate) mod affine;
pub(crate) mod field;
pub(crate) mod projective;
pub(crate) mod scalar;

use self::{
affine::AffinePoint,
field::{FieldElement, MODULUS},
projective::ProjectivePoint,
scalar::Scalar,
};

/// a = -3 (0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000fffffffc)
const CURVE_EQUATION_A: FieldElement = FieldElement::ZERO
.sub(&FieldElement::ONE)
.sub(&FieldElement::ONE)
.sub(&FieldElement::ONE);

/// b = b3312fa7 e23ee7e4 988e056b e3f82d19 181d9c6e fe814112
/// 0314088f 5013875a c656398d 8a2ed19d 2a85c8ed d3ec2aef
const CURVE_EQUATION_B: FieldElement =
FieldElement::from_be_hex("b3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef");
use self::field::FieldElement;
use crate::NistP384;
use elliptic_curve::{AffineArithmetic, PrimeCurveArithmetic, ProjectiveArithmetic};
use weierstrass::WeierstrassCurve;

/// Elliptic curve point in affine coordinates.
pub type AffinePoint = weierstrass::AffinePoint<NistP384>;

/// Elliptic curve point in projective coordinates.
pub type ProjectivePoint = weierstrass::ProjectivePoint<NistP384>;

impl WeierstrassCurve for NistP384 {
type FieldElement = FieldElement;

const ZERO: FieldElement = FieldElement::ZERO;
const ONE: FieldElement = FieldElement::ONE;

/// a = -3 (0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000fffffffc)
const EQUATION_A: FieldElement = FieldElement::ZERO
.sub(&FieldElement::ONE)
.sub(&FieldElement::ONE)
.sub(&FieldElement::ONE);

/// b = b3312fa7 e23ee7e4 988e056b e3f82d19 181d9c6e fe814112
/// 0314088f 5013875a c656398d 8a2ed19d 2a85c8ed d3ec2aef
const EQUATION_B: FieldElement = FieldElement::from_be_hex("b3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef");

/// Base point of P-384.
///
/// Defined in FIPS 186-4 § D.1.2.4:
///
/// ```text
/// Gₓ = aa87ca22 be8b0537 8eb1c71e f320ad74 6e1d3b62 8ba79b98
/// 59f741e0 82542a38 5502f25d bf55296c 3a545e38 72760ab7
/// Gᵧ = 3617de4a 96262c6f 5d9e98bf 9292dc29 f8f41dbd 289a147c
/// e9da3113 b5f0b8c0 0a60b1ce 1d7e819d 7a431d7c 90ea0e5f
/// ```
///
/// NOTE: coordinate field elements have been translated into the Montgomery
/// domain.
const GENERATOR: (FieldElement, FieldElement) = (
FieldElement::from_be_hex("aa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7"),
FieldElement::from_be_hex("3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f"),
);
}

impl AffineArithmetic for NistP384 {
type AffinePoint = AffinePoint;
}

impl ProjectiveArithmetic for NistP384 {
type ProjectivePoint = ProjectivePoint;
}

impl PrimeCurveArithmetic for NistP384 {
type CurveGroup = ProjectivePoint;
}
Loading

0 comments on commit ee4b565

Please sign in to comment.