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

randombit / botan / 20579846577

29 Dec 2025 06:24PM UTC coverage: 90.415% (+0.2%) from 90.243%
20579846577

push

github

web-flow
Merge pull request #5167 from randombit/jack/src-size-reductions

Changes to reduce unnecessary inclusions

101523 of 112285 relevant lines covered (90.42%)

12817276.56 hits per line

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

96.19
/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
   #include <botan/rng.h>
14

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

20
namespace Botan_Tests {
21

22
namespace {
23

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

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

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

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

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

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

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

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

67
   return res;
1✔
68
}
×
69

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

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

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

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

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

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

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

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

118
using Kyberish_Trait = Mock_Trait<Kyberish_Constants>;
119

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

287
namespace {
288

289
   #if defined(BOTAN_HAS_XOF)
290

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

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

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

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

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

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

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

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

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

315
      void reset() override {}
×
316

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

321
   #endif
322

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

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

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

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

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

353
}  // namespace
354

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

531
   private:
532
      size_t m_counter;
533
};
534

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

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

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

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

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

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

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

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

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

596
}  // namespace
597

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

601
}  // namespace Botan_Tests
602

603
#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