7#include <botan/bls12_381.h>
9#include <botan/mem_ops.h>
10#include <botan/internal/ct_utils.h>
11#include <botan/internal/mp_core.h>
18constexpr auto SCALAR_P =
hex_to_words<word>(
"73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001");
21constexpr auto SCALAR_R1 =
hex_to_words<word>(
"1824b159acc5056f998c4fefecbc4ff55884b7fa0003480200000001fffffffe");
24constexpr auto SCALAR_R2 =
hex_to_words<word>(
"748d9d99f59ff1105d314967254398f2b6cedcb87925c23c999e990f3f29c6d");
27constexpr auto SCALAR_R3 =
hex_to_words<word>(
"6e2a5bb9c8db33e973d13c71c7b5f4181b3e0d188cf06990c62c1807439b73af");
31std::array<word, Scalar::N> scalar_redc(
const std::array<word, 2 * Scalar::N>& v) {
32 std::array<word, Scalar::N> r{};
33 std::array<word, Scalar::N> ws{};
34 bigint_monty_redc(r.data(), v.data(), SCALAR_P.data(), SCALAR_P.size(), SCALAR_P_DASH, ws.data(), ws.size());
38std::array<word, Scalar::N> scalar_to_rep(std::array<word, Scalar::N> v) {
39 std::array<word, 2 * Scalar::N> z{};
41 return scalar_redc(z);
44std::array<word, Scalar::N> scalar_wide_to_rep(
const std::array<word, 2 * Scalar::N>& v) {
45 auto redc_v = scalar_redc(v);
46 std::array<word, 2 * Scalar::N> z{};
48 return scalar_redc(z);
51std::array<word, Scalar::N> scalar_from_rep(
const std::array<word, Scalar::N>& v) {
52 std::array<word, 2 * Scalar::N> z{};
53 copy_mem(z.data(), v.data(), v.size());
54 return scalar_redc(z);
64Scalar Scalar::from_words(std::array<word, Scalar::N> v) {
65 return Scalar(scalar_to_rep(v));
70 std::array<word, Scalar::N> w{};
72 return Scalar::from_words(w);
97 return Scalar::from_words(words);
105 auto v = scalar_from_rep(m_val);
106 std::reverse(v.begin(), v.end());
113 std::array<word, Scalar::N> t{};
116 t[i] =
word_add(m_val[i], other.m_val[i], &carry);
119 std::array<word, Scalar::N> r{};
125 std::array<word, Scalar::N> r;
128 r[i] =
word_sub(m_val[i], other.m_val[i], &borrow);
134 r[i] =
word_add(r[i], SCALAR_P[i] & mask, &carry);
141 std::array<word, 2 * Scalar::N> z{};
143 return Scalar(scalar_redc(z));
147 std::array<word, 2 * Scalar::N> z{};
149 return Scalar(scalar_redc(z));
152void Scalar::square_n(
size_t n) {
153 std::array<word, 2 * N> z{};
154 for(
size_t i = 0; i != n; ++i) {
156 m_val = scalar_redc(z);
163 std::array<word, N> r{};
165 v_is_zero.if_set_zero_out(r.data(),
N);
173 auto z = t3 * (*this);
174 auto t14 = (*this) * z;
176 auto t4 = t2.square();
178 auto t5 = t8 * (*this);
192 auto t13 = t12 * t14;
static Scalar from_u32(uint32_t v)
static std::optional< Scalar > deserialize(std::span< const uint8_t > bytes)
static constexpr size_t N
Scalar mul(const Scalar &x) const
void serialize_to(std::span< uint8_t, Scalar::BYTES > bytes) const
static constexpr size_t BYTES
static Scalar from_bytes_wide(std::span< const uint8_t, 64 > bytes)
Scalar sub(const Scalar &x) const
Scalar add(const Scalar &x) const
static constexpr Mask< T > expand(T v)
constexpr CT::Mask< T > all_zeros(const T elem[], size_t len)
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 secure_scrub_memory(void *ptr, 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)