9#include <botan/internal/cmce_keys_internal.h>
23std::optional<Classic_McEliece_KeyPair_Internal> try_generate_keypair(std::span<uint8_t> out_next_seed,
24 const Classic_McEliece_Parameters& params,
29 auto big_e_xof = params.prg(seed);
32 auto ordering_bits = big_e_xof->output<
CmceOrderingBits>((params.sigma2() * params.q()) / 8);
33 auto irreducible_bits = big_e_xof->output<
CmceIrreducibleBits>((params.sigma1() * params.t()) / 8);
34 big_e_xof->output(out_next_seed);
43 auto g = params.poly_ring().compute_minimal_polynomial(irreducible_bits);
49 auto pk_matrix_and_pivots =
51 if(!pk_matrix_and_pivots) {
54 auto& [pk_matrix, pivots] = pk_matrix_and_pivots.value();
57 return Classic_McEliece_KeyPair_Internal{
58 .private_key = std::make_shared<Classic_McEliece_PrivateKeyInternal>(
59 params, std::move(seed), pivots, std::move(g.value()), std::move(field_ordering.value()), std::move(s)),
60 .public_key = std::make_shared<Classic_McEliece_PublicKeyInternal>(params, std::move(pk_matrix))};
93 .ct_is_equal(m_field_ordering)
95 "Control Bit Computation Check");
101 auto prg = m_params.
prg(m_delta);
114 for(
size_t i = 0; i <
g->
degree() - 1; ++i) {
123 field_ord_from_seed->permute_with_pivots(m_params,
c());
129 return ret.as_bool();
135 if(!pk_matrix_and_pivot.has_value()) {
136 throw Decoding_Error(
"Cannot create public key from private key. Private key is invalid.");
138 auto& [pk_matrix, pivot] = pk_matrix_and_pivot.value();
143 throw Decoding_Error(
"Cannot create public key from private key. Private key is invalid.");
146 return std::make_shared<Classic_McEliece_PublicKeyInternal>(sk.
params(), std::move(pk_matrix));
157 if(
auto keypair = try_generate_keypair(next_seed, params, std::move(current_seed))) {
158 return keypair.value();
160 current_seed = next_seed;
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
#define BOTAN_ASSERT(expr, assertion_made)
auto copy(const size_t count)
std::span< const uint8_t > take(const size_t count)
static constexpr Mask< T > expand(T v)
static constexpr Mask< T > is_equal(T x, T y)
static constexpr Mask< T > cleared()
static std::optional< Classic_McEliece_Field_Ordering > create_field_ordering(const Classic_McEliece_Parameters ¶ms, StrongSpan< const CmceOrderingBits > random_bits)
Creates a field ordering from a random bit sequence. Corresponds to the algorithm described in Classi...
static Classic_McEliece_Field_Ordering create_from_control_bits(const Classic_McEliece_Parameters ¶ms, 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.
static std::optional< std::pair< Classic_McEliece_Matrix, CmceColumnSelection > > create_matrix(const Classic_McEliece_Parameters ¶ms, const Classic_McEliece_Field_Ordering &field_ordering, const Classic_McEliece_Minimal_Polynomial &g)
Create the matrix H for a Classic McEliece instance given its parameters, field ordering and minimal ...
static std::optional< std::pair< Classic_McEliece_Matrix, CmceColumnSelection > > create_matrix_and_apply_pivots(const Classic_McEliece_Parameters ¶ms, Classic_McEliece_Field_Ordering &field_ordering, const Classic_McEliece_Minimal_Polynomial &g)
Create the matrix H for a Classic McEliece instance given its parameters, field ordering and minimal ...
secure_vector< uint8_t > serialize() const
Serialize the polynomial to bytes according to ISO Section 9.2.9.
static Classic_McEliece_Minimal_Polynomial from_bytes(std::span< const uint8_t > bytes, CmceGfMod poly_f)
Create a polynomial from bytes according to ISO Section 9.2.9.
static constexpr size_t seed_len()
The byte length of the seed delta. See ISO 9.2.12.
size_t q() const
The field size of the Classic McEliece instance's underlying Galois Field, i.e. GF(q) is the underlyi...
CmceGfMod poly_f() const
The monic irreducible polynomial f(z) of degree m over GF(2). Used for modular reduction in GF(2^m).
static constexpr size_t sk_c_bytes()
The byte length of the column selection c. See ISO 9.2.12.
std::unique_ptr< XOF > prg(std::span< const uint8_t > seed) const
Create a seeded XOF object representing Classic McEliece's PRG. See Classic McEliece ISO 9....
size_t n() const
The code length of the Classic McEliece instance.
size_t sk_poly_g_bytes() const
The length of the byte representation of the minimal polynomial g. See ISO 9.2.12.
size_t sk_alpha_control_bytes() const
The length of the byte representation of the field ordering's control bits. See ISO 9....
const Classic_McEliece_Polynomial_Ring & poly_ring() const
The underlying polynomial ring.
size_t sk_s_bytes() const
The byte length of the seed s. s is used for implicit rejection. See ISO 9.2.12.
static constexpr size_t sigma2()
Constant for field-ordering generation. (see Classic McEliece ISO 8.2)
size_t sk_size_bytes() const
The byte length of the secret key sk. See ISO 9.2.12.
static constexpr size_t sigma1()
The number of bits each GF element is encoded with.
size_t t() const
The weight of the error vector e.
std::optional< Classic_McEliece_Minimal_Polynomial > compute_minimal_polynomial(StrongSpan< const CmceIrreducibleBits > seed) const
Compute the minimal polynomial g of polynomial created from a seed.
size_t degree() const
Get the degree of the polynomial.
Classic_McEliece_GF & coef_at(size_t i)
Get the coefficient of the i-th monomial as a reference (from low to high degree).
Representation of a Classic McEliece private key.
const CmceRejectionSeed & s() const
The seed s for implicit rejection on decryption failure.
const Classic_McEliece_Field_Ordering & field_ordering() const
The field ordering alpha.
bool check_key() const
Checks the private key for consistency with the first component delta, i.e., recomputes s as a hash o...
secure_vector< uint8_t > serialize() const
Serializes the Classic McEliece private key as defined in Classic McEliece ISO Section 9....
const CmceKeyGenSeed & delta() const
The seed delta that was used to create the private key.
const Classic_McEliece_Parameters & params() const
The Classic McEliece parameters.
static Classic_McEliece_PrivateKeyInternal from_bytes(const Classic_McEliece_Parameters ¶ms, std::span< const uint8_t > sk_bytes)
Parses a Classic McEliece private key from a byte sequence.
Classic_McEliece_PrivateKeyInternal(const Classic_McEliece_Parameters ¶ms, CmceKeyGenSeed delta, CmceColumnSelection c, Classic_McEliece_Minimal_Polynomial g, Classic_McEliece_Field_Ordering alpha, CmceRejectionSeed s)
Construct a Classic McEliece private key.
const CmceColumnSelection & c() const
The column selection pivot vector c as defined in Classic McEliece ISO Section 9.2....
const Classic_McEliece_Minimal_Polynomial & g() const
The minimal polynomial g.
static std::shared_ptr< Classic_McEliece_PublicKeyInternal > create_from_private_key(const Classic_McEliece_PrivateKeyInternal &sk)
Create a Classic McEliece public key from a private key.
static GF_Mask is_equal(Classic_McEliece_GF a, Classic_McEliece_GF b)
CT::Mask< uint16_t > & elem_mask()
decltype(auto) begin() noexcept(noexcept(this->m_span.begin()))
decltype(auto) end() noexcept(noexcept(this->m_span.end()))
decltype(auto) size() const noexcept(noexcept(this->m_span.size()))
decltype(auto) driveby_unpoison(T &&v)
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Strong< secure_vector< uint8_t >, struct CmceOrderingBits_ > CmceOrderingBits
bitvector_base< secure_allocator > secure_bitvector
Strong< secure_vector< uint8_t >, struct CmceIrreducibleBits_ > CmceIrreducibleBits
Strong< secure_bitvector, struct CmceColumnSelection_ > CmceColumnSelection
Represents c of private key.
constexpr auto concat(Rs &&... ranges)
Strong< secure_vector< uint8_t >, struct CmceKeyGenSeed_ > CmceKeyGenSeed
Represents a delta (can be altered; final value stored in private key)
std::vector< T, secure_allocator< T > > secure_vector
Strong< secure_vector< uint8_t >, struct CmceRejectionSeed_ > CmceRejectionSeed
Represents s of private key.
Representation of a Classic McEliece key pair.
static Classic_McEliece_KeyPair_Internal generate(const Classic_McEliece_Parameters ¶ms, StrongSpan< const CmceInitialSeed > seed)
Generate a Classic McEliece key pair using the algorithm described in Classic McEliece ISO Section 8....