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
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
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
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));
88template <
typename T>
static inline uint256_t fast_log_distributed_uint256(T& rng)
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);
110 uint64_t parts[4] = { 0, 0, 0, 0 };
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;
116 return uint256_t(parts[0], parts[1], parts[2], parts[3]);
120#ifdef FUZZING_SHOW_INFORMATION
127template <
typename... Args>
inline void debug_log(Args&&... args)
151 std::make_tuple(T::CONSTANT,
164 T::SUBTRACT_WITH_CONSTRAINT,
165 T::DIVIDE_WITH_CONSTRAINTS,
180 std::make_tuple(T::GEN_MUTATION_COUNT_LOG, T::GEN_STRUCTURAL_MUTATION_PROBABILITY)
182 T::GEN_MUTATION_COUNT_LOG <= 7;
191 typename T::ArgSizes;
192 typename T::Instruction;
193 typename T::ExecutionState;
194 typename T::ExecutionHandler;
215template <
typename T,
typename Composer,
typename Context>
228 typename T::InstructionWeights;
229 T::InstructionWeights::_LIMIT;
241template <
typename T,
typename FF>
242inline static FF mutateFieldElement(
FF e, T& rng)
248 bool convert_to_montgomery = (rng.next() & 1);
251#define MONT_CONVERSION_LOCAL \
252 if (convert_to_montgomery) { \
253 value_data = uint256_t(e.to_montgomery_form()); \
255 value_data = uint256_t(e); \
258#define INV_MONT_CONVERSION_LOCAL \
259 if (convert_to_montgomery) { \
260 e = FF(value_data).from_montgomery_form(); \
262 e = FF(value_data); \
267 const size_t choice = rng.next() % 4;
275 }
else if (choice < 3) {
278 if (convert_to_montgomery) {
279 e = e.to_montgomery_form();
281 if (rng.next() & 1) {
282 e +=
FF(rng.next() & 0xff);
284 e -=
FF(rng.next() & 0xff);
286 if (convert_to_montgomery) {
287 e = e.from_montgomery_form();
292 switch (rng.next() % 8) {
321 if (convert_to_montgomery) {
322 e = e.from_montgomery_form();
346 const size_t instructions_count = instructions.size();
347 if (instructions_count <= 2) {
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;
355 std::iter_swap(instructions.begin() +
static_cast<int>(first_element_index),
356 instructions.begin() +
static_cast<int>(second_element_index));
371 const size_t instructions_count = instructions.size();
372 if (instructions_count == 0) {
375 if ((rng.
next() & 1) != 0U) {
376 instructions.erase(instructions.begin() + (rng.
next() % instructions_count));
379 const size_t max_deletion_log =
380 std::min(
static_cast<size_t>(64 - __builtin_clzll(
static_cast<uint64_t
>(instructions_count)) - 1),
383 if (max_deletion_log == 0) {
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));
403 const size_t instructions_count = instructions.size();
404 if (instructions_count == 0) {
408 typename T::Instruction chosen_instruction = instructions[rng.
next() % instructions_count];
410 instructions.begin() + (rng.
next() % (instructions_count + 1)), duplication_size, chosen_instruction);
416 (void)havoc_settings;
417 instructions.insert(instructions.begin() +
static_cast<int>(rng.
next() % (instructions.size() + 1)),
418 T::Instruction::template generateRandom<FastRandom>(rng));
433 const size_t choice = rng.
next() % prob_pool;
458 const size_t instructions_count = instructions.size();
459 if (instructions_count == 0) {
462 const size_t chosen = rng.
next() % instructions_count;
463 instructions[chosen] =
464 T::Instruction::template mutateInstruction<FastRandom>(instructions[chosen], rng, havoc_settings);
473 const size_t mutation_count = 1 << T::HavocConfig::MUTATION_COUNT_LOG;
477 for (
size_t i = 0; i < mutation_count; i++) {
478 uint32_t val = rng.
next();
507 const size_t vecA_size = vecA.size();
508 const size_t vecB_size = vecB.size();
510 if (vecA_size == 0) {
513 if (vecB_size == 0) {
518 const size_t final_result_size = rng.
next() % (vecA_size + vecB_size) + 1;
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;
527 while (current_result_size < final_result_size && (indexA < vecA_size || indexB < vecB_size)) {
529 size_t result_size_left = final_result_size - current_result_size;
531 if (*inIndex < inSize) {
533 size_t inSizeLeft = inSize - *inIndex;
534 size_t maxExtraSize = std::min(result_size_left, inSizeLeft);
535 if (maxExtraSize != 0) {
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)),
541 inIterator +
static_cast<long>((*inIndex) + copySize));
543 *inIndex += copySize;
544 current_result_size += copySize;
548 inIndex = currentlyUsingA ? &indexB : &indexA;
549 inSize = currentlyUsingA ? vecB_size : vecA_size;
550 inIterator = currentlyUsingA ? vecB.begin() : vecA.begin();
551 currentlyUsingA = !currentlyUsingA;
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;
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; \
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; \
590#define PARSE_ALL_OPCODES(...) FOR_EACH(PARSE_OPCODE, __VA_ARGS__)
594 return fuzzingInstructions;
608 uint8_t* pData = Data;
609 size_t size_left = MaxSize;
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); \
621 return MaxSize - size_left; \
627#define WRITE_ALL_OPCODES(...) FOR_EACH(WRITE_OPCODE_IF, __VA_ARGS__)
631 return MaxSize - size_left;
640 template <
typename Composer>
646 typename T::ExecutionState state;
647 Composer composer = Composer();
650 size_t total_instruction_weight = 0;
651 (void)total_instruction_weight;
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)) { \
669 if (T::ExecutionHandler::execute_##name(&composer, state, instruction)) { \
675#define EXECUTE_ALL_OPCODES(...) FOR_EACH(EXECUTE_OPCODE_IF, __VA_ARGS__)
679 bool final_value_check =
true;
682 final_value_check = T::postProcess(&composer, state);
684#ifdef FUZZING_SHOW_INFORMATION
685 if (!final_value_check) {
691#ifndef FUZZING_DISABLE_WARNINGS
693 info(
"circuit should fail");
702 if (!final_value_check) {
731template <
template <
typename>
class Fuzzer,
typename Composer>
739template <
template <
typename>
class Fuzzer, uint64_t Composers>
742 RunWithBuilder<Fuzzer, bb::UltraCircuitBuilder>(Data, Size,
VarianceRNG);
#define BB_ASSERT_LTE(left, right,...)
bb::field< bb::Bn254FrParams > FF
FastRandom VarianceRNG(0)
A templated class containing most of the fuzzing logic for a generic Arithmetic class.
static void mutateInstructionVector(std::vector< typename T::Instruction > &instructions, FastRandom &rng)
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.
static void duplicateInstruction(std::vector< typename T::Instruction > &instructions, FastRandom &rng, HavocSettings &havoc_settings)
Mutator duplicating an instruction.
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.
static void swapTwoInstructions(std::vector< typename T::Instruction > &instructions, FastRandom &rng)
Mutator swapping two instructions together.
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.
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.
static void deleteInstructions(std::vector< typename T::Instruction > &instructions, FastRandom &rng, HavocSettings &havoc_settings)
Mutator, deleting a sequence of instructions.
static void executeInstructions(std::vector< typename T::Instruction > &instructions)
Execute instructions in a loop.
static void insertRandomInstruction(std::vector< typename T::Instruction > &instructions, FastRandom &rng, HavocSettings &havoc_settings)
static void mutateInstructionValue(std::vector< typename T::Instruction > &instructions, FastRandom &rng, HavocSettings &havoc_settings)
Choose a random instruction from the vector and mutate it.
static void mutateInstructionStructure(std::vector< typename T::Instruction > &instructions, FastRandom &rng, HavocSettings &havoc_settings)
Mutator for instruction structure.
Class for quickly deterministically creating new random values. We don't care about distribution much...
FastRandom(uint32_t seed)
void reseed(uint32_t seed)
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
Concept specifying the class used by the fuzzer.
Fuzzer uses only composers with check_circuit function.
Concept for Havoc Configurations.
Concept for forcing ArgumentSizes to be size_t.
This concept is used when we want to limit the number of executions of certain instructions (for exam...
The fuzzer can use a postprocessing function that is specific to the type being fuzzed.
Concept for a simple PRNG which returns a uint32_t when next is called.
const std::vector< MemoryValue > data
StrictMock< MockContext > context
#define INV_MONT_CONVERSION_LOCAL
#define WRITE_ALL_OPCODES(...)
uint256_t read_uint256(const uint8_t *data, size_t buffer_size=32)
size_t LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize)
void debug_log(Args &&... args)
Compile-time debug logging helper.
constexpr void RunWithBuilder(const uint8_t *Data, const size_t Size, FastRandom &VarianceRNG)
#define EXECUTE_ALL_OPCODES(...)
#define PARSE_ALL_OPCODES(...)
#define ALL_POSSIBLE_OPCODES
constexpr bool SHOW_FUZZING_INFO
#define MONT_CONVERSION_LOCAL
constexpr void RunWithBuilders(const uint8_t *Data, const size_t Size, FastRandom &VarianceRNG)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
size_t GEN_VALUE_MUTATION_PROBABILITY
size_t ST_MUT_MAXIMUM_DELETION_LOG
size_t GEN_MUTATION_COUNT_LOG
size_t ST_MUT_MAXIMUM_DUPLICATION_LOG
size_t VAL_MUT_LLVM_MUTATE_PROBABILITY
size_t ST_MUT_DELETION_PROBABILITY
size_t VAL_MUT_SMALL_ADDITION_PROBABILITY
size_t ST_MUT_DUPLICATION_PROBABILITY
size_t VAL_MUT_SPECIAL_VALUE_PROBABILITY
std::vector< size_t > value_mutation_distribution
size_t GEN_STRUCTURAL_MUTATION_PROBABILITY
size_t VAL_MUT_MONTGOMERY_PROBABILITY
size_t VAL_MUT_NON_MONTGOMERY_PROBABILITY
size_t ST_MUT_SWAP_PROBABILITY
size_t GEN_LLVM_POST_MUTATION_PROB
std::vector< size_t > structural_mutation_distribution
size_t ST_MUT_INSERTION_PROBABILITY
static constexpr field get_root_of_unity(size_t subgroup_size) noexcept
static constexpr field one()
static constexpr uint256_t modulus
constexpr std::pair< bool, field > sqrt() const noexcept
Compute square root of the field element.
static constexpr field zero()