9#include <botan/internal/ed25519_scalar.h>
11#include <botan/internal/ct_utils.h>
12#include <botan/internal/loadstor.h>
13#include <botan/internal/mp_core.h>
26consteval std::array<word, SC_WORDS> sc_l_words() {
27 const std::array<uint8_t, 32> l_bytes{0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7,
28 0xa2, 0xde, 0xf9, 0xde, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
29 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10};
33constexpr std::array<word, SC_WORDS> SC_L = sc_l_words();
38consteval word sc_l_dash() {
41 for(
size_t i = 0; i != 6; ++i) {
42 y =
static_cast<word>(y * (
static_cast<word>(2) - SC_L[0] * y));
44 return static_cast<word>(0) - y;
47constexpr word SC_L_DASH = sc_l_dash();
52consteval std::array<word, SC_WORDS> sc_2_pow_mod_l(
size_t k) {
53 std::array<word, SC_WORDS> r{};
55 for(
size_t i = 0; i != k; ++i) {
57 for(
size_t j = 0; j != SC_WORDS; ++j) {
59 r[j] =
static_cast<word>(w << 1) | carry;
63 std::array<word, SC_WORDS> t{};
65 for(
size_t j = 0; j != SC_WORDS; ++j) {
66 t[j] =
word_sub(r[j], SC_L[j], &borrow);
75constexpr std::array<word, SC_WORDS> SC_R1 = sc_2_pow_mod_l(256);
76constexpr std::array<word, SC_WORDS> SC_R2 = sc_2_pow_mod_l(512);
82std::array<word, SC_WORDS> sc_mont_mul(
const std::array<word, SC_WORDS>& x,
const std::array<word, SC_WORDS>& y) {
83 std::array<word, 2 * SC_WORDS> z;
86 std::array<word, SC_WORDS> r;
87 std::array<word, SC_WORDS> ws;
88 bigint_monty_redc(r.data(), z.data(), SC_L.data(), SC_WORDS, SC_L_DASH, ws.data(), ws.size());
95std::array<word, SC_WORDS> sc_add(
const std::array<word, SC_WORDS>& x,
const std::array<word, SC_WORDS>& y) {
97 std::array<word, SC_WORDS> r;
99 for(
size_t i = 0; i != SC_WORDS; ++i) {
100 r[i] =
word_add(x[i], y[i], &carry);
103 std::array<word, SC_WORDS> t;
105 for(
size_t i = 0; i != SC_WORDS; ++i) {
106 t[i] =
word_sub(r[i], SC_L[i], &borrow);
112std::array<word, SC_WORDS> sc_load(std::span<const uint8_t, 32> b) {
113 std::array<word, SC_WORDS> r;
114 for(
size_t i = 0; i != SC_WORDS; ++i) {
124 return Ed25519_Scalar(sc_mont_mul(sc_load(b), SC_R1));
128 const auto lo = sc_load(b.first<
BYTES>());
129 const auto hi = sc_load(b.last<
BYTES>());
132 return Ed25519_Scalar(sc_add(sc_mont_mul(hi, SC_R2), sc_mont_mul(lo, SC_R1)));
136 const auto w = sc_load(b);
138 std::array<word, WORDS> t;
140 for(
size_t i = 0; i != WORDS; ++i) {
141 t[i] =
word_sub(w[i], SC_L[i], &borrow);
148 return Ed25519_Scalar(w);
152 for(
size_t i = 0; i != WORDS; ++i) {
158 return Ed25519_Scalar(sc_add(m_val, other.m_val));
163 std::array<word, WORDS> t;
165 for(
size_t i = 0; i != WORDS; ++i) {
166 t[i] =
word_sub(SC_L[i], m_val[i], &borrow);
169 const std::array<word, WORDS> zeros{};
170 const auto x_is_zero =
CT::all_zeros(m_val.data(), WORDS).value();
172 return Ed25519_Scalar(t);
177 return Ed25519_Scalar(sc_mont_mul(sc_mont_mul(m_val, SC_R2), other.m_val));
static constexpr size_t BYTES
static Ed25519_Scalar from_wide_bytes(std::span< const uint8_t, 2 *BYTES > b)
static std::optional< Ed25519_Scalar > from_canonical_bytes(std::span< const uint8_t, BYTES > b)
Ed25519_Scalar operator*(const Ed25519_Scalar &other) const
Ed25519_Scalar operator+(const Ed25519_Scalar &other) const
static Ed25519_Scalar from_bytes(std::span< const uint8_t, BYTES > b)
void serialize_to(std::span< uint8_t, BYTES > b) const
Ed25519_Scalar operator-() const
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 word_sub(W x, W y, W *carry) -> W
constexpr auto word_add(W x, W y, W *carry) -> W
constexpr void comba_mul(W z[2 *N], const W x[N], const W y[N])
constexpr auto store_le(ParamTs &&... params)
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 auto load_le(ParamTs &&... params)
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
The native machine word, used as the limb type for multiprecision integers.