-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_utils.hpp
53 lines (43 loc) · 1.16 KB
/
test_utils.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#pragma once
#include <cassert>
#include <charconv>
#include <random>
#include <span>
#include <type_traits>
#include <vector>
namespace sha3_test_utils {
// Generates N -many random values of type T | N >= 0
template<typename T>
static inline void
random_data(std::span<T> data)
requires(std::is_unsigned_v<T>)
{
std::random_device rd;
std::mt19937_64 gen(rd());
std::uniform_int_distribution<T> dis;
const size_t len = data.size();
for (size_t i = 0; i < len; i++) {
data[i] = dis(gen);
}
}
// Given a hex encoded string of length 2*L, this routine can be used for parsing it as a byte array of length L.
//
// Taken from
// https://github.com/itzmeanjan/ascon/blob/603ba1f223ddd3a46cb0b3d31d014312d96792b5/include/utils.hpp#L120-L145
static inline std::vector<uint8_t>
from_hex(std::string_view hex)
{
const size_t hlen = hex.length();
assert(hlen % 2 == 0);
const size_t blen = hlen / 2;
std::vector<uint8_t> res(blen, 0);
for (size_t i = 0; i < blen; i++) {
const size_t off = i * 2;
uint8_t byte = 0;
auto sstr = hex.substr(off, 2);
std::from_chars(sstr.data(), sstr.data() + 2, byte, 16);
res[i] = byte;
}
return res;
}
}