7#include <botan/internal/bls12_381_fields.h>
9#include <botan/internal/ct_utils.h>
10#include <botan/internal/mp_core.h>
18 "1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab");
22 "15f65ec3fa80e4935c071a97a256ec6d77ce5853705257455f48985753c758baebf4000bc40c0002760900000002fffd");
26 "11988fe592cae3aa9a793e85b519952d67eb88a9939d83c08de5476c4c95b6d50a76e6a609d104f1f4df1f341c341746");
30 "aa6346091755d4d2512d4356572472834c04e5e921e17619a53352a615e29dd315f831e03a7adf8ed48ac6bd94ca1e0");
35constexpr auto FE_P_HALF = [] {
43constexpr auto FE_SQRT_EXP = [] {
50std::array<word, FieldElement::N> fe_redc(
const std::array<word, 2 * FieldElement::N>& v) {
51 std::array<word, FieldElement::N> r{};
52 std::array<word, FieldElement::N> ws{};
53 bigint_monty_redc(r.data(), v.data(), FE_P.data(), FE_P.size(), FE_P_DASH, ws.data(), ws.size());
57std::array<word, FieldElement::N> fe_to_rep(std::array<word, FieldElement::N> v) {
58 std::array<word, 2 * FieldElement::N> z{};
69std::array<word, FieldElement::N> fe_wide_to_rep(
const std::array<word, 2 * FieldElement::N>& v) {
70 auto redc_v = fe_redc(v);
71 std::array<word, 2 * FieldElement::N> z{};
76std::array<word, FieldElement::N> fe_from_rep(
const std::array<word, FieldElement::N>& v) {
77 std::array<word, 2 * FieldElement::N> z{};
78 copy_mem(z.data(), v.data(), v.size());
85FieldElement FieldElement::from_words(std::array<word, FieldElement::N> v) {
91 std::array<word, FieldElement::N> w{};
93 return FieldElement::from_words(w);
112 return FieldElement::from_words(words);
120 auto v = fe_from_rep(m_val);
121 std::reverse(v.begin(), v.end());
126 std::array<word, FieldElement::N> t{};
129 t[i] =
word_add(m_val[i], other.m_val[i], &carry);
132 std::array<word, FieldElement::N> r{};
138 std::array<word, FieldElement::N> r;
141 r[i] =
word_sub(m_val[i], other.m_val[i], &borrow);
147 r[i] =
word_add(r[i], FE_P[i] & mask, &carry);
154 std::array<word, 2 * FieldElement::N> z{};
160 std::array<word, 2 * FieldElement::N> z{};
165void FieldElement::square_n(
size_t n) {
166 std::array<word, 2 * N> z{};
167 for(
size_t i = 0; i != n; ++i) {
176 std::array<word, N> r{};
178 v_is_zero.if_set_zero_out(r.data(),
N);
187 const auto v = fe_from_rep(m_val);
193 return CT::is_equal(this->data(), other.data(),
N).as_choice();
202 const auto r =
pow_msb(*
this, FE_SQRT_EXP);
209 auto z = (*this).
square();
212 auto t6 = t10 * (*this);
217 auto t17 = t10 * t24;
218 auto t9 = t17 * (*this);
221 auto t2 = t4 * (*this);
222 auto t22 = t12 * t17;
CT::Choice is_zero() const
FieldElement add(const FieldElement &x) const
CT::Choice _is_lexicographically_largest() const
FieldElement square() const
static FieldElement one()
FieldElement invert() const
static constexpr size_t BYTES
void conditional_assign(CT::Choice cnd, const FieldElement &other)
static std::optional< FieldElement > deserialize(std::span< const uint8_t > bytes)
CT::Choice operator==(const FieldElement &other) const
void serialize_to(std::span< uint8_t, FieldElement::BYTES > bytes) const
FieldElement mul(const FieldElement &x) const
FieldElement sub(const FieldElement &x) const
CT::Option< FieldElement > sqrt() const
static constexpr size_t N
static FieldElement from_bytes_wide(std::span< const uint8_t, 96 > bytes)
static FieldElement from_u32(uint32_t v)
FieldElement negate() const
static constexpr Mask< T > expand(T v)
FE pow_msb(const FE &x, const std::array< word, N > &exp)
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
constexpr CT::Mask< T > all_zeros(const T elem[], size_t len)
constexpr Mask< T > conditional_assign_mem(T cnd, T *dest, const T *src, size_t elems)
constexpr auto bytes_to_words(std::span< const uint8_t, L > bytes)
constexpr auto word_sub(W x, W y, W *carry) -> W
constexpr auto word_add(W x, W y, W *carry) -> W
constexpr void comba_sqr(W z[2 *N], const W x[N])
constexpr void comba_mul(W z[2 *N], const W x[N], const W y[N])
constexpr auto bigint_sub3(W z[], const W x[], size_t x_size, const W y[], size_t y_size) -> W
constexpr auto monty_inverse(W a) -> W
constexpr void copy_mem(T *out, const T *in, size_t n)
void bigint_monty_redc(word r[], const word z[], const word p[], size_t p_size, word p_dash, word ws[], size_t ws_size)
constexpr void bigint_monty_maybe_sub(size_t N, W z[], W x0, const W x[], const W p[])
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 >
constexpr auto hex_to_words(const char(&s)[N])
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
The native machine word, used as the limb type for multiprecision integers.
constexpr auto store_be(ParamTs &&... params)
constexpr W shift_right(std::array< W, N > &x)