Botan 3.4.0
Crypto and TLS for C&
concepts.h
Go to the documentation of this file.
1/**
2 * Useful concepts that are available throughout the library
3 * (C) 2023 Jack Lloyd
4 * 2023 René Meusel - Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8
9#ifndef BOTAN_CONCEPTS_H_
10#define BOTAN_CONCEPTS_H_
11
12#include <botan/assert.h>
13
14#include <compare>
15#include <concepts>
16#include <cstdint>
17#include <ostream>
18#include <ranges>
19#include <span>
20#include <type_traits>
21
22namespace Botan {
23
24template <typename T, typename Tag, typename... Capabilities>
25class Strong;
26
27template <typename... Ts>
28struct is_strong_type : std::false_type {};
29
30template <typename... Ts>
31struct is_strong_type<Strong<Ts...>> : std::true_type {};
32
33template <typename... Ts>
35
36template <typename T0, typename... Ts>
37struct all_same {
38 static constexpr bool value = (std::is_same_v<T0, Ts> && ...);
39};
40
41template <typename... Ts>
42static constexpr bool all_same_v = all_same<Ts...>::value;
43
44namespace ranges {
45
46/**
47 * Models a std::ranges::contiguous_range that (optionally) restricts its
48 * value_type to ValueT. In other words: a stretch of contiguous memory of
49 * a certain type (optional ValueT).
50 */
51template <typename T, typename ValueT = std::ranges::range_value_t<T>>
52concept contiguous_range = std::ranges::contiguous_range<T> && std::same_as<ValueT, std::ranges::range_value_t<T>>;
53
54/**
55 * Models a std::ranges::contiguous_range that satisfies
56 * std::ranges::output_range with an arbitrary value_type. In other words: a
57 * stretch of contiguous memory of a certain type (optional ValueT) that can be
58 * written to.
59 */
60template <typename T, typename ValueT = std::ranges::range_value_t<T>>
61concept contiguous_output_range = contiguous_range<T, ValueT> && std::ranges::output_range<T, ValueT>;
62
63/**
64 * Models a range that can be turned into a std::span<>. Typically, this is some
65 * form of ranges::contiguous_range.
66 */
67template <typename T>
68concept spanable_range = std::constructible_from<std::span<const std::ranges::range_value_t<T>>, T>;
69
70/**
71 * Models a range that can be turned into a std::span<> with a static extent.
72 * Typically, this is a std::array or a std::span derived from an array.
73 */
74// clang-format off
75template <typename T>
77 decltype(std::span{std::declval<T&>()})::extent != std::dynamic_extent;
78
79// clang-format on
80
81/**
82 * Find the length in bytes of a given contiguous range @p r.
83 */
84inline constexpr size_t size_bytes(spanable_range auto&& r) {
85 return std::span{r}.size_bytes();
86}
87
88/**
89 * Check that a given range @p r has a certain statically-known byte length. If
90 * the range's extent is known at compile time, this is a static check,
91 * otherwise a runtime argument check will be added.
92 *
93 * @throws Invalid_Argument if range @p r has a dynamic extent and does not
94 * feature the expected byte length.
95 */
96template <size_t expected, spanable_range R>
97inline constexpr void assert_exact_byte_length(R&& r) {
98 const std::span s{r};
99 if constexpr(statically_spanable_range<R>) {
100 static_assert(s.size_bytes() == expected, "memory region does not have expected byte lengths");
101 } else {
102 BOTAN_ASSERT(s.size_bytes() == expected, "memory region does not have expected byte lengths");
103 }
104}
105
106/**
107 * Check that a list of ranges (in @p r0 and @p rs) all have the same byte
108 * lengths. If the first range's extent is known at compile time, this will be a
109 * static check for all other ranges whose extents are known at compile time,
110 * otherwise a runtime argument check will be added.
111 *
112 * @throws Invalid_Argument if any range has a dynamic extent and not all
113 * ranges feature the same byte length.
114 */
115template <spanable_range R0, spanable_range... Rs>
116inline constexpr void assert_equal_byte_lengths(R0&& r0, Rs&&... rs)
117 requires(sizeof...(Rs) > 0)
118{
119 const std::span s0{r0};
120
121 if constexpr(statically_spanable_range<R0>) {
122 constexpr size_t expected_size = s0.size_bytes();
123 (assert_exact_byte_length<expected_size>(rs), ...);
124 } else {
125 const size_t expected_size = s0.size_bytes();
126 BOTAN_ARG_CHECK(((std::span<const std::ranges::range_value_t<Rs>>{rs}.size_bytes() == expected_size) && ...),
127 "memory regions don't have equal lengths");
128 }
129}
130
131} // namespace ranges
132
133namespace concepts {
134
135// TODO: C++20 provides concepts like std::ranges::range or ::sized_range
136// but at the time of this writing clang had not caught up on all
137// platforms. E.g. clang 14 on Xcode does not support ranges properly.
138
139template <typename IterT, typename ContainerT>
141 std::same_as<IterT, typename ContainerT::iterator> || std::same_as<IterT, typename ContainerT::const_iterator>;
142
143template <typename PtrT, typename ContainerT>
145 std::same_as<PtrT, typename ContainerT::pointer> || std::same_as<PtrT, typename ContainerT::const_pointer>;
146
147template <typename T>
148concept container = requires(T a) {
149 { a.begin() } -> container_iterator<T>;
150 { a.end() } -> container_iterator<T>;
151 { a.cbegin() } -> container_iterator<T>;
152 { a.cend() } -> container_iterator<T>;
153 { a.size() } -> std::same_as<typename T::size_type>;
154 typename T::value_type;
155 };
156
157template <typename T>
158concept contiguous_container = container<T> && requires(T a) {
159 { a.data() } -> container_pointer<T>;
160 };
161
162template <typename T>
163concept has_empty = requires(T a) {
164 { a.empty() } -> std::same_as<bool>;
165 };
166
167template <typename T>
168concept resizable_container = container<T> && requires(T& c, typename T::size_type s) {
169 T(s);
170 c.resize(s);
171 };
172
173template <typename T>
175 contiguous_container<T> && resizable_container<T> && std::same_as<typename T::value_type, uint8_t>;
176
177template <typename T>
178concept streamable = requires(std::ostream& os, T a) { os << a; };
179
180template <class T>
181concept strong_type = is_strong_type_v<T>;
182
183template <class T>
185
186template <class T>
187concept unsigned_integral_strong_type = strong_type<T> && std::unsigned_integral<typename T::wrapped_type>;
188
189} // namespace concepts
190
191} // namespace Botan
192
193#endif
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:50
FE_25519 T
Definition ge.cpp:34
constexpr void assert_exact_byte_length(R &&r)
Definition concepts.h:97
constexpr size_t size_bytes(spanable_range auto &&r)
Definition concepts.h:84
constexpr void assert_equal_byte_lengths(R0 &&r0, Rs &&... rs)
Definition concepts.h:116
constexpr bool is_strong_type_v
Definition concepts.h:34
static constexpr bool value
Definition concepts.h:38