7#include <botan/internal/bls12_381_fields.h>
9#include <botan/internal/ct_utils.h>
10#include <botan/internal/mp_core.h>
145 const auto t = pow_p_minus_11_over_16(x);
146 const auto t2 = t.square();
147 const auto t4 = t2.square();
148 const auto t8 = t4.square();
149 const auto t11 = t8 * t2 * t;
151 const auto x2 = x.
square();
152 const auto x3 = x2 * x;
153 const auto x6 = x3.
square();
154 const auto x7 = x6 * x;
183 return m_c0.is_zero() && m_c1.is_zero();
197 const auto v0 = m_c0 * x.m_c0;
198 const auto v1 = m_c1 * x.m_c1;
199 const auto s = (m_c0 + m_c1) * (x.m_c0 + x.m_c1);
207 const auto t0 = (m_c0 + m_c1) * (m_c0 - m_c1);
208 const auto t1 = m_c0 * m_c1;
214 for(
size_t i = 0; i != n; ++i) {
241 const auto ninv = (m_c0.square() + m_c1.square()).
invert();
248 return (m_c0 == other.m_c0) && (m_c1 == other.m_c1);
252 m_c0.conditional_assign(cnd, other.m_c0);
253 m_c1.conditional_assign(cnd, other.m_c1);
257 return m_c1._is_lexicographically_largest() || (m_c1.is_zero() && m_c0._is_lexicographically_largest());
264 "1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab");
267 constexpr auto FP2_SQRT_EXP = [FP2_P] {
274 constexpr auto FP2_P_HALF = [FP2_P] {
281 const auto a1 =
pow_msb(*
this, FP2_SQRT_EXP);
284 const auto alpha = a1.square() * (*this);
287 const auto x0 = a1 * (*this);
296 candidate.conditional_assign(alpha_is_neg_one, x0_u);
FieldElement2 add(const FieldElement2 &x) const
void conditional_assign(CT::Choice cnd, const FieldElement2 &other)
FieldElement2 square() const
static FieldElement2 one()
CT::Option< FieldElement2 > sqrt() const
FieldElement2 invert() const
FieldElement2 mul_by_nonresidue() const
constexpr FieldElement2()=default
CT::Choice _is_lexicographically_largest() const
FieldElement2 mul(const FieldElement2 &x) const
FieldElement2 conjugate() const
CT::Choice operator==(const FieldElement2 &other) const
static std::optional< FieldElement2 > deserialize(std::span< const uint8_t > bytes)
FieldElement2 negate() const
FieldElement2 sub(const FieldElement2 &x) const
static constexpr size_t BYTES
const FieldElement & c0() const
const FieldElement & c1() const
void serialize_to(std::span< uint8_t, FieldElement2::BYTES > bytes) const
CT::Choice is_zero() const
static constexpr size_t BYTES
static std::optional< FieldElement > deserialize(std::span< const uint8_t > bytes)
FieldElement2 pow_p2_minus_9_over_16(const FieldElement2 &x)
FE pow_msb(const FE &x, const std::array< word, N > &exp)
constexpr auto hex_to_words(const char(&s)[N])
constexpr W shift_right(std::array< W, N > &x)