Skip to content

Commit 5b25e50

Browse files
authored
p256: consolidate ProjectivePoint impl blocks (#512)
Moves the inherent method definitions to the same impl block as the one that defines the `IDENTITY` and `GENERATOR` constants.
1 parent 2384c41 commit 5b25e50

File tree

2 files changed

+131
-115
lines changed

2 files changed

+131
-115
lines changed

k256/src/arithmetic/projective.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,12 @@ impl From<ProjectivePoint> for AffinePoint {
253253
}
254254
}
255255

256+
impl From<&ProjectivePoint> for AffinePoint {
257+
fn from(p: &ProjectivePoint) -> AffinePoint {
258+
p.to_affine()
259+
}
260+
}
261+
256262
impl FromEncodedPoint<Secp256k1> for ProjectivePoint {
257263
fn from_encoded_point(p: &EncodedPoint) -> CtOption<Self> {
258264
AffinePoint::from_encoded_point(p).map(ProjectivePoint::from)

p256/src/arithmetic/projective.rs

Lines changed: 125 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -54,122 +54,7 @@ impl ProjectivePoint {
5454
y: AffinePoint::GENERATOR.y,
5555
z: FieldElement::ONE,
5656
};
57-
}
58-
59-
impl Group for ProjectivePoint {
60-
type Scalar = Scalar;
61-
62-
fn random(mut rng: impl RngCore) -> Self {
63-
Self::GENERATOR * Scalar::random(&mut rng)
64-
}
65-
66-
fn identity() -> Self {
67-
Self::IDENTITY
68-
}
69-
70-
fn generator() -> Self {
71-
Self::GENERATOR
72-
}
73-
74-
fn is_identity(&self) -> Choice {
75-
self.ct_eq(&Self::IDENTITY)
76-
}
77-
78-
#[must_use]
79-
fn double(&self) -> Self {
80-
ProjectivePoint::double(self)
81-
}
82-
}
83-
84-
impl GroupEncoding for ProjectivePoint {
85-
type Repr = CompressedPoint;
86-
87-
fn from_bytes(bytes: &Self::Repr) -> CtOption<Self> {
88-
<AffinePoint as GroupEncoding>::from_bytes(bytes).map(Into::into)
89-
}
90-
91-
fn from_bytes_unchecked(bytes: &Self::Repr) -> CtOption<Self> {
92-
// No unchecked conversion possible for compressed points
93-
Self::from_bytes(bytes)
94-
}
95-
96-
fn to_bytes(&self) -> Self::Repr {
97-
self.to_affine().to_bytes()
98-
}
99-
}
100-
101-
impl PrimeGroup for ProjectivePoint {}
102-
103-
impl Curve for ProjectivePoint {
104-
type AffineRepr = AffinePoint;
105-
106-
fn to_affine(&self) -> AffinePoint {
107-
ProjectivePoint::to_affine(self)
108-
}
109-
}
110-
111-
impl PrimeCurve for ProjectivePoint {
112-
type Affine = AffinePoint;
113-
}
114-
115-
impl LinearCombination for ProjectivePoint {}
11657

117-
impl From<AffinePoint> for ProjectivePoint {
118-
fn from(p: AffinePoint) -> Self {
119-
let projective = ProjectivePoint {
120-
x: p.x,
121-
y: p.y,
122-
z: FieldElement::ONE,
123-
};
124-
Self::conditional_select(&projective, &Self::IDENTITY, p.is_identity())
125-
}
126-
}
127-
128-
impl From<ProjectivePoint> for AffinePoint {
129-
fn from(p: ProjectivePoint) -> AffinePoint {
130-
p.to_affine()
131-
}
132-
}
133-
134-
impl FromEncodedPoint<NistP256> for ProjectivePoint {
135-
fn from_encoded_point(p: &EncodedPoint) -> CtOption<Self> {
136-
AffinePoint::from_encoded_point(p).map(ProjectivePoint::from)
137-
}
138-
}
139-
140-
impl ToEncodedPoint<NistP256> for ProjectivePoint {
141-
fn to_encoded_point(&self, compress: bool) -> EncodedPoint {
142-
self.to_affine().to_encoded_point(compress)
143-
}
144-
}
145-
146-
impl ConditionallySelectable for ProjectivePoint {
147-
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
148-
ProjectivePoint {
149-
x: FieldElement::conditional_select(&a.x, &b.x, choice),
150-
y: FieldElement::conditional_select(&a.y, &b.y, choice),
151-
z: FieldElement::conditional_select(&a.z, &b.z, choice),
152-
}
153-
}
154-
}
155-
156-
impl ConstantTimeEq for ProjectivePoint {
157-
fn ct_eq(&self, other: &Self) -> Choice {
158-
self.to_affine().ct_eq(&other.to_affine())
159-
}
160-
}
161-
162-
impl DefaultIsZeroes for ProjectivePoint {}
163-
164-
impl Eq for ProjectivePoint {}
165-
166-
impl PartialEq for ProjectivePoint {
167-
fn eq(&self, other: &Self) -> bool {
168-
self.ct_eq(other).into()
169-
}
170-
}
171-
172-
impl ProjectivePoint {
17358
/// Returns the additive identity of P-256, also known as the "neutral element" or
17459
/// "point at infinity".
17560
#[deprecated(since = "0.10.1", note = "use `ProjectivePoint::IDENTITY` instead")]
@@ -322,6 +207,131 @@ impl ProjectivePoint {
322207
}
323208
}
324209

210+
impl Group for ProjectivePoint {
211+
type Scalar = Scalar;
212+
213+
fn random(mut rng: impl RngCore) -> Self {
214+
Self::GENERATOR * Scalar::random(&mut rng)
215+
}
216+
217+
fn identity() -> Self {
218+
Self::IDENTITY
219+
}
220+
221+
fn generator() -> Self {
222+
Self::GENERATOR
223+
}
224+
225+
fn is_identity(&self) -> Choice {
226+
self.ct_eq(&Self::IDENTITY)
227+
}
228+
229+
#[must_use]
230+
fn double(&self) -> Self {
231+
ProjectivePoint::double(self)
232+
}
233+
}
234+
235+
impl GroupEncoding for ProjectivePoint {
236+
type Repr = CompressedPoint;
237+
238+
fn from_bytes(bytes: &Self::Repr) -> CtOption<Self> {
239+
<AffinePoint as GroupEncoding>::from_bytes(bytes).map(Into::into)
240+
}
241+
242+
fn from_bytes_unchecked(bytes: &Self::Repr) -> CtOption<Self> {
243+
// No unchecked conversion possible for compressed points
244+
Self::from_bytes(bytes)
245+
}
246+
247+
fn to_bytes(&self) -> Self::Repr {
248+
self.to_affine().to_bytes()
249+
}
250+
}
251+
252+
impl PrimeGroup for ProjectivePoint {}
253+
254+
impl Curve for ProjectivePoint {
255+
type AffineRepr = AffinePoint;
256+
257+
fn to_affine(&self) -> AffinePoint {
258+
ProjectivePoint::to_affine(self)
259+
}
260+
}
261+
262+
impl PrimeCurve for ProjectivePoint {
263+
type Affine = AffinePoint;
264+
}
265+
266+
impl LinearCombination for ProjectivePoint {}
267+
268+
impl From<AffinePoint> for ProjectivePoint {
269+
fn from(p: AffinePoint) -> Self {
270+
let projective = ProjectivePoint {
271+
x: p.x,
272+
y: p.y,
273+
z: FieldElement::ONE,
274+
};
275+
Self::conditional_select(&projective, &Self::IDENTITY, p.is_identity())
276+
}
277+
}
278+
279+
impl From<&AffinePoint> for ProjectivePoint {
280+
fn from(p: &AffinePoint) -> Self {
281+
Self::from(*p)
282+
}
283+
}
284+
285+
impl From<ProjectivePoint> for AffinePoint {
286+
fn from(p: ProjectivePoint) -> AffinePoint {
287+
p.to_affine()
288+
}
289+
}
290+
291+
impl From<&ProjectivePoint> for AffinePoint {
292+
fn from(p: &ProjectivePoint) -> AffinePoint {
293+
p.to_affine()
294+
}
295+
}
296+
297+
impl FromEncodedPoint<NistP256> for ProjectivePoint {
298+
fn from_encoded_point(p: &EncodedPoint) -> CtOption<Self> {
299+
AffinePoint::from_encoded_point(p).map(ProjectivePoint::from)
300+
}
301+
}
302+
303+
impl ToEncodedPoint<NistP256> for ProjectivePoint {
304+
fn to_encoded_point(&self, compress: bool) -> EncodedPoint {
305+
self.to_affine().to_encoded_point(compress)
306+
}
307+
}
308+
309+
impl ConditionallySelectable for ProjectivePoint {
310+
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
311+
ProjectivePoint {
312+
x: FieldElement::conditional_select(&a.x, &b.x, choice),
313+
y: FieldElement::conditional_select(&a.y, &b.y, choice),
314+
z: FieldElement::conditional_select(&a.z, &b.z, choice),
315+
}
316+
}
317+
}
318+
319+
impl ConstantTimeEq for ProjectivePoint {
320+
fn ct_eq(&self, other: &Self) -> Choice {
321+
self.to_affine().ct_eq(&other.to_affine())
322+
}
323+
}
324+
325+
impl DefaultIsZeroes for ProjectivePoint {}
326+
327+
impl Eq for ProjectivePoint {}
328+
329+
impl PartialEq for ProjectivePoint {
330+
fn eq(&self, other: &Self) -> bool {
331+
self.ct_eq(other).into()
332+
}
333+
}
334+
325335
impl Default for ProjectivePoint {
326336
fn default() -> Self {
327337
Self::IDENTITY

0 commit comments

Comments
 (0)