Botan 3.6.1
Crypto and TLS for C&
Botan::Scalar448 Class Reference

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(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)

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

Definition at line 147 of file curve448_scalar.cpp.

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

References BOTAN_ARG_CHECK, 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 185 of file curve448_scalar.cpp.

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

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

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 156 of file curve448_scalar.cpp.

156 {
157 BOTAN_ARG_CHECK(bit_pos < 446, "Bit position out of range");
158 constexpr size_t word_sz = sizeof(word) * 8;
159 return (m_scalar_words[bit_pos / word_sz] >> (bit_pos % word_sz)) & 1;
160}

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 168 of file curve448_scalar.cpp.

168 {
169 std::array<word, WORDS_REDUCE_SZ> product = {0};
170 std::array<word, WORDS_REDUCE_SZ> ws = {0};
171 bigint_mul(product.data(),
172 product.size(),
173 m_scalar_words.data(),
174 m_scalar_words.size(),
175 m_scalar_words.size(),
176 other.m_scalar_words.data(),
177 other.m_scalar_words.size(),
178 other.m_scalar_words.size(),
179 ws.data(),
180 ws.size());
181
182 return Scalar448(ct_reduce_mod_L(product));
183}
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:282

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

◆ operator+()

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

scalar = (scalar + other) mod L

Definition at line 162 of file curve448_scalar.cpp.

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

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:764

References Botan::store_le().

Member Data Documentation

◆ BYTES

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

Definition at line 37 of file curve448_scalar.h.

Referenced by bytes_are_reduced().

◆ 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: