Botan 3.5.0
Crypto and TLS for C&
Botan::ProjectiveCurvePoint< FieldElement, Params > Class Template Reference

#include <pcurves_impl.h>

Public Types

using AffinePoint = AffineCurvePoint<FieldElement, Params>
 
using Self = ProjectiveCurvePoint<FieldElement, Params>
 

Public Member Functions

constexpr void conditional_assign (CT::Choice cond, const Self &pt)
 
constexpr void ct_poison () const
 
constexpr void ct_unpoison () const
 
constexpr Self dbl () const
 
constexpr Self dbl_n (size_t n) const
 
constexpr CT::Choice is_identity () const
 
constexpr Self negate () const
 
constexpr Selfoperator+= (const AffinePoint &other)
 
constexpr Selfoperator+= (const Self &other)
 
ProjectiveCurvePointoperator= (const Self &other)=default
 
ProjectiveCurvePointoperator= (Self &&other)=default
 
constexpr ProjectiveCurvePoint ()
 
constexpr ProjectiveCurvePoint (const FieldElement &x, const FieldElement &y)
 
constexpr ProjectiveCurvePoint (const FieldElement &x, const FieldElement &y, const FieldElement &z)
 
 ProjectiveCurvePoint (const Self &other)=default
 
 ProjectiveCurvePoint (Self &&other)=default
 
void randomize_rep (RandomNumberGenerator &rng)
 
constexpr AffinePoint to_affine () const
 
constexpr const FieldElement & x () const
 
constexpr const FieldElement & y () const
 
constexpr const FieldElement & z () const
 

Static Public Member Functions

static constexpr Self add (const Self &a, const Self &b)
 
static constexpr Self add_mixed (const Self &a, const AffinePoint &b)
 
static constexpr Self from_affine (const AffinePoint &pt)
 
static constexpr Self identity ()
 
static std::vector< AffinePointto_affine_batch (std::span< const Self > projective)
 

Static Public Attributes

static constexpr FieldElement A = FieldElement::from_words(Params::AW)
 
static constexpr bool A_is_minus_3 = (A == FieldElement::constant(-3)).as_bool()
 
static constexpr bool A_is_zero = A.is_zero().as_bool()
 

Friends

constexpr Self operator+ (const AffinePoint &a, const Self &b)
 
constexpr Self operator+ (const Self &a, const AffinePoint &b)
 
constexpr Self operator+ (const Self &a, const Self &b)
 
constexpr Self operator- (const Self &a, const Self &b)
 

Detailed Description

template<typename FieldElement, typename Params>
class Botan::ProjectiveCurvePoint< FieldElement, Params >

Definition at line 541 of file pcurves_impl.h.

Member Typedef Documentation

◆ AffinePoint

template<typename FieldElement , typename Params >
using Botan::ProjectiveCurvePoint< FieldElement, Params >::AffinePoint = AffineCurvePoint<FieldElement, Params>

Definition at line 552 of file pcurves_impl.h.

◆ Self

template<typename FieldElement , typename Params >
using Botan::ProjectiveCurvePoint< FieldElement, Params >::Self = ProjectiveCurvePoint<FieldElement, Params>

Definition at line 551 of file pcurves_impl.h.

Constructor & Destructor Documentation

◆ ProjectiveCurvePoint() [1/5]

template<typename FieldElement , typename Params >
Botan::ProjectiveCurvePoint< FieldElement, Params >::ProjectiveCurvePoint ( )
inlineconstexpr

Definition at line 564 of file pcurves_impl.h.

564 :
565 m_x(FieldElement::zero()), m_y(FieldElement::one()), m_z(FieldElement::zero()) {}

Referenced by Botan::ProjectiveCurvePoint< FieldElement, Params >::from_affine().

◆ ProjectiveCurvePoint() [2/5]

template<typename FieldElement , typename Params >
Botan::ProjectiveCurvePoint< FieldElement, Params >::ProjectiveCurvePoint ( const FieldElement & x,
const FieldElement & y )
inlineconstexpr

Definition at line 567 of file pcurves_impl.h.

567 :
568 m_x(x), m_y(y), m_z(FieldElement::one()) {}
constexpr const FieldElement & x() const
constexpr const FieldElement & y() const

◆ ProjectiveCurvePoint() [3/5]

template<typename FieldElement , typename Params >
Botan::ProjectiveCurvePoint< FieldElement, Params >::ProjectiveCurvePoint ( const FieldElement & x,
const FieldElement & y,
const FieldElement & z )
inlineconstexpr

Definition at line 570 of file pcurves_impl.h.

570 :
571 m_x(x), m_y(y), m_z(z) {}
constexpr const FieldElement & z() const

◆ ProjectiveCurvePoint() [4/5]

template<typename FieldElement , typename Params >
Botan::ProjectiveCurvePoint< FieldElement, Params >::ProjectiveCurvePoint ( const Self & other)
default

◆ ProjectiveCurvePoint() [5/5]

template<typename FieldElement , typename Params >
Botan::ProjectiveCurvePoint< FieldElement, Params >::ProjectiveCurvePoint ( Self && other)
default

Member Function Documentation

◆ add()

template<typename FieldElement , typename Params >
static constexpr Self Botan::ProjectiveCurvePoint< FieldElement, Params >::add ( const Self & a,
const Self & b )
inlinestaticconstexpr

Definition at line 655 of file pcurves_impl.h.

655 {
656 const auto a_is_identity = a.is_identity();
657 const auto b_is_identity = b.is_identity();
658 if((a_is_identity && b_is_identity).as_bool()) {
659 return Self::identity();
660 }
661
662 /*
663 https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-1998-cmo-2
664 */
665
666 const auto Z1Z1 = a.z().square();
667 const auto Z2Z2 = b.z().square();
668 const auto U1 = a.x() * Z2Z2;
669 const auto U2 = b.x() * Z1Z1;
670 const auto S1 = a.y() * b.z() * Z2Z2;
671 const auto S2 = b.y() * a.z() * Z1Z1;
672 const auto H = U2 - U1;
673 const auto r = S2 - S1;
674
675 if(r.is_zero().as_bool()) {
676 return a.dbl();
677 }
678
679 const auto HH = H.square();
680 const auto HHH = H * HH;
681 const auto V = U1 * HH;
682 const auto t2 = r.square();
683 const auto t3 = V + V;
684 const auto t4 = t2 - HHH;
685 auto X3 = t4 - t3;
686 const auto t5 = V - X3;
687 const auto t6 = S1 * HHH;
688 const auto t7 = r * t5;
689 auto Y3 = t7 - t6;
690 const auto t8 = b.z() * H;
691 auto Z3 = a.z() * t8;
692
693 // TODO these could be combined
694 // if a is identity then return b
695 X3.conditional_assign(a_is_identity, b.x());
696 Y3.conditional_assign(a_is_identity, b.y());
697 Z3.conditional_assign(a_is_identity, b.z());
698
699 // if b is identity then return a
700 X3.conditional_assign(b_is_identity, a.x());
701 Y3.conditional_assign(b_is_identity, a.y());
702 Z3.conditional_assign(b_is_identity, a.z());
703
704 return Self(X3, Y3, Z3);
705 }
ProjectiveCurvePoint< FieldElement, Params > Self
static constexpr Self identity()

References Botan::ProjectiveCurvePoint< FieldElement, Params >::dbl(), Botan::ProjectiveCurvePoint< FieldElement, Params >::identity(), Botan::ProjectiveCurvePoint< FieldElement, Params >::is_identity(), Botan::ProjectiveCurvePoint< FieldElement, Params >::x(), Botan::ProjectiveCurvePoint< FieldElement, Params >::y(), and Botan::ProjectiveCurvePoint< FieldElement, Params >::z().

◆ add_mixed()

template<typename FieldElement , typename Params >
static constexpr Self Botan::ProjectiveCurvePoint< FieldElement, Params >::add_mixed ( const Self & a,
const AffinePoint & b )
inlinestaticconstexpr

Definition at line 604 of file pcurves_impl.h.

604 {
605 const auto a_is_identity = a.is_identity();
606 const auto b_is_identity = b.is_identity();
607 if((a_is_identity && b_is_identity).as_bool()) {
608 return Self::identity();
609 }
610
611 /*
612 https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-1998-cmo-2
613
614 12M + 4S + 6add + 1*2
615 */
616
617 const auto Z1Z1 = a.z().square();
618 const auto U2 = b.x() * Z1Z1;
619 const auto S2 = b.y() * a.z() * Z1Z1;
620 const auto H = U2 - a.x();
621 const auto r = S2 - a.y();
622
623 // If r is zero then we are in the doubling case
624 if(r.is_zero().as_bool()) {
625 return a.dbl();
626 }
627
628 const auto HH = H.square();
629 const auto HHH = H * HH;
630 const auto V = a.x() * HH;
631 const auto t2 = r.square();
632 const auto t3 = V + V;
633 const auto t4 = t2 - HHH;
634 auto X3 = t4 - t3;
635 const auto t5 = V - X3;
636 const auto t6 = a.y() * HHH;
637 const auto t7 = r * t5;
638 auto Y3 = t7 - t6;
639 auto Z3 = a.z() * H;
640
641 // TODO these could be combined
642 // if a is identity then return b
643 X3.conditional_assign(a_is_identity, b.x());
644 Y3.conditional_assign(a_is_identity, b.y());
645 Z3.conditional_assign(a_is_identity, FieldElement::one());
646
647 // if b is identity then return a
648 X3.conditional_assign(b_is_identity, a.x());
649 Y3.conditional_assign(b_is_identity, a.y());
650 Z3.conditional_assign(b_is_identity, a.z());
651
652 return Self(X3, Y3, Z3);
653 }

References Botan::ProjectiveCurvePoint< FieldElement, Params >::dbl(), Botan::ProjectiveCurvePoint< FieldElement, Params >::identity(), Botan::AffineCurvePoint< FieldElement, Params >::is_identity(), Botan::ProjectiveCurvePoint< FieldElement, Params >::is_identity(), Botan::AffineCurvePoint< FieldElement, Params >::x(), Botan::ProjectiveCurvePoint< FieldElement, Params >::x(), Botan::AffineCurvePoint< FieldElement, Params >::y(), Botan::ProjectiveCurvePoint< FieldElement, Params >::y(), and Botan::ProjectiveCurvePoint< FieldElement, Params >::z().

◆ conditional_assign()

template<typename FieldElement , typename Params >
void Botan::ProjectiveCurvePoint< FieldElement, Params >::conditional_assign ( CT::Choice cond,
const Self & pt )
inlineconstexpr

Definition at line 598 of file pcurves_impl.h.

598 {
599 m_x.conditional_assign(cond, pt.x());
600 m_y.conditional_assign(cond, pt.y());
601 m_z.conditional_assign(cond, pt.z());
602 }

References Botan::ProjectiveCurvePoint< FieldElement, Params >::x(), Botan::ProjectiveCurvePoint< FieldElement, Params >::y(), and Botan::ProjectiveCurvePoint< FieldElement, Params >::z().

◆ ct_poison()

template<typename FieldElement , typename Params >
void Botan::ProjectiveCurvePoint< FieldElement, Params >::ct_poison ( ) const
inlineconstexpr

◆ ct_unpoison()

template<typename FieldElement , typename Params >
void Botan::ProjectiveCurvePoint< FieldElement, Params >::ct_unpoison ( ) const
inlineconstexpr

Definition at line 847 of file pcurves_impl.h.

847 {
848 x().ct_unpoison();
849 y().ct_unpoison();
850 z().ct_unpoison();
851 }

References Botan::ProjectiveCurvePoint< FieldElement, Params >::x(), Botan::ProjectiveCurvePoint< FieldElement, Params >::y(), and Botan::ProjectiveCurvePoint< FieldElement, Params >::z().

◆ dbl()

template<typename FieldElement , typename Params >
Self Botan::ProjectiveCurvePoint< FieldElement, Params >::dbl ( ) const
inlineconstexpr

Definition at line 718 of file pcurves_impl.h.

718 {
719 //https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-1998-cmo-2
720
721 FieldElement m = FieldElement::zero();
722
723 if constexpr(Self::A_is_minus_3) {
724 /*
725 if a == -3 then
726 3*x^2 + a*z^4 == 3*x^2 - 3*z^4 == 3*(x^2-z^4) == 3*(x-z^2)*(x+z^2)
727
728 Cost: 2M + 2A + 1*3
729 */
730 const auto z2 = z().square();
731 m = (x() - z2).mul3() * (x() + z2);
732 } else if constexpr(Self::A_is_zero) {
733 // If a == 0 then 3*x^2 + a*z^4 == 3*x^2
734 // Cost: 1S + 1*3
735 m = x().square().mul3();
736 } else {
737 // Cost: 1M + 3S + 1A + 1*3
738 const auto z2 = z().square();
739 m = x().square().mul3() + A * z2.square();
740 }
741
742 const auto y2 = y().square();
743 const auto s = x().mul4() * y2;
744 const auto nx = m.square() - s.mul2();
745 const auto ny = m * (s - nx) - y2.square().mul8();
746 const auto nz = y().mul2() * z();
747
748 return Self(nx, ny, nz);
749 }
static constexpr bool A_is_zero
static constexpr bool A_is_minus_3
static constexpr FieldElement A

References Botan::ProjectiveCurvePoint< FieldElement, Params >::A, Botan::ProjectiveCurvePoint< FieldElement, Params >::A_is_minus_3, Botan::ProjectiveCurvePoint< FieldElement, Params >::A_is_zero, Botan::ProjectiveCurvePoint< FieldElement, Params >::x(), Botan::ProjectiveCurvePoint< FieldElement, Params >::y(), and Botan::ProjectiveCurvePoint< FieldElement, Params >::z().

Referenced by Botan::ProjectiveCurvePoint< FieldElement, Params >::add(), Botan::ProjectiveCurvePoint< FieldElement, Params >::add_mixed(), and Botan::ProjectiveCurvePoint< FieldElement, Params >::dbl_n().

◆ dbl_n()

template<typename FieldElement , typename Params >
Self Botan::ProjectiveCurvePoint< FieldElement, Params >::dbl_n ( size_t n) const
inlineconstexpr

Definition at line 707 of file pcurves_impl.h.

707 {
708 // TODO it is possible to optimize this by carrying over values from
709 // the previous iteration into the next
710
711 Self pt = (*this);
712 for(size_t i = 0; i != n; ++i) {
713 pt = pt.dbl();
714 }
715 return pt;
716 }

References Botan::ProjectiveCurvePoint< FieldElement, Params >::dbl().

◆ from_affine()

template<typename FieldElement , typename Params >
static constexpr Self Botan::ProjectiveCurvePoint< FieldElement, Params >::from_affine ( const AffinePoint & pt)
inlinestaticconstexpr

◆ identity()

template<typename FieldElement , typename Params >
static constexpr Self Botan::ProjectiveCurvePoint< FieldElement, Params >::identity ( )
inlinestaticconstexpr

◆ is_identity()

◆ negate()

◆ operator+=() [1/2]

template<typename FieldElement , typename Params >
Self & Botan::ProjectiveCurvePoint< FieldElement, Params >::operator+= ( const AffinePoint & other)
inlineconstexpr

Definition at line 589 of file pcurves_impl.h.

589 {
590 (*this) = (*this) + other;
591 return (*this);
592 }

◆ operator+=() [2/2]

template<typename FieldElement , typename Params >
Self & Botan::ProjectiveCurvePoint< FieldElement, Params >::operator+= ( const Self & other)
inlineconstexpr

Definition at line 584 of file pcurves_impl.h.

584 {
585 (*this) = (*this) + other;
586 return (*this);
587 }

◆ operator=() [1/2]

template<typename FieldElement , typename Params >
ProjectiveCurvePoint & Botan::ProjectiveCurvePoint< FieldElement, Params >::operator= ( const Self & other)
default

◆ operator=() [2/2]

template<typename FieldElement , typename Params >
ProjectiveCurvePoint & Botan::ProjectiveCurvePoint< FieldElement, Params >::operator= ( Self && other)
default

◆ randomize_rep()

template<typename FieldElement , typename Params >
void Botan::ProjectiveCurvePoint< FieldElement, Params >::randomize_rep ( RandomNumberGenerator & rng)
inline

Definition at line 824 of file pcurves_impl.h.

824 {
825 auto r = FieldElement::random(rng);
826
827 auto r2 = r.square();
828 auto r3 = r2 * r;
829
830 m_x *= r2;
831 m_y *= r3;
832 m_z *= r;
833 }

◆ to_affine()

template<typename FieldElement , typename Params >
AffinePoint Botan::ProjectiveCurvePoint< FieldElement, Params >::to_affine ( ) const
inlineconstexpr

Definition at line 753 of file pcurves_impl.h.

753 {
754 // Not strictly required right? - default should work as long
755 // as (0,0) is identity and invert returns 0 on 0
756 if(this->is_identity().as_bool()) {
757 return AffinePoint::identity();
758 }
759
760 const auto z_inv = m_z.invert();
761 const auto z2_inv = z_inv.square();
762 const auto z3_inv = z_inv * z2_inv;
763
764 const auto x = m_x * z2_inv;
765 const auto y = m_y * z3_inv;
766 return AffinePoint(x, y);
767 }
static constexpr Self identity()
constexpr CT::Choice is_identity() const
AffineCurvePoint< FieldElement, Params > AffinePoint

References Botan::AffineCurvePoint< FieldElement, Params >::identity(), Botan::ProjectiveCurvePoint< FieldElement, Params >::is_identity(), Botan::ProjectiveCurvePoint< FieldElement, Params >::x(), and Botan::ProjectiveCurvePoint< FieldElement, Params >::y().

◆ to_affine_batch()

template<typename FieldElement , typename Params >
static std::vector< AffinePoint > Botan::ProjectiveCurvePoint< FieldElement, Params >::to_affine_batch ( std::span< const Self > projective)
inlinestatic

Definition at line 769 of file pcurves_impl.h.

769 {
770 const size_t N = projective.size();
771 std::vector<AffinePoint> affine(N, AffinePoint::identity());
772
773 bool any_identity = false;
774 for(size_t i = 0; i != N; ++i) {
775 if(projective[i].is_identity().as_bool()) {
776 any_identity = true;
777 // If any of the elements are the identity we fall back to
778 // performing the conversion without a batch
779 break;
780 }
781 }
782
783 if(N <= 2 || any_identity) {
784 for(size_t i = 0; i != N; ++i) {
785 affine[i] = projective[i].to_affine();
786 }
787 } else {
788 std::vector<FieldElement> c(N);
789
790 /*
791 Batch projective->affine using Montgomery's trick
792
793 See Algorithm 2.26 in "Guide to Elliptic Curve Cryptography"
794 (Hankerson, Menezes, Vanstone)
795 */
796
797 c[0] = projective[0].z();
798 for(size_t i = 1; i != N; ++i) {
799 c[i] = c[i - 1] * projective[i].z();
800 }
801
802 auto s_inv = c[N - 1].invert();
803
804 for(size_t i = N - 1; i > 0; --i) {
805 const auto& p = projective[i];
806
807 const auto z_inv = s_inv * c[i - 1];
808 const auto z2_inv = z_inv.square();
809 const auto z3_inv = z_inv * z2_inv;
810
811 s_inv = s_inv * p.z();
812
813 affine[i] = AffinePoint(p.x() * z2_inv, p.y() * z3_inv);
814 }
815
816 const auto z2_inv = s_inv.square();
817 const auto z3_inv = s_inv * z2_inv;
818 affine[0] = AffinePoint(projective[0].x() * z2_inv, projective[0].y() * z3_inv);
819 }
820
821 return affine;
822 }

References Botan::AffineCurvePoint< FieldElement, Params >::identity(), Botan::ProjectiveCurvePoint< FieldElement, Params >::is_identity(), Botan::ProjectiveCurvePoint< FieldElement, Params >::x(), and Botan::ProjectiveCurvePoint< FieldElement, Params >::y().

◆ x()

◆ y()

◆ z()

Friends And Related Symbol Documentation

◆ operator+ [1/3]

template<typename FieldElement , typename Params >
Self operator+ ( const AffinePoint & a,
const Self & b )
friend

Definition at line 582 of file pcurves_impl.h.

582{ return Self::add_mixed(b, a); }
static constexpr Self add_mixed(const Self &a, const AffinePoint &b)

◆ operator+ [2/3]

template<typename FieldElement , typename Params >
Self operator+ ( const Self & a,
const AffinePoint & b )
friend

Definition at line 580 of file pcurves_impl.h.

580{ return Self::add_mixed(a, b); }

◆ operator+ [3/3]

template<typename FieldElement , typename Params >
Self operator+ ( const Self & a,
const Self & b )
friend

Definition at line 578 of file pcurves_impl.h.

578{ return Self::add(a, b); }
static constexpr Self add(const Self &a, const Self &b)

◆ operator-

template<typename FieldElement , typename Params >
Self operator- ( const Self & a,
const Self & b )
friend

Definition at line 594 of file pcurves_impl.h.

594{ return a + b.negate(); }

Member Data Documentation

◆ A

template<typename FieldElement , typename Params >
FieldElement Botan::ProjectiveCurvePoint< FieldElement, Params >::A = FieldElement::from_words(Params::AW)
staticconstexpr

◆ A_is_minus_3

template<typename FieldElement , typename Params >
bool Botan::ProjectiveCurvePoint< FieldElement, Params >::A_is_minus_3 = (A == FieldElement::constant(-3)).as_bool()
staticconstexpr

◆ A_is_zero

template<typename FieldElement , typename Params >
bool Botan::ProjectiveCurvePoint< FieldElement, Params >::A_is_zero = A.is_zero().as_bool()
staticconstexpr

The documentation for this class was generated from the following file: