Botan 3.7.1
Crypto and TLS for C&
cmce_matrix.h
Go to the documentation of this file.
1/*
2 * Classic McEliece Matrix Logic
3 *
4 * (C) 2023 Jack Lloyd
5 * 2023,2024 Fabian Albert, Amos Treiber - Rohde & Schwarz Cybersecurity
6 *
7 * Botan is released under the Simplified BSD License (see license.txt)
8 **/
9
10#ifndef BOTAN_CMCE_MATRIX_H_
11#define BOTAN_CMCE_MATRIX_H_
12
13#include <botan/internal/bitvector.h>
14#include <botan/internal/cmce_field_ordering.h>
15#include <botan/internal/cmce_parameters.h>
16#include <botan/internal/cmce_poly.h>
17#include <botan/internal/cmce_types.h>
18
19namespace Botan {
20
21/**
22 * @brief Representation of the binary Classic McEliece matrix H, with H = (I_mt | T).
23 *
24 * Only the bytes of the submatrix T are stored.
25 */
27 public:
28 /**
29 * @brief Create the matrix H for a Classic McEliece instance given its
30 * parameters, field ordering and minimal polynomial.
31 *
32 * Output is a pair of the matrix and the pivot vector c that was used to
33 * create it in the semi-systematic form as described in Classic McEliece ISO
34 * Section 9.2.11.
35 *
36 * The update of alpha values as per Classic McEliece ISO Section 7.2.3 Step 5
37 * is not performed by this method because it is only used for public key loading
38 * where the values are already permuted and field_ordering cannot be altered.
39 *
40 * @param params Classic McEliece parameters
41 * @param field_ordering Field ordering
42 * @param g Minimal polynomial
43 * @return Pair(the matrix H, pivot vector c)
44 */
45 static std::optional<std::pair<Classic_McEliece_Matrix, CmceColumnSelection>> create_matrix(
46 const Classic_McEliece_Parameters& params,
47 const Classic_McEliece_Field_Ordering& field_ordering,
49
50 /**
51 * @brief Create the matrix H for a Classic McEliece instance given its
52 * parameters, field ordering and minimal polynomial.
53 *
54 * Output is a pair of the matrix and the pivot vector c that was used to
55 * create it in the semi-systematic form as described in Classic McEliece ISO
56 * Section 9.2.11.
57 *
58 * This method directly updates the field ordering values as described in Classic McEliece
59 * ISO Section 7.2.3 Step 5 (for f parameter sets).
60 *
61 * @param params Classic McEliece parameters
62 * @param field_ordering Field ordering (will be updated)
63 * @param g Minimal polynomial
64 * @return Pair(the matrix H, pivot vector c)
65 */
66 static std::optional<std::pair<Classic_McEliece_Matrix, CmceColumnSelection>> create_matrix_and_apply_pivots(
67 const Classic_McEliece_Parameters& params,
68 Classic_McEliece_Field_Ordering& field_ordering,
70
71 /**
72 * @brief The bytes of the submatrix T, with H=(I_mt, T) as defined in Classic
73 * McEliece ISO Section 9.2.7.
74 *
75 * @return The matrix bytes
76 */
77 const std::vector<uint8_t>& bytes() const { return m_mat_bytes; }
78
79 /**
80 * @brief Create a Classic_McEliece_Matrix from bytes.
81 *
82 * @param mat_bytes The bytes of the submatrix T as defined in Classic McEliece ISO Section 9.2.7.
83 */
84 Classic_McEliece_Matrix(const Classic_McEliece_Parameters& params, std::vector<uint8_t> mat_bytes) :
85 m_mat_bytes(std::move(mat_bytes)) {
86 BOTAN_ARG_CHECK(m_mat_bytes.size() == params.pk_size_bytes(), "Invalid byte size for matrix");
87 if(params.pk_no_cols() % 8 == 0) {
88 return;
89 }
90 // Check padding of mat_bytes rows
91 BOTAN_ASSERT_NOMSG(m_mat_bytes.size() == params.pk_no_rows() * params.pk_row_size_bytes());
92 for(size_t row = 0; row < params.pk_no_rows(); ++row) {
93 uint8_t padded_byte = m_mat_bytes[(row + 1) * params.pk_row_size_bytes() - 1];
94 CT::unpoison(padded_byte);
95 BOTAN_ARG_CHECK(padded_byte >> (params.pk_no_cols() % 8) == 0, "Valid padding of unused bytes");
96 }
97 }
98
99 /**
100 * @brief Multiply the Classic McEliece matrix H with a bitvector e.
101 *
102 * @param params Classic McEliece parameters
103 * @param e The bitvector e
104 * @return H*e
105 */
106 CmceCodeWord mul(const Classic_McEliece_Parameters& params, const CmceErrorVector& e) const;
107
108 constexpr void _const_time_unpoison() const { CT::unpoison(m_mat_bytes); }
109
110 private:
111 /// The bytes of the submatrix T
112 const std::vector<uint8_t> m_mat_bytes; // can we use bitvector?
113};
114
115} // namespace Botan
116
117#endif // BOTAN_CMCE_MATRIX_H_
#define BOTAN_TEST_API
Definition api.h:39
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
Represents a field ordering for the Classic McEliece cryptosystem.
Representation of the binary Classic McEliece matrix H, with H = (I_mt | T).
Definition cmce_matrix.h:26
constexpr void _const_time_unpoison() const
const std::vector< uint8_t > & bytes() const
The bytes of the submatrix T, with H=(I_mt, T) as defined in Classic McEliece ISO Section 9....
Definition cmce_matrix.h:77
Classic_McEliece_Matrix(const Classic_McEliece_Parameters &params, std::vector< uint8_t > mat_bytes)
Create a Classic_McEliece_Matrix from bytes.
Definition cmce_matrix.h:84
Representation of a minimal polynomial in GF(q)[y].
Definition cmce_poly.h:81
size_t pk_no_rows() const
The number of rows in the public key's matrix.
size_t pk_row_size_bytes() const
The number of bytes for each row in the public key's matrix.
size_t pk_no_cols() const
The number of columns in the public key's matrix.
size_t pk_size_bytes() const
The number of bytes for the public key.