Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
fuzzer.hpp
Go to the documentation of this file.
1#pragma once
5
6// NOLINTBEGIN(cppcoreguidelines-macro-usage, google-runtime-int)
7#define PARENS ()
8
9// Rescan macro tokens 256 times
10#define EXPAND(arg) EXPAND1(EXPAND1(EXPAND1(EXPAND1(arg))))
11#define EXPAND1(arg) EXPAND2(EXPAND2(EXPAND2(EXPAND2(arg))))
12#define EXPAND2(arg) EXPAND3(EXPAND3(EXPAND3(EXPAND3(arg))))
13#define EXPAND3(arg) EXPAND4(EXPAND4(EXPAND4(EXPAND4(arg))))
14#define EXPAND4(arg) arg
15
16#define FOR_EACH(macro, ...) __VA_OPT__(EXPAND(FOR_EACH_HELPER(macro, __VA_ARGS__)))
17#define FOR_EACH_HELPER(macro, a1, ...) macro(a1) __VA_OPT__(FOR_EACH_AGAIN PARENS(macro, __VA_ARGS__))
18#define FOR_EACH_AGAIN() FOR_EACH_HELPER
19
20#define ALL_POSSIBLE_OPCODES \
21 CONSTANT, WITNESS, CONSTANT_WITNESS, ADD, SUBTRACT, MULTIPLY, DIVIDE, ADD_TWO, MADD, MULT_MADD, MSUB_DIV, SQR, \
22 ASSERT_EQUAL, ASSERT_NOT_EQUAL, SQR_ADD, ASSERT_EQUAL, ASSERT_NOT_EQUAL, SQR_ADD, SUBTRACT_WITH_CONSTRAINT, \
23 DIVIDE_WITH_CONSTRAINTS, SLICE, ASSERT_ZERO, ASSERT_NOT_ZERO, COND_NEGATE, ADD_MULTI, ASSERT_VALID, \
24 COND_SELECT, DOUBLE, RANDOMSEED, SELECT_IF_ZERO, SELECT_IF_EQ, REVERSE, GET_BIT, SET_BIT, SET, INVERT, AND, \
25 OR, XOR, MODULO, SHL, SHR, ROL, ROR, NOT, BATCH_MUL, COND_ASSIGN, VALIDATE_ON_CURVE
26
28 size_t GEN_LLVM_POST_MUTATION_PROB; // Controls frequency of additional mutation after structural ones
29 size_t GEN_MUTATION_COUNT_LOG; // This is the logarithm of the number of micromutations applied during mutation of a
30 // testcase
31 size_t GEN_STRUCTURAL_MUTATION_PROBABILITY; // The probability of applying a structural mutation
32 // (DELETION/DUPLICATION/INSERTION/SWAP)
33 size_t GEN_VALUE_MUTATION_PROBABILITY; // The probability of applying a value mutation
34 size_t ST_MUT_DELETION_PROBABILITY; // The probability of applying DELETION mutation
35 size_t ST_MUT_DUPLICATION_PROBABILITY; // The probability of applying DUPLICATION mutation
36 size_t ST_MUT_INSERTION_PROBABILITY; // The probability of applying INSERTION mutation
37 size_t ST_MUT_MAXIMUM_DELETION_LOG; // The logarithm of the maximum of deletions
38 size_t ST_MUT_MAXIMUM_DUPLICATION_LOG; // The logarithm of the maximum of duplication
39 size_t ST_MUT_SWAP_PROBABILITY; // The probability of a SWAP mutation
40 size_t VAL_MUT_LLVM_MUTATE_PROBABILITY; // The probablity of using the LLVM mutator on field element value
41 size_t VAL_MUT_MONTGOMERY_PROBABILITY; // The probability of converting to montgomery form before applying value
42 // mutations
43 size_t VAL_MUT_NON_MONTGOMERY_PROBABILITY; // The probability of not converting to montgomery form before applying
44 // value mutations
45 size_t VAL_MUT_SMALL_ADDITION_PROBABILITY; // The probability of performing small additions
46 size_t VAL_MUT_SPECIAL_VALUE_PROBABILITY; // The probability of assigning special values (0,1, p-1, p-2, p-1/2)
47 std::vector<size_t> structural_mutation_distribution; // Holds the values to quickly select a structural mutation
48 // based on chosen probabilities
49 std::vector<size_t> value_mutation_distribution; // Holds the values to quickly select a value mutation based on
50 // chosen probabilities
51};
52#ifdef HAVOC_TESTING
53
54HavocSettings fuzzer_havoc_settings;
55#endif
56// This is an external function in Libfuzzer used internally by custom mutators
57extern "C" size_t LLVMFuzzerMutate(uint8_t* Data, size_t Size, size_t MaxSize);
58
64 uint32_t state;
65
66 public:
67 FastRandom(uint32_t seed) { reseed(seed); }
68 uint32_t next()
69 {
70 state = static_cast<uint32_t>(
71 (static_cast<uint64_t>(state) * static_cast<uint64_t>(363364578) + static_cast<uint64_t>(537)) %
72 static_cast<uint64_t>(3758096939));
73 return state;
74 }
75 void reseed(uint32_t seed)
76 {
77 if (seed == 0) {
78 seed = 1;
79 }
80 state = seed;
81 }
82};
83
84// Sample a uint256_t value from log distribution
85// That is we first sample the bit count in [0..255]
86// And then shrink the random [0..2^255] value
87// This helps to get smaller values more frequently
88template <typename T> static inline uint256_t fast_log_distributed_uint256(T& rng)
89{
90 uint256_t temp;
91 // Generate a random mask_size-bit value
92 // We want to sample from log distribution instead of uniform
93 uint16_t* p = (uint16_t*)&temp;
94 uint8_t mask_size = static_cast<uint8_t>(rng.next() & 0xff);
95 for (size_t i = 0; i < 16; i++) {
96 *(p + i) = static_cast<uint16_t>(rng.next() & 0xffff);
97 }
98 uint256_t mask = (uint256_t(1) << mask_size) - 1;
99 temp &= mask;
100 temp += 1; // I believe we want to avoid lots of infs
101 return temp;
102}
103
104// Read uint256_t from raw bytes.
105// Don't use dereference casts, since the data may be not aligned and it causes segfault
106uint256_t read_uint256(const uint8_t* data, size_t buffer_size = 32)
107{
108 BB_ASSERT_LTE(buffer_size, 32U);
109
110 uint64_t parts[4] = { 0, 0, 0, 0 };
111
112 for (size_t i = 0; i < (buffer_size + 7) / 8; i++) {
113 size_t to_read = (buffer_size - (i * 8)) < 8 ? buffer_size - (i * 8) : 8;
114 std::memcpy(&parts[i], data + (i * 8), to_read);
115 }
116 return uint256_t(parts[0], parts[1], parts[2], parts[3]);
117}
118
119// Convert preprocessor flag to constexpr for cleaner call sites
120#ifdef FUZZING_SHOW_INFORMATION
121constexpr bool SHOW_FUZZING_INFO = true;
122#else
123constexpr bool SHOW_FUZZING_INFO = false;
124#endif
125
127template <typename... Args> inline void debug_log(Args&&... args)
128{
129 if constexpr (SHOW_FUZZING_INFO) {
130 (std::cout << ... << std::forward<Args>(args));
131 }
132}
133
139template <typename T>
140concept SimpleRng = requires(T a) {
141 { a.next() } -> std::convertible_to<uint32_t>;
142};
148template <typename T>
149concept InstructionArgumentSizes = requires {
150 {
151 std::make_tuple(T::CONSTANT,
152 T::WITNESS,
153 T::CONSTANT_WITNESS,
154 T::ADD,
155 T::SUBTRACT,
156 T::MULTIPLY,
157 T::DIVIDE,
158 T::ADD_TWO,
159 T::MADD,
160 T::MULT_MADD,
161 T::MSUB_DIV,
162 T::SQR,
163 T::SQR_ADD,
164 T::SUBTRACT_WITH_CONSTRAINT,
165 T::DIVIDE_WITH_CONSTRAINTS,
166 T::SLICE,
167 T::ASSERT_ZERO,
168 T::ASSERT_NOT_ZERO)
170};
171
177template <typename T>
178concept HavocConfigConstraint = requires {
179 {
180 std::make_tuple(T::GEN_MUTATION_COUNT_LOG, T::GEN_STRUCTURAL_MUTATION_PROBABILITY)
182 T::GEN_MUTATION_COUNT_LOG <= 7;
183};
189template <typename T>
191 typename T::ArgSizes;
192 typename T::Instruction;
193 typename T::ExecutionState;
194 typename T::ExecutionHandler;
196};
197
203template <typename T>
204concept CheckableComposer = requires(T a) {
206};
207
215template <typename T, typename Composer, typename Context>
216concept PostProcessingEnabled = requires(Composer composer, Context context) {
217 { T::postProcess(&composer, context) } -> std::same_as<bool>;
218};
219
226template <typename T>
227concept InstructionWeightsEnabled = requires {
228 typename T::InstructionWeights;
229 T::InstructionWeights::_LIMIT;
230};
231
241template <typename T, typename FF>
242inline static FF mutateFieldElement(FF e, T& rng)
243 requires SimpleRng<T>
244{
245 // With a certain probability, we apply changes to the Montgomery form, rather than the plain form. This
246 // has merit, since the computation is performed in montgomery form and comparisons are often performed
247 // in it, too. Libfuzzer comparison tracing logic can then be enabled in Montgomery form
248 bool convert_to_montgomery = (rng.next() & 1);
249 uint256_t value_data;
250 // Conversion at the start
251#define MONT_CONVERSION_LOCAL \
252 if (convert_to_montgomery) { \
253 value_data = uint256_t(e.to_montgomery_form()); \
254 } else { \
255 value_data = uint256_t(e); \
256 }
257 // Inverse conversion at the end
258#define INV_MONT_CONVERSION_LOCAL \
259 if (convert_to_montgomery) { \
260 e = FF(value_data).from_montgomery_form(); \
261 } else { \
262 e = FF(value_data); \
263 }
264
265 // Pick the last value from the mutation distribution vector
266 // Choose mutation
267 const size_t choice = rng.next() % 4;
268 // 50% probability to use standard mutation
269 if (choice < 2) {
270 // Delegate mutation to libfuzzer (bit/byte mutations, autodictionary, etc)
272 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
273 LLVMFuzzerMutate(reinterpret_cast<uint8_t*>(&value_data), sizeof(uint256_t), sizeof(uint256_t));
275 } else if (choice < 3) { // 25% to use small additions
276
277 // Small addition/subtraction
278 if (convert_to_montgomery) {
279 e = e.to_montgomery_form();
280 }
281 if (rng.next() & 1) {
282 e += FF(rng.next() & 0xff);
283 } else {
284 e -= FF(rng.next() & 0xff);
285 }
286 if (convert_to_montgomery) {
287 e = e.from_montgomery_form();
288 }
289 } else { // 25% to use special values
290
291 // Substitute field element with a special value
292 switch (rng.next() % 8) {
293 case 0:
294 e = FF::zero();
295 break;
296 case 1:
297 e = FF::one();
298 break;
299 case 2:
300 e = -FF::one();
301 break;
302 case 3:
303 e = FF::one().sqrt().second;
304 break;
305 case 4:
306 e = FF::one().sqrt().second.invert();
307 break;
308 case 5:
310 break;
311 case 6:
312 e = FF(2);
313 break;
314 case 7:
315 e = FF((FF::modulus - 1) / 2);
316 break;
317 default:
318 abort();
319 break;
320 }
321 if (convert_to_montgomery) {
322 e = e.from_montgomery_form();
323 }
324 }
325 // Return instruction
326 return e;
327}
328
334template <typename T>
337 private:
345 {
346 const size_t instructions_count = instructions.size();
347 if (instructions_count <= 2) {
348 return;
349 }
350 const size_t first_element_index = rng.next() % instructions_count;
351 size_t second_element_index = rng.next() % instructions_count;
352 if (first_element_index == second_element_index) {
353 second_element_index = (second_element_index + 1) % instructions_count;
354 }
355 std::iter_swap(instructions.begin() + static_cast<int>(first_element_index),
356 instructions.begin() + static_cast<int>(second_element_index));
357 }
358
367 FastRandom& rng,
368 HavocSettings& havoc_settings)
369 {
370
371 const size_t instructions_count = instructions.size();
372 if (instructions_count == 0) {
373 return;
374 }
375 if ((rng.next() & 1) != 0U) {
376 instructions.erase(instructions.begin() + (rng.next() % instructions_count));
377 } else {
378 // We get the logarithm of number of instructions and subtract 1 to delete at most half
379 const size_t max_deletion_log =
380 std::min(static_cast<size_t>(64 - __builtin_clzll(static_cast<uint64_t>(instructions_count)) - 1),
381 havoc_settings.ST_MUT_MAXIMUM_DELETION_LOG);
382
383 if (max_deletion_log == 0) {
384 return;
385 }
386 const size_t deletion_size = 1 << (rng.next() % max_deletion_log);
387 const size_t start = rng.next() % (instructions_count + 1 - deletion_size);
388 instructions.erase(instructions.begin() + static_cast<int>(start),
389 instructions.begin() + static_cast<int>(start + deletion_size));
390 }
391 }
400 FastRandom& rng,
401 HavocSettings& havoc_settings)
402 {
403 const size_t instructions_count = instructions.size();
404 if (instructions_count == 0) {
405 return;
406 }
407 const size_t duplication_size = 1 << (rng.next() % havoc_settings.ST_MUT_MAXIMUM_DUPLICATION_LOG);
408 typename T::Instruction chosen_instruction = instructions[rng.next() % instructions_count];
409 instructions.insert(
410 instructions.begin() + (rng.next() % (instructions_count + 1)), duplication_size, chosen_instruction);
411 }
413 FastRandom& rng,
414 HavocSettings& havoc_settings)
415 {
416 (void)havoc_settings;
417 instructions.insert(instructions.begin() + static_cast<int>(rng.next() % (instructions.size() + 1)),
418 T::Instruction::template generateRandom<FastRandom>(rng));
419 }
428 FastRandom& rng,
429 HavocSettings& havoc_settings)
430 {
431 const size_t structural_mutators_count = havoc_settings.structural_mutation_distribution.size();
432 const size_t prob_pool = havoc_settings.structural_mutation_distribution[structural_mutators_count - 1];
433 const size_t choice = rng.next() % prob_pool;
434 if (choice < havoc_settings.structural_mutation_distribution[0]) {
435 deleteInstructions(instructions, rng, havoc_settings);
436 } else if (choice < havoc_settings.structural_mutation_distribution[1]) {
437
438 duplicateInstruction(instructions, rng, havoc_settings);
439 } else if (choice < havoc_settings.structural_mutation_distribution[2]) {
440 insertRandomInstruction(instructions, rng, havoc_settings);
441 } else {
442
443 swapTwoInstructions(instructions, rng);
444 }
445 }
454 FastRandom& rng,
455 HavocSettings& havoc_settings)
456 {
457
458 const size_t instructions_count = instructions.size();
459 if (instructions_count == 0) {
460 return;
461 }
462 const size_t chosen = rng.next() % instructions_count;
463 instructions[chosen] =
464 T::Instruction::template mutateInstruction<FastRandom>(instructions[chosen], rng, havoc_settings);
465 }
466
468 {
469#ifdef HAVOC_TESTING
470 // If we are testing which havoc settings are best, then we use global parameters
471 const size_t mutation_count = 1 << fuzzer_havoc_settings.GEN_MUTATION_COUNT_LOG;
472#else
473 const size_t mutation_count = 1 << T::HavocConfig::MUTATION_COUNT_LOG;
474 HavocSettings fuzzer_havoc_settings;
475 // FILL the values
476#endif
477 for (size_t i = 0; i < mutation_count; i++) {
478 uint32_t val = rng.next();
479 if ((val % (fuzzer_havoc_settings.GEN_STRUCTURAL_MUTATION_PROBABILITY +
480 fuzzer_havoc_settings.GEN_VALUE_MUTATION_PROBABILITY)) <
481 fuzzer_havoc_settings.GEN_STRUCTURAL_MUTATION_PROBABILITY) {
482 // mutate structure
483 mutateInstructionStructure(instructions, rng, fuzzer_havoc_settings);
484 } else {
485 // mutate a single instruction vector
486
487 mutateInstructionValue(instructions, rng, fuzzer_havoc_settings);
488 }
489 }
490 }
491
492 public:
504 FastRandom& rng)
505 {
506 // Get vector sizes
507 const size_t vecA_size = vecA.size();
508 const size_t vecB_size = vecB.size();
509 // If one of them is empty, just return the other one
510 if (vecA_size == 0) {
511 return vecB;
512 }
513 if (vecB_size == 0) {
514 return vecA;
515 }
517 // Choose the size of th resulting vector
518 const size_t final_result_size = rng.next() % (vecA_size + vecB_size) + 1;
519 size_t indexA = 0;
520 size_t indexB = 0;
521 size_t* inIndex = &indexA;
522 size_t inSize = vecA_size;
523 auto inIterator = vecA.begin();
524 size_t current_result_size = 0;
525 bool currentlyUsingA = true;
526 // What we do is basically pick a sequence from one, follow with a sequence from the other
527 while (current_result_size < final_result_size && (indexA < vecA_size || indexB < vecB_size)) {
528 // Get the size left
529 size_t result_size_left = final_result_size - current_result_size;
530 // If we can still read from this vector
531 if (*inIndex < inSize) {
532 // Get the size left in this vector and in the output vector and pick the lowest
533 size_t inSizeLeft = inSize - *inIndex;
534 size_t maxExtraSize = std::min(result_size_left, inSizeLeft);
535 if (maxExtraSize != 0) {
536 // If not zero, get a random number of elements from input
537 size_t copySize = (rng.next() % maxExtraSize) + 1;
538 result.insert(result.begin() + static_cast<long>(current_result_size),
539 inIterator + static_cast<long>((*inIndex)),
540
541 inIterator + static_cast<long>((*inIndex) + copySize));
542 // Update indexes and sizes
543 *inIndex += copySize;
544 current_result_size += copySize;
545 }
546 }
547 // Switch input vector
548 inIndex = currentlyUsingA ? &indexB : &indexA;
549 inSize = currentlyUsingA ? vecB_size : vecA_size;
550 inIterator = currentlyUsingA ? vecB.begin() : vecA.begin();
551 currentlyUsingA = !currentlyUsingA;
552 }
553 // Return spliced vector
554 return result;
555 }
564 {
565 std::vector<typename T::Instruction> fuzzingInstructions;
566 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
567 auto* pData = const_cast<uint8_t*>(Data);
568 size_t size_left = Size;
569 while (size_left != 0) {
570 uint8_t chosen_operation = *pData;
571 size_left -= 1;
572 pData++;
573 // If the opcode is enabled (exists and arguments' size is not -1), check if it's the right opcode. If it
574 // is, parse it with a designated function
575#define PARSE_OPCODE(name) \
576 if constexpr (requires { T::ArgSizes::name; }) \
577 if constexpr (T::ArgSizes::name != size_t(-1)) { \
578 if (chosen_operation == T::Instruction::OPCODE::name) { \
579 if (size_left < T::ArgSizes::name) { \
580 return fuzzingInstructions; \
581 } \
582 fuzzingInstructions.push_back( \
583 T::Parser::template parseInstructionArgs<T::Instruction::OPCODE::name>(pData)); \
584 size_left -= T::ArgSizes::name; \
585 pData += T::ArgSizes::name; \
586 continue; \
587 } \
588 }
589 // Create handlers for all opcodes that are in ArgsSizes
590#define PARSE_ALL_OPCODES(...) FOR_EACH(PARSE_OPCODE, __VA_ARGS__)
591
593 }
594 return fuzzingInstructions;
595 }
605 uint8_t* Data,
606 size_t MaxSize)
607 {
608 uint8_t* pData = Data;
609 size_t size_left = MaxSize;
610 for (auto& instruction : instructions) {
611 // If the opcode is enabled and it's this opcode, use a designated function to serialize it
612#define WRITE_OPCODE_IF(name) \
613 if constexpr (requires { T::ArgSizes::name; }) \
614 if constexpr (T::ArgSizes::name != (size_t)-1) { \
615 if (instruction.id == T::Instruction::OPCODE::name) { \
616 if (size_left >= (T::ArgSizes::name + 1)) { \
617 T::Parser::template writeInstruction<T::Instruction::OPCODE::name>(instruction, pData); \
618 size_left -= (T::ArgSizes::name + 1); \
619 pData += (T::ArgSizes::name + 1); \
620 } else { \
621 return MaxSize - size_left; \
622 } \
623 continue; \
624 } \
625 }
626 // Create handlers for all opcodes that are in ArgsSizes
627#define WRITE_ALL_OPCODES(...) FOR_EACH(WRITE_OPCODE_IF, __VA_ARGS__)
628
630 }
631 return MaxSize - size_left;
632 }
633
640 template <typename Composer>
641 // TODO(@Rumata888)(from Zac: maybe try to fix? not comfortable refactoring this myself. Issue #1807)
642 // NOLINTNEXTLINE(readability-function-size, google-readability-function-size)
645 {
646 typename T::ExecutionState state;
647 Composer composer = Composer();
648 // This is a global variable, so that the execution handling class could alter it and signal to the input tester
649 circuit_should_fail = false;
650 size_t total_instruction_weight = 0;
651 (void)total_instruction_weight;
652 for (auto& instruction : instructions) {
653 // If instruction enabled and this is it, delegate to the handler
654#define EXECUTE_OPCODE_IF(name) \
655 if constexpr (requires { T::ArgSizes::name; }) \
656 if constexpr (T::ArgSizes::name != size_t(-1)) { \
657 if (instruction.id == T::Instruction::OPCODE::name) { \
658 if constexpr (InstructionWeightsEnabled<T>) { \
659 if (!((total_instruction_weight + T::InstructionWeights::name) > T::InstructionWeights::_LIMIT)) { \
660 total_instruction_weight += T::InstructionWeights::name; \
661 if (T::ExecutionHandler::execute_##name(&composer, state, instruction)) { \
662 return; \
663 } \
664 } else { \
665 return; \
666 } \
667 } else { \
668 \
669 if (T::ExecutionHandler::execute_##name(&composer, state, instruction)) { \
670 return; \
671 } \
672 } \
673 } \
674 }
675#define EXECUTE_ALL_OPCODES(...) FOR_EACH(EXECUTE_OPCODE_IF, __VA_ARGS__)
676
678 }
679 bool final_value_check = true;
680 // If there is a posprocessing function, use it
682 final_value_check = T::postProcess(&composer, state);
683
684#ifdef FUZZING_SHOW_INFORMATION
685 if (!final_value_check) {
686 std::cerr << "Final value check failed" << std::endl;
687 }
688#endif
689 }
690 bool check_result = bb::CircuitChecker::check(composer) && final_value_check;
691#ifndef FUZZING_DISABLE_WARNINGS
693 info("circuit should fail");
694 }
695#endif
696 // If the circuit is correct, but it should fail, abort
697 if (check_result && circuit_should_fail) {
698 abort();
699 }
700 // If the circuit is incorrect, but there's no reason, abort
701 if ((!check_result) && (!circuit_should_fail)) {
702 if (!final_value_check) {
703 std::cerr << "Final value check failed" << std::endl;
704 } else {
705 std::cerr << "Circuit failed" << std::endl;
706 }
707
708 abort();
709 }
710 }
711
720 static size_t MutateInstructionBuffer(uint8_t* Data, size_t Size, size_t MaxSize, FastRandom& rng)
721 {
722 // Parse the vector
724 // Mutate the vector of instructions
725 mutateInstructionVector(instructions, rng);
726 // Serialize the vector of instructions back to buffer
727 return writeInstructionsToBuffer(instructions, Data, MaxSize);
728 }
729};
730
731template <template <typename> class Fuzzer, typename Composer>
732constexpr void RunWithBuilder(const uint8_t* Data, const size_t Size, FastRandom& VarianceRNG)
733{
735 auto instructions = ArithmeticFuzzHelper<Fuzzer<Composer>>::parseDataIntoInstructions(Data, Size);
736 ArithmeticFuzzHelper<Fuzzer<Composer>>::template executeInstructions<Composer>(instructions);
737}
738
739template <template <typename> class Fuzzer, uint64_t Composers>
740constexpr void RunWithBuilders(const uint8_t* Data, const size_t Size, FastRandom& VarianceRNG)
741{
742 RunWithBuilder<Fuzzer, bb::UltraCircuitBuilder>(Data, Size, VarianceRNG);
743}
744
745// NOLINTEND(cppcoreguidelines-macro-usage, google-runtime-int)
#define BB_ASSERT_LTE(left, right,...)
Definition assert.hpp:158
bb::field< bb::Bn254FrParams > FF
Definition field.cpp:24
FastRandom VarianceRNG(0)
bool circuit_should_fail
A templated class containing most of the fuzzing logic for a generic Arithmetic class.
Definition fuzzer.hpp:336
static void mutateInstructionVector(std::vector< typename T::Instruction > &instructions, FastRandom &rng)
Definition fuzzer.hpp:467
static size_t writeInstructionsToBuffer(std::vector< typename T::Instruction > &instructions, uint8_t *Data, size_t MaxSize)
Write instructions into the buffer until there are no instructions left or there is no more space.
Definition fuzzer.hpp:604
static void duplicateInstruction(std::vector< typename T::Instruction > &instructions, FastRandom &rng, HavocSettings &havoc_settings)
Mutator duplicating an instruction.
Definition fuzzer.hpp:399
static std::vector< typename T::Instruction > parseDataIntoInstructions(const uint8_t *Data, size_t Size)
Parses a given data buffer into a vector of instructions for testing the arithmetic.
Definition fuzzer.hpp:563
static void swapTwoInstructions(std::vector< typename T::Instruction > &instructions, FastRandom &rng)
Mutator swapping two instructions together.
Definition fuzzer.hpp:344
static size_t MutateInstructionBuffer(uint8_t *Data, size_t Size, size_t MaxSize, FastRandom &rng)
Interpret the data buffer as a series of arithmetic instructions and mutate it accordingly.
Definition fuzzer.hpp:720
static std::vector< typename T::Instruction > crossoverInstructionVector(const std::vector< typename T::Instruction > &vecA, const std::vector< typename T::Instruction > &vecB, FastRandom &rng)
Splice two instruction vectors into one randomly.
Definition fuzzer.hpp:501
static void deleteInstructions(std::vector< typename T::Instruction > &instructions, FastRandom &rng, HavocSettings &havoc_settings)
Mutator, deleting a sequence of instructions.
Definition fuzzer.hpp:366
static void executeInstructions(std::vector< typename T::Instruction > &instructions)
Execute instructions in a loop.
Definition fuzzer.hpp:643
static void insertRandomInstruction(std::vector< typename T::Instruction > &instructions, FastRandom &rng, HavocSettings &havoc_settings)
Definition fuzzer.hpp:412
static void mutateInstructionValue(std::vector< typename T::Instruction > &instructions, FastRandom &rng, HavocSettings &havoc_settings)
Choose a random instruction from the vector and mutate it.
Definition fuzzer.hpp:453
static void mutateInstructionStructure(std::vector< typename T::Instruction > &instructions, FastRandom &rng, HavocSettings &havoc_settings)
Mutator for instruction structure.
Definition fuzzer.hpp:427
Class for quickly deterministically creating new random values. We don't care about distribution much...
Definition fuzzer.hpp:63
FastRandom(uint32_t seed)
Definition fuzzer.hpp:67
uint32_t state
Definition fuzzer.hpp:64
void reseed(uint32_t seed)
Definition fuzzer.hpp:75
uint32_t next()
Definition fuzzer.hpp:68
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
#define info(...)
Definition log.hpp:93
Concept specifying the class used by the fuzzer.
Definition fuzzer.hpp:190
Fuzzer uses only composers with check_circuit function.
Definition fuzzer.hpp:204
Concept for Havoc Configurations.
Definition fuzzer.hpp:178
Concept for forcing ArgumentSizes to be size_t.
Definition fuzzer.hpp:149
This concept is used when we want to limit the number of executions of certain instructions (for exam...
Definition fuzzer.hpp:227
The fuzzer can use a postprocessing function that is specific to the type being fuzzed.
Definition fuzzer.hpp:216
Concept for a simple PRNG which returns a uint32_t when next is called.
Definition fuzzer.hpp:140
const std::vector< MemoryValue > data
StrictMock< MockContext > context
FF a
#define INV_MONT_CONVERSION_LOCAL
#define WRITE_ALL_OPCODES(...)
uint256_t read_uint256(const uint8_t *data, size_t buffer_size=32)
Definition fuzzer.hpp:106
size_t LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize)
void debug_log(Args &&... args)
Compile-time debug logging helper.
Definition fuzzer.hpp:127
constexpr void RunWithBuilder(const uint8_t *Data, const size_t Size, FastRandom &VarianceRNG)
Definition fuzzer.hpp:732
#define EXECUTE_ALL_OPCODES(...)
#define PARSE_ALL_OPCODES(...)
#define ALL_POSSIBLE_OPCODES
Definition fuzzer.hpp:20
constexpr bool SHOW_FUZZING_INFO
Definition fuzzer.hpp:123
#define MONT_CONVERSION_LOCAL
constexpr void RunWithBuilders(const uint8_t *Data, const size_t Size, FastRandom &VarianceRNG)
Definition fuzzer.hpp:740
Instruction instruction
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
size_t GEN_VALUE_MUTATION_PROBABILITY
Definition fuzzer.hpp:33
size_t ST_MUT_MAXIMUM_DELETION_LOG
Definition fuzzer.hpp:37
size_t GEN_MUTATION_COUNT_LOG
Definition fuzzer.hpp:29
size_t ST_MUT_MAXIMUM_DUPLICATION_LOG
Definition fuzzer.hpp:38
size_t VAL_MUT_LLVM_MUTATE_PROBABILITY
Definition fuzzer.hpp:40
size_t ST_MUT_DELETION_PROBABILITY
Definition fuzzer.hpp:34
size_t VAL_MUT_SMALL_ADDITION_PROBABILITY
Definition fuzzer.hpp:45
size_t ST_MUT_DUPLICATION_PROBABILITY
Definition fuzzer.hpp:35
size_t VAL_MUT_SPECIAL_VALUE_PROBABILITY
Definition fuzzer.hpp:46
std::vector< size_t > value_mutation_distribution
Definition fuzzer.hpp:49
size_t GEN_STRUCTURAL_MUTATION_PROBABILITY
Definition fuzzer.hpp:31
size_t VAL_MUT_MONTGOMERY_PROBABILITY
Definition fuzzer.hpp:41
size_t VAL_MUT_NON_MONTGOMERY_PROBABILITY
Definition fuzzer.hpp:43
size_t ST_MUT_SWAP_PROBABILITY
Definition fuzzer.hpp:39
size_t GEN_LLVM_POST_MUTATION_PROB
Definition fuzzer.hpp:28
std::vector< size_t > structural_mutation_distribution
Definition fuzzer.hpp:47
size_t ST_MUT_INSERTION_PROBABILITY
Definition fuzzer.hpp:36
static constexpr field get_root_of_unity(size_t subgroup_size) noexcept
static constexpr uint256_t modulus
constexpr std::pair< bool, field > sqrt() const noexcept
Compute square root of the field element.