Skip to content

Commit 3b1d44a

Browse files
authored
k256+p256: make FromEncodedPoint return a CtOption (#445)
Corresponding changes for: RustCrypto/traits#782 Internally these were already returning `CtOption` anyway. It should also simplify the implementation of `GroupEncoding`.
1 parent 9a93c97 commit 3b1d44a

File tree

5 files changed

+59
-72
lines changed

5 files changed

+59
-72
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

k256/src/arithmetic/affine.rs

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -105,40 +105,6 @@ impl PartialEq for AffinePoint {
105105

106106
impl Eq for AffinePoint {}
107107

108-
impl AffinePoint {
109-
/// Decode this point from a SEC1-encoded point.
110-
pub(crate) fn decode(encoded_point: &EncodedPoint) -> CtOption<Self> {
111-
match encoded_point.coordinates() {
112-
sec1::Coordinates::Identity => CtOption::new(Self::identity(), 1.into()),
113-
sec1::Coordinates::Compact { .. } => {
114-
// TODO(tarcieri): add decompaction support
115-
CtOption::new(Self::default(), 0.into())
116-
}
117-
sec1::Coordinates::Compressed { x, y_is_odd } => {
118-
AffinePoint::decompress(x, Choice::from(y_is_odd as u8))
119-
}
120-
sec1::Coordinates::Uncompressed { x, y } => {
121-
let x = FieldElement::from_bytes(x);
122-
let y = FieldElement::from_bytes(y);
123-
124-
x.and_then(|x| {
125-
y.and_then(|y| {
126-
// Check that the point is on the curve
127-
let lhs = (y * &y).negate(1);
128-
let rhs = x * &x * &x + &CURVE_EQUATION_B;
129-
let point = AffinePoint {
130-
x,
131-
y,
132-
infinity: Choice::from(0),
133-
};
134-
CtOption::new(point, (lhs + &rhs).normalizes_to_zero())
135-
})
136-
})
137-
}
138-
}
139-
}
140-
}
141-
142108
impl DecompressPoint<Secp256k1> for AffinePoint {
143109
fn decompress(x_bytes: &FieldBytes, y_is_odd: Choice) -> CtOption<Self> {
144110
FieldElement::from_bytes(x_bytes).and_then(|x| {
@@ -189,8 +155,35 @@ impl FromEncodedPoint<Secp256k1> for AffinePoint {
189155
/// # Returns
190156
///
191157
/// `None` value if `encoded_point` is not on the secp256k1 curve.
192-
fn from_encoded_point(encoded_point: &EncodedPoint) -> Option<Self> {
193-
Self::decode(encoded_point).into()
158+
fn from_encoded_point(encoded_point: &EncodedPoint) -> CtOption<Self> {
159+
match encoded_point.coordinates() {
160+
sec1::Coordinates::Identity => CtOption::new(Self::identity(), 1.into()),
161+
sec1::Coordinates::Compact { .. } => {
162+
// TODO(tarcieri): add decompaction support
163+
CtOption::new(Self::default(), 0.into())
164+
}
165+
sec1::Coordinates::Compressed { x, y_is_odd } => {
166+
AffinePoint::decompress(x, Choice::from(y_is_odd as u8))
167+
}
168+
sec1::Coordinates::Uncompressed { x, y } => {
169+
let x = FieldElement::from_bytes(x);
170+
let y = FieldElement::from_bytes(y);
171+
172+
x.and_then(|x| {
173+
y.and_then(|y| {
174+
// Check that the point is on the curve
175+
let lhs = (y * &y).negate(1);
176+
let rhs = x * &x * &x + &CURVE_EQUATION_B;
177+
let point = AffinePoint {
178+
x,
179+
y,
180+
infinity: Choice::from(0),
181+
};
182+
CtOption::new(point, (lhs + &rhs).normalizes_to_zero())
183+
})
184+
})
185+
}
186+
}
194187
}
195188
}
196189

k256/src/arithmetic/projective.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl From<ProjectivePoint> for AffinePoint {
6262
}
6363

6464
impl FromEncodedPoint<Secp256k1> for ProjectivePoint {
65-
fn from_encoded_point(p: &EncodedPoint) -> Option<Self> {
65+
fn from_encoded_point(p: &EncodedPoint) -> CtOption<Self> {
6666
AffinePoint::from_encoded_point(p).map(ProjectivePoint::from)
6767
}
6868
}

p256/src/arithmetic/affine.rs

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -105,36 +105,6 @@ impl PartialEq for AffinePoint {
105105
}
106106
}
107107

108-
impl AffinePoint {
109-
fn decode(encoded_point: &EncodedPoint) -> CtOption<Self> {
110-
match encoded_point.coordinates() {
111-
sec1::Coordinates::Identity => CtOption::new(Self::identity(), 1.into()),
112-
sec1::Coordinates::Compact { x } => AffinePoint::decompact(x),
113-
sec1::Coordinates::Compressed { x, y_is_odd } => {
114-
AffinePoint::decompress(x, Choice::from(y_is_odd as u8))
115-
}
116-
sec1::Coordinates::Uncompressed { x, y } => {
117-
let x = FieldElement::from_bytes(x);
118-
let y = FieldElement::from_bytes(y);
119-
120-
x.and_then(|x| {
121-
y.and_then(|y| {
122-
// Check that the point is on the curve
123-
let lhs = y * &y;
124-
let rhs = x * &x * &x + &(CURVE_EQUATION_A * &x) + &CURVE_EQUATION_B;
125-
let point = AffinePoint {
126-
x,
127-
y,
128-
infinity: Choice::from(0),
129-
};
130-
CtOption::new(point, lhs.ct_eq(&rhs))
131-
})
132-
})
133-
}
134-
}
135-
}
136-
}
137-
138108
impl DecompressPoint<NistP256> for AffinePoint {
139109
fn decompress(x_bytes: &FieldBytes, y_is_odd: Choice) -> CtOption<Self> {
140110
FieldElement::from_bytes(x_bytes).and_then(|x| {
@@ -205,8 +175,32 @@ impl FromEncodedPoint<NistP256> for AffinePoint {
205175
/// # Returns
206176
///
207177
/// `None` value if `encoded_point` is not on the secp256r1 curve.
208-
fn from_encoded_point(encoded_point: &EncodedPoint) -> Option<Self> {
209-
Self::decode(encoded_point).into()
178+
fn from_encoded_point(encoded_point: &EncodedPoint) -> CtOption<Self> {
179+
match encoded_point.coordinates() {
180+
sec1::Coordinates::Identity => CtOption::new(Self::identity(), 1.into()),
181+
sec1::Coordinates::Compact { x } => AffinePoint::decompact(x),
182+
sec1::Coordinates::Compressed { x, y_is_odd } => {
183+
AffinePoint::decompress(x, Choice::from(y_is_odd as u8))
184+
}
185+
sec1::Coordinates::Uncompressed { x, y } => {
186+
let x = FieldElement::from_bytes(x);
187+
let y = FieldElement::from_bytes(y);
188+
189+
x.and_then(|x| {
190+
y.and_then(|y| {
191+
// Check that the point is on the curve
192+
let lhs = y * &y;
193+
let rhs = x * &x * &x + &(CURVE_EQUATION_A * &x) + &CURVE_EQUATION_B;
194+
let point = AffinePoint {
195+
x,
196+
y,
197+
infinity: Choice::from(0),
198+
};
199+
CtOption::new(point, lhs.ct_eq(&rhs))
200+
})
201+
})
202+
}
203+
}
210204
}
211205
}
212206

p256/src/arithmetic/projective.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl From<ProjectivePoint> for AffinePoint {
115115
}
116116

117117
impl FromEncodedPoint<NistP256> for ProjectivePoint {
118-
fn from_encoded_point(p: &EncodedPoint) -> Option<Self> {
118+
fn from_encoded_point(p: &EncodedPoint) -> CtOption<Self> {
119119
AffinePoint::from_encoded_point(p).map(ProjectivePoint::from)
120120
}
121121
}

0 commit comments

Comments
 (0)