7#include <botan/internal/ec_inner_bn.h>
14 throw Invalid_State(
"Failed conversion to EC_Scalar_Data_BN");
24 return this->
group()->order_bytes();
28 return std::make_unique<EC_Scalar_Data_BN>(this->
group(), this->
value());
44 m_group->square_mod_order(m_v);
48 return std::make_unique<EC_Scalar_Data_BN>(m_group, m_group->mod_order(-m_v));
52 return std::make_unique<EC_Scalar_Data_BN>(m_group, m_group->inverse_mod_order(m_v));
56 return std::make_unique<EC_Scalar_Data_BN>(m_group, m_group->mod_order(m_v +
checked_ref(other).
value()));
60 return std::make_unique<EC_Scalar_Data_BN>(m_group, m_group->mod_order(m_v -
checked_ref(other).
value()));
64 return std::make_unique<EC_Scalar_Data_BN>(m_group, m_group->multiply_mod_order(m_v,
checked_ref(other).
value()));
73 m_group(std::move(group)), m_pt(std::move(pt)) {
76 m_xy = m_pt.xy_bytes();
81 std::span<const uint8_t> pt) :
82 m_group(std::move(group)) {
91 return std::make_unique<EC_AffinePoint_Data_BN>(m_group, m_pt);
100 std::vector<BigInt>& ws)
const {
111 const auto order = m_group->order() * m_group->cofactor();
112 auto pt =
mul.mul(bn.value(), rng, order, ws);
113 return std::make_unique<EC_AffinePoint_Data_BN>(m_group, std::move(pt));
117 return m_group->p_bytes();
128 copy_mem(bytes, std::span{m_xy}.first(fe_bytes));
135 copy_mem(bytes, std::span{m_xy}.last(fe_bytes));
149 const bool y_is_odd = (m_xy[m_xy.size() - 1] & 0x01) == 0x01;
152 stuffer.
append(y_is_odd ? 0x03 : 0x02);
159 BOTAN_ARG_CHECK(bytes.size() == 1 + 2 * fe_bytes,
"Invalid output size");
166 m_group(g.group()), m_tbl(g.to_legacy_point(), h.to_legacy_point()) {
176 auto pt = m_tbl.
multi_exp(bn_x.value(), bn_y.value());
181 return std::make_unique<EC_AffinePoint_Data_BN>(m_group, std::move(pt));
192 const auto pt = m_tbl.
multi_exp(bn_x.value(), bn_y.value());
201 if(m_group->has_cofactor()) {
202 return m_group->mod_order(pt.get_affine_x()) == bn_v.value();
220 auto& curve = m_group->curve();
224 curve.to_rep(vr, ws);
226 curve.sqr(z2, pt.get_z(), ws);
227 curve.
mul(v_z2, vr, z2, ws);
238 if(pt.get_x() == v_z2) {
242 if(m_group->order_is_less_than_p()) {
243 vr = bn_v.value() + m_group->order();
244 if(vr < m_group->p()) {
245 curve.to_rep(vr, ws);
246 curve.mul(v_z2, vr, z2, ws);
248 if(pt.get_x() == v_z2) {
#define BOTAN_STATE_CHECK(expr)
#define BOTAN_ASSERT_NONNULL(ptr)
#define BOTAN_ARG_CHECK(expr, msg)
BigInt & mul(const BigInt &y, secure_vector< word > &ws)
void serialize_to(std::span< uint8_t > out) const
Helper class to ease in-place marshalling of concatenated fixed-length values.
constexpr void append(std::span< const uint8_t > buffer)
constexpr std::span< uint8_t > next(size_t bytes)
void serialize_x_to(std::span< uint8_t > bytes) const override
const std::shared_ptr< const EC_Group_Data > & group() const override
EC_AffinePoint_Data_BN(std::shared_ptr< const EC_Group_Data > group, EC_Point pt)
void serialize_compressed_to(std::span< uint8_t > bytes) const override
std::unique_ptr< EC_AffinePoint_Data > mul(const EC_Scalar_Data &scalar, RandomNumberGenerator &rng, std::vector< BigInt > &ws) const override
std::unique_ptr< EC_AffinePoint_Data > clone() const override
size_t field_element_bytes() const override
bool is_identity() const override
void serialize_y_to(std::span< uint8_t > bytes) const override
void serialize_xy_to(std::span< uint8_t > bytes) const override
void serialize_uncompressed_to(std::span< uint8_t > bytes) const override
virtual const std::shared_ptr< const EC_Group_Data > & group() const =0
EC_Mul2Table_Data_BN(const EC_AffinePoint_Data &g, const EC_AffinePoint_Data &h)
bool mul2_vartime_x_mod_order_eq(const EC_Scalar_Data &v, const EC_Scalar_Data &x, const EC_Scalar_Data &y) const override
std::unique_ptr< EC_AffinePoint_Data > mul2_vartime(const EC_Scalar_Data &x, const EC_Scalar_Data &y) const override
EC_Point multi_exp(const BigInt &k1, const BigInt &k2) const
secure_vector< uint8_t > xy_bytes() const
std::unique_ptr< EC_Scalar_Data > invert() const override
std::unique_ptr< EC_Scalar_Data > clone() const override
std::unique_ptr< EC_Scalar_Data > negate() const override
const std::shared_ptr< const EC_Group_Data > & group() const override
bool is_zero() const override
std::unique_ptr< EC_Scalar_Data > sub(const EC_Scalar_Data &other) const override
void square_self() override
std::unique_ptr< EC_Scalar_Data > add(const EC_Scalar_Data &other) const override
std::unique_ptr< EC_Scalar_Data > mul(const EC_Scalar_Data &other) const override
const BigInt & value() const
void serialize_to(std::span< uint8_t > bytes) const override
static const EC_Scalar_Data_BN & checked_ref(const EC_Scalar_Data &data)
bool is_eq(const EC_Scalar_Data &y) const override
void assign(const EC_Scalar_Data &y) override
size_t bytes() const override
virtual const std::shared_ptr< const EC_Group_Data > & group() const =0
EC_Point OS2ECP(const uint8_t data[], size_t data_len, const CurveGFp &curve)
std::vector< T, secure_allocator< T > > secure_vector
constexpr void copy_mem(T *out, const T *in, size_t n)