7#include <botan/internal/ec_inner_data.h>
9#include <botan/der_enc.h>
10#include <botan/internal/ec_inner_pc.h>
11#include <botan/internal/fmt.h>
12#include <botan/internal/pcurves.h>
14#if defined(BOTAN_HAS_LEGACY_EC_POINT)
15 #include <botan/internal/ec_inner_bn.h>
16 #include <botan/internal/point_mul.h>
43 m_monty(m_p, m_mod_field),
46 m_p_words(
p.sig_words()),
48 m_order_bits(
order.bits()),
49 m_order_bytes((m_order_bits + 7) / 8),
50 m_a_is_minus_3(
a ==
p - 3),
51 m_a_is_zero(
a.is_zero()),
52 m_has_cofactor(m_cofactor != 1),
53 m_order_is_less_than_p(m_order <
p),
56 DER_Encoder der(m_der_named_curve);
59 if(const auto id = PCurve::PrimeOrderCurveId::from_oid(m_oid)) {
60 m_pcurve = PCurve::PrimeOrderCurve::from_id(*id);
62 m_engine = EC_Group_Engine::Optimized;
69#if defined(BOTAN_HAS_LEGACY_EC_POINT)
71 m_a_r = m_monty.mul(a, m_monty.R2(), ws);
72 m_b_r = m_monty.mul(
b, m_monty.R2(), ws);
74 m_engine = EC_Group_Engine::Legacy;
79 throw Not_Implemented(
"EC_Group this group is not supported in this build configuration");
81 throw Not_Implemented(
82 fmt(
"EC_Group the group {} is not supported in this build configuration", oid.to_string()));
99#if defined(BOTAN_HAS_LEGACY_EC_POINT)
100 group->m_curve =
CurveGFp(group.get());
102 if(!group->m_pcurve) {
103 group->m_base_mult = std::make_unique<EC_Point_Base_Point_Precompute>(group->m_base_point, group->m_mod_order);
117 return (this->p() ==
p && this->a() ==
a && this->b() ==
b && this->order() ==
order &&
118 this->cofactor() ==
cofactor && this->g_x() ==
g_x && this->g_y() ==
g_y);
135 const size_t bit_length = 8 * bytes.size();
141 const size_t shift = bit_length -
order_bits();
143 const size_t new_length = bytes.size() - (shift / 8);
144 const size_t bit_shift = shift % 8;
150 std::vector<uint8_t> sbytes(new_length);
153 for(
size_t i = 0; i != new_length; ++i) {
154 const uint8_t w = bytes[i];
155 sbytes[i] = (w >> bit_shift) |
carry;
156 carry = w << (8 - bit_shift);
170 if(
auto s = m_pcurve->scalar_from_wide_bytes(bytes)) {
171 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), std::move(*s));
176#if defined(BOTAN_HAS_LEGACY_EC_POINT)
177 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), m_mod_order.reduce(
BigInt(bytes)));
179 throw Not_Implemented(
"Legacy EC interfaces disabled in this build configuration");
186 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), m_pcurve->random_scalar(rng));
188#if defined(BOTAN_HAS_LEGACY_EC_POINT)
189 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(),
192 throw Not_Implemented(
"Legacy EC interfaces disabled in this build configuration");
199 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), m_pcurve->scalar_zero());
201#if defined(BOTAN_HAS_LEGACY_EC_POINT)
202 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(),
BigInt::zero());
204 throw Not_Implemented(
"Legacy EC interfaces disabled in this build configuration");
211 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), m_pcurve->scalar_one());
213#if defined(BOTAN_HAS_LEGACY_EC_POINT)
214 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(),
BigInt::one());
216 throw Not_Implemented(
"Legacy EC interfaces disabled in this build configuration");
222 if(bn <= 0 || bn >= m_order) {
229#if defined(BOTAN_HAS_LEGACY_EC_POINT)
230 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), bn);
232 throw Not_Implemented(
"Legacy EC interfaces disabled in this build configuration");
239 std::vector<BigInt>& ws)
const {
242 auto gk_x_mod_order = m_pcurve->base_point_mul_x_mod_order(k.value(), rng);
243 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(),
gk_x_mod_order);
245#if defined(BOTAN_HAS_LEGACY_EC_POINT)
248 const auto pt = m_base_mult->mul(k.value(), rng, m_order, ws);
253 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), m_mod_order.reduce(pt.get_affine_x()));
257 throw Not_Implemented(
"Legacy EC interfaces disabled in this build configuration");
263 if(bytes.size() != m_order_bytes) {
268 if(
auto s = m_pcurve->deserialize_scalar(bytes)) {
269 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), *s);
274#if defined(BOTAN_HAS_LEGACY_EC_POINT)
277 if(r.
is_zero() || r >= m_order) {
281 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), std::move(r));
283 throw Not_Implemented(
"Legacy EC interfaces disabled in this build configuration");
291 if(
auto pt = m_pcurve->deserialize_point(bytes)) {
292 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(*pt));
297#if defined(BOTAN_HAS_LEGACY_EC_POINT)
298 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), bytes);
300 throw Not_Implemented(
"Legacy EC interfaces disabled in this build configuration");
309 std::span<const uint8_t> input,
310 std::span<const uint8_t> domain_sep)
const {
312 auto pt = m_pcurve->hash_to_curve_ro(hash_fn, input, domain_sep);
313 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), pt.to_affine());
315 throw Not_Implemented(
"Hash to curve is not implemented for this curve");
320 std::span<const uint8_t> input,
321 std::span<const uint8_t> domain_sep)
const {
323 auto pt = m_pcurve->hash_to_curve_nu(hash_fn, input, domain_sep);
324 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(pt));
326 throw Not_Implemented(
"Hash to curve is not implemented for this curve");
332 std::vector<BigInt>& ws)
const {
335 auto pt = m_pcurve->mul_by_g(k.value(), rng).to_affine();
336 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(pt));
338#if defined(BOTAN_HAS_LEGACY_EC_POINT)
339 const auto& group = scalar.
group();
343 auto pt = group->m_base_mult->mul(bn.value(), rng, m_order, ws);
344 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
347 throw Not_Implemented(
"Legacy EC interfaces disabled in this build configuration");
365 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), pt->to_affine());
370#if defined(BOTAN_HAS_LEGACY_EC_POINT)
371 std::vector<BigInt> ws;
372 const auto& group =
p.group();
378 const auto order = group->order() * group->cofactor();
383 auto px_qy = px + qy;
385 if(!px_qy.is_zero()) {
386 px_qy.force_affine();
387 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(px_qy));
392 throw Not_Implemented(
"Legacy EC interfaces disabled in this build configuration");
400 auto pt = m_pcurve->point_add_mixed(
404 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), pt.to_affine());
406#if defined(BOTAN_HAS_LEGACY_EC_POINT)
407 auto pt =
p.to_legacy_point() + q.to_legacy_point();
408 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
410 throw Not_Implemented(
"Legacy EC interfaces disabled in this build configuration");
418 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), pt);
420#if defined(BOTAN_HAS_LEGACY_EC_POINT)
421 auto pt =
p.to_legacy_point();
423 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
425 throw Not_Implemented(
"Legacy EC interfaces disabled in this build configuration");
432 return std::make_unique<EC_Mul2Table_Data_PC>(h);
434#if defined(BOTAN_HAS_LEGACY_EC_POINT)
436 return std::make_unique<EC_Mul2Table_Data_BN>(g, h);
438 throw Not_Implemented(
"Legacy EC interfaces disabled in this build configuration");
#define BOTAN_STATE_CHECK(expr)
#define BOTAN_ARG_CHECK(expr, msg)
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
T serialize(size_t len) const
DER_Encoder & encode(bool b)
static const EC_AffinePoint_Data_PC & checked_ref(const EC_AffinePoint_Data &data)
const BigInt & g_x() const
std::unique_ptr< EC_AffinePoint_Data > affine_neg(const EC_AffinePoint_Data &p) const
std::unique_ptr< EC_Scalar_Data > gk_x_mod_order(const EC_Scalar_Data &scalar, RandomNumberGenerator &rng, std::vector< BigInt > &ws) const
std::unique_ptr< EC_Scalar_Data > scalar_from_bytes_mod_order(std::span< const uint8_t > bytes) const
std::unique_ptr< EC_AffinePoint_Data > point_deserialize(std::span< const uint8_t > bytes) const
std::unique_ptr< EC_Scalar_Data > scalar_random(RandomNumberGenerator &rng) const
std::unique_ptr< EC_Scalar_Data > scalar_zero() const
std::unique_ptr< EC_Scalar_Data > scalar_deserialize(std::span< const uint8_t > bytes) const
bool params_match(const BigInt &p, const BigInt &a, const BigInt &b, const BigInt &g_x, const BigInt &g_y, const BigInt &order, const BigInt &cofactor) const
static std::shared_ptr< EC_Group_Data > create(const BigInt &p, const BigInt &a, const BigInt &b, const BigInt &g_x, const BigInt &g_y, const BigInt &order, const BigInt &cofactor, const OID &oid, EC_Group_Source source)
std::unique_ptr< EC_AffinePoint_Data > affine_add(const EC_AffinePoint_Data &p, const EC_AffinePoint_Data &q) const
EC_Group_Data(const BigInt &p, const BigInt &a, const BigInt &b, const BigInt &g_x, const BigInt &g_y, const BigInt &order, const BigInt &cofactor, const OID &oid, EC_Group_Source source)
std::unique_ptr< EC_Scalar_Data > scalar_from_bytes_with_trunc(std::span< const uint8_t > bytes) const
std::unique_ptr< EC_Mul2Table_Data > make_mul2_table(const EC_AffinePoint_Data &pt) const
std::unique_ptr< EC_AffinePoint_Data > mul_px_qy(const EC_AffinePoint_Data &p, const EC_Scalar_Data &x, const EC_AffinePoint_Data &q, const EC_Scalar_Data &y, RandomNumberGenerator &rng) const
std::unique_ptr< EC_Scalar_Data > scalar_one() const
size_t order_bits() const
const BigInt & cofactor() const
std::unique_ptr< EC_AffinePoint_Data > point_g_mul(const EC_Scalar_Data &scalar, RandomNumberGenerator &rng, std::vector< BigInt > &ws) const
EC_Group_Source source() const
void set_oid(const OID &oid)
const BigInt & g_y() const
size_t order_bytes() const
std::unique_ptr< EC_Scalar_Data > scalar_from_bigint(const BigInt &bn) const
std::unique_ptr< EC_AffinePoint_Data > point_hash_to_curve_ro(std::string_view hash_fn, std::span< const uint8_t > input, std::span< const uint8_t > domain_sep) const
std::unique_ptr< EC_AffinePoint_Data > point_hash_to_curve_nu(std::string_view hash_fn, std::span< const uint8_t > input, std::span< const uint8_t > domain_sep) const
const BigInt & order() const
EC_Point mul(const BigInt &k, RandomNumberGenerator &rng, const BigInt &group_order, std::vector< BigInt > &ws) const
static const EC_Scalar_Data_BN & checked_ref(const EC_Scalar_Data &data)
static const EC_Scalar_Data_PC & checked_ref(const EC_Scalar_Data &data)
virtual const std::shared_ptr< const EC_Group_Data > & group() const =0
static ProjectivePoint from_affine(const AffinePoint &pt)
#define BOTAN_HAS_LEGACY_EC_POINT
void carry(int64_t &h0, int64_t &h1)
std::vector< T, secure_allocator< T > > secure_vector