1use super::{AffinePoint, FieldElement, Scalar, CURVE_EQUATION_B};
4use crate::{CompressedPoint, EncodedPoint, NistP256};
5use core::{
6 iter::Sum,
7 ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
8};
9use elliptic_curve::{
10 group::{
11 ff::Field,
12 prime::{PrimeCurve, PrimeCurveAffine, PrimeGroup},
13 Curve, Group, GroupEncoding,
14 },
15 rand_core::RngCore,
16 sec1::{FromEncodedPoint, ToEncodedPoint},
17 subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption},
18 ProjectiveArithmetic,
19};
20
21impl ProjectiveArithmetic for NistP256 {
22 type ProjectivePoint = ProjectivePoint;
23}
24
25#[derive(Clone, Copy, Debug)]
27#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
28pub struct ProjectivePoint {
29 x: FieldElement,
30 y: FieldElement,
31 z: FieldElement,
32}
33
34impl Group for ProjectivePoint {
35 type Scalar = Scalar;
36
37 fn random(mut rng: impl RngCore) -> Self {
38 Self::generator() * Scalar::random(&mut rng)
39 }
40
41 fn identity() -> Self {
42 ProjectivePoint::identity()
43 }
44
45 fn generator() -> Self {
46 ProjectivePoint::generator()
47 }
48
49 fn is_identity(&self) -> Choice {
50 self.ct_eq(&Self::identity())
51 }
52
53 #[must_use]
54 fn double(&self) -> Self {
55 ProjectivePoint::double(self)
56 }
57}
58
59impl GroupEncoding for ProjectivePoint {
60 type Repr = CompressedPoint;
61
62 fn from_bytes(bytes: &Self::Repr) -> CtOption<Self> {
63 <AffinePoint as GroupEncoding>::from_bytes(bytes).map(|point| point.into())
64 }
65
66 fn from_bytes_unchecked(bytes: &Self::Repr) -> CtOption<Self> {
67 Self::from_bytes(bytes)
69 }
70
71 fn to_bytes(&self) -> Self::Repr {
72 CompressedPoint::clone_from_slice(self.to_affine().to_encoded_point(true).as_bytes())
73 }
74}
75
76impl PrimeGroup for ProjectivePoint {}
77
78impl Curve for ProjectivePoint {
79 type AffineRepr = AffinePoint;
80
81 fn to_affine(&self) -> AffinePoint {
82 ProjectivePoint::to_affine(self)
83 }
84}
85
86impl PrimeCurve for ProjectivePoint {
87 type Affine = AffinePoint;
88}
89
90impl From<AffinePoint> for ProjectivePoint {
91 fn from(p: AffinePoint) -> Self {
92 let projective = ProjectivePoint {
93 x: p.x,
94 y: p.y,
95 z: FieldElement::one(),
96 };
97 Self::conditional_select(&projective, &Self::identity(), p.infinity)
98 }
99}
100
101impl From<ProjectivePoint> for AffinePoint {
102 fn from(p: ProjectivePoint) -> AffinePoint {
103 p.to_affine()
104 }
105}
106
107impl FromEncodedPoint<NistP256> for ProjectivePoint {
108 fn from_encoded_point(p: &EncodedPoint) -> Option<Self> {
109 AffinePoint::from_encoded_point(p).map(ProjectivePoint::from)
110 }
111}
112
113impl ToEncodedPoint<NistP256> for ProjectivePoint {
114 fn to_encoded_point(&self, compress: bool) -> EncodedPoint {
115 self.to_affine().to_encoded_point(compress)
116 }
117}
118
119impl ConditionallySelectable for ProjectivePoint {
120 fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
121 ProjectivePoint {
122 x: FieldElement::conditional_select(&a.x, &b.x, choice),
123 y: FieldElement::conditional_select(&a.y, &b.y, choice),
124 z: FieldElement::conditional_select(&a.z, &b.z, choice),
125 }
126 }
127}
128
129impl ConstantTimeEq for ProjectivePoint {
130 fn ct_eq(&self, other: &Self) -> Choice {
131 self.to_affine().ct_eq(&other.to_affine())
132 }
133}
134
135impl PartialEq for ProjectivePoint {
136 fn eq(&self, other: &Self) -> bool {
137 self.ct_eq(other).into()
138 }
139}
140
141impl Eq for ProjectivePoint {}
142
143impl ProjectivePoint {
144 pub const fn identity() -> ProjectivePoint {
147 ProjectivePoint {
148 x: FieldElement::zero(),
149 y: FieldElement::one(),
150 z: FieldElement::zero(),
151 }
152 }
153
154 pub fn generator() -> ProjectivePoint {
156 AffinePoint::generator().into()
157 }
158
159 pub fn to_affine(&self) -> AffinePoint {
161 self.z
162 .invert()
163 .map(|zinv| AffinePoint {
164 x: self.x * &zinv,
165 y: self.y * &zinv,
166 infinity: Choice::from(0),
167 })
168 .unwrap_or_else(AffinePoint::identity)
169 }
170
171 fn neg(&self) -> ProjectivePoint {
173 ProjectivePoint {
174 x: self.x,
175 y: self.y.neg(),
176 z: self.z,
177 }
178 }
179
180 fn add(&self, other: &ProjectivePoint) -> ProjectivePoint {
182 let xx = self.x * &other.x; let yy = self.y * &other.y; let zz = self.z * &other.z; let xy_pairs = ((self.x + &self.y) * &(other.x + &other.y)) - &(xx + &yy); let yz_pairs = ((self.y + &self.z) * &(other.y + &other.z)) - &(yy + &zz); let xz_pairs = ((self.x + &self.z) * &(other.x + &other.z)) - &(xx + &zz); let bzz_part = xz_pairs - &(CURVE_EQUATION_B * &zz); let bzz3_part = bzz_part.double() + &bzz_part; let yy_m_bzz3 = yy - &bzz3_part; let yy_p_bzz3 = yy + &bzz3_part; let zz3 = zz.double() + &zz; let bxz_part = (CURVE_EQUATION_B * &xz_pairs) - &(zz3 + &xx); let bxz3_part = bxz_part.double() + &bxz_part; let xx3_m_zz3 = xx.double() + &xx - &zz3; ProjectivePoint {
204 x: (yy_p_bzz3 * &xy_pairs) - &(yz_pairs * &bxz3_part), y: (yy_p_bzz3 * &yy_m_bzz3) + &(xx3_m_zz3 * &bxz3_part), z: (yy_m_bzz3 * &yz_pairs) + &(xy_pairs * &xx3_m_zz3), }
208 }
209
210 fn add_mixed(&self, other: &AffinePoint) -> ProjectivePoint {
212 let xx = self.x * &other.x; let yy = self.y * &other.y; let xy_pairs = ((self.x + &self.y) * &(other.x + &other.y)) - &(xx + &yy); let yz_pairs = (other.y * &self.z) + &self.y; let xz_pairs = (other.x * &self.z) + &self.x; let bz_part = xz_pairs - &(CURVE_EQUATION_B * &self.z); let bz3_part = bz_part.double() + &bz_part; let yy_m_bzz3 = yy - &bz3_part; let yy_p_bzz3 = yy + &bz3_part; let z3 = self.z.double() + &self.z; let bxz_part = (CURVE_EQUATION_B * &xz_pairs) - &(z3 + &xx); let bxz3_part = bxz_part.double() + &bxz_part; let xx3_m_zz3 = xx.double() + &xx - &z3; let mut ret = ProjectivePoint {
233 x: (yy_p_bzz3 * &xy_pairs) - &(yz_pairs * &bxz3_part), y: (yy_p_bzz3 * &yy_m_bzz3) + &(xx3_m_zz3 * &bxz3_part), z: (yy_m_bzz3 * &yz_pairs) + &(xy_pairs * &xx3_m_zz3), };
237 ret.conditional_assign(self, other.is_identity());
238 ret
239 }
240
241 pub fn double(&self) -> ProjectivePoint {
243 let xx = self.x.square(); let yy = self.y.square(); let zz = self.z.square(); let xy2 = (self.x * &self.y).double(); let xz2 = (self.x * &self.z).double(); let bzz_part = (CURVE_EQUATION_B * &zz) - &xz2; let bzz3_part = bzz_part.double() + &bzz_part; let yy_m_bzz3 = yy - &bzz3_part; let yy_p_bzz3 = yy + &bzz3_part; let y_frag = yy_p_bzz3 * &yy_m_bzz3; let x_frag = yy_m_bzz3 * &xy2; let zz3 = zz.double() + &zz; let bxz2_part = (CURVE_EQUATION_B * &xz2) - &(zz3 + &xx); let bxz6_part = bxz2_part.double() + &bxz2_part; let xx3_m_zz3 = xx.double() + &xx - &zz3; let y = y_frag + &(xx3_m_zz3 * &bxz6_part); let yz2 = (self.y * &self.z).double(); let x = x_frag - &(bxz6_part * &yz2); let z = (yz2 * &yy).double().double(); ProjectivePoint { x, y, z }
271 }
272
273 fn sub(&self, other: &ProjectivePoint) -> ProjectivePoint {
275 self.add(&other.neg())
276 }
277
278 fn sub_mixed(&self, other: &AffinePoint) -> ProjectivePoint {
280 self.add_mixed(&other.neg())
281 }
282
283 fn mul(&self, k: &Scalar) -> ProjectivePoint {
285 let mut ret = ProjectivePoint::identity();
286
287 for limb in k.0.iter().rev() {
288 for i in (0..64).rev() {
289 ret = ret.double();
290 ret.conditional_assign(&(ret + self), Choice::from(((limb >> i) & 1u64) as u8));
291 }
292 }
293
294 ret
295 }
296}
297
298impl Default for ProjectivePoint {
299 fn default() -> Self {
300 Self::identity()
301 }
302}
303
304impl Add<ProjectivePoint> for ProjectivePoint {
305 type Output = ProjectivePoint;
306
307 fn add(self, other: ProjectivePoint) -> ProjectivePoint {
308 ProjectivePoint::add(&self, &other)
309 }
310}
311
312impl Add<&ProjectivePoint> for &ProjectivePoint {
313 type Output = ProjectivePoint;
314
315 fn add(self, other: &ProjectivePoint) -> ProjectivePoint {
316 ProjectivePoint::add(self, other)
317 }
318}
319
320impl Add<&ProjectivePoint> for ProjectivePoint {
321 type Output = ProjectivePoint;
322
323 fn add(self, other: &ProjectivePoint) -> ProjectivePoint {
324 ProjectivePoint::add(&self, other)
325 }
326}
327
328impl AddAssign<ProjectivePoint> for ProjectivePoint {
329 fn add_assign(&mut self, rhs: ProjectivePoint) {
330 *self = ProjectivePoint::add(self, &rhs);
331 }
332}
333
334impl AddAssign<&ProjectivePoint> for ProjectivePoint {
335 fn add_assign(&mut self, rhs: &ProjectivePoint) {
336 *self = ProjectivePoint::add(self, rhs);
337 }
338}
339
340impl Add<AffinePoint> for ProjectivePoint {
341 type Output = ProjectivePoint;
342
343 fn add(self, other: AffinePoint) -> ProjectivePoint {
344 ProjectivePoint::add_mixed(&self, &other)
345 }
346}
347
348impl Add<&AffinePoint> for &ProjectivePoint {
349 type Output = ProjectivePoint;
350
351 fn add(self, other: &AffinePoint) -> ProjectivePoint {
352 ProjectivePoint::add_mixed(self, other)
353 }
354}
355
356impl Add<&AffinePoint> for ProjectivePoint {
357 type Output = ProjectivePoint;
358
359 fn add(self, other: &AffinePoint) -> ProjectivePoint {
360 ProjectivePoint::add_mixed(&self, other)
361 }
362}
363
364impl AddAssign<AffinePoint> for ProjectivePoint {
365 fn add_assign(&mut self, rhs: AffinePoint) {
366 *self = ProjectivePoint::add_mixed(self, &rhs);
367 }
368}
369
370impl AddAssign<&AffinePoint> for ProjectivePoint {
371 fn add_assign(&mut self, rhs: &AffinePoint) {
372 *self = ProjectivePoint::add_mixed(self, rhs);
373 }
374}
375
376impl Sum for ProjectivePoint {
377 fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
378 iter.fold(ProjectivePoint::identity(), |a, b| a + b)
379 }
380}
381
382impl<'a> Sum<&'a ProjectivePoint> for ProjectivePoint {
383 fn sum<I: Iterator<Item = &'a ProjectivePoint>>(iter: I) -> Self {
384 iter.cloned().sum()
385 }
386}
387
388impl Sub<ProjectivePoint> for ProjectivePoint {
389 type Output = ProjectivePoint;
390
391 fn sub(self, other: ProjectivePoint) -> ProjectivePoint {
392 ProjectivePoint::sub(&self, &other)
393 }
394}
395
396impl Sub<&ProjectivePoint> for &ProjectivePoint {
397 type Output = ProjectivePoint;
398
399 fn sub(self, other: &ProjectivePoint) -> ProjectivePoint {
400 ProjectivePoint::sub(self, other)
401 }
402}
403
404impl Sub<&ProjectivePoint> for ProjectivePoint {
405 type Output = ProjectivePoint;
406
407 fn sub(self, other: &ProjectivePoint) -> ProjectivePoint {
408 ProjectivePoint::sub(&self, other)
409 }
410}
411
412impl SubAssign<ProjectivePoint> for ProjectivePoint {
413 fn sub_assign(&mut self, rhs: ProjectivePoint) {
414 *self = ProjectivePoint::sub(self, &rhs);
415 }
416}
417
418impl SubAssign<&ProjectivePoint> for ProjectivePoint {
419 fn sub_assign(&mut self, rhs: &ProjectivePoint) {
420 *self = ProjectivePoint::sub(self, rhs);
421 }
422}
423
424impl Sub<AffinePoint> for ProjectivePoint {
425 type Output = ProjectivePoint;
426
427 fn sub(self, other: AffinePoint) -> ProjectivePoint {
428 ProjectivePoint::sub_mixed(&self, &other)
429 }
430}
431
432impl Sub<&AffinePoint> for &ProjectivePoint {
433 type Output = ProjectivePoint;
434
435 fn sub(self, other: &AffinePoint) -> ProjectivePoint {
436 ProjectivePoint::sub_mixed(self, other)
437 }
438}
439
440impl Sub<&AffinePoint> for ProjectivePoint {
441 type Output = ProjectivePoint;
442
443 fn sub(self, other: &AffinePoint) -> ProjectivePoint {
444 ProjectivePoint::sub_mixed(&self, other)
445 }
446}
447
448impl SubAssign<AffinePoint> for ProjectivePoint {
449 fn sub_assign(&mut self, rhs: AffinePoint) {
450 *self = ProjectivePoint::sub_mixed(self, &rhs);
451 }
452}
453
454impl SubAssign<&AffinePoint> for ProjectivePoint {
455 fn sub_assign(&mut self, rhs: &AffinePoint) {
456 *self = ProjectivePoint::sub_mixed(self, rhs);
457 }
458}
459
460impl Mul<Scalar> for ProjectivePoint {
461 type Output = ProjectivePoint;
462
463 fn mul(self, other: Scalar) -> ProjectivePoint {
464 ProjectivePoint::mul(&self, &other)
465 }
466}
467
468impl Mul<&Scalar> for &ProjectivePoint {
469 type Output = ProjectivePoint;
470
471 fn mul(self, other: &Scalar) -> ProjectivePoint {
472 ProjectivePoint::mul(self, other)
473 }
474}
475
476impl Mul<&Scalar> for ProjectivePoint {
477 type Output = ProjectivePoint;
478
479 fn mul(self, other: &Scalar) -> ProjectivePoint {
480 ProjectivePoint::mul(&self, other)
481 }
482}
483
484impl MulAssign<Scalar> for ProjectivePoint {
485 fn mul_assign(&mut self, rhs: Scalar) {
486 *self = ProjectivePoint::mul(self, &rhs);
487 }
488}
489
490impl MulAssign<&Scalar> for ProjectivePoint {
491 fn mul_assign(&mut self, rhs: &Scalar) {
492 *self = ProjectivePoint::mul(self, rhs);
493 }
494}
495
496impl Neg for ProjectivePoint {
497 type Output = ProjectivePoint;
498
499 fn neg(self) -> ProjectivePoint {
500 ProjectivePoint::neg(&self)
501 }
502}
503
504impl<'a> Neg for &'a ProjectivePoint {
505 type Output = ProjectivePoint;
506
507 fn neg(self) -> ProjectivePoint {
508 ProjectivePoint::neg(self)
509 }
510}
511
512#[cfg(test)]
513mod tests {
514 use super::{AffinePoint, ProjectivePoint, Scalar};
515 use crate::test_vectors::group::{ADD_TEST_VECTORS, MUL_TEST_VECTORS};
516 use elliptic_curve::group::{ff::PrimeField, prime::PrimeCurveAffine};
517
518 #[test]
519 fn affine_to_projective() {
520 let basepoint_affine = AffinePoint::generator();
521 let basepoint_projective = ProjectivePoint::generator();
522
523 assert_eq!(
524 ProjectivePoint::from(basepoint_affine),
525 basepoint_projective,
526 );
527 assert_eq!(basepoint_projective.to_affine(), basepoint_affine);
528 assert!(!bool::from(basepoint_projective.to_affine().is_identity()));
529
530 assert!(bool::from(
531 ProjectivePoint::identity().to_affine().is_identity()
532 ));
533 }
534
535 #[test]
536 fn projective_identity_addition() {
537 let identity = ProjectivePoint::identity();
538 let generator = ProjectivePoint::generator();
539
540 assert_eq!(identity + &generator, generator);
541 assert_eq!(generator + &identity, generator);
542 }
543
544 #[test]
545 fn projective_mixed_addition() {
546 let identity = ProjectivePoint::identity();
547 let basepoint_affine = AffinePoint::generator();
548 let basepoint_projective = ProjectivePoint::generator();
549
550 assert_eq!(identity + &basepoint_affine, basepoint_projective);
551 assert_eq!(
552 basepoint_projective + &basepoint_affine,
553 basepoint_projective + &basepoint_projective
554 );
555 }
556
557 #[test]
558 fn test_vector_repeated_add() {
559 let generator = ProjectivePoint::generator();
560 let mut p = generator;
561
562 for i in 0..ADD_TEST_VECTORS.len() {
563 let affine = p.to_affine();
564
565 let (expected_x, expected_y) = ADD_TEST_VECTORS[i];
566 assert_eq!(affine.x.to_bytes(), expected_x.into());
567 assert_eq!(affine.y.to_bytes(), expected_y.into());
568
569 p += &generator;
570 }
571 }
572
573 #[test]
574 fn test_vector_repeated_add_mixed() {
575 let generator = AffinePoint::generator();
576 let mut p = ProjectivePoint::generator();
577
578 for i in 0..ADD_TEST_VECTORS.len() {
579 let affine = p.to_affine();
580
581 let (expected_x, expected_y) = ADD_TEST_VECTORS[i];
582 assert_eq!(affine.x.to_bytes(), expected_x.into());
583 assert_eq!(affine.y.to_bytes(), expected_y.into());
584
585 p += &generator;
586 }
587 }
588
589 #[test]
590 fn test_vector_add_mixed_identity() {
591 let generator = ProjectivePoint::generator();
592 let p0 = generator + ProjectivePoint::identity();
593 let p1 = generator + AffinePoint::identity();
594 assert_eq!(p0, p1);
595 }
596
597 #[test]
598 fn test_vector_double_generator() {
599 let generator = ProjectivePoint::generator();
600 let mut p = generator;
601
602 for i in 0..2 {
603 let affine = p.to_affine();
604
605 let (expected_x, expected_y) = ADD_TEST_VECTORS[i];
606 assert_eq!(affine.x.to_bytes(), expected_x.into());
607 assert_eq!(affine.y.to_bytes(), expected_y.into());
608
609 p = p.double();
610 }
611 }
612
613 #[test]
614 fn projective_add_vs_double() {
615 let generator = ProjectivePoint::generator();
616 assert_eq!(generator + &generator, generator.double());
617 }
618
619 #[test]
620 fn projective_add_and_sub() {
621 let basepoint_affine = AffinePoint::generator();
622 let basepoint_projective = ProjectivePoint::generator();
623
624 assert_eq!(
625 (basepoint_projective + &basepoint_projective) - &basepoint_projective,
626 basepoint_projective
627 );
628 assert_eq!(
629 (basepoint_projective + &basepoint_affine) - &basepoint_affine,
630 basepoint_projective
631 );
632 }
633
634 #[test]
635 fn projective_double_and_sub() {
636 let generator = ProjectivePoint::generator();
637 assert_eq!(generator.double() - &generator, generator);
638 }
639
640 #[test]
641 fn test_vector_scalar_mult() {
642 let generator = ProjectivePoint::generator();
643
644 for (k, coords) in ADD_TEST_VECTORS
645 .iter()
646 .enumerate()
647 .map(|(k, coords)| (Scalar::from(k as u64 + 1), *coords))
648 .chain(
649 MUL_TEST_VECTORS
650 .iter()
651 .cloned()
652 .map(|(k, x, y)| (Scalar::from_repr(k.into()).unwrap(), (x, y))),
653 )
654 {
655 let res = (generator * &k).to_affine();
656 assert_eq!(res.x.to_bytes(), coords.0.into());
657 assert_eq!(res.y.to_bytes(), coords.1.into());
658 }
659 }
660}