36 std::vector<FF> next_packed_pc_min_pc_inverses = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
37 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
38 FF::batch_invert(next_packed_pc_min_pc_inverses);
43 for (
const auto&
event : events) {
44 const auto& bytecode = *
event.bytecode;
45 const auto id =
event.bytecode_id;
46 auto bytecode_at = [&bytecode](
size_t i) -> uint8_t {
return i < bytecode.size() ? bytecode[i] : 0; };
47 const uint32_t bytecode_len =
static_cast<uint32_t
>(bytecode.size());
49 for (uint32_t i = 0; i < bytecode_len; i++) {
50 const uint32_t remaining = bytecode_len - i;
52 const bool is_last = remaining == 1;
61 { C::bc_decomposition_sel, 1 },
62 { C::bc_decomposition_id,
id },
63 { C::bc_decomposition_pc, i },
64 { C::bc_decomposition_last_of_contract, is_last ? 1 : 0 },
65 { C::bc_decomposition_bytes_remaining, remaining },
66 { C::bc_decomposition_bytes_to_read, bytes_to_read },
68 { C::bc_decomposition_is_windows_eq_remaining, is_windows_eq_remaining ? 1 : 0 },
70 { C::bc_decomposition_bytes_rem_inv, remaining },
71 { C::bc_decomposition_bytes_rem_min_one_inv, is_last ? 0 :
FF(remaining - 1) },
72 { C::bc_decomposition_windows_min_remaining_inv,
75 { C::bc_decomposition_bytes, bytecode_at(i) },
76 { C::bc_decomposition_bytes_pc_plus_1, bytecode_at(i + 1) },
77 { C::bc_decomposition_bytes_pc_plus_2, bytecode_at(i + 2) },
78 { C::bc_decomposition_bytes_pc_plus_3, bytecode_at(i + 3) },
79 { C::bc_decomposition_bytes_pc_plus_4, bytecode_at(i + 4) },
80 { C::bc_decomposition_bytes_pc_plus_5, bytecode_at(i + 5) },
81 { C::bc_decomposition_bytes_pc_plus_6, bytecode_at(i + 6) },
82 { C::bc_decomposition_bytes_pc_plus_7, bytecode_at(i + 7) },
83 { C::bc_decomposition_bytes_pc_plus_8, bytecode_at(i + 8) },
84 { C::bc_decomposition_bytes_pc_plus_9, bytecode_at(i + 9) },
85 { C::bc_decomposition_bytes_pc_plus_10, bytecode_at(i + 10) },
86 { C::bc_decomposition_bytes_pc_plus_11, bytecode_at(i + 11) },
87 { C::bc_decomposition_bytes_pc_plus_12, bytecode_at(i + 12) },
88 { C::bc_decomposition_bytes_pc_plus_13, bytecode_at(i + 13) },
89 { C::bc_decomposition_bytes_pc_plus_14, bytecode_at(i + 14) },
90 { C::bc_decomposition_bytes_pc_plus_15, bytecode_at(i + 15) },
91 { C::bc_decomposition_bytes_pc_plus_16, bytecode_at(i + 16) },
92 { C::bc_decomposition_bytes_pc_plus_17, bytecode_at(i + 17) },
93 { C::bc_decomposition_bytes_pc_plus_18, bytecode_at(i + 18) },
94 { C::bc_decomposition_bytes_pc_plus_19, bytecode_at(i + 19) },
95 { C::bc_decomposition_bytes_pc_plus_20, bytecode_at(i + 20) },
96 { C::bc_decomposition_bytes_pc_plus_21, bytecode_at(i + 21) },
97 { C::bc_decomposition_bytes_pc_plus_22, bytecode_at(i + 22) },
98 { C::bc_decomposition_bytes_pc_plus_23, bytecode_at(i + 23) },
99 { C::bc_decomposition_bytes_pc_plus_24, bytecode_at(i + 24) },
100 { C::bc_decomposition_bytes_pc_plus_25, bytecode_at(i + 25) },
101 { C::bc_decomposition_bytes_pc_plus_26, bytecode_at(i + 26) },
102 { C::bc_decomposition_bytes_pc_plus_27, bytecode_at(i + 27) },
103 { C::bc_decomposition_bytes_pc_plus_28, bytecode_at(i + 28) },
104 { C::bc_decomposition_bytes_pc_plus_29, bytecode_at(i + 29) },
105 { C::bc_decomposition_bytes_pc_plus_30, bytecode_at(i + 30) },
106 { C::bc_decomposition_bytes_pc_plus_31, bytecode_at(i + 31) },
107 { C::bc_decomposition_bytes_pc_plus_32, bytecode_at(i + 32) },
108 { C::bc_decomposition_bytes_pc_plus_33, bytecode_at(i + 33) },
109 { C::bc_decomposition_bytes_pc_plus_34, bytecode_at(i + 34) },
110 { C::bc_decomposition_bytes_pc_plus_35, bytecode_at(i + 35) },
111 { C::bc_decomposition_bytes_pc_plus_36, bytecode_at(i + 36) },
116 auto bytecode_field_at = [&](
size_t i) ->
FF {
119 if (bytecode_len - i >= 32) {
120 as_int = from_buffer<uint256_t>(bytecode, i);
122 std::vector<uint8_t> tail(bytecode.begin() +
static_cast<ssize_t
>(i), bytecode.end());
124 as_int = from_buffer<uint256_t>(tail, 0);
128 for (uint32_t i = 0; i < bytecode_len; i += 31) {
131 { C::bc_decomposition_sel_packed, 1 },
132 { C::bc_decomposition_packed_field, bytecode_field_at(i) },
133 { C::bc_decomposition_next_packed_pc, i },
134 { C::bc_decomposition_next_packed_pc_min_pc_inv, 0 },
136 for (uint32_t j = i + 1; j < std::min(bytecode_len, i + 31); j++) {
140 { C::bc_decomposition_next_packed_pc, i + 31 },
141 { C::bc_decomposition_next_packed_pc_min_pc_inv, next_packed_pc_min_pc_inverses[i + 31 - j] },
151 trace.invert_columns({ { C::bc_decomposition_bytes_rem_inv,
152 C::bc_decomposition_bytes_rem_min_one_inv,
153 C::bc_decomposition_windows_min_remaining_inv } });
162 for (
const auto&
event : events) {
163 const auto id =
event.bytecode_id;
166 fields.insert(fields.end(),
event.bytecode_fields.begin(),
event.bytecode_fields.end());
167 auto bytecode_field_at = [&fields](
size_t i) ->
FF {
return i < fields.size() ? fields[i] : 0; };
169 auto padding_amount = (3 - (fields.size() % 3)) % 3;
170 auto num_rounds = (fields.size() + padding_amount) / 3;
171 uint32_t pc_index = 0;
172 for (uint32_t i = 0; i < fields.size(); i += 3) {
173 bool start_of_bytecode = i == 0;
174 bool end_of_bytecode = i + 3 >= fields.size();
177 uint32_t pc_index_1 = start_of_bytecode ? 0 : pc_index + 31;
179 { { { C::bc_hashing_sel, 1 },
180 { C::bc_hashing_start, start_of_bytecode },
181 { C::bc_hashing_sel_not_start, !start_of_bytecode },
182 { C::bc_hashing_latch, end_of_bytecode },
183 { C::bc_hashing_bytecode_id,
id },
184 { C::bc_hashing_input_len, fields.size() },
185 { C::bc_hashing_rounds_rem, num_rounds },
186 { C::bc_hashing_pc_index, pc_index },
187 { C::bc_hashing_pc_index_1, pc_index_1 },
188 { C::bc_hashing_pc_index_2, pc_index_1 + 31 },
189 { C::bc_hashing_packed_fields_0, bytecode_field_at(i) },
190 { C::bc_hashing_packed_fields_1, bytecode_field_at(i + 1) },
191 { C::bc_hashing_packed_fields_2, bytecode_field_at(i + 2) },
192 { C::bc_hashing_sel_not_padding_1, end_of_bytecode && padding_amount == 2 ? 0 : 1 },
193 { C::bc_hashing_sel_not_padding_2, end_of_bytecode && padding_amount > 0 ? 0 : 1 },
194 { C::bc_hashing_output_hash, output_hash } } });
195 if (end_of_bytecode) {
201 { C::bc_hashing_pc_at_final_field,
202 padding_amount == 2 ? pc_index : pc_index_1 + (31 * (1 - padding_amount)) },
205 pc_index = pc_index_1 + 62;
219 for (
const auto&
event : events) {
222 event.retrieved_bytecodes_snapshot_before.nextAvailableLeafIndex;
223 bool error =
event.instance_not_found_error ||
event.limit_error;
227 { C::bc_retrieval_sel, 1 },
228 { C::bc_retrieval_bytecode_id,
event.bytecode_id },
229 { C::bc_retrieval_address,
event.address },
230 { C::bc_retrieval_error, error },
233 { C::bc_retrieval_current_class_id,
event.current_class_id },
236 { C::bc_retrieval_public_data_tree_root,
event.public_data_tree_root },
237 { C::bc_retrieval_nullifier_tree_root,
event.nullifier_root },
240 { C::bc_retrieval_prev_retrieved_bytecodes_tree_root,
event.retrieved_bytecodes_snapshot_before.root },
241 { C::bc_retrieval_prev_retrieved_bytecodes_tree_size,
242 event.retrieved_bytecodes_snapshot_before.nextAvailableLeafIndex },
243 { C::bc_retrieval_next_retrieved_bytecodes_tree_root,
event.retrieved_bytecodes_snapshot_after.root },
244 { C::bc_retrieval_next_retrieved_bytecodes_tree_size,
245 event.retrieved_bytecodes_snapshot_after.nextAvailableLeafIndex },
248 { C::bc_retrieval_instance_exists, !
event.instance_not_found_error },
251 { C::bc_retrieval_no_remaining_bytecodes, remaining_bytecodes == 0 },
252 { C::bc_retrieval_remaining_bytecodes_inv, remaining_bytecodes },
253 { C::bc_retrieval_is_new_class,
event.is_new_class },
254 { C::bc_retrieval_should_retrieve, !error },
257 { C::bc_retrieval_artifact_hash,
event.contract_class.artifact_hash },
258 { C::bc_retrieval_private_function_root,
event.contract_class.private_function_root },
265 trace.invert_columns({ { C::bc_retrieval_remaining_bytecodes_inv } });
282 for (
const auto&
event : events) {
284 const auto bytecode_size =
event.bytecode->size();
286 auto get_operand = [&](
size_t i) ->
FF {
287 return i <
event.instruction.operands.size() ?
static_cast<FF>(
event.instruction.operands[i]) : 0;
289 auto bytecode_at = [&](
size_t i) -> uint8_t {
return i < bytecode_size ? (*
event.bytecode)[i] : 0; };
291 const uint8_t wire_opcode = bytecode_at(
event.pc);
292 const bool wire_opcode_in_range =
295 uint32_t size_in_bytes = 0;
297 std::array<uint8_t, NUM_OP_DC_SELECTORS> op_dc_selectors{};
299 uint8_t tag_is_op2 = 0;
300 uint8_t tag_value = 0;
302 if (wire_opcode_in_range) {
304 size_in_bytes = wire_instr_spec.size_in_bytes;
305 exec_opcode = wire_instr_spec.exec_opcode;
306 op_dc_selectors = wire_instr_spec.op_dc_selectors;
308 if (wire_instr_spec.tag_operand_idx.has_value()) {
309 const auto tag_value_idx = wire_instr_spec.tag_operand_idx.value();
310 assert((tag_value_idx == 2 || tag_value_idx == 3) &&
311 "Current constraints support only tag for operand index equal to 2 or 3");
314 if (tag_value_idx == 2) {
316 tag_value =
static_cast<uint8_t
>(get_operand(1));
318 tag_value =
static_cast<uint8_t
>(get_operand(2));
323 const uint32_t bytes_remaining =
324 event.error ==
PC_OUT_OF_RANGE ? 0 :
static_cast<uint32_t
>(bytecode_size -
event.pc);
327 uint32_t instr_abs_diff = 0;
328 if (size_in_bytes <= bytes_to_read) {
329 instr_abs_diff = bytes_to_read - size_in_bytes;
331 instr_abs_diff = size_in_bytes - bytes_to_read - 1;
334 uint32_t bytecode_size_u32 =
static_cast<uint32_t
>(bytecode_size);
335 uint32_t pc_abs_diff =
336 bytecode_size_u32 >
event.pc ? bytecode_size_u32 -
event.pc - 1 :
event.pc - bytecode_size_u32;
340 { C::instr_fetching_sel, 1 },
341 { C::instr_fetching_bytecode_id, bytecode_id },
342 { C::instr_fetching_pc,
event.pc },
344 { C::instr_fetching_indirect,
event.instruction.indirect },
345 { C::instr_fetching_op1, get_operand(0) },
346 { C::instr_fetching_op2, get_operand(1) },
347 { C::instr_fetching_op3, get_operand(2) },
348 { C::instr_fetching_op4, get_operand(3) },
349 { C::instr_fetching_op5, get_operand(4) },
350 { C::instr_fetching_op6, get_operand(5) },
351 { C::instr_fetching_op7, get_operand(6) },
353 { C::instr_fetching_bd0, wire_opcode },
354 { C::instr_fetching_bd1, bytecode_at(
event.pc + 1) },
355 { C::instr_fetching_bd2, bytecode_at(
event.pc + 2) },
356 { C::instr_fetching_bd3, bytecode_at(
event.pc + 3) },
357 { C::instr_fetching_bd4, bytecode_at(
event.pc + 4) },
358 { C::instr_fetching_bd5, bytecode_at(
event.pc + 5) },
359 { C::instr_fetching_bd6, bytecode_at(
event.pc + 6) },
360 { C::instr_fetching_bd7, bytecode_at(
event.pc + 7) },
361 { C::instr_fetching_bd8, bytecode_at(
event.pc + 8) },
362 { C::instr_fetching_bd9, bytecode_at(
event.pc + 9) },
363 { C::instr_fetching_bd10, bytecode_at(
event.pc + 10) },
364 { C::instr_fetching_bd11, bytecode_at(
event.pc + 11) },
365 { C::instr_fetching_bd12, bytecode_at(
event.pc + 12) },
366 { C::instr_fetching_bd13, bytecode_at(
event.pc + 13) },
367 { C::instr_fetching_bd14, bytecode_at(
event.pc + 14) },
368 { C::instr_fetching_bd15, bytecode_at(
event.pc + 15) },
369 { C::instr_fetching_bd16, bytecode_at(
event.pc + 16) },
370 { C::instr_fetching_bd17, bytecode_at(
event.pc + 17) },
371 { C::instr_fetching_bd18, bytecode_at(
event.pc + 18) },
372 { C::instr_fetching_bd19, bytecode_at(
event.pc + 19) },
373 { C::instr_fetching_bd20, bytecode_at(
event.pc + 20) },
374 { C::instr_fetching_bd21, bytecode_at(
event.pc + 21) },
375 { C::instr_fetching_bd22, bytecode_at(
event.pc + 22) },
376 { C::instr_fetching_bd23, bytecode_at(
event.pc + 23) },
377 { C::instr_fetching_bd24, bytecode_at(
event.pc + 24) },
378 { C::instr_fetching_bd25, bytecode_at(
event.pc + 25) },
379 { C::instr_fetching_bd26, bytecode_at(
event.pc + 26) },
380 { C::instr_fetching_bd27, bytecode_at(
event.pc + 27) },
381 { C::instr_fetching_bd28, bytecode_at(
event.pc + 28) },
382 { C::instr_fetching_bd29, bytecode_at(
event.pc + 29) },
383 { C::instr_fetching_bd30, bytecode_at(
event.pc + 30) },
384 { C::instr_fetching_bd31, bytecode_at(
event.pc + 31) },
385 { C::instr_fetching_bd32, bytecode_at(
event.pc + 32) },
386 { C::instr_fetching_bd33, bytecode_at(
event.pc + 33) },
387 { C::instr_fetching_bd34, bytecode_at(
event.pc + 34) },
388 { C::instr_fetching_bd35, bytecode_at(
event.pc + 35) },
389 { C::instr_fetching_bd36, bytecode_at(
event.pc + 36) },
392 { C::instr_fetching_exec_opcode,
static_cast<uint32_t
>(exec_opcode) },
393 { C::instr_fetching_instr_size, size_in_bytes },
394 { C::instr_fetching_sel_has_tag, has_tag },
395 { C::instr_fetching_sel_tag_is_op2, tag_is_op2 },
398 { C::instr_fetching_sel_op_dc_0, op_dc_selectors.at(0) },
399 { C::instr_fetching_sel_op_dc_1, op_dc_selectors.at(1) },
400 { C::instr_fetching_sel_op_dc_2, op_dc_selectors.at(2) },
401 { C::instr_fetching_sel_op_dc_3, op_dc_selectors.at(3) },
402 { C::instr_fetching_sel_op_dc_4, op_dc_selectors.at(4) },
403 { C::instr_fetching_sel_op_dc_5, op_dc_selectors.at(5) },
404 { C::instr_fetching_sel_op_dc_6, op_dc_selectors.at(6) },
405 { C::instr_fetching_sel_op_dc_7, op_dc_selectors.at(7) },
406 { C::instr_fetching_sel_op_dc_8, op_dc_selectors.at(8) },
407 { C::instr_fetching_sel_op_dc_9, op_dc_selectors.at(9) },
408 { C::instr_fetching_sel_op_dc_10, op_dc_selectors.at(10) },
409 { C::instr_fetching_sel_op_dc_11, op_dc_selectors.at(11) },
410 { C::instr_fetching_sel_op_dc_12, op_dc_selectors.at(12) },
411 { C::instr_fetching_sel_op_dc_13, op_dc_selectors.at(13) },
412 { C::instr_fetching_sel_op_dc_14, op_dc_selectors.at(14) },
413 { C::instr_fetching_sel_op_dc_15, op_dc_selectors.at(15) },
414 { C::instr_fetching_sel_op_dc_16, op_dc_selectors.at(16) },
417 { C::instr_fetching_pc_out_of_range,
event.error ==
PC_OUT_OF_RANGE ? 1 : 0 },
420 { C::instr_fetching_tag_out_of_range,
event.error ==
TAG_OUT_OF_RANGE ? 1 : 0 },
421 { C::instr_fetching_sel_parsing_err,
event.error.has_value() ? 1 : 0 },
424 { C::instr_fetching_sel_pc_in_range,
event.error !=
PC_OUT_OF_RANGE ? 1 : 0 },
426 { C::instr_fetching_bytecode_size, bytecode_size },
427 { C::instr_fetching_bytes_to_read, bytes_to_read },
428 { C::instr_fetching_instr_abs_diff, instr_abs_diff },
429 { C::instr_fetching_pc_abs_diff, pc_abs_diff },
430 { C::instr_fetching_pc_size_in_bits,
432 { C::instr_fetching_tag_value, tag_value },