7#ifndef BOTAN_PCURVES_H_
8#define BOTAN_PCURVES_H_
10#include <botan/internal/pcurves_id.h>
12#include <botan/concepts.h>
13#include <botan/secmem.h>
14#include <botan/types.h>
23class RandomNumberGenerator;
37 static const size_t MaximumBitLength = 521;
39 static const size_t MaximumByteLength = (MaximumBitLength + 7) / 8;
42 static const size_t StorageWords = (MaximumByteLength +
sizeof(word) - 1) /
sizeof(word);
44 static std::shared_ptr<const PrimeOrderCurve>
from_name(std::string_view
name) {
45 if(
auto id = PrimeOrderCurveId::from_string(
name)) {
46 return PrimeOrderCurve::from_id(
id.value());
55 typedef std::shared_ptr<const PrimeOrderCurve>
CurvePtr;
72 size_t bytes()
const {
return m_curve->scalar_bytes(); }
77 template <concepts::resizable_
byte_buffer T = std::vector<u
int8_t>>
79 T bytes(this->bytes());
80 m_curve->serialize_scalar(bytes, *
this);
124 bool is_zero()
const {
return m_curve->scalar_is_zero(*
this); }
126 const auto&
_curve()
const {
return m_curve; }
128 const auto&
_value()
const {
return m_value; }
157 size_t bytes()
const {
return 1 + 2 * m_curve->field_element_bytes(); }
167 template <concepts::resizable_
byte_buffer T = std::vector<u
int8_t>>
169 T bytes(this->bytes());
170 m_curve->serialize_point(bytes, *
this);
177 template <concepts::resizable_
byte_buffer T = std::vector<u
int8_t>>
179 T bytes(this->compressed_bytes());
180 m_curve->serialize_point_compressed(bytes, *
this);
187 template <concepts::resizable_
byte_buffer T = secure_vector<u
int8_t>>
190 m_curve->serialize_point_x(bytes, *
this);
197 bool is_identity()
const {
return m_curve->affine_point_is_identity(*
this); }
199 const auto&
_curve()
const {
return m_curve; }
201 const auto&
_x()
const {
return m_x; }
203 const auto&
_y()
const {
return m_y; }
249 return x.m_curve->point_add(x, y);
253 return x.m_curve->point_add_mixed(x, y);
256 const auto&
_curve()
const {
return m_curve; }
258 const auto&
_x()
const {
return m_x; }
260 const auto&
_y()
const {
return m_y; }
262 const auto&
_z()
const {
return m_z; }
270 m_curve(std::move(curve)), m_x(x), m_y(y), m_z(z) {}
324 const Scalar& s2)
const = 0;
335 const Scalar& s2)
const = 0;
417 std::span<const uint8_t> input,
418 std::span<const uint8_t> domain_sep)
const = 0;
426 std::span<const uint8_t> input,
427 std::span<const uint8_t> domain_sep)
const = 0;
Identifier for a named prime order curve.
T serialize_compressed() const
size_t compressed_bytes() const
AffinePoint & operator=(const AffinePoint &other)=default
static AffinePoint generator(CurvePtr curve)
static AffinePoint _create(CurvePtr curve, StorageUnit x, StorageUnit y)
AffinePoint(AffinePoint &&other)=default
const auto & _curve() const
AffinePoint & operator=(AffinePoint &&other)=default
AffinePoint(const AffinePoint &other)=default
virtual ~PrecomputedMul2Table()=default
friend ProjectivePoint operator+(const ProjectivePoint &x, const AffinePoint &y)
AffinePoint to_affine() const
ProjectivePoint & operator=(const ProjectivePoint &other)=default
ProjectivePoint(const ProjectivePoint &other)=default
ProjectivePoint(ProjectivePoint &&other)=default
ProjectivePoint & operator=(ProjectivePoint &&other)=default
static ProjectivePoint _create(CurvePtr curve, StorageUnit x, StorageUnit y, StorageUnit z)
friend ProjectivePoint operator+(const ProjectivePoint &x, const ProjectivePoint &y)
ProjectivePoint dbl() const
~ProjectivePoint()=default
static ProjectivePoint from_affine(const AffinePoint &pt)
ProjectivePoint negate() const
const auto & _curve() const
Scalar(const Scalar &other)=default
friend Scalar operator-(const Scalar &a, const Scalar &b)
const auto & _curve() const
const auto & _value() const
friend Scalar operator*(const Scalar &a, const Scalar &b)
Scalar & operator=(Scalar &&other)=default
Scalar(Scalar &&other)=default
Scalar & operator=(const Scalar &other)=default
friend bool operator==(const Scalar &a, const Scalar &b)
static Scalar _create(CurvePtr curve, StorageUnit v)
friend Scalar operator+(const Scalar &a, const Scalar &b)
virtual ~PrimeOrderCurve()=default
virtual Scalar scalar_add(const Scalar &a, const Scalar &b) const =0
virtual std::unique_ptr< const PrecomputedMul2Table > mul2_setup(const AffinePoint &pt1, const AffinePoint &pt2) const =0
Setup a table for 2-ary multiplication.
virtual AffinePoint hash_to_curve_nu(std::string_view hash, std::span< const uint8_t > input, std::span< const uint8_t > domain_sep) const =0
virtual void serialize_point(std::span< uint8_t > bytes, const AffinePoint &pt) const =0
virtual std::optional< AffinePoint > deserialize_point(std::span< const uint8_t > bytes) const =0
virtual Scalar scalar_zero() const =0
virtual size_t field_element_bytes() const =0
virtual size_t order_bits() const =0
Return the bit length of the group order.
virtual size_t scalar_bytes() const =0
Return the byte length of the scalar element.
virtual bool scalar_is_zero(const Scalar &s) const =0
std::array< word, StorageWords > StorageUnit
virtual std::optional< Scalar > deserialize_scalar(std::span< const uint8_t > bytes) const =0
virtual Scalar scalar_invert(const Scalar &s) const =0
virtual Scalar scalar_mul(const Scalar &a, const Scalar &b) const =0
virtual void serialize_point_compressed(std::span< uint8_t > bytes, const AffinePoint &pt) const =0
virtual Scalar scalar_from_u32(uint32_t x) const =0
virtual ProjectivePoint hash_to_curve_ro(std::string_view hash, std::span< const uint8_t > input, std::span< const uint8_t > domain_sep) const =0
virtual Scalar random_scalar(RandomNumberGenerator &rng) const =0
virtual AffinePoint point_to_affine(const ProjectivePoint &pt) const =0
virtual AffinePoint generator() const =0
Return the standard generator.
virtual Scalar scalar_one() const =0
virtual ProjectivePoint point_negate(const ProjectivePoint &pt) const =0
virtual Scalar scalar_negate(const Scalar &s) const =0
virtual void serialize_point_x(std::span< uint8_t > bytes, const AffinePoint &pt) const =0
virtual ProjectivePoint mul_by_g(const Scalar &scalar, RandomNumberGenerator &rng) const =0
virtual std::optional< Scalar > scalar_from_wide_bytes(std::span< const uint8_t > bytes) const =0
virtual bool mul2_vartime_x_mod_order_eq(const PrecomputedMul2Table &table, const Scalar &v, const Scalar &s1, const Scalar &s2) const =0
virtual std::optional< ProjectivePoint > mul2_vartime(const PrecomputedMul2Table &table, const Scalar &s1, const Scalar &s2) const =0
virtual ProjectivePoint point_to_projective(const AffinePoint &pt) const =0
static std::shared_ptr< const PrimeOrderCurve > from_name(std::string_view name)
virtual ProjectivePoint mul(const AffinePoint &pt, const Scalar &scalar, RandomNumberGenerator &rng) const =0
virtual Scalar scalar_square(const Scalar &s) const =0
virtual bool scalar_equal(const Scalar &a, const Scalar &b) const =0
virtual ProjectivePoint point_add_mixed(const ProjectivePoint &a, const AffinePoint &b) const =0
virtual bool affine_point_is_identity(const AffinePoint &pt) const =0
std::shared_ptr< const PrimeOrderCurve > CurvePtr
virtual Scalar base_point_mul_x_mod_order(const Scalar &scalar, RandomNumberGenerator &rng) const =0
virtual ProjectivePoint point_add(const ProjectivePoint &a, const ProjectivePoint &b) const =0
virtual void serialize_scalar(std::span< uint8_t > bytes, const Scalar &scalar) const =0
virtual Scalar scalar_sub(const Scalar &a, const Scalar &b) const =0
virtual ProjectivePoint point_double(const ProjectivePoint &pt) const =0
int(* final)(unsigned char *, CTX *)
std::vector< T, secure_allocator< T > > secure_vector