Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions elliptic-curve-crate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ default-features = false
hex = "0.4"

[features]
default = []
legacy = []
weierstrass = []
std = []

[package.metadata.docs.rs]
all-features = true
features = ["weierstrass", "std"]
rustdoc-args = ["--cfg", "docsrs"]
26 changes: 26 additions & 0 deletions elliptic-curve-crate/src/legacy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! Support for implementing legacy protocols that require direct access to
//! coordinates of affine points on elliptic curves.
//!
//! These APIs violate the group abstraction and expose coordinate field
//! elements which could be potentially misused when designing new protocols
//! based on elliptic curve groups.
//!
//! For that reason, we strongly suggest they aren't used in new protocols, but
//! only as needed when implementing legacy protocols which require them.

use generic_array::ArrayLength;

/// Byte array containing a serialized field element
pub type FieldElementBytes<Size> = generic_array::GenericArray<u8, Size>;

/// Access to the coordinates of an affine point
pub trait AffineCoordinates {
/// Size of a field element representing an affine coordinate
type FieldElementSize: ArrayLength<u8>;

/// x-coordinate (field element)
fn x(&self) -> FieldElementBytes<Self::FieldElementSize>;

/// y-coordinate (field element)
fn y(&self) -> FieldElementBytes<Self::FieldElementSize>;
}
7 changes: 5 additions & 2 deletions elliptic-curve-crate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@ pub use rand_core;
pub mod error;
pub mod secret_key;

pub use generic_array::{self, typenum::consts};
pub use subtle;
#[cfg(feature = "legacy")]
#[cfg_attr(docsrs, doc(cfg(feature = "legacy")))]
pub mod legacy;

// TODO(tarcieri): other curve forms
#[cfg(feature = "weierstrass")]
#[cfg_attr(docsrs, doc(cfg(feature = "weierstrass")))]
pub mod weierstrass;

pub use self::{error::Error, secret_key::SecretKey};
pub use generic_array::{self, typenum::consts};
pub use subtle;

/// Byte array containing a serialized scalar value (i.e. an integer)
pub type ScalarBytes<Size> = generic_array::GenericArray<u8, Size>;
3 changes: 2 additions & 1 deletion k256/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ proptest = "0.10"
[features]
default = ["arithmetic", "std"]
arithmetic = []
legacy = ["arithmetic", "elliptic-curve/legacy"]
rand = ["elliptic-curve/rand_core"]
test-vectors = []
std = ["elliptic-curve/std"]

[package.metadata.docs.rs]
all-features = true
features = ["arithmetic", "rand", "test-vectors", "std"]
rustdoc-args = ["--cfg", "docsrs"]
16 changes: 16 additions & 0 deletions k256/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use crate::{CompressedPoint, PublicKey, ScalarBytes, Secp256k1, UncompressedPoin
use field::{FieldElement, MODULUS};
use scalar::Scalar;

#[cfg(feature = "legacy")]
use elliptic_curve::{consts::U32, legacy};

#[cfg(feature = "rand")]
use crate::SecretKey;

Expand Down Expand Up @@ -186,6 +189,19 @@ impl Neg for AffinePoint {
}
}

#[cfg(feature = "legacy")]
impl legacy::AffineCoordinates for AffinePoint {
type FieldElementSize = U32;

fn x(&self) -> legacy::FieldElementBytes<U32> {
self.x.to_bytes().into()
}

fn y(&self) -> legacy::FieldElementBytes<U32> {
self.y.to_bytes().into()
}
}

/// A point on the secp256k1 curve in projective coordinates.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
Expand Down
3 changes: 2 additions & 1 deletion p256/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ proptest = "0.10"
[features]
default = ["arithmetic", "std"]
arithmetic = []
legacy = ["arithmetic", "elliptic-curve/legacy"]
rand = ["elliptic-curve/rand_core"]
test-vectors = []
std = ["elliptic-curve/std"]

[package.metadata.docs.rs]
all-features = true
features = ["arithmetic", "rand", "test-vectors", "std"]
rustdoc-args = ["--cfg", "docsrs"]
16 changes: 16 additions & 0 deletions p256/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use crate::{CompressedPoint, NistP256, PublicKey, ScalarBytes, UncompressedPoint
use field::{FieldElement, MODULUS};
use scalar::Scalar;

#[cfg(feature = "legacy")]
use elliptic_curve::{consts::U32, legacy};

#[cfg(feature = "rand")]
use crate::SecretKey;

Expand Down Expand Up @@ -192,6 +195,19 @@ impl Neg for AffinePoint {
}
}

#[cfg(feature = "legacy")]
impl legacy::AffineCoordinates for AffinePoint {
type FieldElementSize = U32;

fn x(&self) -> legacy::FieldElementBytes<U32> {
self.x.to_bytes().into()
}

fn y(&self) -> legacy::FieldElementBytes<U32> {
self.y.to_bytes().into()
}
}

/// A point on the secp256r1 curve in projective coordinates.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
Expand Down