300 const FailingContexts failures = preprocess_for_discard(ex_events);
304 uint32_t dying_context_id = 0;
307 auto is_discarding = [&dying_context_id]() {
return dying_context_id != 0; };
308 bool is_first_event_in_enqueued_call =
true;
309 bool prev_row_was_enter_call =
false;
311 for (
const auto& ex_event : ex_events) {
314 if (!is_discarding() && is_first_event_in_enqueued_call &&
315 is_phase_discarded(ex_event.after_context_event.phase, failures)) {
316 dying_context_id = dying_context_for_phase(ex_event.after_context_event.phase, failures);
319 const bool has_parent = ex_event.after_context_event.parent_id != 0;
328 { C::execution_sel, 1 },
331 { C::execution_enqueued_call_start, is_first_event_in_enqueued_call ? 1 : 0 },
333 { C::execution_context_id, ex_event.after_context_event.id },
334 { C::execution_parent_id, ex_event.after_context_event.parent_id },
335 { C::execution_pc, ex_event.before_context_event.pc },
336 { C::execution_msg_sender, ex_event.after_context_event.msg_sender },
337 { C::execution_contract_address, ex_event.after_context_event.contract_addr },
338 { C::execution_transaction_fee, ex_event.after_context_event.transaction_fee },
339 { C::execution_is_static, ex_event.after_context_event.is_static },
340 { C::execution_parent_calldata_addr, ex_event.after_context_event.parent_cd_addr },
341 { C::execution_parent_calldata_size, ex_event.after_context_event.parent_cd_size },
342 { C::execution_last_child_returndata_addr, ex_event.after_context_event.last_child_rd_addr },
343 { C::execution_last_child_returndata_size, ex_event.after_context_event.last_child_rd_size },
344 { C::execution_last_child_success, ex_event.after_context_event.last_child_success },
345 { C::execution_last_child_id, ex_event.after_context_event.last_child_id },
346 { C::execution_l2_gas_limit, ex_event.after_context_event.gas_limit.l2_gas },
347 { C::execution_da_gas_limit, ex_event.after_context_event.gas_limit.da_gas },
348 { C::execution_l2_gas_used, ex_event.after_context_event.gas_used.l2_gas },
349 { C::execution_da_gas_used, ex_event.after_context_event.gas_used.da_gas },
350 { C::execution_parent_l2_gas_limit, ex_event.after_context_event.parent_gas_limit.l2_gas },
351 { C::execution_parent_da_gas_limit, ex_event.after_context_event.parent_gas_limit.da_gas },
352 { C::execution_parent_l2_gas_used, ex_event.after_context_event.parent_gas_used.l2_gas },
353 { C::execution_parent_da_gas_used, ex_event.after_context_event.parent_gas_used.da_gas },
354 { C::execution_next_context_id, ex_event.next_context_id },
356 { C::execution_prev_l2_gas_used, ex_event.before_context_event.gas_used.l2_gas },
357 { C::execution_prev_da_gas_used, ex_event.before_context_event.gas_used.da_gas },
360 { C::execution_prev_written_public_data_slots_tree_root,
361 ex_event.before_context_event.written_public_data_slots_tree_snapshot.root },
362 { C::execution_prev_written_public_data_slots_tree_size,
363 ex_event.before_context_event.written_public_data_slots_tree_snapshot.next_available_leaf_index },
364 { C::execution_written_public_data_slots_tree_root,
365 ex_event.after_context_event.written_public_data_slots_tree_snapshot.root },
366 { C::execution_written_public_data_slots_tree_size,
367 ex_event.after_context_event.written_public_data_slots_tree_snapshot.next_available_leaf_index },
369 { C::execution_prev_nullifier_tree_root,
370 ex_event.before_context_event.tree_states.nullifier_tree.tree.root },
371 { C::execution_prev_nullifier_tree_size,
372 ex_event.before_context_event.tree_states.nullifier_tree.tree.next_available_leaf_index },
373 { C::execution_prev_num_nullifiers_emitted,
374 ex_event.before_context_event.tree_states.nullifier_tree.counter },
375 { C::execution_nullifier_tree_root, ex_event.after_context_event.tree_states.nullifier_tree.tree.root },
376 { C::execution_nullifier_tree_size,
377 ex_event.after_context_event.tree_states.nullifier_tree.tree.next_available_leaf_index },
378 { C::execution_num_nullifiers_emitted,
379 ex_event.after_context_event.tree_states.nullifier_tree.counter },
381 { C::execution_prev_public_data_tree_root,
382 ex_event.before_context_event.tree_states.public_data_tree.tree.root },
383 { C::execution_prev_public_data_tree_size,
384 ex_event.before_context_event.tree_states.public_data_tree.tree.next_available_leaf_index },
385 { C::execution_public_data_tree_root,
386 ex_event.after_context_event.tree_states.public_data_tree.tree.root },
387 { C::execution_public_data_tree_size,
388 ex_event.after_context_event.tree_states.public_data_tree.tree.next_available_leaf_index },
390 { C::execution_prev_note_hash_tree_root,
391 ex_event.before_context_event.tree_states.note_hash_tree.tree.root },
392 { C::execution_prev_note_hash_tree_size,
393 ex_event.before_context_event.tree_states.note_hash_tree.tree.next_available_leaf_index },
394 { C::execution_prev_num_note_hashes_emitted,
395 ex_event.before_context_event.tree_states.note_hash_tree.counter },
396 { C::execution_note_hash_tree_root, ex_event.after_context_event.tree_states.note_hash_tree.tree.root },
397 { C::execution_note_hash_tree_size,
398 ex_event.after_context_event.tree_states.note_hash_tree.tree.next_available_leaf_index },
399 { C::execution_num_note_hashes_emitted,
400 ex_event.after_context_event.tree_states.note_hash_tree.counter },
402 { C::execution_l1_l2_tree_root,
403 ex_event.after_context_event.tree_states.l1_to_l2_message_tree.tree.root },
405 { C::execution_prev_retrieved_bytecodes_tree_root,
406 ex_event.before_context_event.retrieved_bytecodes_tree_snapshot.root },
407 { C::execution_prev_retrieved_bytecodes_tree_size,
408 ex_event.before_context_event.retrieved_bytecodes_tree_snapshot.next_available_leaf_index },
409 { C::execution_retrieved_bytecodes_tree_root,
410 ex_event.after_context_event.retrieved_bytecodes_tree_snapshot.root },
411 { C::execution_retrieved_bytecodes_tree_size,
412 ex_event.after_context_event.retrieved_bytecodes_tree_snapshot.next_available_leaf_index },
414 { C::execution_prev_num_unencrypted_log_fields, ex_event.before_context_event.numUnencryptedLogFields },
415 { C::execution_num_unencrypted_log_fields, ex_event.after_context_event.numUnencryptedLogFields },
416 { C::execution_prev_num_l2_to_l1_messages, ex_event.before_context_event.numL2ToL1Messages },
417 { C::execution_num_l2_to_l1_messages, ex_event.after_context_event.numL2ToL1Messages },
419 { C::execution_has_parent_ctx, has_parent ? 1 : 0 },
420 { C::execution_is_parent_id_inv, ex_event.after_context_event.parent_id },
426 { C::execution_internal_call_id, ex_event.before_context_event.internal_call_id },
427 { C::execution_internal_call_return_id, ex_event.before_context_event.internal_call_return_id },
428 { C::execution_next_internal_call_id, ex_event.before_context_event.next_internal_call_id },
435 const bool bytecode_retrieval_failed = ex_event.error == ExecutionError::BYTECODE_RETRIEVAL;
436 const bool sel_first_row_in_context = prev_row_was_enter_call || is_first_event_in_enqueued_call;
439 { C::execution_sel_first_row_in_context, sel_first_row_in_context ? 1 : 0 },
440 { C::execution_sel_bytecode_retrieval_failure, bytecode_retrieval_failed ? 1 : 0 },
441 { C::execution_sel_bytecode_retrieval_success, !bytecode_retrieval_failed ? 1 : 0 },
442 { C::execution_bytecode_id, ex_event.after_context_event.bytecode_id },
451 const bool error_in_instruction_fetching = ex_event.error == ExecutionError::INSTRUCTION_FETCHING;
452 const bool instruction_fetching_success = !bytecode_retrieval_failed && !error_in_instruction_fetching;
453 trace.set(C::execution_sel_instruction_fetching_failure, row, error_in_instruction_fetching ? 1 : 0);
455 if (instruction_fetching_success) {
456 exec_opcode = ex_event.wire_instruction.get_exec_opcode();
461 { C::execution_next_pc,
462 static_cast<uint32_t
>(ex_event.before_context_event.pc +
463 ex_event.wire_instruction.size_in_bytes()) },
472 const bool addressing_failed = ex_event.error == ExecutionError::ADDRESSING;
481 const bool should_process_registers = instruction_fetching_success && !addressing_failed;
482 const bool register_processing_failed = ex_event.error == ExecutionError::REGISTER_READ;
483 if (should_process_registers) {
485 *exec_opcode, ex_event.inputs, ex_event.output,
registers, register_processing_failed,
trace, row);
492 const bool should_check_gas = should_process_registers && !register_processing_failed;
493 if (should_check_gas) {
500 uint32_t radix = ex_event.inputs[1].as<uint32_t>();
501 uint32_t num_limbs = ex_event.inputs[2].as<uint32_t>();
506 { C::execution_two_five_six, 256 },
507 { C::execution_sel_radix_gt_256, radix > 256 ? 1 : 0 },
508 { C::execution_sel_lookup_num_p_limbs, radix <= 256 ? 1 : 0 },
509 { C::execution_num_p_limbs, num_p_limbs },
510 { C::execution_sel_use_num_limbs, num_limbs > num_p_limbs ? 1 : 0 },
516 const bool oog = ex_event.error == ExecutionError::GAS;
521 const bool should_execute_opcode = should_check_gas && !oog;
525 bool sel_enter_call =
false;
526 bool sel_exit_call =
false;
527 bool should_execute_revert =
false;
529 const bool opcode_execution_failed = ex_event.error == ExecutionError::OPCODE_EXECUTION;
530 if (should_execute_opcode) {
535 { C::execution_sel_should_execute_opcode, 1 },
536 { C::execution_sel_opcode_error, opcode_execution_failed ? 1 : 0 },
542 trace.set(get_execution_opcode_selector(*exec_opcode), row, 1);
548 sel_enter_call =
true;
550 const Gas gas_left = ex_event.after_context_event.gas_limit - ex_event.after_context_event.gas_used;
552 uint32_t allocated_l2_gas =
registers[0].as<uint32_t>();
553 bool is_l2_gas_left_gt_allocated = gas_left.
l2_gas > allocated_l2_gas;
555 uint32_t allocated_da_gas =
registers[1].as<uint32_t>();
556 bool is_da_gas_left_gt_allocated = gas_left.
da_gas > allocated_da_gas;
560 { C::execution_sel_enter_call, 1 },
561 { C::execution_l2_gas_left, gas_left.
l2_gas },
562 { C::execution_da_gas_left, gas_left.
da_gas },
563 { C::execution_is_l2_gas_left_gt_allocated, is_l2_gas_left_gt_allocated ? 1 : 0 },
564 { C::execution_is_da_gas_left_gt_allocated, is_da_gas_left_gt_allocated ? 1 : 0 },
567 sel_exit_call =
true;
570 { C::execution_nested_return, has_parent ? 1 : 0 },
573 sel_exit_call =
true;
574 should_execute_revert =
true;
576 BB_ASSERT_EQ(ex_event.addressing_event.resolution_info.size(),
577 static_cast<size_t>(2),
578 "GETENVVAR should have exactly two resolved operands (envvar enum and output)");
580 Operand envvar_enum = ex_event.addressing_event.resolution_info[1].resolved_operand;
583 if (!opcode_execution_failed) {
586 C::execution_internal_call_return_id_inv,
588 ex_event.before_context_event.internal_call_return_id);
589 trace.set(C::execution_sel_read_unwind_call_stack, row, 1);
595 ex_event.before_context_event.tree_states.public_data_tree.counter;
599 { C::execution_max_data_writes_reached, remaining_data_writes == 0 },
600 { C::execution_remaining_data_writes_inv,
601 remaining_data_writes },
602 { C::execution_sel_write_public_data, !opcode_execution_failed },
605 uint64_t leaf_index =
registers[1].as<uint64_t>();
607 bool note_hash_leaf_in_range = leaf_index < note_hash_tree_leaf_count;
611 { C::execution_note_hash_leaf_in_range, note_hash_leaf_in_range },
612 { C::execution_note_hash_tree_leaf_count,
FF(note_hash_tree_leaf_count) },
615 uint32_t remaining_note_hashes =
620 { C::execution_sel_reached_max_note_hashes, remaining_note_hashes == 0 },
621 { C::execution_remaining_note_hashes_inv,
622 remaining_note_hashes },
623 { C::execution_sel_write_note_hash, !opcode_execution_failed },
626 uint64_t leaf_index =
registers[1].as<uint64_t>();
628 bool l1_to_l2_msg_leaf_in_range = leaf_index < l1_to_l2_msg_tree_leaf_count;
632 { C::execution_l1_to_l2_msg_leaf_in_range, l1_to_l2_msg_leaf_in_range },
633 { C::execution_l1_to_l2_msg_tree_leaf_count,
FF(l1_to_l2_msg_tree_leaf_count) },
638 uint32_t remaining_nullifiers =
643 { C::execution_sel_reached_max_nullifiers, remaining_nullifiers == 0 },
644 { C::execution_remaining_nullifiers_inv,
645 remaining_nullifiers },
646 { C::execution_sel_write_nullifier,
647 remaining_nullifiers != 0 && !ex_event.before_context_event.is_static },
650 uint32_t remaining_l2_to_l1_msgs =
654 { { { C::execution_sel_l2_to_l1_msg_limit_error, remaining_l2_to_l1_msgs == 0 },
655 { C::execution_remaining_l2_to_l1_msgs_inv,
656 remaining_l2_to_l1_msgs },
657 { C::execution_sel_write_l2_to_l1_msg, !opcode_execution_failed && !is_discarding() },
659 C::execution_public_inputs_index,
661 ex_event.before_context_event.numL2ToL1Messages,
670 const bool should_process_register_write = should_execute_opcode && !opcode_execution_failed;
671 if (should_process_register_write) {
679 const bool is_dying_context = ex_event.after_context_event.id == dying_context_id;
682 const FF dying_context_diff =
FF(ex_event.after_context_event.id) -
FF(dying_context_id);
686 const bool is_err = ex_event.error != ExecutionError::NONE;
687 sel_exit_call = sel_exit_call || is_err;
688 const bool is_failure = should_execute_revert || is_err;
689 const bool nested_exit_call = sel_exit_call && has_parent;
690 const bool enqueued_call_end = sel_exit_call && !has_parent;
691 const bool nested_failure = is_failure && has_parent;
695 { C::execution_sel_exit_call, sel_exit_call ? 1 : 0 },
696 { C::execution_nested_exit_call, nested_exit_call ? 1 : 0 },
697 { C::execution_nested_failure, nested_failure ? 1 : 0 },
698 { C::execution_sel_error, is_err ? 1 : 0 },
699 { C::execution_sel_failure, is_failure ? 1 : 0 },
700 { C::execution_discard, is_discarding() ? 1 : 0 },
701 { C::execution_dying_context_id, dying_context_id },
702 { C::execution_dying_context_id_inv, dying_context_id },
703 { C::execution_is_dying_context, is_dying_context ? 1 : 0 },
704 { C::execution_dying_context_diff_inv, dying_context_diff },
705 { C::execution_enqueued_call_end, enqueued_call_end ? 1 : 0 },
711 const bool event_kills_dying_context = is_failure && is_dying_context;
713 if (event_kills_dying_context) {
715 dying_context_id = 0;
716 }
else if (sel_enter_call && !is_discarding() &&
717 failures.does_context_fail.contains(ex_event.next_context_id)) {
724 dying_context_id = ex_event.next_context_id;
732 is_first_event_in_enqueued_call = !has_parent && sel_exit_call;
735 prev_row_was_enter_call = sel_enter_call;
851 resolution_info_vec.size(),
static_cast<size_t>(
AVM_MAX_OPERANDS),
"Resolution info size is out of range");
870 uint8_t num_relative_operands = 0;
873 bool base_address_invalid = resolution_info_vec[0].error.has_value() &&
874 *resolution_info_vec[0].error == AddressingEventError::BASE_ADDRESS_INVALID;
875 bool do_base_check =
false;
880 const auto& resolution_info = resolution_info_vec[i];
882 relative_oob[i] = resolution_info.error.has_value() &&
883 *resolution_info.error == AddressingEventError::RELATIVE_COMPUTATION_OOB;
886 is_relative_effective[i] = op_is_address && is_relative[i];
887 is_indirect_effective[i] = op_is_address && is_indirect[i];
888 should_apply_indirection[i] = is_indirect_effective[i] && !relative_oob[i] && !base_address_invalid;
889 resolved_operand_tag[i] =
static_cast<uint8_t
>(resolution_info.resolved_operand.get_tag());
890 after_relative[i] = resolution_info.after_relative;
891 resolved_operand[i] = resolution_info.resolved_operand;
892 if (is_relative_effective[i]) {
893 do_base_check =
true;
894 num_relative_operands++;
898 BB_ASSERT(do_base_check || !base_address_invalid,
"Base address is invalid but we are not checking it.");
904 { OPERAND_IS_RELATIVE_WIRE_COLUMNS[i], is_relative[i] ? 1 : 0 },
905 { OPERAND_IS_INDIRECT_WIRE_COLUMNS[i], is_indirect[i] ? 1 : 0 },
906 { OPERAND_RELATIVE_OVERFLOW_COLUMNS[i], relative_oob[i] ? 1 : 0 },
907 { OPERAND_AFTER_RELATIVE_COLUMNS[i], after_relative[i] },
908 { OPERAND_SHOULD_APPLY_INDIRECTION_COLUMNS[i], should_apply_indirection[i] ? 1 : 0 },
909 { OPERAND_IS_RELATIVE_VALID_BASE_COLUMNS[i],
910 (is_relative_effective[i] && !base_address_invalid) ? 1 : 0 },
911 { RESOLVED_OPERAND_COLUMNS[i], resolved_operand[i] },
912 { RESOLVED_OPERAND_TAG_COLUMNS[i], resolved_operand_tag[i] },
923 { OPERAND_IS_RELATIVE_WIRE_COLUMNS[i], is_relative ? 1 : 0 },
924 { OPERAND_IS_INDIRECT_WIRE_COLUMNS[i], is_indirect ? 1 : 0 },
929 FF base_address_tag_diff = base_address_invalid ?
FF(
static_cast<uint8_t
>(addr_event.
base_address.
get_tag())) -
934 bool some_final_check_failed = std::ranges::any_of(addr_event.
resolution_info, [](
const auto&
info) {
935 return info.error.has_value() && *info.error == AddressingEventError::INVALID_ADDRESS_AFTER_INDIRECTION;
937 FF batched_tags_diff = 0;
938 if (some_final_check_failed) {
941 if (should_apply_indirection[i]) {
942 batched_tags_diff += power_of_2 * (
FF(resolved_operand_tag[i]) -
FF(
MEM_TAG_U32));
949 bool addressing_failed =
950 std::ranges::any_of(addr_event.
resolution_info, [](
const auto&
info) { return info.error.has_value(); });
951 FF addressing_error_collection =
955 (base_address_invalid ? 1 : 0) +
959 static_cast<uint32_t
>(0),
960 [](uint32_t acc,
const auto&
info) {
962 (
info.error.has_value() &&
963 *
info.error == AddressingEventError::RELATIVE_COMPUTATION_OOB
968 (some_final_check_failed ? 1 : 0))
974 { C::execution_sel_addressing_error, addressing_failed ? 1 : 0 },
975 { C::execution_addressing_error_collection_inv, addressing_error_collection },
977 { C::execution_base_address_tag,
static_cast<uint8_t
>(addr_event.
base_address.
get_tag()) },
978 { C::execution_base_address_tag_diff_inv, base_address_tag_diff },
979 { C::execution_batched_tags_diff_inv, batched_tags_diff },
980 { C::execution_sel_some_final_check_failed, some_final_check_failed ? 1 : 0 },
981 { C::execution_sel_base_address_failure, base_address_invalid ? 1 : 0 },
982 { C::execution_num_relative_operands_inv, num_relative_operands },
983 { C::execution_sel_do_base_check, do_base_check ? 1 : 0 },