Botan 3.9.0
Crypto and TLS for C&
Botan::Scalar448 Class Referencefinal

Representation of a scalar for X448. More...

#include <curve448_scalar.h>

Public Member Functions

bool get_bit (size_t i) const
 Access the i-th bit of the scalar. From 0 (lsb) to 445 (msb).
Scalar448 operator* (const Scalar448 &other) const
 scalar = (scalar * other) mod L
Scalar448 operator+ (const Scalar448 &other) const
 scalar = (scalar + other) mod L
 Scalar448 (std::span< const uint8_t > x)
 Construct a new scalar from (max. 114) bytes. Little endian.
template<size_t S = BYTES>
requires (S >= BYTES)
std::array< uint8_t, S > to_bytes () const
 Convert the scalar to bytes in little endian.

Static Public Member Functions

static bool bytes_are_reduced (std::span< const uint8_t > x)

Static Public Attributes

static constexpr size_t BYTES = ceil_tobytes<size_t>(446)
static constexpr size_t WORDS = words_for_bits(446)

Detailed Description

Representation of a scalar for X448.

The scalar is an element in 0 <= s < L, where L is the group order of X448. The constructor and all operations on scalars reduce the element mod L internally. All operations are constant time.

L = 2^446 - 13818066809895115352007386748515426880336692474882178609894547503885 (RFC 7748 4.2)

Definition at line 34 of file curve448_scalar.h.

Constructor & Destructor Documentation

◆ Scalar448()

Botan::Scalar448::Scalar448 ( std::span< const uint8_t > x)
explicit

Construct a new scalar from (max. 114) bytes. Little endian.

Definition at line 148 of file curve448_scalar.cpp.

148 {
149 BOTAN_ARG_CHECK(in_bytes.size() <= 114, "Input must be at most 114 bytes long");
150 std::array<uint8_t, 114> max_bytes = {0};
151 copy_mem(std::span(max_bytes).first(in_bytes.size()), in_bytes);
152
153 const auto x_words = bytes_to_words(std::span<const uint8_t, 114>(max_bytes));
154 m_scalar_words = ct_reduce_mod_L(x_words);
155}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
constexpr auto bytes_to_words(std::span< const uint8_t, L > bytes)
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:145

References BOTAN_ARG_CHECK, Botan::bytes_to_words(), and Botan::copy_mem().

Referenced by operator*(), and operator+().

Member Function Documentation

◆ bytes_are_reduced()

bool Botan::Scalar448::bytes_are_reduced ( std::span< const uint8_t > x)
static
Returns
true iff x >= L.

Definition at line 186 of file curve448_scalar.cpp.

186 {
187 BOTAN_ARG_CHECK(x.size() >= BYTES, "Input is not long enough (at least 446 bits)");
188 // remember: `x` contains a big int in little-endian
189 const auto leading_zeros = x.subspan(BYTES);
190 const auto leading_zeros_are_zero = CT::all_zeros(leading_zeros.data(), leading_zeros.size());
191 auto x_sig_words = bytes_to_words(x.first<56>());
192 const auto least_56_bytes_smaller_L = CT::Mask<uint8_t>::from_choice(!ct_subtract_L_if_bigger(x_sig_words));
193 return (leading_zeros_are_zero & least_56_bytes_smaller_L).as_bool();
194}
static constexpr Mask< T > from_choice(Choice c)
Definition ct_utils.h:430
static constexpr size_t BYTES
constexpr CT::Mask< T > all_zeros(const T elem[], size_t len)
Definition ct_utils.h:813

References Botan::CT::all_zeros(), BOTAN_ARG_CHECK, BYTES, Botan::bytes_to_words(), and Botan::CT::Mask< T >::from_choice().

Referenced by Botan::verify_signature().

◆ get_bit()

bool Botan::Scalar448::get_bit ( size_t i) const

Access the i-th bit of the scalar. From 0 (lsb) to 445 (msb).

Definition at line 157 of file curve448_scalar.cpp.

157 {
158 BOTAN_ARG_CHECK(bit_pos < 446, "Bit position out of range");
159 constexpr size_t word_sz = sizeof(word) * 8;
160 return (((m_scalar_words[bit_pos / word_sz] >> (bit_pos % word_sz)) & 1) == 1);
161}
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
Definition types.h:119

References BOTAN_ARG_CHECK.

Referenced by Botan::Ed448Point::scalar_mul().

◆ operator*()

Scalar448 Botan::Scalar448::operator* ( const Scalar448 & other) const

scalar = (scalar * other) mod L

Definition at line 169 of file curve448_scalar.cpp.

169 {
170 std::array<word, WORDS_REDUCE_SZ> product = {0};
171 std::array<word, WORDS_REDUCE_SZ> ws = {0};
172 bigint_mul(product.data(),
173 product.size(),
174 m_scalar_words.data(),
175 m_scalar_words.size(),
176 m_scalar_words.size(),
177 other.m_scalar_words.data(),
178 other.m_scalar_words.size(),
179 other.m_scalar_words.size(),
180 ws.data(),
181 ws.size());
182
183 return Scalar448(ct_reduce_mod_L(product));
184}
Scalar448(std::span< const uint8_t > x)
Construct a new scalar from (max. 114) bytes. Little endian.
void bigint_mul(word z[], size_t z_size, const word x[], size_t x_size, size_t x_sw, const word y[], size_t y_size, size_t y_sw, word workspace[], size_t ws_size)
Definition mp_karat.cpp:283

References Botan::bigint_mul(), and Scalar448().

◆ operator+()

Scalar448 Botan::Scalar448::operator+ ( const Scalar448 & other) const

scalar = (scalar + other) mod L

Definition at line 163 of file curve448_scalar.cpp.

163 {
164 auto sum = add(m_scalar_words, other.m_scalar_words);
165 ct_subtract_L_if_bigger(sum);
166 return Scalar448(sum);
167}

References Scalar448().

◆ to_bytes()

template<size_t S = BYTES>
requires (S >= BYTES)
std::array< uint8_t, S > Botan::Scalar448::to_bytes ( ) const
inline

Convert the scalar to bytes in little endian.

Definition at line 44 of file curve448_scalar.h.

46 {
47 std::array<uint8_t, S> result = {0};
48 store_le(std::span(result).template first<BYTES>(), m_scalar_words);
49 return result;
50 }
constexpr auto store_le(ParamTs &&... params)
Definition loadstor.h:736

References BYTES, and Botan::store_le().

Member Data Documentation

◆ BYTES

size_t Botan::Scalar448::BYTES = ceil_tobytes<size_t>(446)
staticconstexpr

Definition at line 37 of file curve448_scalar.h.

Referenced by bytes_are_reduced(), and to_bytes().

◆ WORDS

size_t Botan::Scalar448::WORDS = words_for_bits(446)
staticconstexpr

Definition at line 36 of file curve448_scalar.h.


The documentation for this class was generated from the following files: