Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
uint_mutations.hpp
Go to the documentation of this file.
1
7
8#pragma once
9
10#include <algorithm>
11#include <array>
12#include <functional>
13#include <random>
14#include <type_traits>
15#include <vector>
16
20
21template <typename T> struct UintTraits {
22 static constexpr bool has_mask = false;
23 static constexpr T mask() { return T(0); }
24};
25
26template <> struct UintTraits<uint8_t> {
27 static constexpr bool has_mask = true;
28 static constexpr uint8_t mask() { return 0xff; }
29};
30
31template <> struct UintTraits<uint16_t> {
32 static constexpr bool has_mask = true;
33 static constexpr uint16_t mask() { return 0xffff; }
34};
35
36template <> struct UintTraits<uint32_t> {
37 static constexpr bool has_mask = true;
38 static constexpr uint32_t mask() { return 0xffffffffUL; }
39};
40
41template <> struct UintTraits<uint64_t> {
42 static constexpr bool has_mask = true;
43 static constexpr uint64_t mask() { return 0xffffffffffffffffULL; }
44};
45
46template <> struct UintTraits<uint128_t> {
47 static constexpr bool has_mask = false;
48};
49
50template <typename T>
56
58{
59 // Generate two random uint64_t values and combine them
60 uint128_t lo = std::uniform_int_distribution<uint64_t>(0, 0xffffffffffffffffULL)(rng);
61 uint128_t hi = std::uniform_int_distribution<uint64_t>(0, 0xffffffffffffffffULL)(rng);
62 return (hi << 64) + lo;
63}
64
65namespace uint_mutation {
66template <typename T> struct RandomSelection {
67 static void mutate(std::mt19937_64& rng, T& value) { value = generate_random_uint<T>(rng); }
68};
69
70template <typename T> struct IncrementBy1 {
71 static void mutate(T& value)
72 {
73 if constexpr (UintTraits<T>::has_mask) {
75 } else {
76 value = value + 1;
77 }
78 }
79};
80
81template <typename T> struct DecrementBy1 {
82 static void mutate(T& value)
83 {
84 if constexpr (UintTraits<T>::has_mask) {
86 } else {
87 value = value - 1;
88 }
89 }
90};
91
92template <typename T> struct AddRandomValue {
93 static void mutate(T& value, std::mt19937_64& rng)
94 {
95 if constexpr (UintTraits<T>::has_mask) {
96 value = (value + generate_random_uint<T>(rng)) & UintTraits<T>::mask();
97 } else {
98 value = value + generate_random_uint<T>(rng);
99 }
100 }
101};
102} // namespace uint_mutation
103
104// Generic mutation function using WeightedSelectionConfig
105template <typename T, typename ConfigType> void mutate_uint(T& value, std::mt19937_64& rng, const ConfigType& config)
106{
107 UintMutationOptions option = config.select(rng);
108
109 switch (option) {
112 break;
115 break;
118 break;
121 break;
122 }
123}
124
126{
127 return generate_random_uint<uint8_t>(rng);
128}
129
131{
132 return generate_random_uint<uint16_t>(rng);
133}
134
136{
137 return generate_random_uint<uint32_t>(rng);
138}
139
141{
142 return generate_random_uint<uint64_t>(rng);
143}
144
UintMutationOptions
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
unsigned __int128 uint128_t
Definition serialize.hpp:44
static constexpr uint16_t mask()
static constexpr uint32_t mask()
static constexpr uint64_t mask()
static constexpr uint8_t mask()
static constexpr bool has_mask
static constexpr T mask()
static void mutate(T &value, std::mt19937_64 &rng)
static void mutate(T &value)
static void mutate(T &value)
static void mutate(std::mt19937_64 &rng, T &value)
uint64_t generate_random_uint64(std::mt19937_64 &rng)
uint128_t generate_random_uint128(std::mt19937_64 &rng)
uint32_t generate_random_uint32(std::mt19937_64 &rng)
void mutate_uint(T &value, std::mt19937_64 &rng, const ConfigType &config)
std::enable_if< std::is_integral< T >::value &&std::is_unsigned< T >::value, T >::type generate_random_uint(std::mt19937_64 &rng)
uint16_t generate_random_uint16(std::mt19937_64 &rng)
uint128_t generate_random_uint< uint128_t >(std::mt19937_64 &rng)
uint8_t generate_random_uint8(std::mt19937_64 &rng)