Skip to content

Commit f406b8e

Browse files
authored
Merge pull request #4946 from randombit/jack/constrain-integer-templates
Use concepts to constrain the allowed types of various integer-related templates
2 parents f950d9e + 6637f93 commit f406b8e

11 files changed

Lines changed: 53 additions & 42 deletions

File tree

src/lib/base/secmem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace Botan {
2424
template <typename T>
2525
#if !defined(_ITERATOR_DEBUG_LEVEL) || _ITERATOR_DEBUG_LEVEL == 0
2626
/*
27-
* Assert exists to prevent someone from doing something that will
27+
* Check exists to prevent someone from doing something that will
2828
* probably crash anyway (like secure_vector<non_POD_t> where ~non_POD_t
2929
* deletes a member pointer which was zeroed before it ran).
3030
* MSVC in debug mode uses non-integral proxy types in container types

src/lib/block/serpent/serpent_fn.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,19 @@
1010
#include <botan/compiler.h>
1111
#include <botan/types.h>
1212
#include <botan/internal/rotate.h>
13+
#include <concepts>
1314

1415
namespace Botan::Serpent_F {
1516

17+
// Concept for types that support bitwise operations (unsigned integers or SIMD types)
18+
template <typename T>
19+
concept BitsliceT = requires(T& a, const T& b) {
20+
a ^= b;
21+
a &= b;
22+
a |= b;
23+
~a;
24+
};
25+
1626
template <size_t S>
1727
BOTAN_FORCE_INLINE uint32_t shl(uint32_t v) {
1828
return v << S;
@@ -21,7 +31,7 @@ BOTAN_FORCE_INLINE uint32_t shl(uint32_t v) {
2131
/*
2232
* Serpent's Linear Transform
2333
*/
24-
template <typename T>
34+
template <BitsliceT T>
2535
BOTAN_FORCE_INLINE void transform(T& B0, T& B1, T& B2, T& B3) {
2636
B0 = rotl<13>(B0);
2737
B2 = rotl<3>(B2);
@@ -38,7 +48,7 @@ BOTAN_FORCE_INLINE void transform(T& B0, T& B1, T& B2, T& B3) {
3848
/*
3949
* Serpent's Inverse Linear Transform
4050
*/
41-
template <typename T>
51+
template <BitsliceT T>
4252
BOTAN_FORCE_INLINE void i_transform(T& B0, T& B1, T& B2, T& B3) {
4353
B2 = rotr<22>(B2);
4454
B0 = rotr<5>(B0);
@@ -56,7 +66,7 @@ class Key_Inserter final {
5666
public:
5767
Key_Inserter(const uint32_t* RK) : m_RK(RK) {}
5868

59-
template <typename T>
69+
template <BitsliceT T>
6070
inline void operator()(size_t R, T& B0, T& B1, T& B2, T& B3) const {
6171
B0 ^= m_RK[4 * R];
6272
B1 ^= m_RK[4 * R + 1];

src/lib/block/serpent/serpent_sbox.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace Botan::Serpent_F {
1717

18-
template <typename T>
18+
template <BitsliceT T>
1919
BOTAN_FORCE_INLINE void SBoxE0(T& a, T& b, T& c, T& d) {
2020
d ^= a;
2121
T t0 = b;
@@ -40,7 +40,7 @@ BOTAN_FORCE_INLINE void SBoxE0(T& a, T& b, T& c, T& d) {
4040
b = t0;
4141
}
4242

43-
template <typename T>
43+
template <BitsliceT T>
4444
BOTAN_FORCE_INLINE void SBoxE1(T& a, T& b, T& c, T& d) {
4545
a = ~a;
4646
c = ~c;
@@ -66,7 +66,7 @@ BOTAN_FORCE_INLINE void SBoxE1(T& a, T& b, T& c, T& d) {
6666
b = t0;
6767
}
6868

69-
template <typename T>
69+
template <BitsliceT T>
7070
BOTAN_FORCE_INLINE void SBoxE2(T& a, T& b, T& c, T& d) {
7171
T t0 = a;
7272
a &= c;
@@ -89,7 +89,7 @@ BOTAN_FORCE_INLINE void SBoxE2(T& a, T& b, T& c, T& d) {
8989
d = ~t0;
9090
}
9191

92-
template <typename T>
92+
template <BitsliceT T>
9393
BOTAN_FORCE_INLINE void SBoxE3(T& a, T& b, T& c, T& d) {
9494
T t0 = a;
9595
a |= d;
@@ -115,7 +115,7 @@ BOTAN_FORCE_INLINE void SBoxE3(T& a, T& b, T& c, T& d) {
115115
d = t0;
116116
}
117117

118-
template <typename T>
118+
template <BitsliceT T>
119119
BOTAN_FORCE_INLINE void SBoxE4(T& a, T& b, T& c, T& d) {
120120
b ^= d;
121121
d = ~d;
@@ -142,7 +142,7 @@ BOTAN_FORCE_INLINE void SBoxE4(T& a, T& b, T& c, T& d) {
142142
b = t0;
143143
}
144144

145-
template <typename T>
145+
template <BitsliceT T>
146146
BOTAN_FORCE_INLINE void SBoxE5(T& a, T& b, T& c, T& d) {
147147
a ^= b;
148148
b ^= d;
@@ -169,7 +169,7 @@ BOTAN_FORCE_INLINE void SBoxE5(T& a, T& b, T& c, T& d) {
169169
d = t0;
170170
}
171171

172-
template <typename T>
172+
template <BitsliceT T>
173173
BOTAN_FORCE_INLINE void SBoxE6(T& a, T& b, T& c, T& d) {
174174
c = ~c;
175175
T t0 = d;
@@ -192,7 +192,7 @@ BOTAN_FORCE_INLINE void SBoxE6(T& a, T& b, T& c, T& d) {
192192
c = t0;
193193
}
194194

195-
template <typename T>
195+
template <BitsliceT T>
196196
BOTAN_FORCE_INLINE void SBoxE7(T& a, T& b, T& c, T& d) {
197197
T t0 = b;
198198
b |= c;
@@ -220,7 +220,7 @@ BOTAN_FORCE_INLINE void SBoxE7(T& a, T& b, T& c, T& d) {
220220
a = t0;
221221
}
222222

223-
template <typename T>
223+
template <BitsliceT T>
224224
BOTAN_FORCE_INLINE void SBoxD0(T& a, T& b, T& c, T& d) {
225225
c = ~c;
226226
T t0 = b;
@@ -245,7 +245,7 @@ BOTAN_FORCE_INLINE void SBoxD0(T& a, T& b, T& c, T& d) {
245245
b = t0;
246246
}
247247

248-
template <typename T>
248+
template <BitsliceT T>
249249
BOTAN_FORCE_INLINE void SBoxD1(T& a, T& b, T& c, T& d) {
250250
T t0 = b;
251251
b ^= d;
@@ -273,7 +273,7 @@ BOTAN_FORCE_INLINE void SBoxD1(T& a, T& b, T& c, T& d) {
273273
d = t0;
274274
}
275275

276-
template <typename T>
276+
template <BitsliceT T>
277277
BOTAN_FORCE_INLINE void SBoxD2(T& a, T& b, T& c, T& d) {
278278
c ^= d;
279279
d ^= a;
@@ -298,7 +298,7 @@ BOTAN_FORCE_INLINE void SBoxD2(T& a, T& b, T& c, T& d) {
298298
b = t0;
299299
}
300300

301-
template <typename T>
301+
template <BitsliceT T>
302302
BOTAN_FORCE_INLINE void SBoxD3(T& a, T& b, T& c, T& d) {
303303
T t0 = c;
304304
c ^= b;
@@ -324,7 +324,7 @@ BOTAN_FORCE_INLINE void SBoxD3(T& a, T& b, T& c, T& d) {
324324
d = t0;
325325
}
326326

327-
template <typename T>
327+
template <BitsliceT T>
328328
BOTAN_FORCE_INLINE void SBoxD4(T& a, T& b, T& c, T& d) {
329329
T t0 = c;
330330
c &= d;
@@ -350,7 +350,7 @@ BOTAN_FORCE_INLINE void SBoxD4(T& a, T& b, T& c, T& d) {
350350
d = t0;
351351
}
352352

353-
template <typename T>
353+
template <BitsliceT T>
354354
BOTAN_FORCE_INLINE void SBoxD5(T& a, T& b, T& c, T& d) {
355355
b = ~b;
356356
T t0 = d;
@@ -378,7 +378,7 @@ BOTAN_FORCE_INLINE void SBoxD5(T& a, T& b, T& c, T& d) {
378378
c = t0;
379379
}
380380

381-
template <typename T>
381+
template <BitsliceT T>
382382
BOTAN_FORCE_INLINE void SBoxD6(T& a, T& b, T& c, T& d) {
383383
a ^= c;
384384
T t0 = c;
@@ -402,7 +402,7 @@ BOTAN_FORCE_INLINE void SBoxD6(T& a, T& b, T& c, T& d) {
402402
c = t0;
403403
}
404404

405-
template <typename T>
405+
template <BitsliceT T>
406406
BOTAN_FORCE_INLINE void SBoxD7(T& a, T& b, T& c, T& d) {
407407
T t0 = c;
408408
c ^= a;

src/lib/math/pcurves/pcurves_impl/pcurves_impl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <botan/internal/pcurves_mul.h>
1515
#include <botan/internal/pcurves_util.h>
1616
#include <botan/internal/stl_util.h>
17+
#include <concepts>
1718
#include <vector>
1819

1920
namespace Botan {
@@ -1548,7 +1549,7 @@ class WindowedBoothMulTable final {
15481549
}
15491550

15501551
private:
1551-
template <size_t B, typename T>
1552+
template <size_t B, std::unsigned_integral T>
15521553
static constexpr std::pair<size_t, CT::Choice> booth_recode(T x) {
15531554
static_assert(B < sizeof(T) * 8 - 2, "Invalid B");
15541555

src/lib/prov/pkcs11/p11_object.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <botan/p11_types.h>
1414
#include <botan/secmem.h>
1515

16+
#include <concepts>
1617
#include <functional>
1718
#include <list>
1819
#include <string>
@@ -92,8 +93,7 @@ class BOTAN_PUBLIC_API(2, 0) AttributeContainer {
9293
* @param attribute attribute type
9394
* @param value numeric value to add
9495
*/
95-
template <typename T>
96-
requires std::is_integral_v<T>
96+
template <std::integral T>
9797
void add_numeric(AttributeType attribute, T value) {
9898
m_numerics.push_back(static_cast<uint64_t>(value));
9999
add_attribute(attribute, reinterpret_cast<uint8_t*>(&m_numerics.back()), sizeof(T));

src/lib/pubkey/xmss/xmss_tools.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace Botan {
2424
* integral value to.
2525
* @param src integral value to concatenate.
2626
**/
27-
template <std::integral T>
27+
template <std::unsigned_integral T>
2828
void xmss_concat(secure_vector<uint8_t>& target, const T& src) {
2929
const uint8_t* src_bytes = reinterpret_cast<const uint8_t*>(&src);
3030
if constexpr(std::endian::native == std::endian::little) {
@@ -44,7 +44,7 @@ void xmss_concat(secure_vector<uint8_t>& target, const T& src) {
4444
* @param len number of bytes to concatenate. This value must be smaller
4545
* or equal to the size of type T.
4646
**/
47-
template <std::integral T>
47+
template <std::unsigned_integral T>
4848
void xmss_concat(secure_vector<uint8_t>& target, const T& src, size_t len) {
4949
size_t c = static_cast<size_t>(std::min(len, sizeof(src)));
5050
if(len > sizeof(src)) {

src/lib/utils/bitvector/bitvector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ class bitvector_base final {
283283

284284
constexpr bool is_set() const noexcept { return (m_block & m_mask) > 0; }
285285

286-
template <std::integral T>
286+
template <std::unsigned_integral T>
287287
constexpr T as() const noexcept {
288288
return static_cast<T>(is_set());
289289
}

src/lib/utils/ct_utils.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,9 @@ template <unpoisonable T>
272272
* constant time code and which does not support GCC-style inline asm.
273273
*
274274
*/
275-
template <typename T>
276-
constexpr inline T value_barrier(T x)
277-
requires std::unsigned_integral<T> && (!std::same_as<bool, T>)
278-
{
275+
template <std::unsigned_integral T>
276+
requires(!std::same_as<bool, T>)
277+
constexpr inline T value_barrier(T x) {
279278
if(std::is_constant_evaluated()) {
280279
return x;
281280
} else {

src/lib/utils/donna128.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <botan/internal/ct_utils.h>
1212
#include <botan/internal/mul128.h>
13+
#include <concepts>
1314
#include <type_traits>
1415

1516
namespace Botan {
@@ -21,7 +22,7 @@ class donna128 final {
2122
h = hh;
2223
}
2324

24-
template <typename T>
25+
template <std::unsigned_integral T>
2526
constexpr friend donna128 operator>>(const donna128& x, T shift) {
2627
donna128 z = x;
2728

@@ -41,7 +42,7 @@ class donna128 final {
4142
return z;
4243
}
4344

44-
template <typename T>
45+
template <std::unsigned_integral T>
4546
constexpr friend donna128 operator<<(const donna128& x, T shift) {
4647
donna128 z = x;
4748
if(shift > 64) {

src/lib/utils/prefetch.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define BOTAN_PREFETCH_UTILS_H_
99

1010
#include <botan/types.h>
11+
#include <concepts>
1112
#include <type_traits>
1213

1314
namespace Botan {
@@ -30,10 +31,8 @@ uint64_t prefetch_array_raw(size_t bytes, const void* array) noexcept;
3031
* to not elide otherwise "useless" reads. The return value will always
3132
* be zero.
3233
*/
33-
template <typename T, size_t... Ns>
34-
T prefetch_arrays(T (&... arr)[Ns]) noexcept
35-
requires std::is_integral_v<T>
36-
{
34+
template <std::unsigned_integral T, size_t... Ns>
35+
T prefetch_arrays(T (&... arr)[Ns]) noexcept {
3736
return (static_cast<T>(prefetch_array_raw(sizeof(T) * Ns, arr)) & ...);
3837
}
3938

0 commit comments

Comments
 (0)