7#ifndef BOTAN_PCURVES_WRAP_H_
8#define BOTAN_PCURVES_WRAP_H_
10#include <botan/internal/pcurves.h>
11#include <botan/internal/pcurves_impl.h>
17 { C::scalar_invert(s) } -> std::same_as<typename C::Scalar>;
27 const WindowedMul2Table<C, WindowBits>&
table()
const {
return m_table; }
33 WindowedMul2Table<C, WindowBits> m_table;
39 size_t order_bits()
const override {
return C::OrderBits; }
46 return stash(m_mul_by_g.mul(from_stash(scalar), rng));
50 auto tbl = WindowedMulTable<C, 4>(from_stash(pt));
51 return stash(tbl.mul(from_stash(scalar), rng));
56 return std::make_unique<PrecomputedMul2TableC>(from_stash(x), from_stash(y));
61 const Scalar& s2)
const override {
64 auto pt = table.
table().mul2_vartime(from_stash(s1), from_stash(s2));
65 if(pt.is_identity().as_bool()) {
70 }
catch(std::bad_cast&) {
78 const Scalar& s2)
const override {
81 const auto pt = table.
table().mul2_vartime(from_stash(s1), from_stash(s2));
83 if(pt.is_identity().as_bool()) {
100 const auto z2 = pt.z().square();
102 std::array<uint8_t, C::Scalar::BYTES> v_bytes;
103 from_stash(v).serialize_to(v_bytes);
105 if(
const auto fe_v = C::FieldElement::deserialize(v_bytes)) {
106 if((*fe_v * z2 == pt.x()).as_bool()) {
121 if constexpr(C::OrderIsLessThanField) {
136 const auto n = C::FieldElement::from_words(C::NW);
137 const auto neg_n = n.negate().to_words();
139 const auto vw = fe_v->to_words();
140 if(
bigint_ct_is_lt(vw.data(), vw.size(), neg_n.data(), neg_n.size()).as_bool()) {
141 return (((*fe_v + n) * z2) == pt.x()).as_bool();
147 }
catch(std::bad_cast&) {
153 auto pt = m_mul_by_g.mul(from_stash(scalar), rng);
154 std::array<uint8_t, C::FieldElement::BYTES> x_bytes;
155 to_affine_x<C>(pt).serialize_to(std::span{x_bytes});
156 return stash(C::Scalar::from_wide_bytes(std::span<const uint8_t, C::FieldElement::BYTES>{x_bytes}));
162 return stash(to_affine<C>(from_stash(pt)));
166 return stash(C::ProjectivePoint::from_affine(from_stash(pt)));
172 return stash(from_stash(a) + from_stash(
b));
176 return stash(from_stash(a) + from_stash(
b));
182 return from_stash(pt).is_identity().as_bool();
186 BOTAN_ARG_CHECK(bytes.size() == C::AffinePoint::BYTES,
"Invalid length for serialize_point");
187 from_stash(pt).serialize_to(bytes.subspan<0, C::AffinePoint::BYTES>());
192 "Invalid length for serialize_point_compressed");
193 from_stash(pt).serialize_compressed_to(bytes.subspan<0, C::AffinePoint::COMPRESSED_BYTES>());
197 BOTAN_ARG_CHECK(bytes.size() == C::FieldElement::BYTES,
"Invalid length for serialize_point_x");
198 from_stash(pt).serialize_x_to(bytes.subspan<0, C::FieldElement::BYTES>());
202 BOTAN_ARG_CHECK(bytes.size() == C::Scalar::BYTES,
"Invalid length to serialize_scalar");
203 return from_stash(scalar).serialize_to(bytes.subspan<0, C::Scalar::BYTES>());
207 if(
auto scalar = C::Scalar::deserialize(bytes)) {
208 if(!scalar->is_zero().as_bool()) {
209 return stash(*scalar);
217 if(
auto s = C::Scalar::from_wide_bytes_varlen(bytes)) {
225 if(
auto pt = C::AffinePoint::deserialize(bytes)) {
233 std::span<const uint8_t> input,
234 std::span<const uint8_t> domain_sep)
const override {
235 if constexpr(C::ValidForSswuHash) {
236 return stash(hash_to_curve_sswu<C, false>(hash, input, domain_sep));
238 throw Not_Implemented(
"Hash to curve is not implemented for this curve");
243 std::span<const uint8_t> input,
244 std::span<const uint8_t> domain_sep)
const override {
245 if constexpr(C::ValidForSswuHash) {
246 return stash(hash_to_curve_sswu<C, true>(hash, input, domain_sep));
248 throw Not_Implemented(
"Hash to curve is not implemented for this curve");
253 return stash(from_stash(a) + from_stash(
b));
257 return stash(from_stash(a) - from_stash(
b));
261 return stash(from_stash(a) * from_stash(
b));
267 auto s = from_stash(ss);
269 return stash(C::scalar_invert(s));
271 return stash(s.invert());
280 return (from_stash(a) == from_stash(
b)).as_bool();
293 static std::shared_ptr<const PrimeOrderCurve>
instance() {
294 static auto g_curve = std::make_shared<const PrimeOrderCurveImpl<C>>();
299 static Scalar stash(
const typename C::Scalar& s) {
300 return Scalar::_create(
instance(), s.template stash_value<StorageWords>());
303 static typename C::Scalar from_stash(
const Scalar& s) {
307 return C::Scalar::from_stash(s._value());
310 static AffinePoint stash(
const typename C::AffinePoint& pt) {
311 auto x_w = pt.x().template stash_value<StorageWords>();
312 auto y_w = pt.y().template stash_value<StorageWords>();
313 return AffinePoint::_create(
instance(), x_w, y_w);
316 static typename C::AffinePoint from_stash(
const AffinePoint& pt) {
318 throw Invalid_Argument(
"Curve mismatch");
320 auto x = C::FieldElement::from_stash(pt._x());
321 auto y = C::FieldElement::from_stash(pt._y());
322 return typename C::AffinePoint(x, y);
325 static ProjectivePoint stash(
const typename C::ProjectivePoint& pt) {
326 auto x_w = pt.x().template stash_value<StorageWords>();
327 auto y_w = pt.y().template stash_value<StorageWords>();
328 auto z_w = pt.z().template stash_value<StorageWords>();
329 return ProjectivePoint::_create(
instance(), x_w, y_w, z_w);
332 static typename C::ProjectivePoint from_stash(
const ProjectivePoint& pt) {
334 throw Invalid_Argument(
"Curve mismatch");
336 auto x = C::FieldElement::from_stash(pt._x());
337 auto y = C::FieldElement::from_stash(pt._y());
338 auto z = C::FieldElement::from_stash(pt._z());
339 return typename C::ProjectivePoint(x, y, z);
343 const PrecomputedBaseMulTable<C, 5> m_mul_by_g;
#define BOTAN_ARG_CHECK(expr, msg)
PrecomputedMul2TableC(const typename C::AffinePoint &x, const typename C::AffinePoint &y)
static constexpr size_t WindowBits
const WindowedMul2Table< C, WindowBits > & table() const
Scalar scalar_invert(const Scalar &ss) const override
size_t field_element_bytes() const override
Scalar scalar_zero() const override
ProjectivePoint hash_to_curve_ro(std::string_view hash, std::span< const uint8_t > input, std::span< const uint8_t > domain_sep) const override
bool scalar_equal(const Scalar &a, const Scalar &b) const override
void serialize_scalar(std::span< uint8_t > bytes, const Scalar &scalar) const override
bool affine_point_is_identity(const AffinePoint &pt) const override
Scalar scalar_from_u32(uint32_t x) const override
Scalar scalar_sub(const Scalar &a, const Scalar &b) const override
size_t order_bits() const override
Return the bit length of the group order.
void serialize_point_x(std::span< uint8_t > bytes, const AffinePoint &pt) const override
AffinePoint generator() const override
Return the standard generator.
ProjectivePoint mul_by_g(const Scalar &scalar, RandomNumberGenerator &rng) const override
size_t scalar_bytes() const override
Return the byte length of the scalar element.
bool scalar_is_zero(const Scalar &s) const override
Scalar scalar_one() const override
ProjectivePoint point_negate(const ProjectivePoint &pt) const override
bool mul2_vartime_x_mod_order_eq(const PrecomputedMul2Table &tableb, const Scalar &v, const Scalar &s1, const Scalar &s2) const override
static std::shared_ptr< const PrimeOrderCurve > instance()
std::optional< Scalar > scalar_from_wide_bytes(std::span< const uint8_t > bytes) const override
std::unique_ptr< const PrecomputedMul2Table > mul2_setup(const AffinePoint &x, const AffinePoint &y) const override
Setup a table for 2-ary multiplication.
Scalar base_point_mul_x_mod_order(const Scalar &scalar, RandomNumberGenerator &rng) const override
ProjectivePoint point_double(const ProjectivePoint &pt) const override
std::optional< Scalar > deserialize_scalar(std::span< const uint8_t > bytes) const override
Scalar scalar_add(const Scalar &a, const Scalar &b) const override
AffinePoint hash_to_curve_nu(std::string_view hash, std::span< const uint8_t > input, std::span< const uint8_t > domain_sep) const override
Scalar scalar_negate(const Scalar &s) const override
std::optional< ProjectivePoint > mul2_vartime(const PrecomputedMul2Table &tableb, const Scalar &s1, const Scalar &s2) const override
std::optional< AffinePoint > deserialize_point(std::span< const uint8_t > bytes) const override
Scalar random_scalar(RandomNumberGenerator &rng) const override
ProjectivePoint point_add(const ProjectivePoint &a, const ProjectivePoint &b) const override
void serialize_point(std::span< uint8_t > bytes, const AffinePoint &pt) const override
Scalar scalar_square(const Scalar &s) const override
ProjectivePoint point_to_projective(const AffinePoint &pt) const override
Scalar scalar_mul(const Scalar &a, const Scalar &b) const override
AffinePoint point_to_affine(const ProjectivePoint &pt) const override
ProjectivePoint point_add_mixed(const ProjectivePoint &a, const AffinePoint &b) const override
ProjectivePoint mul(const AffinePoint &pt, const Scalar &scalar, RandomNumberGenerator &rng) const override
void serialize_point_compressed(std::span< uint8_t > bytes, const AffinePoint &pt) const override
static const size_t MaximumBitLength
int(* final)(unsigned char *, CTX *)
BigInt square(const BigInt &x)
constexpr auto bigint_ct_is_lt(const W x[], size_t x_size, const W y[], size_t y_size, bool lt_or_equal=false) -> CT::Mask< W >