Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
translator.fuzzer.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
17
18using namespace bb;
19
23
24// Read uint256_t from raw bytes.
25// Don't use dereference casts, since the data may be not aligned and it causes segfault
26uint256_t read_uint256(const uint8_t* data, size_t buffer_size = 32)
27{
28 BB_ASSERT_LTE(buffer_size, 32U);
29
30 uint64_t parts[4] = { 0, 0, 0, 0 };
31
32 for (size_t i = 0; i < (buffer_size + 7) / 8; i++) {
33 size_t to_read = (buffer_size - i * 8) < 8 ? buffer_size - i * 8 : 8;
34 std::memcpy(&parts[i], data + i * 8, to_read);
35 }
36 return uint256_t(parts[0], parts[1], parts[2], parts[3]);
37}
38
46std::vector<ECCVMOperation> parse_operations(const unsigned char* data, size_t size)
47{
48 std::vector<ECCVMOperation> eccvm_ops;
49
50 size_t size_left = size;
51 // Just iterate and parse until there's no data left
52 while (size_left >= sizeof(ECCVMOperation)) {
54 std::memcpy(static_cast<void*>(&op), data + (size - size_left), sizeof(ECCVMOperation));
55 eccvm_ops.emplace_back(op);
56 size_left -= sizeof(ECCVMOperation);
57 }
58 return eccvm_ops;
59}
60
69 size_t size)
70{
71 std::vector<ECCVMOperation> eccvm_ops;
72
73 // Try to parse batching challenge
74 size_t size_left = size;
75 if (size_left < sizeof(uint256_t)) {
76 return {};
77 }
78 const auto batching_challenge = Fq(read_uint256(data));
79
80 // Try to parse evaluation challenge
81 size_left -= sizeof(uint256_t);
82 if (size_left < sizeof(uint256_t)) {
83 return {};
84 }
85 const auto x = Fq(read_uint256(data));
86 if (x.is_zero()) {
87 return {};
88 }
89 size_left -= sizeof(uint256_t);
90
91 // Try to parse operations
92 eccvm_ops = parse_operations(data + (size - size_left), size_left);
93 if (eccvm_ops.empty()) {
94 return {};
95 }
96
97 // Add a padding element to avoid non-zero commitments
98 const auto p_x = uint256_t(0xd3c208c16d87cfd3, 0xd97816a916871ca8, 0x9b85045b68181585, 0x30644e72e131a02);
99 const auto p_y = uint256_t(0x3ce1cc9c7e645a83, 0x2edac647851e3ac5, 0xd0cbe61fced2bc53, 0x1a76dae6d3272396);
100 auto padding_element = G1(p_x, p_y);
101 auto padding_scalar = -Fr::one();
102 auto ecc_op_queue = std::make_shared<ECCOpQueue>();
103 ecc_op_queue->set_eccvm_ops_for_fuzzing(eccvm_ops);
104 ecc_op_queue->mul_accumulate(padding_element, padding_scalar);
105
106 // Return the batching challenge, evaluation challenge and the constructed queue
107 return std::make_tuple(batching_challenge, x, ecc_op_queue);
108}
#define BB_ASSERT_LTE(left, right,...)
Definition assert.hpp:129
bb::fq BaseField
Definition bn254.hpp:19
typename Group::affine_element AffineElement
Definition bn254.hpp:22
bb::fr ScalarField
Definition bn254.hpp:18
const std::vector< FF > data
Entry point for Barretenberg command-line interface.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
Curve::AffineElement G1
static constexpr field one()
curve::BN254::BaseField Fq
std::optional< std::tuple< Fq, Fq, std::shared_ptr< ECCOpQueue > > > parse_and_construct_opqueue(const unsigned char *data, size_t size)
Try to parse out the batching and evaluating challenges and then the ECCOpQueue from the data.
uint256_t read_uint256(const uint8_t *data, size_t buffer_size=32)
std::vector< ECCVMOperation > parse_operations(const unsigned char *data, size_t size)
Parse raw operations for ECCOpQueue from the data stream.
curve::BN254::AffineElement G1