Botan 3.14.1
Crypto and TLS for C&
curve448_scalar.h
Go to the documentation of this file.
1/*
2 * Ed448 Scalar
3 * (C) 2024 Jack Lloyd
4 * 2024 Fabian Albert - Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8#ifndef BOTAN_CURVE448_SCALAR_H_
9#define BOTAN_CURVE448_SCALAR_H_
10
11#include <botan/types.h>
12#include <botan/internal/bit_ops.h>
13#include <botan/internal/loadstor.h>
14
15namespace Botan {
16
17constexpr size_t words_for_bits(size_t x) {
18 constexpr size_t word_bits = sizeof(word) * 8;
19 return (x + word_bits - 1) / word_bits;
20}
21
22/**
23 * @brief Representation of a scalar for X448.
24 *
25 * The scalar is an element in 0 <= s < L, where L is the group
26 * order of X448. The constructor and all operations on
27 * scalars reduce the element mod L internally. All operations are
28 * constant time.
29 *
30 * L = 2^446 - 13818066809895115352007386748515426880336692474882178609894547503885
31 * (RFC 7748 4.2)
32 */
34 public:
35 constexpr static size_t WORDS = words_for_bits(446);
36 constexpr static size_t BYTES = ceil_tobytes<size_t>(446);
37
38 /// @brief Construct a new scalar from (max. 114) bytes. Little endian.
39 explicit Scalar448(std::span<const uint8_t> x);
40
41 /// @brief Convert the scalar to bytes in little endian.
42 template <size_t S = BYTES>
43 std::array<uint8_t, S> to_bytes() const
44 requires(S >= BYTES)
45 {
46 std::array<uint8_t, S> result = {0};
47 store_le(std::span(result).template first<BYTES>(), m_scalar_words);
48 return result;
49 }
50
51 /// @brief Access the i-th bit of the scalar. From 0 (lsb) to 445 (msb).
52 bool get_bit(size_t i) const;
53
54 /// @brief Extract a window of @p width bits starting at bit position @p starting_pos.
55 /// Bits beyond position 445 are treated as zero.
56 uint32_t get_window(size_t starting_pos, size_t width) const;
57
58 /// @brief scalar = (scalar + other) mod L
59 Scalar448 operator+(const Scalar448& other) const;
60
61 /// @brief scalar = (scalar * other) mod L
62 Scalar448 operator*(const Scalar448& other) const;
63
64 /// @return true iff x < L (and any bytes beyond BYTES are zero). Little endian.
65 static bool bytes_are_reduced(std::span<const uint8_t> x);
66
67 private:
68 // NOLINTNEXTLINE(*-member-init)
69 explicit Scalar448(std::span<const word, WORDS> scalar_words) { copy_mem(m_scalar_words, scalar_words); }
70
71 std::array<word, WORDS> m_scalar_words;
72};
73
74} // namespace Botan
75
76#endif // BOTAN_CURVE448_SCALAR_H_
#define BOTAN_TEST_API
Definition api.h:41
Representation of a scalar for X448.
Scalar448(std::span< const uint8_t > x)
Construct a new scalar from (max. 114) bytes. Little endian.
std::array< uint8_t, S > to_bytes() const
Convert the scalar to bytes in little endian.
static constexpr size_t WORDS
static constexpr size_t BYTES
BigInt operator*(const BigInt &x, const BigInt &y)
Definition big_ops3.cpp:57
constexpr size_t words_for_bits(size_t x)
OctetString operator+(const OctetString &k1, const OctetString &k2)
Definition symkey.cpp:99
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
constexpr auto store_le(ParamTs &&... params)
Definition loadstor.h:736
BOTAN_FORCE_INLINE constexpr T ceil_tobytes(T bits)
Definition bit_ops.h:176
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
The native machine word, used as the limb type for multiprecision integers.
Definition types.h:131