Botan 3.4.0
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/strong_type.h>
12#include <botan/types.h>
13#include <botan/internal/bit_ops.h>
14#include <botan/internal/loadstor.h>
15
16namespace Botan {
17
18constexpr size_t words_for_bits(size_t x) {
19 constexpr size_t word_bits = sizeof(word) * 8;
20 return (x + word_bits - 1) / word_bits;
21}
22
23/**
24 * @brief Representation of a scalar for X448.
25 *
26 * The scalar is an element in 0 <= s < L, where L is the group
27 * order of X448. The constructor and all operations on
28 * scalars reduce the element mod L internally. All operations are
29 * constant time.
30 *
31 * L = 2^446 - 13818066809895115352007386748515426880336692474882178609894547503885
32 * (RFC 7748 4.2)
33 */
35 public:
36 constexpr static size_t WORDS = words_for_bits(446);
37 constexpr static size_t BYTES = ceil_tobytes(446);
38
39 /// @brief Construct a new scalar from (max. 114) bytes. Little endian.
40 Scalar448(std::span<const uint8_t> x);
41
42 /// @brief Convert the scalar to bytes in little endian.
43 template <size_t S = BYTES>
44 std::array<uint8_t, S> to_bytes() const
45 requires(S >= BYTES)
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 }
51
52 /// @brief Access the i-th bit of the scalar. From 0 (lsb) to 445 (msb).
53 bool get_bit(size_t i) const;
54
55 /// @brief scalar = (scalar + other) mod L
56 Scalar448 operator+(const Scalar448& other) const;
57
58 /// @brief scalar = (scalar * other) mod L
59 Scalar448 operator*(const Scalar448& other) const;
60
61 /// @return true iff x >= L.
62 static bool bytes_are_reduced(std::span<const uint8_t> x);
63
64 private:
65 Scalar448(std::span<const word, WORDS> scalar_words) { copy_mem(m_scalar_words, scalar_words); }
66
67 std::array<word, WORDS> m_scalar_words;
68};
69
70} // namespace Botan
71
72#endif // BOTAN_CURVE448_SCALAR_H_
Representation of a scalar for X448.
std::array< uint8_t, S > to_bytes() const
Convert the scalar to bytes in little endian.
#define BOTAN_TEST_API
Definition compiler.h:51
BigInt operator*(const BigInt &x, const BigInt &y)
Definition big_ops3.cpp:46
constexpr size_t words_for_bits(size_t x)
OctetString operator+(const OctetString &k1, const OctetString &k2)
Definition symkey.cpp:99
constexpr auto store_le(ParamTs &&... params)
Definition loadstor.h:702
constexpr T ceil_tobytes(T bits)
Definition bit_ops.h:144
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:146