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

randombit / botan / 13073795340

31 Jan 2025 01:32PM UTC coverage: 91.224% (-0.009%) from 91.233%
13073795340

push

github

web-flow
Merge pull request #4617 from randombit/jack/fix-build-configs

Fix build/test errors caught by test_all_configs.py

94150 of 103208 relevant lines covered (91.22%)

11427437.8 hits per line

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

96.46
/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/fmt.h>
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) {
98
         throw Botan_Tests::Test_Error("montgomery reduction not implemented");
99
      }
100

101
      static T barrett_reduce_coefficient(T) { throw Botan_Tests::Test_Error("barrett reduction not implemented"); }
102

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

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

107
      static void poly_pointwise_montgomery(std::span<T, N>, std::span<T, N>, std::span<T, N>) {
108
         throw Botan_Tests::Test_Error("pointwise multiplication not implemented");
109
      }
110
};
111

112
using Kyberish_Trait = Mock_Trait<Kyberish_Constants>;
113

114
using Domain = Botan::CRYSTALS::Domain;
115

116
template <Domain D>
117
using Kyberish_Poly = Botan::CRYSTALS::Polynomial<Kyberish_Trait, D>;
118

119
template <Domain D>
120
using Kyberish_PolyVec = Botan::CRYSTALS::PolynomialVector<Kyberish_Trait, D>;
121

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

132
               Kyberish_Poly<Domain::NTT> p_ntt;
1✔
133
               res.confirm("default constructed poly owns memory (NTT)", p_ntt.owns_storage());
2✔
134
               for(auto coeff : p) {
257✔
135
                  res.test_is_eq<int16_t>("default constructed poly (NTT) has 0 coefficients", coeff, 0);
512✔
136
               }
137
            }),
2✔
138

139
      CHECK("polynomial vector managing storage",
140
            [](Test::Result& res) {
1✔
141
               Kyberish_PolyVec<Domain::Normal> polys(4);
1✔
142
               res.test_is_eq<size_t>("requested size", polys.size(), 4);
1✔
143

144
               for(const auto& poly : polys) {
5✔
145
                  res.confirm("poly embedded in vector does not own memory", !poly.owns_storage());
8✔
146
               }
147

148
               Kyberish_PolyVec<Domain::NTT> polys_ntt(4);
1✔
149
               res.test_is_eq<size_t>("requested size (NTT)", polys.size(), 4);
1✔
150

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

156
      CHECK("cloned polynomials always manage their storge",
157
            [](Test::Result& res) {
1✔
158
               Kyberish_Poly<Domain::Normal> p;
1✔
159
               auto p2 = p.clone();
1✔
160
               res.confirm("cloned poly owns memory", p2.owns_storage());
2✔
161

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

169
               auto pv2 = pv.clone();
1✔
170
               for(const auto& poly : pv2) {
4✔
171
                  res.confirm("cloned vector polynomial don't own memory", !poly.owns_storage());
6✔
172
               }
173

174
               Kyberish_Poly<Domain::NTT> p_ntt;
1✔
175
               auto p2_ntt = p_ntt.clone();
1✔
176
               res.confirm("cloned poly (NTT) owns memory", p2_ntt.owns_storage());
2✔
177

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

185
               auto pv2_ntt = pv_ntt.clone();
1✔
186
               for(const auto& poly : pv2_ntt) {
4✔
187
                  res.confirm("cloned vector polynomial (NTT) don't own memory", !poly.owns_storage());
6✔
188
               }
189
            }),
5✔
190

191
      CHECK("hamming weight of polynomials",
192
            [](Test::Result& res) {
1✔
193
               Kyberish_Poly<Domain::Normal> p;
1✔
194
               res.test_is_eq<size_t>("hamming weight of 0", p.hamming_weight(), 0);
2✔
195

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

199
               p[1] = 42;
1✔
200
               res.test_is_eq<size_t>("hamming weight of 2", p.hamming_weight(), 2);
2✔
201

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

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

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

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

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

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

221
      CHECK("hamming weight of polynomial vectors",
222
            [](Test::Result& res) {
1✔
223
               Kyberish_PolyVec<Domain::Normal> pv(3);
1✔
224
               res.test_is_eq<size_t>("hamming weight of 0", pv.hamming_weight(), 0);
2✔
225

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

229
               pv[1][1] = 42;
1✔
230
               res.test_is_eq<size_t>("hamming weight of 2", pv.hamming_weight(), 2);
2✔
231

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

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

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

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

245
      CHECK("value range validation",
246
            [](Test::Result& res) {
1✔
247
               Kyberish_Poly<Domain::Normal> p;
1✔
248
               res.confirm("value range validation (all zero)", p.ct_validate_value_range(0, 1));
2✔
249

250
               p[0] = 1;
1✔
251
               p[32] = 1;
1✔
252
               p[172] = 1;
1✔
253
               res.confirm("value range validation", p.ct_validate_value_range(0, 1));
2✔
254

255
               p[11] = 2;
1✔
256
               res.confirm("value range validation", !p.ct_validate_value_range(0, 1));
2✔
257

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

262
      CHECK("value range validation for polynomial vectors",
263
            [](Test::Result& res) {
1✔
264
               Kyberish_PolyVec<Domain::Normal> pv(3);
1✔
265
               res.confirm("value range validation (all zero)", pv.ct_validate_value_range(0, 1));
2✔
266

267
               pv[0][0] = 1;
1✔
268
               pv[1][32] = 1;
1✔
269
               pv[2][172] = 1;
1✔
270
               res.confirm("value range validation", pv.ct_validate_value_range(0, 1));
2✔
271

272
               pv[0][11] = 2;
1✔
273
               res.confirm("value range validation", !pv.ct_validate_value_range(0, 1));
2✔
274

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

281
namespace {
282

283
   #if defined(BOTAN_HAS_XOF)
284

285
class DeterministicXOF : public Botan::XOF {
1✔
286
   public:
287
      DeterministicXOF(std::span<const uint8_t> data) : m_data(data) {}
3✔
288

289
      std::string name() const override { return "DeterministicXOF"; }
×
290

291
      bool accepts_input() const override { return false; }
×
292

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

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

297
      size_t block_size() const override { return 1; }
×
298

299
      void start_msg(std::span<const uint8_t>, std::span<const uint8_t>) override {
×
300
         throw Botan_Tests::Test_Error("start_msg not implemented");
×
301
      }
302

303
      void add_data(std::span<const uint8_t>) override { throw Botan_Tests::Test_Error("add_data not implemented"); }
×
304

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

307
      void reset() override {}
×
308

309
   private:
310
      Botan::BufferSlicer m_data;
311
};
312

313
   #endif
314

315
template <Botan::CRYSTALS::crystals_trait Trait, int32_t range>
316
void random_encoding_roundtrips(Test::Result& res, Botan::RandomNumberGenerator& rng, size_t expected_encoding_bits) {
22✔
317
   using Poly = Botan::CRYSTALS::Polynomial<Trait, Domain::Normal>;
318
   using T = typename Trait::T;
319

320
   auto random_poly = [&rng]() -> Poly {
44✔
321
      Poly p;
22✔
322
      std::array<uint8_t, sizeof(T)> buf;
323
      for(auto& coeff : p) {
5,654✔
324
         rng.randomize(buf);
5,632✔
325
         coeff = static_cast<T>((Botan::load_be(buf) % (range + 1)));
5,632✔
326
      }
327
      return p;
22✔
328
   };
×
329

330
   const auto p = random_poly();
22✔
331
   std::vector<uint8_t> buffer((p.size() * expected_encoding_bits + 7) / 8);
22✔
332
   Botan::BufferStuffer stuffer(buffer);
22✔
333
   Botan::CRYSTALS::pack<range>(p, stuffer);
22✔
334
   res.confirm("encoded polynomial fills buffer", stuffer.full());
44✔
335

336
   Botan::BufferSlicer slicer(buffer);
22✔
337
   Poly p_unpacked;
22✔
338
   Botan::CRYSTALS::unpack<range>(p_unpacked, slicer);
22✔
339
   res.confirm("decoded polynomial reads all bytes", slicer.empty());
44✔
340

341
   p_unpacked -= p;
22✔
342
   res.test_eq("p = unpack(pack(p))", p_unpacked.hamming_weight(), 0);
44✔
343
}
66✔
344

345
}  // namespace
346

347
std::vector<Test::Result> test_encoding() {
1✔
348
   const auto threebitencoding = Botan::hex_decode(
1✔
349
      "88C61AD158231A6B44638D68AC118D35A2B14634D688C61AD158231A6B44638D68AC118D"
350
      "35A2B14634D688C61AD158231A6B44638D68AC118D35A2B14634D688C61AD158231A6B44"
351
      "638D68AC118D35A2B14634D688C61AD158231A6B44638D68");
1✔
352

353
   const auto eightbitencoding = Botan::hex_decode(
1✔
354
      "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20212223"
355
      "2425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F4041424344454647"
356
      "48494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B"
357
      "6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F"
358
      "909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3"
359
      "B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7"
360
      "D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFB"
361
      "FCFDFEFF");
1✔
362

363
   const auto tenbitencoding = Botan::hex_decode(
1✔
364
      "00084080010828C0800310484081051868C08107208840820928A8C0820B30C840830D38"
365
      "E8C0830F40084184114828C1841350484185155868C18517608841861968A8C1861B70C8"
366
      "41871D78E8C1871F80084288218828C2882390484289259868C28927A088428A29A8A8C2"
367
      "8A2BB0C8428B2DB8E8C28B2FC008438C31C828C38C33D048438D35D868C38D37E088438E"
368
      "39E8A8C38E3BF0C8438F3DF8E8C38F3F00094490410829C4904310494491451869C49147"
369
      "208944924928A9C4924B30C944934D38E9C4934F40094594514829C59453504945955558"
370
      "69C59557608945965968A9C5965B70C945975D78E9C5975F80094698618829C698639049"
371
      "4699659869C69967A089469A69A8A9C69A6BB0C9469B6DB8E9C69B6FC009479C71C829C7"
372
      "9C73D049479D75D869C79D77E089479E79E8A9C79E7BF0C9479F7DF8E9C79F7F");
1✔
373

374
   return {
1✔
375
      CHECK("encode polynomial coefficients into buffer",
376
            [&](Test::Result& res) {
1✔
377
               // value range is about 3 bits
378
               Kyberish_Poly<Domain::Normal> p1;
1✔
379
               for(size_t i = 0; i < p1.size(); ++i) {
257✔
380
                  p1[i] = static_cast<Kyberish_Constants::T>(i % 7);
256✔
381
               }
382

383
               std::vector<uint8_t> buffer1(96);
1✔
384
               Botan::BufferStuffer stuffer1(buffer1);
1✔
385
               Botan::CRYSTALS::pack<6>(p1, stuffer1);
1✔
386
               res.test_eq("3 bit encoding", buffer1, threebitencoding);
2✔
387

388
               // value range is exactly one byte
389
               Kyberish_Poly<Domain::Normal> p2;
1✔
390
               for(size_t i = 0; i < p2.size(); ++i) {
257✔
391
                  p2[i] = static_cast<Kyberish_Constants::T>(i);
256✔
392
               }
393

394
               std::vector<uint8_t> buffer2(256);
1✔
395
               Botan::BufferStuffer stuffer2(buffer2);
1✔
396
               Botan::CRYSTALS::pack<255>(p2, stuffer2);
1✔
397
               res.test_eq("8 bit encoding", buffer2, eightbitencoding);
2✔
398

399
               // value range for 10 bits, with mapping function
400
               std::vector<uint8_t> buffer3(p2.size() / 8 * 10 /* bits */);
1✔
401
               Botan::BufferStuffer stuffer3(buffer3);
1✔
402
               Botan::CRYSTALS::pack<512>(p2, stuffer3, [](int16_t x) -> uint16_t { return x * 2; });
257✔
403
               res.test_eq("10 bit encoding", buffer3, tenbitencoding);
2✔
404
            }),
5✔
405

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

416
               Kyberish_Poly<Domain::Normal> p2;
1✔
417
               Botan::BufferSlicer slicer2(eightbitencoding);
1✔
418
               Botan::CRYSTALS::unpack<255>(p2, slicer2);
1✔
419
               res.require("read all bytes from 8-bit encoding", slicer2.empty());
1✔
420
               for(size_t i = 0; i < p2.size(); ++i) {
257✔
421
                  res.test_is_eq<size_t>("decoded 8-bit coefficient", p2[i], i);
512✔
422
               }
423

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

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

443
               Kyberish_Poly<Domain::Normal> p2;
1✔
444
               DeterministicXOF xof2(eightbitencoding);
1✔
445
               Botan::CRYSTALS::unpack<255>(p2, xof2);
1✔
446
               for(size_t i = 0; i < p2.size(); ++i) {
257✔
447
                  res.test_is_eq<size_t>("decoded 8-bit coefficient", p2[i], i);
512✔
448
               }
449

450
               Kyberish_Poly<Domain::Normal> p3;
1✔
451
               DeterministicXOF xof3(tenbitencoding);
1✔
452
               Botan::CRYSTALS::unpack<512>(p3, xof3, [](int16_t x) -> int16_t { return x / 2; });
257✔
453
               for(size_t i = 0; i < p3.size(); ++i) {
257✔
454
                  res.test_is_eq<size_t>("decoded 10-bit coefficient with mapping", p3[i], i);
512✔
455
               }
456
   #endif
457
            }),
4✔
458

459
      CHECK("random encoding roundtrips (0 to x)",
460
            [](Test::Result& res) {
1✔
461
               auto rng = Test::new_rng("CRYSTALS encoding roundtrips");
1✔
462
               random_encoding_roundtrips<Kyberish_Trait, 3>(res, *rng, 2);
1✔
463
               random_encoding_roundtrips<Kyberish_Trait, 6>(res, *rng, 3);
1✔
464
               random_encoding_roundtrips<Kyberish_Trait, 12>(res, *rng, 4);
1✔
465
               random_encoding_roundtrips<Kyberish_Trait, 15>(res, *rng, 4);
1✔
466
               random_encoding_roundtrips<Kyberish_Trait, 31>(res, *rng, 5);
1✔
467
               random_encoding_roundtrips<Kyberish_Trait, 42>(res, *rng, 6);
1✔
468
               random_encoding_roundtrips<Kyberish_Trait, 128>(res, *rng, 8);
1✔
469
               random_encoding_roundtrips<Kyberish_Trait, 1337>(res, *rng, 11);
1✔
470
            }),
1✔
471

472
      CHECK("random encoding roundtrips (Kyber ranges)",
473
            [](Test::Result& res) {
1✔
474
               auto rng = Test::new_rng("CRYSTALS encoding roundtrips as used in kyber");
1✔
475
               random_encoding_roundtrips<Kyberish_Trait, 1>(res, *rng, 1);
1✔
476
               random_encoding_roundtrips<Kyberish_Trait, (1 << 4) - 1>(res, *rng, 4);
1✔
477
               random_encoding_roundtrips<Kyberish_Trait, (1 << 5) - 1>(res, *rng, 5);
1✔
478
               random_encoding_roundtrips<Kyberish_Trait, (1 << 10) - 1>(res, *rng, 10);
1✔
479
               random_encoding_roundtrips<Kyberish_Trait, (1 << 11) - 1>(res, *rng, 11);
1✔
480
               random_encoding_roundtrips<Kyberish_Trait, Kyberish_Constants::Q - 1>(res, *rng, 12);
1✔
481
            }),
1✔
482

483
      CHECK("random encoding roundtrips (Dilithium ranges)",
484
            [](Test::Result& res) {
1✔
485
               using Dilithiumish_Trait = Mock_Trait<Dilithiumish_Constants>;
1✔
486

487
               auto rng = Test::new_rng("CRYSTALS encoding roundtrips as used in kyber");
1✔
488
               constexpr auto t1 = 1023;
1✔
489
               constexpr auto gamma2_32 = 15;
1✔
490
               constexpr auto gamma2_88 = 43;
1✔
491
               constexpr auto gamma1_17 = 131072;
1✔
492
               constexpr auto gamma1_19 = 524288;
1✔
493
               constexpr auto eta2 = 2;
1✔
494
               constexpr auto eta4 = 4;
1✔
495
               constexpr auto twotothed = 4096;
1✔
496
               random_encoding_roundtrips<Dilithiumish_Trait, t1>(res, *rng, 10);
1✔
497
               random_encoding_roundtrips<Dilithiumish_Trait, gamma2_32>(res, *rng, 4);
1✔
498
               random_encoding_roundtrips<Dilithiumish_Trait, gamma2_88>(res, *rng, 6);
1✔
499
               random_encoding_roundtrips<Dilithiumish_Trait, 2 * gamma1_17 - 1>(res, *rng, 18);
1✔
500
               random_encoding_roundtrips<Dilithiumish_Trait, 2 * gamma1_19 - 1>(res, *rng, 20);
1✔
501
               random_encoding_roundtrips<Dilithiumish_Trait, 2 * eta2>(res, *rng, 3);
1✔
502
               random_encoding_roundtrips<Dilithiumish_Trait, 2 * eta4>(res, *rng, 4);
1✔
503
               random_encoding_roundtrips<Dilithiumish_Trait, 2 * twotothed - 1>(res, *rng, 13);
1✔
504
            }),
1✔
505
   };
7✔
506
}
4✔
507

508
class MockedXOF {
509
   public:
510
      MockedXOF() : m_counter(0) {}
5✔
511

512
      template <size_t bytes>
513
      auto output() {
514
         std::array<uint8_t, bytes> result;
515
         for(uint8_t& byte : result) {
56✔
516
            byte = static_cast<uint8_t>(m_counter++);
39✔
517
         }
518
         return result;
17✔
519
      }
520

521
   private:
522
      size_t m_counter;
523
};
524

525
template <size_t bound>
526
using Mocked_Bounded_XOF = Botan::detail::Bounded_XOF<MockedXOF, bound>;
527

528
std::vector<Test::Result> test_bounded_xof() {
1✔
529
   return {
1✔
530
      CHECK("zero bound is reached immediately",
531
            [](Test::Result& result) {
1✔
532
               Mocked_Bounded_XOF<0> xof;
1✔
533
               result.test_throws<Botan::Internal_Error>("output<1> throws", [&xof]() { xof.next_byte(); });
3✔
534
            }),
1✔
535

536
      CHECK("bounded XOF with small bound",
537
            [](Test::Result& result) {
1✔
538
               Mocked_Bounded_XOF<3> xof;
1✔
539
               result.test_is_eq("next_byte() returns 0", xof.next_byte(), uint8_t(0));
2✔
540
               result.test_is_eq("next_byte() returns 1", xof.next_byte(), uint8_t(1));
2✔
541
               result.test_is_eq("next_byte() returns 2", xof.next_byte(), uint8_t(2));
2✔
542
               result.test_throws<Botan::Internal_Error>("next_byte() throws", [&xof]() { xof.next_byte(); });
3✔
543
            }),
1✔
544

545
      CHECK("filter bytes",
546
            [](Test::Result& result) {
1✔
547
               auto filter = [](uint8_t byte) {
6✔
548
                  //test
549
                  return byte % 2 == 1;
5✔
550
               };
551

552
               Mocked_Bounded_XOF<5> xof;
1✔
553
               result.test_is_eq("next_byte() returns 1", xof.next_byte(filter), uint8_t(1));
2✔
554
               result.test_is_eq("next_byte() returns 3", xof.next_byte(filter), uint8_t(3));
2✔
555
               result.test_throws<Botan::Internal_Error>("next_byte() throws", [&]() { xof.next_byte(filter); });
3✔
556
            }),
1✔
557

558
      CHECK("map bytes",
559
            [](Test::Result& result) {
1✔
560
               auto map = [](auto bytes) { return Botan::load_be(bytes); };
4✔
561

562
               Mocked_Bounded_XOF<17> xof;
1✔
563
               result.test_is_eq("next returns 0x00010203", xof.next<4>(map), uint32_t(0x00010203));
1✔
564
               result.test_is_eq("next returns 0x04050607", xof.next<4>(map), uint32_t(0x04050607));
1✔
565
               result.test_is_eq("next returns 0x08090A0B", xof.next<4>(map), uint32_t(0x08090A0B));
1✔
566
               result.test_is_eq("next returns 0x0C0D0E0F", xof.next<4>(map), uint32_t(0x0C0D0E0F));
1✔
567
               result.test_throws<Botan::Internal_Error>("next() throws", [&]() { xof.next<4>(map); });
3✔
568
            }),
1✔
569

570
      CHECK("map and filter bytes",
571
            [](Test::Result& result) {
1✔
572
               auto map = [](std::array<uint8_t, 3> bytes) -> uint32_t { return bytes[0] + bytes[1] + bytes[2]; };
5✔
573
               auto filter = [](uint32_t number) { return number < 50; };
1✔
574

575
               Mocked_Bounded_XOF<17> xof;
1✔
576
               result.test_is_eq("next returns 3", xof.next<3>(map, filter), uint32_t(3));
2✔
577
               result.test_is_eq("next returns 12", xof.next<3>(map, filter), uint32_t(12));
2✔
578
               result.test_is_eq("next returns 21", xof.next<3>(map, filter), uint32_t(21));
2✔
579
               result.test_is_eq("next returns 30", xof.next<3>(map, filter), uint32_t(30));
2✔
580
               result.test_is_eq("next returns 39", xof.next<3>(map, filter), uint32_t(39));
2✔
581
               result.test_throws<Botan::Internal_Error>("next() throws", [&]() { xof.next<3>(map, filter); });
3✔
582
            }),
1✔
583
   };
6✔
584
}
1✔
585

586
}  // namespace
587

588
BOTAN_REGISTER_TEST_FN(
589
   "pubkey", "crystals", test_extended_euclidean_algorithm, test_polynomial_basics, test_encoding, test_bounded_xof);
590

591
}  // namespace Botan_Tests
592

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