Botan 3.11.0
Crypto and TLS for C&
cmce_field_ordering.h
Go to the documentation of this file.
1/*
2 * Classic McEliece Field Ordering Generation
3 * (C) 2023 Jack Lloyd
4 * 2023,2024 Fabian Albert, Amos Treiber - Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 **/
8
9#ifndef BOTAN_CMCE_FIELD_ORDERING_H_
10#define BOTAN_CMCE_FIELD_ORDERING_H_
11
12#include <botan/internal/cmce_parameters.h>
13#include <botan/internal/cmce_types.h>
14
15namespace Botan {
16
17/**
18 * @brief Represents a field ordering for the Classic McEliece cryptosystem.
19 *
20 * Field ordering corresponds to the permutation pi defining the alpha sequence in
21 * the Classic McEliece specification (see Classic McEliece ISO Sec. 8.2.).
22 */
23class BOTAN_TEST_API Classic_McEliece_Field_Ordering {
24 public:
25 /**
26 * @brief Creates a field ordering from a random bit sequence. Corresponds to
27 * the algorithm described in Classic McEliece ISO Sec. 8.2.
28 *
29 * @param params The McEliece parameters.
30 * @param random_bits The random bit sequence.
31 * @return The field ordering.
32 */
33 static std::optional<Classic_McEliece_Field_Ordering> create_field_ordering(
35
36 /**
37 * @brief Create the field ordering from the control bits of a benes network.
38 *
39 * @param params The McEliece parameters.
40 * @param control_bits The control bits of the benes network.
41 * @return The field ordering.
42 */
43 static Classic_McEliece_Field_Ordering create_from_control_bits(const Classic_McEliece_Parameters& params,
44 const secure_bitvector& control_bits);
45
46 /**
47 * @brief Returns the field ordering as a vector of all alphas from alpha_0 to alpha_{n-1}.
48 *
49 * @param n The number of alphas to return.
50 * @return the vector of n alphas.
51 */
52 std::vector<Classic_McEliece_GF> alphas(size_t n) const;
53
54 /**
55 * @brief Generates the control bits of the benes network corresponding to the field ordering.
56 *
57 * @return the control bits.
58 */
60
61 /**
62 * @brief The pi values representing the field ordering.
63 *
64 * @return pi values.
65 */
66 CmcePermutation& pi_ref() { return m_pi; }
67
68 /**
69 * @brief The pi values representing the field ordering.
70 *
71 * @return pi values.
72 */
73 const CmcePermutation& pi_ref() const { return m_pi; }
74
75 /**
76 * @brief Constant time comparison of two field orderings.
77 *
78 * @param other The other field ordering.
79 * @return Mask of equality value
80 */
81 CT::Mask<uint16_t> ct_is_equal(const Classic_McEliece_Field_Ordering& other) const {
82 BOTAN_ARG_CHECK(other.pi_ref().size() == pi_ref().size(), "Field orderings must have the same size");
83 return CT::is_equal(pi_ref().data(), other.pi_ref().data(), pi_ref().size());
84 }
85
86 /**
87 * @brief Permute the field ordering with the given pivots.
88 *
89 * For example: If the pivot vector is 10101, the first, third and fifth element of the field ordering
90 * are permuted to positions 0, 1 and 2, respectively. The remaining elements are put at the end.
91 *
92 * The permutation is done for the elements from position m*t - mu,..., m*t + mu (excl.).
93 * This function implements Classic McEliece ISO Sec. 7.2.3 Steps 4-5.
94 *
95 * @param params The McEliece parameters.
96 * @param pivots The pivot vector.
97 */
98 void permute_with_pivots(const Classic_McEliece_Parameters& params, const CmceColumnSelection& pivots);
99
100 void _const_time_poison() const { CT::poison(m_pi); }
101
102 void _const_time_unpoison() const { CT::unpoison(m_pi); }
103
104 private:
105 Classic_McEliece_Field_Ordering(CmcePermutation pi, CmceGfMod poly_f) : m_pi(std::move(pi)), m_poly_f(poly_f) {}
106
107 private:
108 CmcePermutation m_pi;
109 CmceGfMod m_poly_f;
110};
111
112} // namespace Botan
113
114#endif
#define BOTAN_TEST_API
Definition api.h:41
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
Represents a field ordering for the Classic McEliece cryptosystem.
CT::Mask< uint16_t > ct_is_equal(const Classic_McEliece_Field_Ordering &other) const
Constant time comparison of two field orderings.
static std::optional< Classic_McEliece_Field_Ordering > create_field_ordering(const Classic_McEliece_Parameters &params, StrongSpan< const CmceOrderingBits > random_bits)
Creates a field ordering from a random bit sequence. Corresponds to the algorithm described in Classi...
const CmcePermutation & pi_ref() const
The pi values representing the field ordering.
static Classic_McEliece_Field_Ordering create_from_control_bits(const Classic_McEliece_Parameters &params, const secure_bitvector &control_bits)
Create the field ordering from the control bits of a benes network.
secure_bitvector alphas_control_bits() const
Generates the control bits of the benes network corresponding to the field ordering.
CmcePermutation & pi_ref()
The pi values representing the field ordering.
std::vector< Classic_McEliece_GF > alphas(size_t n) const
Returns the field ordering as a vector of all alphas from alpha_0 to alpha_{n-1}.
size_type size() const noexcept(noexcept(this->get().size()))
decltype(auto) data() noexcept(noexcept(this->get().data()))
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:798
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:67
constexpr void poison(const T *p, size_t n)
Definition ct_utils.h:56
Strong< uint16_t, struct CmceGfMod_ > CmceGfMod
Represents a GF(q) modulus.
Definition cmce_types.h:22
bitvector_base< secure_allocator > secure_bitvector
Definition bitvector.h:1304
Strong< secure_bitvector, struct CmceColumnSelection_ > CmceColumnSelection
Represents c of private key.
Definition cmce_types.h:46
Strong< secure_vector< uint16_t >, struct CmcePermutation_ > CmcePermutation
Represents a permutation (pi in spec). Used in field ordering creation.
Definition cmce_types.h:28