Skip to content

Commit d69d5b9

Browse files
authored
elliptic-curve: consolidate AffineCoordinates trait (RustCrypto#1237)
See RustCrypto/elliptic-curves#50 for some historic context. After being able to get by on `AffineXCoordinate` for generic ECDH and ECDSA, RustCrypto#1199 added an `AffineYIsOdd` trait which was needed to enable the generic ECDSA implementation in the `ecdsa` crate to compute the "recovery ID" for signatures (which is effectively point compression for the `R` curve point). This commit consolidates `AffineXCoordinate` and `AffineYIsOdd` into an `AffineCoordinates` trait. Some observations since prior discussion in RustCrypto/elliptic-curves#50: - Access to coordinates is through bytes, namely `FieldBytes`. This is so as to avoid exposing a crate's field element type. This approach isn't type safe (base field elements and scalar field elements share the same serialization) but does make ECDSA's weird reduction of a base field element into the scalar field straightforward in generic code. - Prior to this attempts were made to extract ECDSA-specific bits into a trait to handle these conversions, but it complicates both writing generic code and optimizing performance. While this still might be worth exploring, so far those explorations have largely failed. - Generally there have been a lot of requests for coordinate access specifically for things like point serialization formats. We ended up adding "compaction" support upstream but we have had requests for several other formats (e.g. Elligator Squared) where direct coordinate access would be useful. This trait can hopefully be replaced by a coordinate access API provided by the `group` crate in the future. See zkcrypto/group#30
1 parent 4ad2fc1 commit d69d5b9

File tree

4 files changed

+8
-13
lines changed

4 files changed

+8
-13
lines changed

elliptic-curve/src/arithmetic.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::{
44
ops::{LinearCombination, MulByGenerator, Reduce, ShrAssign},
5-
point::{AffineXCoordinate, AffineYIsOdd},
5+
point::AffineCoordinates,
66
scalar::FromUintUnchecked,
77
scalar::IsHigh,
88
Curve, FieldBytes, PrimeCurve, ScalarPrimitive,
@@ -15,8 +15,7 @@ use zeroize::DefaultIsZeroes;
1515
pub trait CurveArithmetic: Curve {
1616
/// Elliptic curve point in affine coordinates.
1717
type AffinePoint: 'static
18-
+ AffineXCoordinate<FieldRepr = FieldBytes<Self>>
19-
+ AffineYIsOdd
18+
+ AffineCoordinates<FieldRepr = FieldBytes<Self>>
2019
+ Copy
2120
+ ConditionallySelectable
2221
+ ConstantTimeEq

elliptic-curve/src/dev.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
generic_array::typenum::U32,
1010
ops::{LinearCombination, MulByGenerator, Reduce, ShrAssign},
1111
pkcs8,
12-
point::{AffineXCoordinate, AffineYIsOdd},
12+
point::AffineCoordinates,
1313
rand_core::RngCore,
1414
scalar::{FromUintUnchecked, IsHigh},
1515
sec1::{CompressedPoint, FromEncodedPoint, ToEncodedPoint},
@@ -415,15 +415,13 @@ pub enum AffinePoint {
415415
Other(EncodedPoint),
416416
}
417417

418-
impl AffineXCoordinate for AffinePoint {
418+
impl AffineCoordinates for AffinePoint {
419419
type FieldRepr = FieldBytes;
420420

421421
fn x(&self) -> FieldBytes {
422422
unimplemented!();
423423
}
424-
}
425424

426-
impl AffineYIsOdd for AffinePoint {
427425
fn y_is_odd(&self) -> Choice {
428426
unimplemented!();
429427
}

elliptic-curve/src/ecdh.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
//! [SIGMA]: https://webee.technion.ac.il/~hugo/sigma-pdf.pdf
2828
2929
use crate::{
30-
point::AffineXCoordinate, AffinePoint, Curve, CurveArithmetic, FieldBytes, NonZeroScalar,
30+
point::AffineCoordinates, AffinePoint, Curve, CurveArithmetic, FieldBytes, NonZeroScalar,
3131
ProjectivePoint, PublicKey,
3232
};
3333
use core::borrow::Borrow;

elliptic-curve/src/point.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,15 @@ pub type AffinePoint<C> = <C as CurveArithmetic>::AffinePoint;
1919
#[cfg(feature = "arithmetic")]
2020
pub type ProjectivePoint<C> = <C as CurveArithmetic>::ProjectivePoint;
2121

22-
/// Obtain the affine x-coordinate of an elliptic curve point.
23-
pub trait AffineXCoordinate {
22+
/// Access to the affine coordinates of an elliptic curve point.
23+
// TODO: use zkcrypto/group#30 coordinate API when available
24+
pub trait AffineCoordinates {
2425
/// Field element representation.
2526
type FieldRepr: AsRef<[u8]>;
2627

2728
/// Get the affine x-coordinate as a serialized field element.
2829
fn x(&self) -> Self::FieldRepr;
29-
}
3030

31-
/// Is the affine y-coordinate of this elliptic curve point odd?
32-
pub trait AffineYIsOdd {
3331
/// Is the affine y-coordinate odd?
3432
fn y_is_odd(&self) -> Choice;
3533
}

0 commit comments

Comments
 (0)