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

randombit / botan / 26995937053

04 Jun 2026 09:38PM UTC coverage: 89.394% (-2.3%) from 91.672%
26995937053

push

github

web-flow
Merge pull request #5642 from randombit/jack/prefetch-in-ks

Improve prefetching for table based implementations

110588 of 123708 relevant lines covered (89.39%)

11056434.37 hits per line

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

98.4
/src/lib/utils/bitvector/bitvector.h
1
/*
2
 * An abstraction for an arbitrarily large bitvector that can
3
 * optionally use the secure_allocator. All bitwise accesses and all
4
 * constructors are implemented in constant time. Otherwise, only methods
5
 * with the "ct_" pre-fix run in constant time.
6
 *
7
 * (C) 2023-2024 Jack Lloyd
8
 * (C) 2023-2024 René Meusel, Rohde & Schwarz Cybersecurity
9
 *
10
 * Botan is released under the Simplified BSD License (see license.txt)
11
 */
12

13
#ifndef BOTAN_BIT_VECTOR_H_
14
#define BOTAN_BIT_VECTOR_H_
15

16
#include <botan/concepts.h>
17
#include <botan/exceptn.h>
18
#include <botan/mem_ops.h>
19
#include <botan/secmem.h>
20
#include <botan/strong_type.h>
21
#include <botan/internal/bit_ops.h>
22
#include <botan/internal/ct_utils.h>
23
#include <botan/internal/int_utils.h>
24
#include <botan/internal/loadstor.h>
25
#include <botan/internal/stl_util.h>
26

27
#include <memory>
28
#include <optional>
29
#include <span>
30
#include <sstream>
31
#include <string>
32
#include <utility>
33
#include <vector>
34

35
namespace Botan {
36

37
template <template <typename> typename AllocatorT>
38
class bitvector_base;
39

40
template <typename T>
41
struct is_bitvector : std::false_type {};
42

43
template <template <typename> typename T>
44
struct is_bitvector<bitvector_base<T>> : std::true_type {};
45

46
template <typename T>
47
constexpr static bool is_bitvector_v = is_bitvector<T>::value;
48

49
template <typename T>
50
concept bitvectorish = is_bitvector_v<strong_type_wrapped_type<T>>;
51

52
namespace detail {
53

54
template <typename T0, typename... Ts>
55
struct first_type {
56
      using type = T0;
57
};
58

59
// get the first type from a parameter pack
60
// TODO: C++26 will bring Parameter Pack indexing:
61
//       using first_t = Ts...[0];
62
template <typename... Ts>
63
   requires(sizeof...(Ts) > 0)
64
using first_t = typename first_type<Ts...>::type;
65

66
// get the first object from a parameter pack
67
// TODO: C++26 will bring Parameter Pack indexing:
68
//       auto first = s...[0];
69
template <typename T0, typename... Ts>
70
constexpr static first_t<T0, Ts...> first(T0&& t, Ts&&... /*rest*/) {
71
   return std::forward<T0>(t);
72
}
73

74
template <typename OutT, typename>
75
using as = OutT;
76

77
template <typename FnT, std::unsigned_integral BlockT, typename... ParamTs>
78
using blockwise_processing_callback_return_type = std::invoke_result_t<FnT, as<BlockT, ParamTs>...>;
79

80
template <typename FnT, typename BlockT, typename... ParamTs>
81
concept is_blockwise_processing_callback_return_type =
82
   std::unsigned_integral<BlockT> &&
83
   (std::same_as<BlockT, blockwise_processing_callback_return_type<FnT, BlockT, ParamTs...>> ||
84
    std::same_as<bool, blockwise_processing_callback_return_type<FnT, BlockT, ParamTs...>> ||
85
    std::same_as<void, blockwise_processing_callback_return_type<FnT, BlockT, ParamTs...>>);
86

87
template <typename FnT, typename... ParamTs>
88
concept blockwise_processing_callback_without_mask =
89
   is_blockwise_processing_callback_return_type<FnT, uint8_t, ParamTs...> &&
90
   is_blockwise_processing_callback_return_type<FnT, uint16_t, ParamTs...> &&
91
   is_blockwise_processing_callback_return_type<FnT, uint32_t, ParamTs...> &&
92
   is_blockwise_processing_callback_return_type<FnT, uint64_t, ParamTs...>;
93

94
template <typename FnT, typename... ParamTs>
95
concept blockwise_processing_callback_with_mask =
96
   is_blockwise_processing_callback_return_type<FnT, uint8_t, ParamTs..., uint8_t /* mask */> &&
97
   is_blockwise_processing_callback_return_type<FnT, uint16_t, ParamTs..., uint16_t /* mask */> &&
98
   is_blockwise_processing_callback_return_type<FnT, uint32_t, ParamTs..., uint32_t /* mask */> &&
99
   is_blockwise_processing_callback_return_type<FnT, uint64_t, ParamTs..., uint64_t /* mask */>;
100

101
/**
102
 * Defines the callback constraints for the BitRangeOperator. For further
103
 * details, see bitvector_base::range_operation().
104
 */
105
template <typename FnT, typename... ParamTs>
106
concept blockwise_processing_callback = blockwise_processing_callback_with_mask<FnT, ParamTs...> ||
107
                                        blockwise_processing_callback_without_mask<FnT, ParamTs...>;
108

109
template <typename FnT, typename... ParamTs>
110
concept manipulating_blockwise_processing_callback =
111
   (blockwise_processing_callback_without_mask<FnT, ParamTs...> &&
112
    std::same_as<uint32_t, blockwise_processing_callback_return_type<FnT, uint32_t, ParamTs...>>) ||
113
   (blockwise_processing_callback_with_mask<FnT, ParamTs...> &&
114
    std::same_as<uint32_t, blockwise_processing_callback_return_type<FnT, uint32_t, ParamTs..., first_t<ParamTs...>>>);
115

116
template <typename FnT, typename... ParamTs>
117
concept predicate_blockwise_processing_callback =
118
   (blockwise_processing_callback_without_mask<FnT, ParamTs...> &&
119
    std::same_as<bool, blockwise_processing_callback_return_type<FnT, uint32_t, ParamTs...>>) ||
120
   (blockwise_processing_callback_with_mask<FnT, ParamTs...> &&
121
    std::same_as<bool, blockwise_processing_callback_return_type<FnT, uint32_t, ParamTs..., first_t<ParamTs...>>>);
122

123
template <typename T>
124
class bitvector_iterator {
125
   private:
126
      using size_type = typename T::size_type;
127

128
   public:
129
      using difference_type = std::make_signed_t<size_type>;
130
      using value_type = std::remove_const_t<decltype(std::declval<T>().at(0))>;
131
      using pointer = value_type*;
132
      using reference = value_type&;
133

134
      // TODO: technically, this could be a random access iterator
135
      using iterator_category = std::bidirectional_iterator_tag;
136

137
   public:
138
      bitvector_iterator() = default;
139
      ~bitvector_iterator() = default;
140

141
      bitvector_iterator(T* bitvector, size_t offset) : m_bitvector(bitvector) { update(offset); }
1,589✔
142

143
      bitvector_iterator(const bitvector_iterator& other) noexcept : m_bitvector(other.m_bitvector) {
2,002✔
144
         update(other.m_offset);
2,004✔
145
      }
146

147
      bitvector_iterator(bitvector_iterator&& other) noexcept : m_bitvector(other.m_bitvector) {
148
         update(other.m_offset);
149
      }
150

151
      bitvector_iterator& operator=(const bitvector_iterator& other) noexcept {
152
         if(this != &other) {
153
            m_bitvector = other.m_bitvector;
154
            update(other.m_offset);
155
         }
156
         return *this;
157
      }
158

159
      bitvector_iterator& operator=(bitvector_iterator&& other) noexcept {
160
         m_bitvector = other.m_bitvector;
161
         update(other.m_offset);
162
         return *this;
163
      }
164

165
      bitvector_iterator& operator++() noexcept {
252,208✔
166
         update(signed_offset() + 1);
504,443✔
167
         return *this;
168
      }
169

170
      bitvector_iterator operator++(int) noexcept {
6✔
171
         auto copy = *this;
6✔
172
         update(signed_offset() + 1);
12✔
173
         return copy;
6✔
174
      }
175

176
      bitvector_iterator& operator--() noexcept {
6✔
177
         update(signed_offset() - 1);
12✔
178
         return *this;
179
      }
180

181
      bitvector_iterator operator--(int) noexcept {
182
         auto copy = *this;
183
         update(signed_offset() - 1);
184
         return copy;
185
      }
186

187
      std::partial_ordering operator<=>(const bitvector_iterator& other) const noexcept {
188
         if(m_bitvector == other.m_bitvector) {
189
            return m_offset <=> other.m_offset;
190
         } else {
191
            return std::partial_ordering::unordered;
192
         }
193
      }
194

195
      bool operator==(const bitvector_iterator& other) const noexcept {
253,249✔
196
         return m_bitvector == other.m_bitvector && m_offset == other.m_offset;
252,721✔
197
      }
198

199
      reference operator*() const { return m_bitref.value(); }
2,363✔
200

201
      pointer operator->() const { return &(m_bitref.value()); }
256✔
202

203
   private:
204
      void update(size_type new_offset) {
524✔
205
         m_offset = new_offset;
1,029✔
206
         if(m_offset < m_bitvector->size()) {
1,506✔
207
            m_bitref.emplace((*m_bitvector)[m_offset]);
254,229✔
208
         } else {
209
            // end() iterator
210
            m_bitref.reset();
251,768✔
211
         }
212
      }
213

214
      difference_type signed_offset() const { return static_cast<difference_type>(m_offset); }
6✔
215

216
   private:
217
      T* m_bitvector;
218
      size_type m_offset;
219
      mutable std::optional<value_type> m_bitref;
220
};
221

222
}  // namespace detail
223

224
/**
225
 * An arbitrarily large bitvector with typical bit manipulation and convenient
226
 * bitwise access methods. Don't use `bitvector_base` directly, but the type
227
 * aliases::
228
 *
229
 *    * bitvector         - with a standard allocator
230
 *    * secure_bitvector  - with a secure allocator that auto-scrubs the memory
231
 */
232
template <template <typename> typename AllocatorT>
233
class bitvector_base final {
1,049,113✔
234
   public:
235
      using block_type = uint8_t;
236
      using size_type = size_t;
237
      using allocator_type = AllocatorT<block_type>;
238
      using value_type = block_type;
239
      using iterator = detail::bitvector_iterator<bitvector_base<AllocatorT>>;
240
      using const_iterator = detail::bitvector_iterator<const bitvector_base<AllocatorT>>;
241

242
      static constexpr size_type block_size_bytes = sizeof(block_type);
243
      static constexpr size_type block_size_bits = block_size_bytes * 8;
244
      static constexpr bool uses_secure_allocator = std::is_same_v<allocator_type, secure_allocator<block_type>>;
245

246
   private:
247
      template <template <typename> typename FriendAllocatorT>
248
      friend class bitvector_base;
249

250
      static constexpr block_type one = block_type(1);
251

252
      static constexpr size_type block_offset_shift = size_type(3) + ceil_log2(block_size_bytes);
253
      static constexpr size_type block_index_mask = (one << block_offset_shift) - 1;
254

255
      static constexpr size_type block_index(size_type pos) { return pos >> block_offset_shift; }
21,179,009✔
256

257
      static constexpr size_type block_offset(size_type pos) { return pos & block_index_mask; }
1,347,604,670✔
258

259
   private:
260
      /**
261
       * Internal helper to wrap a single bit in the bitvector and provide
262
       * certain convenience access methods.
263
       */
264
      template <typename BlockT>
265
         requires std::same_as<block_type, std::remove_cv_t<BlockT>>
266
      class bitref_base {
267
         private:
268
            friend class bitvector_base<AllocatorT>;
269

270
            constexpr bitref_base(std::span<BlockT> blocks, size_type pos) noexcept :
1,798,136,893✔
271
                  m_block(blocks[block_index(pos)]), m_mask(one << block_offset(pos)) {}
1,798,136,893✔
272

273
         public:
274
            bitref_base() = delete;
275
            bitref_base(const bitref_base&) noexcept = default;
276
            bitref_base(bitref_base&&) noexcept = default;
277
            bitref_base& operator=(const bitref_base&) = delete;
278
            bitref_base& operator=(bitref_base&&) = delete;
279

280
            ~bitref_base() = default;
281

282
         public:
283
            // NOLINTNEXTLINE(*-explicit-conversions)
284
            constexpr operator bool() const noexcept { return is_set(); }
13,551,536✔
285

286
            constexpr bool is_set() const noexcept { return (m_block & m_mask) > 0; }
13,551,564✔
287

288
            template <std::unsigned_integral T>
289
            constexpr T as() const noexcept {
312,633✔
290
               return static_cast<T>(is_set());
119,250✔
291
            }
292

293
            constexpr CT::Choice as_choice() const noexcept {
448,922,479✔
294
               return CT::Choice::from_int(static_cast<BlockT>(m_block & m_mask));
448,922,479✔
295
            }
296

297
         protected:
298
            BlockT& m_block;  // NOLINT(*-non-private-member-variable*)
299
            BlockT m_mask;    // NOLINT(*-non-private-member-variable*)
300
      };
301

302
   public:
303
      /**
304
       * Wraps a constant reference into the bitvector. Bit can be accessed
305
       * but not modified.
306
       */
307
      template <typename BlockT>
308
      class bitref final : public bitref_base<BlockT> {
309
         public:
310
            using bitref_base<BlockT>::bitref_base;
12,641,743✔
311
      };
312

313
      /**
314
       * Wraps a modifiable reference into the bitvector. Bit may be accessed
315
       * and modified (e.g. flipped or XOR'ed).
316
       *
317
       * Constant-time operations are used for the bit manipulations. The
318
       * location of the bit in the bit vector may be leaked, though.
319
       */
320
      template <typename BlockT>
321
         requires(!std::is_const_v<BlockT>)
322
      class bitref<BlockT> : public bitref_base<BlockT> {
323
         public:
324
            using bitref_base<BlockT>::bitref_base;
1,335,235,935✔
325

326
            ~bitref() = default;
327
            bitref(const bitref&) noexcept = default;
328
            bitref(bitref&&) noexcept = default;
329

330
            constexpr bitref& set() noexcept {
354✔
331
               this->m_block |= this->m_mask;
24✔
332
               return *this;
250✔
333
            }
334

335
            constexpr bitref& unset() noexcept {
6✔
336
               this->m_block &= ~this->m_mask;
1✔
337
               return *this;
338
            }
339

340
            constexpr bitref& flip() noexcept {
16✔
341
               this->m_block ^= this->m_mask;
7✔
342
               return *this;
343
            }
344

345
            // NOLINTBEGIN
346

347
            constexpr bitref& operator=(bool bit) noexcept {
1,335,019,199✔
348
               this->m_block =
1,335,019,199✔
349
                  CT::Mask<BlockT>::expand(bit).select(this->m_mask | this->m_block, this->m_block & ~this->m_mask);
1,335,019,199✔
350
               return *this;
1,335,019,199✔
351
            }
352

353
            constexpr bitref& operator=(const bitref& bit) noexcept { return *this = bit.is_set(); }
354

355
            constexpr bitref& operator=(bitref&& bit) noexcept { return *this = bit.is_set(); }
356

357
            // NOLINTEND
358

359
            constexpr bitref& operator&=(bool other) noexcept {
4✔
360
               this->m_block &= ~CT::Mask<BlockT>::expand(other).if_not_set_return(this->m_mask);
4✔
361
               return *this;
4✔
362
            }
363

364
            constexpr bitref& operator|=(bool other) noexcept {
1✔
365
               this->m_block |= CT::Mask<BlockT>::expand(other).if_set_return(this->m_mask);
1✔
366
               return *this;
367
            }
368

369
            constexpr bitref& operator^=(bool other) noexcept {
79,045✔
370
               this->m_block ^= CT::Mask<BlockT>::expand(other).if_set_return(this->m_mask);
79,045✔
371
               return *this;
372
            }
373
      };
374

375
   public:
376
      bitvector_base() : m_bits(0) {}
80✔
377

378
      explicit bitvector_base(size_type bits) : m_bits(bits), m_blocks(ceil_toblocks(bits)) {}
386,050✔
379

380
      /**
381
       * Initialize the bitvector from a byte-array. Bits are taken byte-wise
382
       * from least significant to most significant. Example::
383
       *
384
       *    bitvector[0] -> LSB(Byte[0])
385
       *    bitvector[1] -> LSB+1(Byte[0])
386
       *    ...
387
       *    bitvector[8] -> LSB(Byte[1])
388
       *
389
       * @param bytes The byte vector to be loaded
390
       * @param bits  The number of bits to be loaded. This must not be more
391
       *              than the number of bytes in @p bytes.
392
       */
393
      bitvector_base(std::span<const uint8_t> bytes, /* NOLINT(*-explicit-conversions) */
80,942✔
394
                     std::optional<size_type> bits = std::nullopt) :
395
            m_bits() {
80,942✔
396
         from_bytes(bytes, bits);
80,942✔
397
      }
80,942✔
398

399
      bitvector_base(std::initializer_list<block_type> blocks, std::optional<size_type> bits = std::nullopt) :
201✔
400
            m_bits(bits.value_or(blocks.size() * block_size_bits)), m_blocks(blocks.begin(), blocks.end()) {}
201✔
401

402
      bool empty() const { return m_bits == 0; }
6✔
403

404
      size_type size() const { return m_bits; }
8,455,928✔
405

406
      /**
407
       * @returns true iff the number of 1-bits in this is odd, false otherwise (constant time)
408
       */
409
      CT::Choice has_odd_hamming_weight() const {
79,047✔
410
         uint64_t acc = 0;
79,047✔
411
         full_range_operation([&](std::unsigned_integral auto block) { acc ^= block; }, *this);
79,047✔
412

413
         for(size_t i = (sizeof(acc) * 8) >> 1; i > 0; i >>= 1) {
553,329✔
414
            acc ^= acc >> i;
474,282✔
415
         }
416

417
         return CT::Choice::from_int(acc & one);
79,047✔
418
      }
419

420
      /**
421
       * Counts the number of 1-bits in the bitvector in constant time.
422
       * @returns the "population count" (or hamming weight) of the bitvector
423
       */
424
      size_type hamming_weight() const {
113✔
425
         size_type acc = 0;
113✔
426
         full_range_operation([&](std::unsigned_integral auto block) { acc += ct_popcount(block); }, *this);
94✔
427
         return acc;
94✔
428
      }
429

430
      /**
431
       * @returns copies this bitvector into a new bitvector of type @p OutT
432
       */
433
      template <bitvectorish OutT>
434
      OutT as() const {
88✔
435
         return subvector<OutT>(0, size());
144✔
436
      }
437

438
      /**
439
       * @returns true if @p other contains the same bit pattern as this
440
       */
441
      template <bitvectorish OtherT>
442
      bool equals_vartime(const OtherT& other) const noexcept {
18✔
443
         return size() == other.size() &&
20✔
444
                full_range_operation([]<std::unsigned_integral BlockT>(BlockT lhs, BlockT rhs) { return lhs == rhs; },
20✔
445
                                     *this,
446
                                     unwrap_strong_type(other));
447
      }
448

449
      /**
450
       * @returns true if @p other contains the same bit pattern as this
451
       */
452
      template <bitvectorish OtherT>
453
      bool equals(const OtherT& other) const noexcept {
50✔
454
         if(size() != other.size()) {
50✔
455
            return false;
456
         }
457

458
         uint64_t acc = 0;
49✔
459
         full_range_operation(
49✔
460
            [&]<std::unsigned_integral BlockT>(BlockT lhs, BlockT rhs) { acc |= static_cast<uint64_t>(lhs ^ rhs); },
53✔
461
            *this,
462
            unwrap_strong_type(other));
463
         return !CT::Choice::from_int(acc).as_bool();
49✔
464
      }
465

466
      /// @name Serialization
467
      /// @{
468

469
      /**
470
       * Re-initialize the bitvector with the given bytes. See the respective
471
       * constructor for details. This should be used only when trying to save
472
       * allocations. Otherwise, use the constructor.
473
       *
474
       * @param bytes  the byte range to load bits from
475
       * @param bits   (optional) if not all @p bytes should be loaded in full
476
       */
477
      void from_bytes(std::span<const uint8_t> bytes, std::optional<size_type> bits = std::nullopt) {
80,942✔
478
         const size_type new_bits = bits.has_value()
161,884✔
479
                                       ? bits.value()
80,942✔
480
                                       : mul_or_throw<size_t>(8, bytes.size_bytes(), "bitvector input is too large");
1,766✔
481
         const size_type bytes_needed = (new_bits / 8) + (new_bits % 8 != 0 ? 1 : 0);
143,306✔
482
         BOTAN_ARG_CHECK(bytes_needed <= bytes.size_bytes(), "not enough data to load so many bits");
80,942✔
483
         resize(new_bits);
80,942✔
484

485
         // load as much aligned data as possible
486
         const auto verbatim_blocks = new_bits / block_size_bits;
80,942✔
487
         const auto verbatim_bytes = verbatim_blocks * block_size_bytes;
80,942✔
488
         if(verbatim_blocks > 0) {
80,942✔
489
            typecast_copy(std::span{m_blocks}.first(verbatim_blocks), bytes.first(verbatim_bytes));
80,941✔
490
         }
491

492
         // load remaining unaligned data
493
         for(size_type i = verbatim_bytes * 8; i < new_bits; ++i) {
173,806✔
494
            ref(i) = ((bytes[i >> 3] & (uint8_t(1) << (i & 7))) != 0);
92,864✔
495
         }
496
      }
80,942✔
497

498
      /**
499
       * Renders the bitvector into a byte array. By default, this will use
500
       * `std::vector<uint8_t>` or `Botan::secure_vector<uint8_t>`, depending on
501
       * the allocator used by the bitvector. The rendering is compatible with
502
       * the bit layout explained in the respective constructor.
503
       */
504
      template <concepts::resizable_byte_buffer OutT =
505
                   std::conditional_t<uses_secure_allocator, secure_vector<uint8_t>, std::vector<uint8_t>>>
506
      OutT to_bytes() const {
328✔
507
         OutT out(ceil_tobytes(m_bits));
328✔
508
         to_bytes(out);
328✔
509
         return out;
328✔
510
      }
×
511

512
      /**
513
       * Renders the bitvector into a properly sized byte range.
514
       *
515
       * @param out  a byte range that has a length of at least `ceil_tobytes(size())`.
516
       */
517
      void to_bytes(std::span<uint8_t> out) const {
128,535✔
518
         const auto bytes_needed = ceil_tobytes(m_bits);
128,535✔
519
         BOTAN_ARG_CHECK(bytes_needed <= out.size_bytes(), "Not enough space to render bitvector");
128,535✔
520

521
         // copy as much aligned data as possible
522
         const auto verbatim_blocks = m_bits / block_size_bits;
128,535✔
523
         const auto verbatim_bytes = verbatim_blocks * block_size_bytes;
128,535✔
524
         if(verbatim_blocks > 0) {
128,535✔
525
            typecast_copy(out.first(verbatim_bytes), std::span{m_blocks}.first(verbatim_blocks));
128,532✔
526
         }
527

528
         // copy remaining unaligned data
529
         clear_mem(out.subspan(verbatim_bytes));
128,535✔
530
         for(size_type i = verbatim_bytes * 8; i < m_bits; ++i) {
321,954✔
531
            out[i >> 3] |= ref(i).template as<uint8_t>() << (i & 7);
193,419✔
532
         }
533
      }
128,535✔
534

535
      /**
536
       * Renders this bitvector into a sequence of "0"s and "1"s.
537
       * This is meant for debugging purposes and is not efficient.
538
       */
539
      std::string to_string() const {
1✔
540
         std::stringstream ss;
1✔
541
         for(size_type i = 0; i < size(); ++i) {
6✔
542
            ss << ref(i);
5✔
543
         }
544
         return ss.str();
1✔
545
      }
1✔
546

547
      /// @}
548

549
      /// @name Capacity Accessors and Modifiers
550
      /// @{
551

552
      size_type capacity() const { return m_blocks.capacity() * block_size_bits; }
6✔
553

554
      void reserve(size_type bits) { m_blocks.reserve(ceil_toblocks(bits)); }
219✔
555

556
      void resize(size_type bits) {
478,325✔
557
         const auto new_number_of_blocks = ceil_toblocks(bits);
478,325✔
558
         const auto old_number_of_blocks = m_blocks.size();
478,325✔
559
         if(new_number_of_blocks > old_number_of_blocks) {
478,325✔
560
            m_blocks.insert(m_blocks.end(), new_number_of_blocks - old_number_of_blocks, block_type(0));
130,681✔
561
         } else if(new_number_of_blocks < old_number_of_blocks) {
347,644✔
562
            m_blocks.erase(m_blocks.begin() + new_number_of_blocks, m_blocks.end());
3✔
563
         }
564

565
         m_bits = bits;
478,325✔
566
         zero_unused_bits();
478,325✔
567
      }
478,325✔
568

569
      void push_back(bool bit) {
397,295✔
570
         const auto i = size();
397,295✔
571
         resize(i + 1);
397,295✔
572
         ref(i) = bit;
794,590✔
573
      }
397,295✔
574

575
      void pop_back() {
9✔
576
         if(!empty()) {
9✔
577
            resize(size() - 1);
9✔
578
         }
579
      }
580

581
      /// @}
582

583
      /// @name Bitwise and Global Accessors and Modifiers
584
      /// @{
585

586
      auto at(size_type pos) {
457,816,311✔
587
         check_offset(pos);
457,816,307✔
588
         return ref(pos);
457,816,094✔
589
      }
590

591
      // TODO C++23: deducing this
592
      auto at(size_type pos) const {
890,817✔
593
         check_offset(pos);
890,816✔
594
         return ref(pos);
890,816✔
595
      }
596

597
      auto front() { return ref(0); }
2✔
598

599
      // TODO C++23: deducing this
600
      auto front() const { return ref(0); }
601

602
      auto back() { return ref(size() - 1); }
5✔
603

604
      // TODO C++23: deducing this
605
      auto back() const { return ref(size() - 1); }
606

607
      /**
608
       * Sets the bit at position @p pos.
609
       * @throws Botan::Invalid_Argument if @p pos is out of range
610
       */
611
      bitvector_base& set(size_type pos) {
81✔
612
         check_offset(pos);
80✔
613
         ref(pos).set();
46✔
614
         return *this;
27✔
615
      }
616

617
      /**
618
       * Sets all currently allocated bits.
619
       */
620
      bitvector_base& set() {
2✔
621
         full_range_operation(
2✔
622
            [](std::unsigned_integral auto block) -> decltype(block) {
2✔
623
               return static_cast<decltype(block)>(~static_cast<decltype(block)>(0));
624
            },
625
            *this);
626
         zero_unused_bits();
2✔
627
         return *this;
2✔
628
      }
629

630
      /**
631
       * Unsets the bit at position @p pos.
632
       * @throws Botan::Invalid_Argument if @p pos is out of range
633
       */
634
      bitvector_base& unset(size_type pos) {
6✔
635
         check_offset(pos);
5✔
636
         ref(pos).unset();
5✔
637
         return *this;
5✔
638
      }
639

640
      /**
641
       * Unsets all currently allocated bits.
642
       */
643
      bitvector_base& unset() {
4✔
644
         full_range_operation(
4✔
645
            [](std::unsigned_integral auto block) -> decltype(block) { return static_cast<decltype(block)>(0); },
4✔
646
            *this);
647
         return *this;
648
      }
649

650
      /**
651
       * Flips the bit at position @p pos.
652
       * @throws Botan::Invalid_Argument if @p pos is out of range
653
       */
654
      bitvector_base& flip(size_type pos) {
10✔
655
         check_offset(pos);
9✔
656
         ref(pos).flip();
9✔
657
         return *this;
9✔
658
      }
659

660
      /**
661
       * Flips all currently allocated bits.
662
       */
663
      bitvector_base& flip() {
5✔
664
         full_range_operation([](std::unsigned_integral auto block) -> decltype(block) { return ~block; }, *this);
5✔
665
         zero_unused_bits();
5✔
666
         return *this;
5✔
667
      }
668

669
      /**
670
       * @returns true iff no bit is set
671
       */
672
      bool none_vartime() const {
15✔
673
         return full_range_operation([](std::unsigned_integral auto block) { return block == 0; }, *this);
7✔
674
      }
675

676
      /**
677
       * @returns true iff no bit is set in constant time
678
       */
679
      bool none() const { return hamming_weight() == 0; }
7✔
680

681
      /**
682
       * @returns true iff at least one bit is set
683
       */
684
      bool any_vartime() const { return !none_vartime(); }
8✔
685

686
      /**
687
       * @returns true iff at least one bit is set in constant time
688
       */
689
      bool any() const { return !none(); }
6✔
690

691
      /**
692
       * @returns true iff all bits are set
693
       */
694
      bool all_vartime() const {
8✔
695
         return full_range_operation(
8✔
696
            []<std::unsigned_integral BlockT>(BlockT block, BlockT mask) { return block == mask; }, *this);
7✔
697
      }
698

699
      /**
700
       * @returns true iff all bits are set in constant time
701
       */
702
      bool all() const { return hamming_weight() == m_bits; }
6✔
703

704
      auto operator[](size_type pos) { return ref(pos); }
1,326,297,767✔
705

706
      // TODO C++23: deducing this
707
      auto operator[](size_type pos) const { return ref(pos); }
12,448,319✔
708

709
      /// @}
710

711
      /// @name Subvectors
712
      /// @{
713

714
      /**
715
       * Creates a new bitvector with a subsection of this bitvector starting at
716
       * @p pos copying exactly @p length bits.
717
       */
718
      template <bitvectorish OutT = bitvector_base<AllocatorT>>
719
      auto subvector(size_type pos, std::optional<size_type> length = std::nullopt) const {
128,376✔
720
         const size_type bitlen = length.value_or(size() - pos);
128,376✔
721
         BOTAN_ARG_CHECK(pos + bitlen <= size(), "Not enough bits to copy");
128,376✔
722

723
         OutT newvector(bitlen);
128,372✔
724

725
         // Handle bitvectors that are wrapped in strong types
726
         auto& newvector_unwrapped = unwrap_strong_type(newvector);
128,372✔
727

728
         if(bitlen > 0) {
128,372✔
729
            if(pos % 8 == 0) {
128,369✔
730
               copy_mem(
89,675✔
731
                  newvector_unwrapped.m_blocks,
89,675✔
732
                  std::span{m_blocks}.subspan(block_index(pos), block_index(pos + bitlen - 1) - block_index(pos) + 1));
89,675✔
733
            } else {
734
               const BitRangeOperator<const bitvector_base<AllocatorT>, BitRangeAlignment::no_alignment> from_op(
38,694✔
735
                  *this, pos, bitlen);
736
               const BitRangeOperator<strong_type_wrapped_type<OutT>> to_op(
38,694✔
737
                  unwrap_strong_type(newvector_unwrapped), 0, bitlen);
738
               range_operation([](auto /* to */, auto from) { return from; }, to_op, from_op);
38,694✔
739
            }
740

741
            newvector_unwrapped.zero_unused_bits();
128,372✔
742
         }
743

744
         return newvector;
128,372✔
745
      }
×
746

747
      /**
748
       * Extracts a subvector of bits as an unsigned integral type @p OutT
749
       * starting from bit @p pos and copying exactly sizeof(OutT)*8 bits.
750
       *
751
       * Hint: The bits are in big-endian order, i.e. the least significant bit
752
       *       is the 0th bit and the most significant bit it the n-th. Hence,
753
       *       addressing the bits with bitwise operations is done like so:
754
       *       bool bit = (out_int >> pos) & 1;
755
       */
756
      template <typename OutT>
757
         requires(std::unsigned_integral<strong_type_wrapped_type<OutT>> &&
758
                  !std::same_as<bool, strong_type_wrapped_type<OutT>>)
759
      OutT subvector(size_type pos) const {
74,960✔
760
         using result_t = strong_type_wrapped_type<OutT>;
761
         constexpr size_t bits = sizeof(result_t) * 8;
74,960✔
762
         BOTAN_ARG_CHECK(pos + bits <= size(), "Not enough bits to copy");
74,960✔
763
         result_t out = 0;
74,956✔
764

765
         if(pos % 8 == 0) {
74,956✔
766
            out = load_le<result_t>(std::span{m_blocks}.subspan(block_index(pos)).template first<sizeof(result_t)>());
48,648✔
767
         } else {
768
            const BitRangeOperator<const bitvector_base<AllocatorT>, BitRangeAlignment::no_alignment> op(
26,308✔
769
               *this, pos, bits);
770
            range_operation(
26,308✔
771
               [&](std::unsigned_integral auto integer) {
26,308✔
772
                  if constexpr(std::same_as<result_t, decltype(integer)>) {
773
                     out = integer;
26,308✔
774
                  }
775
               },
776
               op);
777
         }
778

779
         return wrap_strong_type<OutT>(out);
74,956✔
780
      }
781

782
      /**
783
       * Replaces a subvector of bits with the bits of another bitvector @p value
784
       * starting at bit @p pos. The number of bits to replace is determined by
785
       * the size of @p value.
786
       *
787
       * @note This is currently supported for byte-aligned @p pos only.
788
       *
789
       * @throws Not_Implemented when called with @p pos not divisible by 8.
790
       *
791
       * @param pos    the position to start replacing bits
792
       * @param value  the bitvector to copy bits from
793
       */
794
      template <typename InT>
795
         requires(std::unsigned_integral<strong_type_wrapped_type<InT>> && !std::same_as<bool, InT>)
796
      void subvector_replace(size_type pos, InT value) {
74,967✔
797
         using in_t = strong_type_wrapped_type<InT>;
798
         constexpr size_t bits = sizeof(in_t) * 8;
74,967✔
799
         BOTAN_ARG_CHECK(pos + bits <= size(), "Not enough bits to replace");
74,967✔
800

801
         if(pos % 8 == 0) {
74,963✔
802
            store_le(std::span{m_blocks}.subspan(block_index(pos)).template first<sizeof(in_t)>(),
48,650✔
803
                     unwrap_strong_type(value));
804
         } else {
805
            const BitRangeOperator<bitvector_base<AllocatorT>, BitRangeAlignment::no_alignment> op(*this, pos, bits);
26,313✔
806
            range_operation(
26,313✔
807
               [&]<std::unsigned_integral BlockT>(BlockT block) -> BlockT {
26,313✔
808
                  if constexpr(std::same_as<in_t, BlockT>) {
809
                     return unwrap_strong_type(value);
26,313✔
810
                  } else {
811
                     // This should never be reached. BOTAN_ASSERT_UNREACHABLE()
812
                     // caused warning "unreachable code" on MSVC, though. You
813
                     // don't say!
814
                     //
815
                     // Returning the given block back, is the most reasonable
816
                     // thing to do in this case, though.
817
                     return block;
818
                  }
819
               },
820
               op);
821
         }
822
      }
74,963✔
823

824
      /// @}
825

826
      /// @name Operators
827
      ///
828
      /// @{
829

830
      auto operator~() {
1✔
831
         auto newbv = *this;
1✔
832
         newbv.flip();
1✔
833
         return newbv;
1✔
834
      }
×
835

836
      template <bitvectorish OtherT>
837
      auto& operator|=(const OtherT& other) {
6✔
838
         full_range_operation([]<std::unsigned_integral BlockT>(BlockT lhs, BlockT rhs) -> BlockT { return lhs | rhs; },
2✔
839
                              *this,
840
                              unwrap_strong_type(other));
841
         return *this;
842
      }
843

844
      template <bitvectorish OtherT>
845
      auto& operator&=(const OtherT& other) {
79,049✔
846
         full_range_operation([]<std::unsigned_integral BlockT>(BlockT lhs, BlockT rhs) -> BlockT { return lhs & rhs; },
79,045✔
847
                              *this,
848
                              unwrap_strong_type(other));
849
         return *this;
850
      }
851

852
      template <bitvectorish OtherT>
853
      auto& operator^=(const OtherT& other) {
8✔
854
         full_range_operation([]<std::unsigned_integral BlockT>(BlockT lhs, BlockT rhs) -> BlockT { return lhs ^ rhs; },
1✔
855
                              *this,
856
                              unwrap_strong_type(other));
857
         return *this;
858
      }
859

860
      /// @}
861

862
      /// @name Constant Time Operations
863
      ///
864
      /// @{
865

866
      /**
867
       * Implements::
868
       *
869
       *    if(condition) {
870
       *       *this ^= other;
871
       *    }
872
       *
873
       * omitting runtime dependence on any of the parameters.
874
       */
875
      template <bitvectorish OtherT>
876
      void ct_conditional_xor(CT::Choice condition, const OtherT& other) {
453,074,997✔
877
         BOTAN_ASSERT_NOMSG(m_bits == other.m_bits);
453,074,997✔
878
         BOTAN_ASSERT_NOMSG(m_blocks.size() == other.m_blocks.size());
453,074,997✔
879

880
         auto maybe_xor = overloaded{
881
            [m = CT::Mask<uint64_t>::from_choice(condition)](uint64_t lhs, uint64_t rhs) -> uint64_t {
2,147,483,647✔
882
               return lhs ^ m.if_set_return(rhs);
2,147,483,647✔
883
            },
884
            [m = CT::Mask<uint32_t>::from_choice(condition)](uint32_t lhs, uint32_t rhs) -> uint32_t {
739,686,734✔
885
               return lhs ^ m.if_set_return(rhs);
286,611,737✔
886
            },
887
            [m = CT::Mask<uint16_t>::from_choice(condition)](uint16_t lhs, uint16_t rhs) -> uint16_t {
592,942,380✔
888
               return lhs ^ m.if_set_return(rhs);
139,867,383✔
889
            },
890
            [m = CT::Mask<uint8_t>::from_choice(condition)](uint8_t lhs, uint8_t rhs) -> uint8_t {
453,075,003✔
891
               return lhs ^ m.if_set_return(rhs);
6✔
892
            },
893
         };
894

895
         full_range_operation(maybe_xor, *this, unwrap_strong_type(other));
453,074,997✔
896
      }
453,074,997✔
897

898
      constexpr void _const_time_poison() const { CT::poison(m_blocks); }
71✔
899

900
      constexpr void _const_time_unpoison() const { CT::unpoison(m_blocks); }
98✔
901

902
      /// @}
903

904
      /// @name Iterators
905
      ///
906
      /// @{
907

908
      iterator begin() noexcept { return iterator(this, 0); }
1,067✔
909

910
      const_iterator begin() const noexcept { return const_iterator(this, 0); }
10✔
911

912
      const_iterator cbegin() const noexcept { return const_iterator(this, 0); }
10✔
913

914
      iterator end() noexcept { return iterator(this, size()); }
537✔
915

916
      const_iterator end() const noexcept { return const_iterator(this, size()); }
5✔
917

918
      const_iterator cend() noexcept { return const_iterator(this, size()); }
8✔
919

920
      /// @}
921

922
   private:
923
      void check_offset(size_type pos) const {
458,706,909✔
924
         // BOTAN_ASSERT_NOMSG(!CT::is_poisoned(&m_bits, sizeof(m_bits)));
925
         // BOTAN_ASSERT_NOMSG(!CT::is_poisoned(&pos, sizeof(pos)));
926
         BOTAN_ARG_CHECK(pos < m_bits, "Out of range");
458,707,191✔
927
      }
928

929
      void zero_unused_bits() {
606,701✔
930
         const auto first_unused_bit = size();
606,701✔
931

932
         // Zero out any unused bits in the last block
933
         if(first_unused_bit % block_size_bits != 0) {
606,701✔
934
            const block_type mask = (one << block_offset(first_unused_bit)) - one;
404,954✔
935
            m_blocks[block_index(first_unused_bit)] &= mask;
404,954✔
936
         }
937
      }
938

939
      static constexpr size_type ceil_toblocks(size_type bits) {
607,128✔
940
         return add_or_throw(bits, block_size_bits - 1, "bitvector size is too large") / block_size_bits;
607,128✔
941
      }
942

943
      auto ref(size_type pos) const { return bitref<const block_type>(m_blocks, pos); }
12,641,743✔
944

945
      auto ref(size_type pos) { return bitref<block_type>(m_blocks, pos); }
1,335,235,935✔
946

947
   private:
948
      enum class BitRangeAlignment : uint8_t { byte_aligned, no_alignment };
949

950
      /**
951
       * Helper construction to implement bit range operations on the bitvector.
952
       * It basically implements an iterator to read and write blocks of bits
953
       * from the underlying bitvector. Where "blocks of bits" are unsigned
954
       * integers of varying bit lengths.
955
       *
956
       * If the iteration starts at a byte boundary in the underlying bitvector,
957
       * this applies certain optimizations (i.e. loading blocks of bits straight
958
       * from the underlying byte buffer). The optimizations are enabled at
959
       * compile time (with the template parameter `alignment`).
960
       */
961
      template <typename BitvectorT, auto alignment = BitRangeAlignment::byte_aligned>
962
         requires is_bitvector_v<std::remove_cvref_t<BitvectorT>>
963
      class BitRangeOperator {
964
         private:
965
            constexpr static bool is_const() { return std::is_const_v<BitvectorT>; }
966

967
            struct UnalignedDataHelper {
968
                  const uint8_t padding_bits;
969
                  const uint8_t bits_to_byte_alignment;
970
            };
971

972
         public:
973
            BitRangeOperator(BitvectorT& source, size_type start_bitoffset, size_type bitlength) :
906,517,453✔
974
                  m_source(source),
906,517,453✔
975
                  m_start_bitoffset(start_bitoffset),
906,517,453✔
976
                  m_bitlength(bitlength),
906,517,453✔
977
                  m_unaligned_helper({.padding_bits = static_cast<uint8_t>(start_bitoffset % 8),
906,517,453✔
978
                                      .bits_to_byte_alignment = static_cast<uint8_t>(8 - (start_bitoffset % 8))}),
906,517,453✔
979
                  m_read_bitpos(start_bitoffset),
906,517,453✔
980
                  m_write_bitpos(start_bitoffset) {
906,517,453✔
981
               BOTAN_ASSERT(is_byte_aligned() == (m_start_bitoffset % 8 == 0), "byte alignment guarantee");
906,517,453✔
982
               BOTAN_ASSERT(m_source.size() >= m_start_bitoffset + m_bitlength, "enough bytes in underlying source");
906,517,453✔
983
            }
906,517,453✔
984

985
            explicit BitRangeOperator(BitvectorT& source) : BitRangeOperator(source, 0, source.size()) {}
906,387,444✔
986

987
            static constexpr bool is_byte_aligned() { return alignment == BitRangeAlignment::byte_aligned; }
988

989
            /**
990
             * @returns the overall number of bits to be iterated with this operator
991
             */
992
            size_type size() const { return m_bitlength; }
453,272,013✔
993

994
            /**
995
             * @returns the number of bits not yet read from this operator
996
             */
997
            size_type bits_to_read() const { return m_bitlength - m_read_bitpos + m_start_bitoffset; }
1,820,061,117✔
998

999
            /**
1000
             * @returns the number of bits still to be written into this operator
1001
             */
1002
            size_type bits_to_write() const { return m_bitlength - m_write_bitpos + m_start_bitoffset; }
3,398,318✔
1003

1004
            /**
1005
             * Loads the next block of bits from the underlying bitvector. No
1006
             * bounds checks are performed. The caller can define the size of
1007
             * the resulting unsigned integer block.
1008
             */
1009
            template <std::unsigned_integral BlockT>
1010
            BlockT load_next() const {
6,762,618✔
1011
               constexpr size_type block_size = sizeof(BlockT);
6,762,618✔
1012
               constexpr size_type block_bits = block_size * 8;
6,762,618✔
1013
               const auto bits_remaining = bits_to_read();
6,762,618✔
1014

1015
               BlockT result_block = 0;
6,762,618✔
1016
               if constexpr(is_byte_aligned()) {
1017
                  result_block = load_le(m_source.as_byte_span().subspan(read_bytepos()).template first<block_size>());
3,382,904✔
1018
               } else {
1019
                  const size_type byte_pos = read_bytepos();
3,379,714✔
1020
                  const size_type bits_to_collect = std::min(block_bits, bits_to_read());
6,759,428✔
1021

1022
                  const uint8_t first_byte = m_source.as_byte_span()[byte_pos];
3,379,714✔
1023

1024
                  // Initialize the left-most bits from the first byte.
1025
                  result_block = BlockT(first_byte) >> m_unaligned_helper.padding_bits;
3,379,714✔
1026

1027
                  // If more bits are needed, we pull them from the remaining bytes.
1028
                  if(m_unaligned_helper.bits_to_byte_alignment < bits_to_collect) {
3,379,714✔
1029
                     const BlockT block =
1030
                        load_le(m_source.as_byte_span().subspan(byte_pos + 1).template first<block_size>());
6,682,042✔
1031
                     result_block |= block << m_unaligned_helper.bits_to_byte_alignment;
3,341,024✔
1032
                  }
1033
               }
1034

1035
               m_read_bitpos += std::min(block_bits, bits_remaining);
6,762,618✔
1036
               return result_block;
6,668,117✔
1037
            }
1038

1039
            /**
1040
             * Stores the next block of bits into the underlying bitvector.
1041
             * No bounds checks are performed. Storing bit blocks that are not
1042
             * aligned at a byte-boundary in the underlying bitvector is
1043
             * currently not implemented.
1044
             */
1045
            template <std::unsigned_integral BlockT>
1046
               requires(!is_const())
1047
            void store_next(BlockT block) {
3,372,005✔
1048
               constexpr size_type block_size = sizeof(BlockT);
3,372,005✔
1049
               constexpr size_type block_bits = block_size * 8;
3,372,005✔
1050

1051
               if constexpr(is_byte_aligned()) {
1052
                  auto sink = m_source.as_byte_span().subspan(write_bytepos()).template first<block_size>();
3,345,692✔
1053
                  store_le(sink, block);
3,345,692✔
1054
               } else {
1055
                  const size_type byte_pos = write_bytepos();
26,313✔
1056
                  const size_type bits_to_store = std::min(block_bits, bits_to_write());
52,626✔
1057

1058
                  uint8_t& first_byte = m_source.as_byte_span()[byte_pos];
26,313✔
1059

1060
                  // Set the left-most bits in the first byte, leaving all others unchanged
1061
                  first_byte = (first_byte & uint8_t(0xFF >> m_unaligned_helper.bits_to_byte_alignment)) |
26,313✔
1062
                               uint8_t(block << m_unaligned_helper.padding_bits);
26,313✔
1063

1064
                  // If more bits are provided, we store them in the remaining bytes.
1065
                  if(m_unaligned_helper.bits_to_byte_alignment < bits_to_store) {
26,313✔
1066
                     const auto remaining_bytes =
1067
                        m_source.as_byte_span().subspan(byte_pos + 1).template first<block_size>();
26,313✔
1068
                     const BlockT padding_mask = ~(BlockT(-1) >> m_unaligned_helper.bits_to_byte_alignment);
26,313✔
1069
                     const BlockT new_bytes =
26,313✔
1070
                        (load_le(remaining_bytes) & padding_mask) | block >> m_unaligned_helper.bits_to_byte_alignment;
26,313✔
1071
                     store_le(remaining_bytes, new_bytes);
26,313✔
1072
                  }
1073
               }
1074

1075
               m_write_bitpos += std::min(block_bits, bits_to_write());
3,372,005✔
1076
            }
3,372,005✔
1077

1078
            template <std::unsigned_integral BlockT>
1079
               requires(is_byte_aligned() && !is_const())
1080
            std::span<BlockT> span(size_type blocks) const {
1,359,462,213✔
1081
               BOTAN_DEBUG_ASSERT(blocks == 0 || is_memory_aligned_to<BlockT>());
1,359,462,213✔
1082
               BOTAN_DEBUG_ASSERT(read_bytepos() % sizeof(BlockT) == 0);
1,359,462,213✔
1083
               // Intermittently casting to void* to avoid a compiler warning
1084
               void* ptr = reinterpret_cast<void*>(m_source.as_byte_span().data() + read_bytepos());
1,359,462,213✔
1085
               return {reinterpret_cast<BlockT*>(ptr), blocks};
1,359,462,213✔
1086
            }
1087

1088
            template <std::unsigned_integral BlockT>
1089
               requires(is_byte_aligned() && is_const())
1090
            std::span<const BlockT> span(size_type blocks) const {
1,359,699,901✔
1091
               BOTAN_DEBUG_ASSERT(blocks == 0 || is_memory_aligned_to<BlockT>());
1,359,699,901✔
1092
               BOTAN_DEBUG_ASSERT(read_bytepos() % sizeof(BlockT) == 0);
1,359,699,901✔
1093
               // Intermittently casting to void* to avoid a compiler warning
1094
               const void* ptr = reinterpret_cast<const void*>(m_source.as_byte_span().data() + read_bytepos());
1,359,699,901✔
1095
               return {reinterpret_cast<const BlockT*>(ptr), blocks};
1,359,699,901✔
1096
            }
1097

1098
            void advance(size_type bytes)
1,359,699,934✔
1099
               requires(is_byte_aligned())
1100
            {
1101
               m_read_bitpos += bytes * 8;
1,359,699,934✔
1102
               m_write_bitpos += bytes * 8;
1,359,699,934✔
1103
            }
1104

1105
            template <std::unsigned_integral BlockT>
1106
               requires(is_byte_aligned())
1107
            size_t is_memory_aligned_to() const {
906,387,424✔
1108
               const void* cptr = m_source.as_byte_span().data() + read_bytepos();
906,387,424✔
1109
               const void* ptr_before = cptr;
906,387,424✔
1110

1111
               // std::align takes `ptr` as a reference (!), i.e. `void*&` and
1112
               // uses it as an out-param. Though, `cptr` is const because this
1113
               // method is const-qualified, hence the const_cast<>.
1114
               void* ptr = const_cast<void*>(cptr);  // NOLINT(*-const-correctness)
906,387,424✔
1115
               size_t size = sizeof(BlockT);
906,387,424✔
1116
               return ptr_before != nullptr && std::align(alignof(BlockT), size, ptr, size) == ptr_before;
1,812,774,848✔
1117
            }
1118

1119
         private:
1120
            size_type read_bytepos() const { return m_read_bitpos / 8; }
913,150,025✔
1121

1122
            size_type write_bytepos() const { return m_write_bitpos / 8; }
3,372,005✔
1123

1124
         private:
1125
            BitvectorT& m_source;
1126
            size_type m_start_bitoffset;
1127
            size_type m_bitlength;
1128

1129
            UnalignedDataHelper m_unaligned_helper;
1130

1131
            mutable size_type m_read_bitpos;
1132
            mutable size_type m_write_bitpos;
1133
      };
1134

1135
      /**
1136
       * Helper struct for the low-level handling of blockwise operations
1137
       *
1138
       * This has two main code paths: Optimized for byte-aligned ranges that
1139
       * can simply be taken from memory as-is. And a generic implementation
1140
       * that must assemble blocks from unaligned bits before processing.
1141
       */
1142
      template <typename FnT, typename... ParamTs>
1143
         requires detail::blockwise_processing_callback<FnT, ParamTs...>
1144
      class blockwise_processing_callback_trait {
1145
         public:
1146
            constexpr static bool needs_mask = detail::blockwise_processing_callback_with_mask<FnT, ParamTs...>;
1147
            constexpr static bool is_manipulator = detail::manipulating_blockwise_processing_callback<FnT, ParamTs...>;
1148
            constexpr static bool is_predicate = detail::predicate_blockwise_processing_callback<FnT, ParamTs...>;
1149
            static_assert(!is_manipulator || !is_predicate, "cannot be manipulator and predicate at the same time");
1150

1151
            /**
1152
             * Applies @p fn to the blocks provided in @p blocks by simply reading from
1153
             * memory without re-arranging any bits across byte-boundaries.
1154
             */
1155
            template <std::unsigned_integral... BlockTs>
1156
               requires(all_same_v<std::remove_cv_t<BlockTs>...> && sizeof...(BlockTs) == sizeof...(ParamTs))
1157
            constexpr static bool apply_on_full_blocks(FnT fn, std::span<BlockTs>... blocks) {
1,359,699,934✔
1158
               constexpr size_type bits = sizeof(detail::first_t<BlockTs...>) * 8;
1,359,699,934✔
1159
               const size_type iterations = detail::first(blocks...).size();
1,359,462,654✔
1160
               for(size_type i = 0; i < iterations; ++i) {
2,147,483,647✔
1161
                  if constexpr(is_predicate) {
1162
                     if(!apply(fn, bits, blocks[i]...)) {
33✔
1163
                        return false;
1164
                     }
1165
                  } else if constexpr(is_manipulator) {
1166
                     detail::first(blocks...)[i] = apply(fn, bits, blocks[i]...);
2,147,483,647✔
1167
                  } else {
1168
                     apply(fn, bits, blocks[i]...);
6,156,777✔
1169
                  }
1170
               }
1171
               return true;
339✔
1172
            }
1173

1174
            /**
1175
             * Applies @p fn to as many blocks as @p ops provide for the given type.
1176
             */
1177
            template <std::unsigned_integral BlockT, typename... BitRangeOperatorTs>
1178
               requires(sizeof...(BitRangeOperatorTs) == sizeof...(ParamTs))
1179
            constexpr static bool apply_on_unaligned_blocks(FnT fn, BitRangeOperatorTs&... ops) {
453,598,565✔
1180
               constexpr size_type block_bits = sizeof(BlockT) * 8;
453,598,565✔
1181
               auto bits = detail::first(ops...).bits_to_read();
453,598,565✔
1182
               if(bits == 0) {
453,598,565✔
1183
                  return true;
1184
               }
1185

1186
               bits += block_bits;  // avoid unsigned integer underflow in the following loop
244,633✔
1187
               while(bits > block_bits * 2 - 8) {
3,661,553✔
1188
                  bits -= block_bits;
3,416,923✔
1189
                  if constexpr(is_predicate) {
1190
                     if(!apply(fn, bits, ops.template load_next<BlockT>()...)) {
29✔
1191
                        return false;
1192
                     }
1193
                  } else if constexpr(is_manipulator) {
1194
                     detail::first(ops...).store_next(apply(fn, bits, ops.template load_next<BlockT>()...));
3,410,695✔
1195
                  } else {
1196
                     apply(fn, bits, ops.template load_next<BlockT>()...);
44,902✔
1197
                  }
1198
               }
1199
               return true;
1200
            }
1201

1202
         private:
1203
            template <std::unsigned_integral... BlockTs>
1204
               requires(all_same_v<std::remove_cv_t<BlockTs>...>)
1205
            constexpr static auto apply(FnT fn, size_type bits, BlockTs... blocks) {
2,147,483,647✔
1206
               if constexpr(needs_mask) {
1207
                  return fn(blocks..., make_mask<detail::first_t<BlockTs...>>(bits));
3✔
1208
               } else {
1209
                  return fn(blocks...);
2,147,483,647✔
1210
               }
1211
            }
1212
      };
1213

1214
      /**
1215
       * Helper function of `full_range_operation` and `range_operation` that
1216
       * calls @p fn on a given aligned unsigned integer block as long as the
1217
       * underlying bit range contains enough bits to fill the block fully.
1218
       *
1219
       * This uses bare memory access to gain a speed up for aligned data.
1220
       */
1221
      template <std::unsigned_integral BlockT, typename FnT, typename... BitRangeOperatorTs>
1222
         requires(detail::blockwise_processing_callback<FnT, BitRangeOperatorTs...> &&
1223
                  sizeof...(BitRangeOperatorTs) > 0)
1224
      static bool _process_in_fully_aligned_blocks_of(FnT fn, BitRangeOperatorTs&... ops) {
1,359,699,934✔
1225
         constexpr size_type block_bytes = sizeof(BlockT);
1,359,699,934✔
1226
         constexpr size_type block_bits = block_bytes * 8;
1,359,699,934✔
1227
         const size_type blocks = detail::first(ops...).bits_to_read() / block_bits;
1,359,699,934✔
1228

1229
         using callback_trait = blockwise_processing_callback_trait<FnT, BitRangeOperatorTs...>;
1230
         const auto result = callback_trait::apply_on_full_blocks(fn, ops.template span<BlockT>(blocks)...);
2,147,483,647✔
1231
         (ops.advance(block_bytes * blocks), ...);
1,359,699,934✔
1232
         return result;
1,359,699,934✔
1233
      }
1234

1235
      /**
1236
       * Helper function of `full_range_operation` and `range_operation` that
1237
       * calls @p fn on a given unsigned integer block size as long as the
1238
       * underlying bit range contains enough bits to fill the block.
1239
       */
1240
      template <std::unsigned_integral BlockT, typename FnT, typename... BitRangeOperatorTs>
1241
         requires(detail::blockwise_processing_callback<FnT, BitRangeOperatorTs...>)
1242
      static bool _process_in_unaligned_blocks_of(FnT fn, BitRangeOperatorTs&... ops) {
453,598,565✔
1243
         using callback_trait = blockwise_processing_callback_trait<FnT, BitRangeOperatorTs...>;
1244
         return callback_trait::template apply_on_unaligned_blocks<BlockT>(fn, ops...);
453,507,250✔
1245
      }
1246

1247
      /**
1248
       * Apply @p fn to all bits in the ranges defined by @p ops. If more than
1249
       * one range operator is passed to @p ops, @p fn receives corresponding
1250
       * blocks of bits from each operator. Therefore, all @p ops have to define
1251
       * the exact same length of their underlying ranges.
1252
       *
1253
       * @p fn may return a bit block that will be stored into the _first_ bit
1254
       * range passed into @p ops. If @p fn returns a boolean, and its value is
1255
       * `false`, the range operation is cancelled and `false` is returned.
1256
       *
1257
       * The implementation ensures to pull bits in the largest bit blocks
1258
       * possible and reverts to smaller bit blocks only when needed.
1259
       */
1260
      template <typename FnT, typename... BitRangeOperatorTs>
1261
         requires(detail::blockwise_processing_callback<FnT, BitRangeOperatorTs...> &&
1262
                  sizeof...(BitRangeOperatorTs) > 0)
1263
      static bool range_operation(FnT fn, BitRangeOperatorTs... ops) {
453,324,634✔
1264
         BOTAN_ASSERT(has_equal_lengths(ops...), "all BitRangeOperators have the same length");
170,509✔
1265

1266
         if constexpr((BitRangeOperatorTs::is_byte_aligned() && ...)) {
1267
            // Note: At the moment we can assume that this will always be used
1268
            //       on the _entire_ bitvector. Therefore, we can safely assume
1269
            //       that the bitvectors' underlying buffers are properly aligned.
1270
            //       If this assumption changes, we need to add further handling
1271
            //       to process a byte padding at the beginning of the bitvector
1272
            //       until a memory alignment boundary is reached.
1273
            //
1274
            // An empty range has no blocks to process and a possibly-null
1275
            // underlying buffer, so the alignment check does not apply.
1276
            if(detail::first(ops...).size() != 0) {
453,233,319✔
1277
               const bool alignment = (ops.template is_memory_aligned_to<uint64_t>() && ...);
906,387,438✔
1278
               BOTAN_ASSERT_NOMSG(alignment);
×
1279
            }
1280

1281
            return _process_in_fully_aligned_blocks_of<uint64_t>(fn, ops...) &&
906,466,628✔
1282
                   _process_in_fully_aligned_blocks_of<uint32_t>(fn, ops...) &&
906,466,615✔
1283
                   _process_in_fully_aligned_blocks_of<uint16_t>(fn, ops...) &&
906,466,625✔
1284
                   _process_in_unaligned_blocks_of<uint8_t>(fn, ops...);
453,233,305✔
1285
         } else {
1286
            return _process_in_unaligned_blocks_of<uint64_t>(fn, ops...) &&
182,630✔
1287
                   _process_in_unaligned_blocks_of<uint32_t>(fn, ops...) &&
91,315✔
1288
                   _process_in_unaligned_blocks_of<uint16_t>(fn, ops...) &&
182,630✔
1289
                   _process_in_unaligned_blocks_of<uint8_t>(fn, ops...);
91,315✔
1290
         }
1291
      }
1292

1293
      /**
1294
       * Apply @p fn to all bit blocks in the bitvector(s).
1295
       */
1296
      template <typename FnT, typename... BitvectorTs>
1297
         requires(detail::blockwise_processing_callback<FnT, BitvectorTs...> &&
1298
                  (is_bitvector_v<std::remove_cvref_t<BitvectorTs>> && ... && true))
1299
      static bool full_range_operation(FnT&& fn, BitvectorTs&... bitvecs) {
453,233,319✔
1300
         BOTAN_ASSERT(has_equal_lengths(bitvecs...), "all bitvectors have the same length");
453,233,319✔
1301
         return range_operation(std::forward<FnT>(fn), BitRangeOperator<BitvectorTs>(bitvecs)...);
453,233,319✔
1302
      }
1303

1304
      template <typename SomeT, typename... SomeTs>
1305
      static bool has_equal_lengths(const SomeT& v, const SomeTs&... vs) {
906,346,944✔
1306
         return ((v.size() == vs.size()) && ... && true);
906,346,944✔
1307
      }
1308

1309
      template <std::unsigned_integral T>
1310
      static constexpr T make_mask(size_type bits) {
3✔
1311
         const bool max = bits >= sizeof(T) * 8;
3✔
1312
         bits &= T(max - 1);
3✔
1313
         return (T(!max) << bits) - 1;
3✔
1314
      }
1315

1316
      auto as_byte_span() { return std::span{m_blocks.data(), m_blocks.size() * sizeof(block_type)}; }
1,819,360,285✔
1317

1318
      auto as_byte_span() const { return std::span{m_blocks.data(), m_blocks.size() * sizeof(block_type)}; }
1,816,323,859✔
1319

1320
   private:
1321
      size_type m_bits;
1322
      std::vector<block_type, allocator_type> m_blocks;
1323
};
1324

1325
using secure_bitvector = bitvector_base<secure_allocator>;
1326
using bitvector = bitvector_base<std::allocator>;
1327

1328
namespace detail {
1329

1330
/**
1331
 * If one of the allocators is a Botan::secure_allocator, this will always
1332
 * prefer it. Otherwise, the allocator of @p lhs will be used as a default.
1333
 */
1334
template <bitvectorish T1, bitvectorish T2>
1335
constexpr auto copy_lhs_allocator_aware(const T1& lhs, const T2& /*rhs*/) {
16✔
1336
   constexpr bool needs_secure_allocator =
16✔
1337
      strong_type_wrapped_type<T1>::uses_secure_allocator || strong_type_wrapped_type<T2>::uses_secure_allocator;
1338

1339
   if constexpr(needs_secure_allocator) {
1340
      return lhs.template as<secure_bitvector>();
11✔
1341
   } else {
1342
      return lhs.template as<bitvector>();
6✔
1343
   }
1344
}
1345

1346
}  // namespace detail
1347

1348
template <bitvectorish T1, bitvectorish T2>
1349
auto operator|(const T1& lhs, const T2& rhs) {
5✔
1350
   auto res = detail::copy_lhs_allocator_aware(lhs, rhs);
5✔
1351
   res |= rhs;
5✔
1352
   return res;
5✔
1353
}
×
1354

1355
template <bitvectorish T1, bitvectorish T2>
1356
auto operator&(const T1& lhs, const T2& rhs) {
4✔
1357
   auto res = detail::copy_lhs_allocator_aware(lhs, rhs);
4✔
1358
   res &= rhs;
4✔
1359
   return res;
4✔
1360
}
×
1361

1362
template <bitvectorish T1, bitvectorish T2>
1363
auto operator^(const T1& lhs, const T2& rhs) {
7✔
1364
   auto res = detail::copy_lhs_allocator_aware(lhs, rhs);
7✔
1365
   res ^= rhs;
7✔
1366
   return res;
7✔
1367
}
×
1368

1369
template <bitvectorish T1, bitvectorish T2>
1370
bool operator==(const T1& lhs, const T2& rhs) {
14✔
1371
   return lhs.equals_vartime(rhs);
14✔
1372
}
1373

1374
namespace detail {
1375

1376
/**
1377
 * A Strong<> adapter for arbitrarily large bitvectors
1378
 */
1379
template <concepts::container T>
1380
   requires is_bitvector_v<T>
1381
class Strong_Adapter<T> : public Container_Strong_Adapter_Base<T> {
627,090✔
1382
   public:
1383
      using size_type = typename T::size_type;
1384

1385
   public:
1386
      using Container_Strong_Adapter_Base<T>::Container_Strong_Adapter_Base;
607✔
1387

1388
      auto at(size_type i) const { return this->get().at(i); }
96,256✔
1389

1390
      auto at(size_type i) { return this->get().at(i); }
449,249,297✔
1391

1392
      auto set(size_type i) { return this->get().set(i); }
1✔
1393

1394
      auto unset(size_type i) { return this->get().unset(i); }
1✔
1395

1396
      auto flip(size_type i) { return this->get().flip(i); }
1✔
1397

1398
      auto flip() { return this->get().flip(); }
1✔
1399

1400
      template <typename OutT>
1401
      auto as() const {
73✔
1402
         return this->get().template as<OutT>();
73✔
1403
      }
1404

1405
      template <bitvectorish OutT = T>
1406
      auto subvector(size_type pos, std::optional<size_type> length = std::nullopt) const {
128,266✔
1407
         return this->get().template subvector<OutT>(pos, length);
128,207✔
1408
      }
1409

1410
      template <typename OutT>
1411
         requires(std::unsigned_integral<strong_type_wrapped_type<OutT>> &&
1412
                  !std::same_as<bool, strong_type_wrapped_type<OutT>>)
1413
      auto subvector(size_type pos) const {
74,940✔
1414
         return this->get().template subvector<OutT>(pos);
74,940✔
1415
      }
1416

1417
      template <typename InT>
1418
         requires(std::unsigned_integral<strong_type_wrapped_type<InT>> && !std::same_as<bool, InT>)
1419
      void subvector_replace(size_type pos, InT value) {
74,939✔
1420
         return this->get().subvector_replace(pos, value);
74,939✔
1421
      }
1422

1423
      template <bitvectorish OtherT>
1424
      auto equals(const OtherT& other) const {
45✔
1425
         return this->get().equals(other);
45✔
1426
      }
1427

1428
      auto push_back(bool b) { return this->get().push_back(b); }
397,281✔
1429

1430
      auto pop_back() { return this->get().pop_back(); }
2✔
1431

1432
      auto front() const { return this->get().front(); }
1433

1434
      auto front() { return this->get().front(); }
1✔
1435

1436
      auto back() const { return this->get().back(); }
1437

1438
      auto back() { return this->get().back(); }
1✔
1439

1440
      auto any_vartime() const { return this->get().any_vartime(); }
1✔
1441

1442
      auto all_vartime() const { return this->get().all_vartime(); }
1✔
1443

1444
      auto none_vartime() const { return this->get().none_vartime(); }
1✔
1445

1446
      auto has_odd_hamming_weight() const { return this->get().has_odd_hamming_weight(); }
1✔
1447

1448
      auto hamming_weight() const { return this->get().hamming_weight(); }
83✔
1449

1450
      auto from_bytes(std::span<const uint8_t> bytes, std::optional<size_type> bits = std::nullopt) {
1451
         return this->get().from_bytes(bytes, bits);
1452
      }
1453

1454
      template <typename OutT = T>
1455
      auto to_bytes() const {
1456
         return this->get().template to_bytes<OutT>();
1457
      }
1458

1459
      auto to_bytes(std::span<uint8_t> out) const { return this->get().to_bytes(out); }
59✔
1460

1461
      auto to_string() const { return this->get().to_string(); }
1✔
1462

1463
      auto capacity() const { return this->get().capacity(); }
1464

1465
      auto reserve(size_type n) { return this->get().reserve(n); }
1466

1467
      constexpr void _const_time_poison() const { this->get()._const_time_poison(); }
71✔
1468

1469
      constexpr void _const_time_unpoison() const { this->get()._const_time_unpoison(); }
98✔
1470
};
1471

1472
}  // namespace detail
1473

1474
}  // namespace Botan
1475

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