45 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,
46 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
52 for (
const auto&
event : events) {
53 const auto&
bytecode = *
event.bytecode;
54 const auto id =
event.bytecode_id;
56 const uint32_t bytecode_len =
static_cast<uint32_t
>(
bytecode.size());
58 for (uint32_t i = 0; i < bytecode_len; i++) {
59 const uint32_t remaining = bytecode_len - i;
61 const bool is_last = remaining == 1;
70 { C::bc_decomposition_sel, 1 },
71 { C::bc_decomposition_id,
id },
72 { C::bc_decomposition_pc, i },
73 { C::bc_decomposition_start, i == 0 ? 1 : 0 },
74 { C::bc_decomposition_last_of_contract, is_last ? 1 : 0 },
75 { C::bc_decomposition_bytes_remaining, remaining },
76 { C::bc_decomposition_bytes_to_read, bytes_to_read },
78 { C::bc_decomposition_sel_windows_eq_remaining, is_windows_eq_remaining ? 1 : 0 },
80 { C::bc_decomposition_bytes_rem_inv, remaining },
81 { C::bc_decomposition_bytes_rem_min_one_inv, is_last ? 0 :
FF(remaining - 1) },
82 { C::bc_decomposition_windows_min_remaining_inv,
85 { C::bc_decomposition_bytes, bytecode_at(i) },
86 { C::bc_decomposition_bytes_pc_plus_1, bytecode_at(i + 1) },
87 { C::bc_decomposition_bytes_pc_plus_2, bytecode_at(i + 2) },
88 { C::bc_decomposition_bytes_pc_plus_3, bytecode_at(i + 3) },
89 { C::bc_decomposition_bytes_pc_plus_4, bytecode_at(i + 4) },
90 { C::bc_decomposition_bytes_pc_plus_5, bytecode_at(i + 5) },
91 { C::bc_decomposition_bytes_pc_plus_6, bytecode_at(i + 6) },
92 { C::bc_decomposition_bytes_pc_plus_7, bytecode_at(i + 7) },
93 { C::bc_decomposition_bytes_pc_plus_8, bytecode_at(i + 8) },
94 { C::bc_decomposition_bytes_pc_plus_9, bytecode_at(i + 9) },
95 { C::bc_decomposition_bytes_pc_plus_10, bytecode_at(i + 10) },
96 { C::bc_decomposition_bytes_pc_plus_11, bytecode_at(i + 11) },
97 { C::bc_decomposition_bytes_pc_plus_12, bytecode_at(i + 12) },
98 { C::bc_decomposition_bytes_pc_plus_13, bytecode_at(i + 13) },
99 { C::bc_decomposition_bytes_pc_plus_14, bytecode_at(i + 14) },
100 { C::bc_decomposition_bytes_pc_plus_15, bytecode_at(i + 15) },
101 { C::bc_decomposition_bytes_pc_plus_16, bytecode_at(i + 16) },
102 { C::bc_decomposition_bytes_pc_plus_17, bytecode_at(i + 17) },
103 { C::bc_decomposition_bytes_pc_plus_18, bytecode_at(i + 18) },
104 { C::bc_decomposition_bytes_pc_plus_19, bytecode_at(i + 19) },
105 { C::bc_decomposition_bytes_pc_plus_20, bytecode_at(i + 20) },
106 { C::bc_decomposition_bytes_pc_plus_21, bytecode_at(i + 21) },
107 { C::bc_decomposition_bytes_pc_plus_22, bytecode_at(i + 22) },
108 { C::bc_decomposition_bytes_pc_plus_23, bytecode_at(i + 23) },
109 { C::bc_decomposition_bytes_pc_plus_24, bytecode_at(i + 24) },
110 { C::bc_decomposition_bytes_pc_plus_25, bytecode_at(i + 25) },
111 { C::bc_decomposition_bytes_pc_plus_26, bytecode_at(i + 26) },
112 { C::bc_decomposition_bytes_pc_plus_27, bytecode_at(i + 27) },
113 { C::bc_decomposition_bytes_pc_plus_28, bytecode_at(i + 28) },
114 { C::bc_decomposition_bytes_pc_plus_29, bytecode_at(i + 29) },
115 { C::bc_decomposition_bytes_pc_plus_30, bytecode_at(i + 30) },
116 { C::bc_decomposition_bytes_pc_plus_31, bytecode_at(i + 31) },
117 { C::bc_decomposition_bytes_pc_plus_32, bytecode_at(i + 32) },
118 { C::bc_decomposition_bytes_pc_plus_33, bytecode_at(i + 33) },
119 { C::bc_decomposition_bytes_pc_plus_34, bytecode_at(i + 34) },
120 { C::bc_decomposition_bytes_pc_plus_35, bytecode_at(i + 35) },
121 { C::bc_decomposition_bytes_pc_plus_36, bytecode_at(i + 36) },
126 auto bytecode_field_at = [&](
size_t i) ->
FF {
129 if (bytecode_len - i >= 32) {
132 as_int = from_buffer<uint256_t>(
bytecode, i);
135 std::vector<uint8_t> tail(
bytecode.begin() +
static_cast<ssize_t
>(i),
bytecode.end());
137 as_int = from_buffer<uint256_t>(tail, 0);
142 for (uint32_t i = 0; i < bytecode_len; i += 31) {
147 { C::bc_decomposition_sel_packed, 1 },
148 { C::bc_decomposition_packed_field, bytecode_field_at(i) },
149 { C::bc_decomposition_next_packed_pc, i },
150 { C::bc_decomposition_next_packed_pc_min_pc_inv, 0 },
154 for (uint32_t j = i + 1; j < std::min(bytecode_len, i + 31); j++) {
158 { C::bc_decomposition_next_packed_pc, i + 31 },
159 { C::bc_decomposition_next_packed_pc_min_pc_inv, next_packed_pc_min_pc_inverses[i + 31 - j] },
169 trace.invert_columns({ { C::bc_decomposition_bytes_rem_inv,
170 C::bc_decomposition_bytes_rem_min_one_inv,
171 C::bc_decomposition_windows_min_remaining_inv } });
192 for (
const auto&
event : events) {
195 const auto&
id =
event.bytecode_id;
196 const auto input_len =
event.bytecode_fields.size() + 1;
197 const auto padding_amount = (3 - (input_len % 3)) % 3;
200 fields.reserve(input_len + padding_amount);
201 fields.insert(fields.end(),
event.bytecode_fields.begin(),
event.bytecode_fields.end());
202 fields.insert(fields.end(), padding_amount,
FF(0));
204 const auto num_rounds = fields.size() / 3;
206 for (
size_t i = 0; i < num_rounds; i++) {
207 bool start_of_bytecode = i == 0;
208 bool end_of_bytecode = i == num_rounds - 1;
214 uint32_t pc_index_1 = 93 *
static_cast<uint32_t
>(i);
215 uint32_t pc_index = i > 0 ? pc_index_1 - 31 : 0;
217 { { { C::bc_hashing_sel, 1 },
218 { C::bc_hashing_start, start_of_bytecode ? 1 : 0 },
219 { C::bc_hashing_sel_not_start, !start_of_bytecode ? 1 : 0 },
220 { C::bc_hashing_end, end_of_bytecode ? 1 : 0 },
221 { C::bc_hashing_bytecode_id,
id },
222 { C::bc_hashing_size_in_bytes,
223 event.bytecode_length_in_bytes },
224 { C::bc_hashing_input_len, input_len },
225 { C::bc_hashing_rounds_rem, num_rounds - i },
226 { C::bc_hashing_pc_index, pc_index },
227 { C::bc_hashing_pc_index_1, pc_index_1 },
228 { C::bc_hashing_pc_index_2, pc_index_1 + 31 },
229 { C::bc_hashing_packed_fields_0, fields[i * 3] },
230 { C::bc_hashing_packed_fields_1, fields[(i * 3) + 1] },
231 { C::bc_hashing_packed_fields_2, fields[(i * 3) + 2] },
232 { C::bc_hashing_sel_not_padding_1, end_of_bytecode && padding_amount == 2 ? 0 : 1 },
233 { C::bc_hashing_sel_not_padding_2, end_of_bytecode && padding_amount > 0 ? 0 : 1 },
234 { C::bc_hashing_padding, padding_amount } } });
258 for (
const auto&
event : events) {
263 event.retrieved_bytecodes_snapshot_before.next_available_leaf_index;
264 bool error =
event.error.has_value();
267 "TOO_MANY_BYTECODES error incorrectly set for bytecode retrieval");
272 { C::bc_retrieval_sel, 1 },
273 { C::bc_retrieval_bytecode_id,
event.bytecode_id },
274 { C::bc_retrieval_address,
event.address },
277 { C::bc_retrieval_current_class_id,
event.current_class_id },
280 { C::bc_retrieval_artifact_hash,
event.contract_class.artifact_hash },
281 { C::bc_retrieval_private_functions_root,
event.contract_class.private_functions_root },
284 { C::bc_retrieval_public_data_tree_root,
event.public_data_tree_root },
285 { C::bc_retrieval_nullifier_tree_root,
event.nullifier_tree_root },
290 { C::bc_retrieval_prev_retrieved_bytecodes_tree_root,
event.retrieved_bytecodes_snapshot_before.root },
291 { C::bc_retrieval_prev_retrieved_bytecodes_tree_size,
292 event.retrieved_bytecodes_snapshot_before.next_available_leaf_index },
293 { C::bc_retrieval_next_retrieved_bytecodes_tree_root,
event.retrieved_bytecodes_snapshot_after.root },
294 { C::bc_retrieval_next_retrieved_bytecodes_tree_size,
295 event.retrieved_bytecodes_snapshot_after.next_available_leaf_index },
298 { C::bc_retrieval_instance_exists,
302 { C::bc_retrieval_error, error ? 1 : 0 },
303 { C::bc_retrieval_is_new_class,
event.is_new_class },
304 { C::bc_retrieval_should_retrieve, error ? 0 : 1 },
306 { C::bc_retrieval_no_remaining_bytecodes, remaining_bytecodes == 0 ? 1 : 0 },
307 { C::bc_retrieval_remaining_bytecodes_inv, remaining_bytecodes },
313 trace.invert_columns({ { C::bc_retrieval_remaining_bytecodes_inv } });
348 for (
const auto&
event : events) {
349 const auto bytecode_size =
event.
bytecode->size();
355 auto get_operand = [&](
size_t i) ->
FF {
356 return i <
event.instruction.operands.size() && !parsing_error_non_tag
357 ?
static_cast<FF>(
event.instruction.operands[i])
360 auto bytecode_at = [&](
size_t i) -> uint8_t {
return i < bytecode_size ? (*
event.bytecode)[i] : 0; };
363 const uint8_t wire_opcode = bytecode_at(
event.pc);
380 uint32_t instr_size = 0;
382 std::array<uint8_t, NUM_OP_DC_SELECTORS> op_dc_selectors{};
383 bool has_tag =
false;
384 bool tag_is_op2 =
false;
385 uint8_t tag_value = 0;
387 if (wire_opcode_in_range) {
389 instr_size = wire_instr_spec.size_in_bytes;
390 exec_opcode = wire_instr_spec.exec_opcode;
391 op_dc_selectors = wire_instr_spec.op_dc_selectors;
393 if (wire_instr_spec.tag_operand_idx.has_value()) {
394 const auto tag_value_idx = wire_instr_spec.tag_operand_idx.value();
395 BB_ASSERT((tag_value_idx == 2 || tag_value_idx == 3),
396 "Current constraints support only tag for operand index equal to 2 or 3");
399 static_cast<uint8_t
>(get_operand(tag_value_idx - 1));
400 tag_is_op2 = tag_value_idx == 2;
404 uint32_t bytecode_size_u32 =
static_cast<uint32_t
>(bytecode_size);
405 uint32_t pc_abs_diff =
406 event.error ==
PC_OUT_OF_RANGE ?
event.pc - bytecode_size_u32 : bytecode_size_u32 -
event.pc - 1;
412 const uint32_t bytes_remaining =
event.error ==
PC_OUT_OF_RANGE ? 0 : bytecode_size_u32 -
event.pc;
414 uint32_t instr_abs_diff =
419 { C::instr_fetching_sel, 1 },
421 { C::instr_fetching_pc,
event.pc },
422 { C::instr_fetching_bytecode_id,
event.bytecode_id },
425 { C::instr_fetching_pc_out_of_range,
event.error ==
PC_OUT_OF_RANGE ? 1 : 0 },
428 { C::instr_fetching_tag_out_of_range,
event.error ==
TAG_OUT_OF_RANGE ? 1 : 0 },
429 { C::instr_fetching_sel_parsing_err,
event.error.has_value() ? 1 : 0 },
430 { C::instr_fetching_sel_pc_in_range,
event.error !=
PC_OUT_OF_RANGE ? 1 : 0 },
433 { C::instr_fetching_bytecode_size, bytecode_size },
434 { C::instr_fetching_bytes_to_read, bytes_to_read },
435 { C::instr_fetching_instr_size, instr_size },
436 { C::instr_fetching_instr_abs_diff, instr_abs_diff },
437 { C::instr_fetching_pc_abs_diff, pc_abs_diff },
442 { C::instr_fetching_tag_value, tag_value },
443 { C::instr_fetching_sel_has_tag, has_tag ? 1 : 0 },
444 { C::instr_fetching_sel_tag_is_op2, tag_is_op2 ? 1 : 0 },
447 { C::instr_fetching_exec_opcode,
static_cast<uint32_t
>(exec_opcode) },
450 { C::instr_fetching_addressing_mode,
event.instruction.addressing_mode },
451 { C::instr_fetching_op1, get_operand(0) },
452 { C::instr_fetching_op2, get_operand(1) },
453 { C::instr_fetching_op3, get_operand(2) },
454 { C::instr_fetching_op4, get_operand(3) },
455 { C::instr_fetching_op5, get_operand(4) },
456 { C::instr_fetching_op6, get_operand(5) },
457 { C::instr_fetching_op7, get_operand(6) },
460 { C::instr_fetching_bd0, wire_opcode },
461 { C::instr_fetching_bd1, bytecode_at(
event.pc + 1) },
462 { C::instr_fetching_bd2, bytecode_at(
event.pc + 2) },
463 { C::instr_fetching_bd3, bytecode_at(
event.pc + 3) },
464 { C::instr_fetching_bd4, bytecode_at(
event.pc + 4) },
465 { C::instr_fetching_bd5, bytecode_at(
event.pc + 5) },
466 { C::instr_fetching_bd6, bytecode_at(
event.pc + 6) },
467 { C::instr_fetching_bd7, bytecode_at(
event.pc + 7) },
468 { C::instr_fetching_bd8, bytecode_at(
event.pc + 8) },
469 { C::instr_fetching_bd9, bytecode_at(
event.pc + 9) },
470 { C::instr_fetching_bd10, bytecode_at(
event.pc + 10) },
471 { C::instr_fetching_bd11, bytecode_at(
event.pc + 11) },
472 { C::instr_fetching_bd12, bytecode_at(
event.pc + 12) },
473 { C::instr_fetching_bd13, bytecode_at(
event.pc + 13) },
474 { C::instr_fetching_bd14, bytecode_at(
event.pc + 14) },
475 { C::instr_fetching_bd15, bytecode_at(
event.pc + 15) },
476 { C::instr_fetching_bd16, bytecode_at(
event.pc + 16) },
477 { C::instr_fetching_bd17, bytecode_at(
event.pc + 17) },
478 { C::instr_fetching_bd18, bytecode_at(
event.pc + 18) },
479 { C::instr_fetching_bd19, bytecode_at(
event.pc + 19) },
480 { C::instr_fetching_bd20, bytecode_at(
event.pc + 20) },
481 { C::instr_fetching_bd21, bytecode_at(
event.pc + 21) },
482 { C::instr_fetching_bd22, bytecode_at(
event.pc + 22) },
483 { C::instr_fetching_bd23, bytecode_at(
event.pc + 23) },
484 { C::instr_fetching_bd24, bytecode_at(
event.pc + 24) },
485 { C::instr_fetching_bd25, bytecode_at(
event.pc + 25) },
486 { C::instr_fetching_bd26, bytecode_at(
event.pc + 26) },
487 { C::instr_fetching_bd27, bytecode_at(
event.pc + 27) },
488 { C::instr_fetching_bd28, bytecode_at(
event.pc + 28) },
489 { C::instr_fetching_bd29, bytecode_at(
event.pc + 29) },
490 { C::instr_fetching_bd30, bytecode_at(
event.pc + 30) },
491 { C::instr_fetching_bd31, bytecode_at(
event.pc + 31) },
492 { C::instr_fetching_bd32, bytecode_at(
event.pc + 32) },
493 { C::instr_fetching_bd33, bytecode_at(
event.pc + 33) },
494 { C::instr_fetching_bd34, bytecode_at(
event.pc + 34) },
495 { C::instr_fetching_bd35, bytecode_at(
event.pc + 35) },
496 { C::instr_fetching_bd36, bytecode_at(
event.pc + 36) },
499 { C::instr_fetching_sel_op_dc_0, op_dc_selectors.at(0) },
500 { C::instr_fetching_sel_op_dc_1, op_dc_selectors.at(1) },
501 { C::instr_fetching_sel_op_dc_2, op_dc_selectors.at(2) },
502 { C::instr_fetching_sel_op_dc_3, op_dc_selectors.at(3) },
503 { C::instr_fetching_sel_op_dc_4, op_dc_selectors.at(4) },
504 { C::instr_fetching_sel_op_dc_5, op_dc_selectors.at(5) },
505 { C::instr_fetching_sel_op_dc_6, op_dc_selectors.at(6) },
506 { C::instr_fetching_sel_op_dc_7, op_dc_selectors.at(7) },
507 { C::instr_fetching_sel_op_dc_8, op_dc_selectors.at(8) },
508 { C::instr_fetching_sel_op_dc_9, op_dc_selectors.at(9) },
509 { C::instr_fetching_sel_op_dc_10, op_dc_selectors.at(10) },
510 { C::instr_fetching_sel_op_dc_11, op_dc_selectors.at(11) },
511 { C::instr_fetching_sel_op_dc_12, op_dc_selectors.at(12) },
512 { C::instr_fetching_sel_op_dc_13, op_dc_selectors.at(13) },
513 { C::instr_fetching_sel_op_dc_14, op_dc_selectors.at(14) },
514 { C::instr_fetching_sel_op_dc_15, op_dc_selectors.at(15) },
515 { C::instr_fetching_sel_op_dc_16, op_dc_selectors.at(16) },