• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 19012754211

02 Nov 2025 01:10PM UTC coverage: 90.677% (+0.006%) from 90.671%
19012754211

push

github

web-flow
Merge pull request #5137 from randombit/jack/clang-tidy-includes

Remove various unused includes flagged by clang-tidy misc-include-cleaner

100457 of 110786 relevant lines covered (90.68%)

12189873.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.18
/src/tests/test_crystals.cpp
1
/*
2
 * Tests for PQ Crystals
3
 * (C) 2024 Jack Lloyd
4
 * (C) 2024 René Meusel, Rohde & Schwarz Cybersecurity
5
 *
6
 * Botan is released under the Simplified BSD License (see license.txt)
7
 */
8

9
#include "tests.h"
10

11
#if defined(BOTAN_HAS_PQCRYSTALS)
12
   #include <botan/hex.h>
13

14
   #include <botan/internal/pqcrystals.h>
15
   #include <botan/internal/pqcrystals_encoding.h>
16
   #include <botan/internal/pqcrystals_helpers.h>
17
   #include <botan/internal/stl_util.h>
18

19
namespace Botan_Tests {
20

21
namespace {
22

23
template <std::integral T>
24
consteval T gcd(T x, T y) {
25
   return Botan::extended_euclidean_algorithm<T>(x, y).gcd;
26
}
27

28
template <std::integral T>
29
consteval T v(T x, T y) {
30
   return Botan::extended_euclidean_algorithm<T>(x, y).v;
31
}
32

33
template <std::integral T>
34
consteval T u(T x, T y) {
35
   return Botan::extended_euclidean_algorithm<T>(x, y).u;
36
}
37

38
Test::Result test_extended_euclidean_algorithm() {
1✔
39
   Test::Result res("Extended Euclidean Algorithm");
1✔
40

41
   // The wrapper template functions gcd<>(), v<>() and u<>() are workarounds
42
   // for an assumed bug in MSVC 19.38.33134 that does not accept the invocation
43
   // of the consteval function `extended_euclidean_algorithm` as a parameter to
44
   // `test_is_eq()`.
45
   //
46
   // The resulting error is:
47
   //    error C7595: 'Botan::extended_euclidean_algorithm': call to immediate function is not a constant expression
48
   //
49
   // What we'd actually want to write here:
50
   //    res.test_is_eq<uint32_t>("gcd(350, 294)", Botan::extended_euclidean_algorithm<uint32_t>(350, 294).gcd, 14);
51
   res.test_is_eq<uint32_t>("gcd(1337, 1337)", gcd<uint32_t>(1337, 1337), 1337);
1✔
52
   res.test_is_eq<uint32_t>("gcd(350, 294)", gcd<uint32_t>(350, 294), 14);
1✔
53
   res.test_is_eq<uint32_t>("gcd(294, 350)", gcd<uint32_t>(294, 350), 14);
1✔
54

55
   res.test_is_eq<uint16_t>("gcd(1337, 1337)", gcd<uint16_t>(1337, 1337), 1337);
1✔
56
   res.test_is_eq<uint16_t>("gcd(350, 294)", gcd<uint16_t>(350, 294), 14);
1✔
57
   res.test_is_eq<uint16_t>("gcd(294, 350)", gcd<uint16_t>(294, 350), 14);
1✔
58

59
   res.test_is_eq<uint16_t>("u(1337, 1337)", u<uint16_t>(1337, 1337), 0);
1✔
60
   res.test_is_eq<uint16_t>("v(1337, 1337)", v<uint16_t>(1337, 1337), 1);
1✔
61
   res.test_is_eq<uint16_t>("u(294, 350)", u<uint16_t>(294, 350), 6);
1✔
62

63
   res.test_is_eq<int16_t>("q^-1(3329) - Kyber::Q", Botan::modular_inverse<int16_t>(3329), -3327);
1✔
64
   res.test_is_eq<int32_t>("q^-1(8380417) - Dilithium::Q", Botan::modular_inverse<int32_t>(8380417), 58728449);
1✔
65

66
   return res;
1✔
67
}
×
68

69
// Equivalent to Kyber's constants
70
struct Kyberish_Constants {
71
      using T = int16_t;
72
      static constexpr T N = 256;
73
      static constexpr T Q = 3329;
74
      static constexpr T F = 3303;
75
      static constexpr T ROOT_OF_UNITY = 17;
76
      static constexpr size_t NTT_Degree = 128;
77
};
78

79
// Equivalent to Dilithium's constants
80
struct Dilithiumish_Constants {
81
      using T = int32_t;
82
      static constexpr T N = 256;
83
      static constexpr T Q = 8380417;
84
      static constexpr T F = 8347681;
85
      static constexpr T ROOT_OF_UNITY = 1753;
86
      static constexpr size_t NTT_Degree = 256;
87
};
88

89
template <typename ConstsT>
90
class Mock_Trait final : public Botan::CRYSTALS::Trait_Base<ConstsT, Mock_Trait<ConstsT>> {
91
   public:
92
      using T = typename Botan::CRYSTALS::Trait_Base<ConstsT, Mock_Trait<ConstsT>>::T;
93
      using T2 = typename Botan::CRYSTALS::Trait_Base<ConstsT, Mock_Trait<ConstsT>>::T2;
94
      constexpr static auto N = Botan::CRYSTALS::Trait_Base<ConstsT, Mock_Trait<ConstsT>>::N;
95

96
      static T montgomery_reduce_coefficient(T2 /*unused*/) {
97
         throw Botan_Tests::Test_Error("montgomery reduction not implemented");
98
      }
99

100
      static T barrett_reduce_coefficient(T /*unused*/) {
101
         throw Botan_Tests::Test_Error("barrett reduction not implemented");
102
      }
103

104
      static void ntt(std::span<T, N> /*unused*/) { throw Botan_Tests::Test_Error("NTT not implemented"); }
105

106
      static void inverse_ntt(std::span<T, N> /*unused*/) {
107
         throw Botan_Tests::Test_Error("inverse NTT not implemented");
108
      }
109

110
      static void poly_pointwise_montgomery(std::span<T, N> /*unused*/,
111
                                            std::span<T, N> /*unused*/,
112
                                            std::span<T, N> /*unused*/) {
113
         throw Botan_Tests::Test_Error("pointwise multiplication not implemented");
114
      }
115
};
116

117
using Kyberish_Trait = Mock_Trait<Kyberish_Constants>;
118

119
using Domain = Botan::CRYSTALS::Domain;
120

121
template <Domain D>
122
using Kyberish_Poly = Botan::CRYSTALS::Polynomial<Kyberish_Trait, D>;
123

124
template <Domain D>
125
using Kyberish_PolyVec = Botan::CRYSTALS::PolynomialVector<Kyberish_Trait, D>;
126

127
std::vector<Test::Result> test_polynomial_basics() {
1✔
128
   return {
1✔
129
      CHECK("polynomial owning storage",
130
            [](Test::Result& res) {
1✔
131
               Kyberish_Poly<Domain::Normal> p;
1✔
132
               res.confirm("default constructed poly owns memory", p.owns_storage());
2✔
133
               for(auto coeff : p) {
257✔
134
                  res.test_is_eq<int16_t>("default constructed poly has 0 coefficients", coeff, 0);
512✔
135
               }
136

137
               Kyberish_Poly<Domain::NTT> p_ntt;
1✔
138
               res.confirm("default constructed poly owns memory (NTT)", p_ntt.owns_storage());
2✔
139
               for(auto coeff : p) {
257✔
140
                  res.test_is_eq<int16_t>("default constructed poly (NTT) has 0 coefficients", coeff, 0);
512✔
141
               }
142
            }),
2✔
143

144
      CHECK("polynomial vector managing storage",
145
            [](Test::Result& res) {
1✔
146
               Kyberish_PolyVec<Domain::Normal> polys(4);
1✔
147
               res.test_is_eq<size_t>("requested size", polys.size(), 4);
1✔
148

149
               for(const auto& poly : polys) {
5✔
150
                  res.confirm("poly embedded in vector does not own memory", !poly.owns_storage());
8✔
151
               }
152

153
               Kyberish_PolyVec<Domain::NTT> polys_ntt(4);
1✔
154
               res.test_is_eq<size_t>("requested size (NTT)", polys.size(), 4);
1✔
155

156
               for(const auto& poly : polys_ntt) {
5✔
157
                  res.confirm("poly (NTT) embedded in vector does not own memory", !poly.owns_storage());
8✔
158
               }
159
            }),
1✔
160

161
      CHECK("cloned polynomials always manage their storage",
162
            [](Test::Result& res) {
1✔
163
               Kyberish_Poly<Domain::Normal> p;
1✔
164
               auto p2 = p.clone();
1✔
165
               res.confirm("cloned poly owns memory", p2.owns_storage());
2✔
166

167
               Kyberish_PolyVec<Domain::Normal> pv(3);
1✔
168
               for(auto& poly : pv) {
4✔
169
                  res.require("poly in vector does not own memory", !poly.owns_storage());
3✔
170
                  auto pv2 = poly.clone();
3✔
171
                  res.confirm("cloned poly in vector owns memory", pv2.owns_storage());
6✔
172
               }
3✔
173

174
               auto pv2 = pv.clone();
1✔
175
               for(const auto& poly : pv2) {
4✔
176
                  res.confirm("cloned vector polynomial don't own memory", !poly.owns_storage());
6✔
177
               }
178

179
               Kyberish_Poly<Domain::NTT> p_ntt;
1✔
180
               auto p2_ntt = p_ntt.clone();
1✔
181
               res.confirm("cloned poly (NTT) owns memory", p2_ntt.owns_storage());
2✔
182

183
               Kyberish_PolyVec<Domain::NTT> pv_ntt(3);
1✔
184
               for(auto& poly : pv_ntt) {
4✔
185
                  res.require("poly (NTT) in vector does not own memory", !poly.owns_storage());
3✔
186
                  auto pv2_ntt = poly.clone();
3✔
187
                  res.confirm("cloned poly (NTT) in vector owns memory", pv2_ntt.owns_storage());
6✔
188
               }
3✔
189

190
               auto pv2_ntt = pv_ntt.clone();
1✔
191
               for(const auto& poly : pv2_ntt) {
4✔
192
                  res.confirm("cloned vector polynomial (NTT) don't own memory", !poly.owns_storage());
6✔
193
               }
194
            }),
5✔
195

196
      CHECK("hamming weight of polynomials",
197
            [](Test::Result& res) {
1✔
198
               Kyberish_Poly<Domain::Normal> p;
1✔
199
               res.test_is_eq<size_t>("hamming weight of 0", p.hamming_weight(), 0);
2✔
200

201
               p[0] = 1337;
1✔
202
               res.test_is_eq<size_t>("hamming weight of 1", p.hamming_weight(), 1);
2✔
203

204
               p[1] = 42;
1✔
205
               res.test_is_eq<size_t>("hamming weight of 2", p.hamming_weight(), 2);
2✔
206

207
               p[2] = 11;
1✔
208
               res.test_is_eq<size_t>("hamming weight of 3", p.hamming_weight(), 3);
2✔
209

210
               p[3] = 4;
1✔
211
               res.test_is_eq<size_t>("hamming weight of 4", p.hamming_weight(), 4);
2✔
212

213
               p[3] = 0;
1✔
214
               res.test_is_eq<size_t>("hamming weight of 3", p.hamming_weight(), 3);
2✔
215

216
               p[2] = 0;
1✔
217
               res.test_is_eq<size_t>("hamming weight of 2", p.hamming_weight(), 2);
2✔
218

219
               p[1] = 0;
1✔
220
               res.test_is_eq<size_t>("hamming weight of 1", p.hamming_weight(), 1);
2✔
221

222
               p[0] = 0;
1✔
223
               res.test_is_eq<size_t>("hamming weight of 0", p.hamming_weight(), 0);
3✔
224
            }),
1✔
225

226
      CHECK("hamming weight of polynomial vectors",
227
            [](Test::Result& res) {
1✔
228
               Kyberish_PolyVec<Domain::Normal> pv(3);
1✔
229
               res.test_is_eq<size_t>("hamming weight of 0", pv.hamming_weight(), 0);
2✔
230

231
               pv[0][0] = 1337;
1✔
232
               res.test_is_eq<size_t>("hamming weight of 1", pv.hamming_weight(), 1);
2✔
233

234
               pv[1][1] = 42;
1✔
235
               res.test_is_eq<size_t>("hamming weight of 2", pv.hamming_weight(), 2);
2✔
236

237
               pv[2][2] = 11;
1✔
238
               res.test_is_eq<size_t>("hamming weight of 3", pv.hamming_weight(), 3);
2✔
239

240
               pv[2][2] = 0;
1✔
241
               res.test_is_eq<size_t>("hamming weight of 2", pv.hamming_weight(), 2);
2✔
242

243
               pv[1][1] = 0;
1✔
244
               res.test_is_eq<size_t>("hamming weight of 1", pv.hamming_weight(), 1);
2✔
245

246
               pv[0][0] = 0;
1✔
247
               res.test_is_eq<size_t>("hamming weight of 0", pv.hamming_weight(), 0);
2✔
248
            }),
1✔
249

250
      CHECK("value range validation",
251
            [](Test::Result& res) {
1✔
252
               Kyberish_Poly<Domain::Normal> p;
1✔
253
               res.confirm("value range validation (all zero)", p.ct_validate_value_range(0, 1));
2✔
254

255
               p[0] = 1;
1✔
256
               p[32] = 1;
1✔
257
               p[172] = 1;
1✔
258
               res.confirm("value range validation", p.ct_validate_value_range(0, 1));
2✔
259

260
               p[11] = 2;
1✔
261
               res.confirm("value range validation", !p.ct_validate_value_range(0, 1));
2✔
262

263
               p[11] = -1;
1✔
264
               res.confirm("value range validation", !p.ct_validate_value_range(0, 1));
2✔
265
            }),
1✔
266

267
      CHECK("value range validation for polynomial vectors",
268
            [](Test::Result& res) {
1✔
269
               Kyberish_PolyVec<Domain::Normal> pv(3);
1✔
270
               res.confirm("value range validation (all zero)", pv.ct_validate_value_range(0, 1));
2✔
271

272
               pv[0][0] = 1;
1✔
273
               pv[1][32] = 1;
1✔
274
               pv[2][172] = 1;
1✔
275
               res.confirm("value range validation", pv.ct_validate_value_range(0, 1));
2✔
276

277
               pv[0][11] = 2;
1✔
278
               res.confirm("value range validation", !pv.ct_validate_value_range(0, 1));
2✔
279

280
               pv[0][11] = -1;
1✔
281
               res.confirm("value range validation", !pv.ct_validate_value_range(0, 1));
2✔
282
            }),
1✔
283
   };
8✔
284
}
1✔
285

286
namespace {
287

288
   #if defined(BOTAN_HAS_XOF)
289

290
class DeterministicXOF : public Botan::XOF {
1✔
291
   public:
292
      explicit DeterministicXOF(std::span<const uint8_t> data) : m_data(data) {}
3✔
293

294
      std::string name() const override { return "DeterministicXOF"; }
×
295

296
      bool accepts_input() const override { return false; }
×
297

298
      std::unique_ptr<XOF> copy_state() const override { throw Botan_Tests::Test_Error("copy_state not implemented"); }
×
299

300
      std::unique_ptr<XOF> new_object() const override { throw Botan_Tests::Test_Error("new_object not implemented"); }
×
301

302
      size_t block_size() const override { return 1; }
×
303

304
      void start_msg(std::span<const uint8_t> /*unused*/, std::span<const uint8_t> /*unused*/) override {
×
305
         throw Botan_Tests::Test_Error("start_msg not implemented");
×
306
      }
307

308
      void add_data(std::span<const uint8_t> /*unused*/) override {
×
309
         throw Botan_Tests::Test_Error("add_data not implemented");
×
310
      }
311

312
      void generate_bytes(std::span<uint8_t> output) override { m_data.copy_into(output); }
112✔
313

314
      void reset() override {}
×
315

316
   private:
317
      Botan::BufferSlicer m_data;
318
};
319

320
   #endif
321

322
template <Botan::CRYSTALS::crystals_trait Trait, int32_t range>
323
void random_encoding_roundtrips(Test::Result& res, Botan::RandomNumberGenerator& rng, size_t expected_encoding_bits) {
22✔
324
   using Poly = Botan::CRYSTALS::Polynomial<Trait, Domain::Normal>;
325
   using T = typename Trait::T;
326

327
   auto random_poly = [&rng]() -> Poly {
44✔
328
      Poly p;
22✔
329
      std::array<uint8_t, sizeof(T)> buf{};
22✔
330
      for(auto& coeff : p) {
5,654✔
331
         rng.randomize(buf);
5,632✔
332
         coeff = static_cast<T>((Botan::load_be(buf) % (range + 1)));
5,632✔
333
      }
334
      return p;
22✔
335
   };
×
336

337
   const auto p = random_poly();
22✔
338
   std::vector<uint8_t> buffer((p.size() * expected_encoding_bits + 7) / 8);
22✔
339
   Botan::BufferStuffer stuffer(buffer);
22✔
340
   Botan::CRYSTALS::pack<range>(p, stuffer);
22✔
341
   res.confirm("encoded polynomial fills buffer", stuffer.full());
44✔
342

343
   Botan::BufferSlicer slicer(buffer);
22✔
344
   Poly p_unpacked;
22✔
345
   Botan::CRYSTALS::unpack<range>(p_unpacked, slicer);
22✔
346
   res.confirm("decoded polynomial reads all bytes", slicer.empty());
44✔
347

348
   p_unpacked -= p;
349
   res.test_eq("p = unpack(pack(p))", p_unpacked.hamming_weight(), 0);
44✔
350
}
66✔
351

352
}  // namespace
353

354
std::vector<Test::Result> test_encoding() {
1✔
355
   const auto threebitencoding = Botan::hex_decode(
1✔
356
      "88C61AD158231A6B44638D68AC118D35A2B14634D688C61AD158231A6B44638D68AC118D"
357
      "35A2B14634D688C61AD158231A6B44638D68AC118D35A2B14634D688C61AD158231A6B44"
358
      "638D68AC118D35A2B14634D688C61AD158231A6B44638D68");
1✔
359

360
   const auto eightbitencoding = Botan::hex_decode(
1✔
361
      "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20212223"
362
      "2425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F4041424344454647"
363
      "48494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B"
364
      "6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F"
365
      "909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3"
366
      "B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7"
367
      "D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFB"
368
      "FCFDFEFF");
1✔
369

370
   const auto tenbitencoding = Botan::hex_decode(
1✔
371
      "00084080010828C0800310484081051868C08107208840820928A8C0820B30C840830D38"
372
      "E8C0830F40084184114828C1841350484185155868C18517608841861968A8C1861B70C8"
373
      "41871D78E8C1871F80084288218828C2882390484289259868C28927A088428A29A8A8C2"
374
      "8A2BB0C8428B2DB8E8C28B2FC008438C31C828C38C33D048438D35D868C38D37E088438E"
375
      "39E8A8C38E3BF0C8438F3DF8E8C38F3F00094490410829C4904310494491451869C49147"
376
      "208944924928A9C4924B30C944934D38E9C4934F40094594514829C59453504945955558"
377
      "69C59557608945965968A9C5965B70C945975D78E9C5975F80094698618829C698639049"
378
      "4699659869C69967A089469A69A8A9C69A6BB0C9469B6DB8E9C69B6FC009479C71C829C7"
379
      "9C73D049479D75D869C79D77E089479E79E8A9C79E7BF0C9479F7DF8E9C79F7F");
1✔
380

381
   return {
1✔
382
      CHECK("encode polynomial coefficients into buffer",
383
            [&](Test::Result& res) {
1✔
384
               // value range is about 3 bits
385
               Kyberish_Poly<Domain::Normal> p1;
1✔
386
               for(size_t i = 0; i < p1.size(); ++i) {
257✔
387
                  p1[i] = static_cast<Kyberish_Constants::T>(i % 7);
256✔
388
               }
389

390
               std::vector<uint8_t> buffer1(96);
1✔
391
               Botan::BufferStuffer stuffer1(buffer1);
1✔
392
               Botan::CRYSTALS::pack<6>(p1, stuffer1);
1✔
393
               res.test_eq("3 bit encoding", buffer1, threebitencoding);
2✔
394

395
               // value range is exactly one byte
396
               Kyberish_Poly<Domain::Normal> p2;
1✔
397
               for(size_t i = 0; i < p2.size(); ++i) {
257✔
398
                  p2[i] = static_cast<Kyberish_Constants::T>(i);
256✔
399
               }
400

401
               std::vector<uint8_t> buffer2(256);
1✔
402
               Botan::BufferStuffer stuffer2(buffer2);
1✔
403
               Botan::CRYSTALS::pack<255>(p2, stuffer2);
1✔
404
               res.test_eq("8 bit encoding", buffer2, eightbitencoding);
2✔
405

406
               // value range for 10 bits, with mapping function
407
               std::vector<uint8_t> buffer3(p2.size() / 8 * 10 /* bits */);
1✔
408
               Botan::BufferStuffer stuffer3(buffer3);
1✔
409
               Botan::CRYSTALS::pack<512>(p2, stuffer3, [](int16_t x) -> uint16_t { return x * 2; });
257✔
410
               res.test_eq("10 bit encoding", buffer3, tenbitencoding);
2✔
411
            }),
5✔
412

413
      CHECK("decode polynomial coefficients from buffer",
414
            [&](Test::Result& res) {
1✔
415
               Kyberish_Poly<Domain::Normal> p1;
1✔
416
               Botan::BufferSlicer slicer1(threebitencoding);
1✔
417
               Botan::CRYSTALS::unpack<6>(p1, slicer1);
1✔
418
               res.require("read all bytes from 3-bit encoding", slicer1.empty());
1✔
419
               for(size_t i = 0; i < p1.size(); ++i) {
257✔
420
                  res.test_is_eq<int16_t>("decoded 3-bit coefficient", p1[i], i % 7);
512✔
421
               }
422

423
               Kyberish_Poly<Domain::Normal> p2;
1✔
424
               Botan::BufferSlicer slicer2(eightbitencoding);
1✔
425
               Botan::CRYSTALS::unpack<255>(p2, slicer2);
1✔
426
               res.require("read all bytes from 8-bit encoding", slicer2.empty());
1✔
427
               for(size_t i = 0; i < p2.size(); ++i) {
257✔
428
                  res.test_is_eq<size_t>("decoded 8-bit coefficient", p2[i], i);
512✔
429
               }
430

431
               Kyberish_Poly<Domain::Normal> p3;
1✔
432
               Botan::BufferSlicer slicer3(tenbitencoding);
1✔
433
               Botan::CRYSTALS::unpack<512>(p3, slicer3, [](uint16_t x) -> int16_t { return x / 2; });
257✔
434
               res.require("read all bytes from 10-bit encoding", slicer3.empty());
1✔
435
               for(size_t i = 0; i < p3.size(); ++i) {
257✔
436
                  res.test_is_eq<size_t>("decoded 10-bit coefficient with mapping", p3[i], i);
512✔
437
               }
438
            }),
3✔
439

440
      CHECK("decode polynomial coefficients from XOF",
441
            [&](Test::Result& res) {
1✔
442
   #if defined(BOTAN_HAS_XOF)
443
               Kyberish_Poly<Domain::Normal> p1;
1✔
444
               DeterministicXOF xof1(threebitencoding);
1✔
445
               Botan::CRYSTALS::unpack<6>(p1, xof1);
1✔
446
               for(size_t i = 0; i < p1.size(); ++i) {
257✔
447
                  res.test_is_eq<int16_t>("decoded 3-bit coefficient", p1[i], i % 7);
512✔
448
               }
449

450
               Kyberish_Poly<Domain::Normal> p2;
1✔
451
               DeterministicXOF xof2(eightbitencoding);
1✔
452
               Botan::CRYSTALS::unpack<255>(p2, xof2);
1✔
453
               for(size_t i = 0; i < p2.size(); ++i) {
257✔
454
                  res.test_is_eq<size_t>("decoded 8-bit coefficient", p2[i], i);
512✔
455
               }
456

457
               Kyberish_Poly<Domain::Normal> p3;
1✔
458
               DeterministicXOF xof3(tenbitencoding);
1✔
459
               Botan::CRYSTALS::unpack<512>(p3, xof3, [](int16_t x) -> int16_t { return x / 2; });
257✔
460
               for(size_t i = 0; i < p3.size(); ++i) {
257✔
461
                  res.test_is_eq<size_t>("decoded 10-bit coefficient with mapping", p3[i], i);
512✔
462
               }
463
   #else
464
               BOTAN_UNUSED(res);
465
   #endif
466
            }),
4✔
467

468
      CHECK("random encoding roundtrips (0 to x)",
469
            [](Test::Result& res) {
1✔
470
               auto rng = Test::new_rng("CRYSTALS encoding roundtrips");
1✔
471
               random_encoding_roundtrips<Kyberish_Trait, 3>(res, *rng, 2);
1✔
472
               random_encoding_roundtrips<Kyberish_Trait, 6>(res, *rng, 3);
1✔
473
               random_encoding_roundtrips<Kyberish_Trait, 12>(res, *rng, 4);
1✔
474
               random_encoding_roundtrips<Kyberish_Trait, 15>(res, *rng, 4);
1✔
475
               random_encoding_roundtrips<Kyberish_Trait, 31>(res, *rng, 5);
1✔
476
               random_encoding_roundtrips<Kyberish_Trait, 42>(res, *rng, 6);
1✔
477
               random_encoding_roundtrips<Kyberish_Trait, 128>(res, *rng, 8);
1✔
478
               random_encoding_roundtrips<Kyberish_Trait, 1337>(res, *rng, 11);
1✔
479
            }),
1✔
480

481
      CHECK("random encoding roundtrips (Kyber ranges)",
482
            [](Test::Result& res) {
1✔
483
               auto rng = Test::new_rng("CRYSTALS encoding roundtrips as used in kyber");
1✔
484
               random_encoding_roundtrips<Kyberish_Trait, 1>(res, *rng, 1);
1✔
485
               random_encoding_roundtrips<Kyberish_Trait, (1 << 4) - 1>(res, *rng, 4);
1✔
486
               random_encoding_roundtrips<Kyberish_Trait, (1 << 5) - 1>(res, *rng, 5);
1✔
487
               random_encoding_roundtrips<Kyberish_Trait, (1 << 10) - 1>(res, *rng, 10);
1✔
488
               random_encoding_roundtrips<Kyberish_Trait, (1 << 11) - 1>(res, *rng, 11);
1✔
489
               random_encoding_roundtrips<Kyberish_Trait, Kyberish_Constants::Q - 1>(res, *rng, 12);
1✔
490
            }),
1✔
491

492
      CHECK("random encoding roundtrips (Dilithium ranges)",
493
            [](Test::Result& res) {
1✔
494
               using Dilithiumish_Trait = Mock_Trait<Dilithiumish_Constants>;
1✔
495

496
               auto rng = Test::new_rng("CRYSTALS encoding roundtrips as used in kyber");
1✔
497
               constexpr auto t1 = 1023;
1✔
498
               constexpr auto gamma2_32 = 15;
1✔
499
               constexpr auto gamma2_88 = 43;
1✔
500
               constexpr auto gamma1_17 = 131072;
1✔
501
               constexpr auto gamma1_19 = 524288;
1✔
502
               constexpr auto eta2 = 2;
1✔
503
               constexpr auto eta4 = 4;
1✔
504
               constexpr auto twotothed = 4096;
1✔
505
               random_encoding_roundtrips<Dilithiumish_Trait, t1>(res, *rng, 10);
1✔
506
               random_encoding_roundtrips<Dilithiumish_Trait, gamma2_32>(res, *rng, 4);
1✔
507
               random_encoding_roundtrips<Dilithiumish_Trait, gamma2_88>(res, *rng, 6);
1✔
508
               random_encoding_roundtrips<Dilithiumish_Trait, 2 * gamma1_17 - 1>(res, *rng, 18);
1✔
509
               random_encoding_roundtrips<Dilithiumish_Trait, 2 * gamma1_19 - 1>(res, *rng, 20);
1✔
510
               random_encoding_roundtrips<Dilithiumish_Trait, 2 * eta2>(res, *rng, 3);
1✔
511
               random_encoding_roundtrips<Dilithiumish_Trait, 2 * eta4>(res, *rng, 4);
1✔
512
               random_encoding_roundtrips<Dilithiumish_Trait, 2 * twotothed - 1>(res, *rng, 13);
1✔
513
            }),
1✔
514
   };
7✔
515
}
4✔
516

517
class MockedXOF {
518
   public:
519
      MockedXOF() : m_counter(0) {}
5✔
520

521
      template <size_t bytes>
522
      auto output() {
17✔
523
         std::array<uint8_t, bytes> result{};
17✔
524
         for(uint8_t& byte : result) {
56✔
525
            byte = static_cast<uint8_t>(m_counter++);
39✔
526
         }
527
         return result;
17✔
528
      }
529

530
   private:
531
      size_t m_counter;
532
};
533

534
template <size_t bound>
535
using Mocked_Bounded_XOF = Botan::detail::Bounded_XOF<MockedXOF, bound>;
536

537
std::vector<Test::Result> test_bounded_xof() {
1✔
538
   return {
1✔
539
      CHECK("zero bound is reached immediately",
540
            [](Test::Result& result) {
1✔
541
               Mocked_Bounded_XOF<0> xof;
1✔
542
               result.test_throws<Botan::Internal_Error>("output<1> throws", [&xof]() { xof.next_byte(); });
3✔
543
            }),
1✔
544

545
      CHECK("bounded XOF with small bound",
546
            [](Test::Result& result) {
1✔
547
               Mocked_Bounded_XOF<3> xof;
1✔
548
               result.test_is_eq("next_byte() returns 0", xof.next_byte(), uint8_t(0));
2✔
549
               result.test_is_eq("next_byte() returns 1", xof.next_byte(), uint8_t(1));
2✔
550
               result.test_is_eq("next_byte() returns 2", xof.next_byte(), uint8_t(2));
2✔
551
               result.test_throws<Botan::Internal_Error>("next_byte() throws", [&xof]() { xof.next_byte(); });
3✔
552
            }),
1✔
553

554
      CHECK("filter bytes",
555
            [](Test::Result& result) {
1✔
556
               auto filter = [](uint8_t byte) {
6✔
557
                  //test
558
                  return byte % 2 == 1;
5✔
559
               };
560

561
               Mocked_Bounded_XOF<5> xof;
1✔
562
               result.test_is_eq("next_byte() returns 1", xof.next_byte(filter), uint8_t(1));
2✔
563
               result.test_is_eq("next_byte() returns 3", xof.next_byte(filter), uint8_t(3));
2✔
564
               result.test_throws<Botan::Internal_Error>("next_byte() throws", [&]() { xof.next_byte(filter); });
3✔
565
            }),
1✔
566

567
      CHECK("map bytes",
568
            [](Test::Result& result) {
1✔
569
               auto map = [](auto bytes) { return Botan::load_be(bytes); };
4✔
570

571
               Mocked_Bounded_XOF<17> xof;
1✔
572
               result.test_is_eq("next returns 0x00010203", xof.next<4>(map), uint32_t(0x00010203));
1✔
573
               result.test_is_eq("next returns 0x04050607", xof.next<4>(map), uint32_t(0x04050607));
1✔
574
               result.test_is_eq("next returns 0x08090A0B", xof.next<4>(map), uint32_t(0x08090A0B));
1✔
575
               result.test_is_eq("next returns 0x0C0D0E0F", xof.next<4>(map), uint32_t(0x0C0D0E0F));
1✔
576
               result.test_throws<Botan::Internal_Error>("next() throws", [&]() { xof.next<4>(map); });
3✔
577
            }),
1✔
578

579
      CHECK("map and filter bytes",
580
            [](Test::Result& result) {
1✔
581
               auto map = [](std::array<uint8_t, 3> bytes) -> uint32_t { return bytes[0] + bytes[1] + bytes[2]; };
5✔
582
               auto filter = [](uint32_t number) { return number < 50; };
1✔
583

584
               Mocked_Bounded_XOF<17> xof;
1✔
585
               result.test_is_eq("next returns 3", xof.next<3>(map, filter), uint32_t(3));
2✔
586
               result.test_is_eq("next returns 12", xof.next<3>(map, filter), uint32_t(12));
2✔
587
               result.test_is_eq("next returns 21", xof.next<3>(map, filter), uint32_t(21));
2✔
588
               result.test_is_eq("next returns 30", xof.next<3>(map, filter), uint32_t(30));
2✔
589
               result.test_is_eq("next returns 39", xof.next<3>(map, filter), uint32_t(39));
2✔
590
               result.test_throws<Botan::Internal_Error>("next() throws", [&]() { xof.next<3>(map, filter); });
3✔
591
            }),
1✔
592
   };
6✔
593
}
1✔
594

595
}  // namespace
596

597
BOTAN_REGISTER_TEST_FN(
598
   "pubkey", "crystals", test_extended_euclidean_algorithm, test_polynomial_basics, test_encoding, test_bounded_xof);
599

600
}  // namespace Botan_Tests
601

602
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc