Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
range_check.fuzzer.cpp
Go to the documentation of this file.
1#include <cassert>
2#include <cstdint>
3#include <fuzzer/FuzzedDataProvider.h>
4
16
17using namespace bb::avm2::simulation;
18using namespace bb::avm2::tracegen;
19using namespace bb::avm2::constraining;
20
21using bb::avm2::FF;
22
24
25// We initialize it here once so it can be shared to other threads.
26// We don't use LLVMFuzzerInitialize since (IIUC) it is not thread safe and we want to run this
27// with multiple worker threads.
28static const TestTraceContainer precomputed_trace = []() {
35 return t;
36}();
37
38// Each worker thread gets its own trace, initialized from precomputed_trace
39thread_local static TestTraceContainer trace = precomputed_trace;
40
41extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
42{
43 // We need at least 17 bytes: 16 bytes for uint128_t value + 1 byte for num_bits
44 size_t minimum_size = 17;
45
46 if (size < minimum_size) {
47 return 0;
48 }
49
50 // Fuzzed Data Provider helps with extracting typed data from the raw byte stream.
51 FuzzedDataProvider fuzzed_data(data, size);
52
53 // Read a uint128_t value (16 bytes)
54 std::array<uint8_t, 16> value_bytes;
55 for (size_t i = 0; i < 16; i++) {
56 value_bytes[i] = fuzzed_data.ConsumeIntegral<uint8_t>();
57 }
58 uint128_t value = 0;
59 for (size_t i = 0; i < 16; i++) {
60 value |= (static_cast<uint128_t>(value_bytes[i]) << (i * 8));
61 }
62
63 // Read num_bits (1-128)
64 uint8_t num_bits = fuzzed_data.ConsumeIntegralInRange<uint8_t>(1, 128);
65
66 // Truncate value to fit within num_bits
67 if (num_bits < 128) {
68 uint128_t mask = (uint128_t(1) << num_bits) - 1;
69 value = value & mask;
70 }
71
72 // Set up gadget and event emitter
75
76 // Execute the range check operation
77 try {
78 // info("Asserting range for value: ", value, " with num_bits: ", static_cast<int>(num_bits));
79 range_check.assert_range(value, num_bits);
80 } catch (const std::exception& e) {
81 // If any exception occurs, we cannot proceed further.
82 return 0;
83 }
84
85 // Process the events to build the trace (using the thread-local trace)
88
89 if (getenv("AVM_DEBUG") != nullptr) {
90 info("Debugging trace:");
91 bb::avm2::InteractiveDebugger debugger(trace);
92 debugger.run();
93 }
94
95 // Check the relation
96 check_relation<range_check_rel>(trace);
97 check_all_interactions<RangeCheckTraceBuilder>(trace);
98
99 return 0;
100}
DeduplicatingEventEmitter< RangeCheckEvent > range_check_emitter
void run(uint32_t starting_row=0)
Definition debugger.cpp:76
void process_misc(TraceContainer &trace, const uint32_t num_rows=MAX_AVM_TRACE_SIZE)
void process(const simulation::EventEmitterInterface< simulation::RangeCheckEvent >::Container &events, TraceContainer &trace)
void info(Args... args)
Definition log.hpp:75
RangeCheckTraceBuilder range_check_builder
Definition alu.test.cpp:121
PrecomputedTraceBuilder precomputed_builder
Definition alu.test.cpp:120
const std::vector< MemoryValue > data
AvmFlavorSettings::FF FF
Definition field.hpp:10
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
unsigned __int128 uint128_t
Definition serialize.hpp:44