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 },
336 { C::execution_pc, ex_event.before_context_event.pc },
337 { C::execution_msg_sender, ex_event.after_context_event.msg_sender },
338 { C::execution_contract_address, ex_event.after_context_event.contract_addr },
339 { C::execution_transaction_fee, ex_event.after_context_event.transaction_fee },
340 { C::execution_is_static, ex_event.after_context_event.is_static },
341 { C::execution_parent_calldata_addr, ex_event.after_context_event.parent_cd_addr },
342 { C::execution_parent_calldata_size, ex_event.after_context_event.parent_cd_size },
343 { C::execution_last_child_returndata_addr, ex_event.after_context_event.last_child_rd_addr },
344 { C::execution_last_child_returndata_size, ex_event.after_context_event.last_child_rd_size },
345 { C::execution_last_child_success, ex_event.after_context_event.last_child_success },
346 { C::execution_last_child_id, ex_event.after_context_event.last_child_id },
347 { C::execution_l2_gas_limit, ex_event.after_context_event.gas_limit.l2_gas },
348 { C::execution_da_gas_limit, ex_event.after_context_event.gas_limit.da_gas },
349 { C::execution_l2_gas_used, ex_event.after_context_event.gas_used.l2_gas },
350 { C::execution_da_gas_used, ex_event.after_context_event.gas_used.da_gas },
351 { C::execution_parent_l2_gas_limit, ex_event.after_context_event.parent_gas_limit.l2_gas },
352 { C::execution_parent_da_gas_limit, ex_event.after_context_event.parent_gas_limit.da_gas },
353 { C::execution_parent_l2_gas_used, ex_event.after_context_event.parent_gas_used.l2_gas },
354 { C::execution_parent_da_gas_used, ex_event.after_context_event.parent_gas_used.da_gas },
355 { C::execution_next_context_id, ex_event.next_context_id },
357 { C::execution_prev_l2_gas_used, ex_event.before_context_event.gas_used.l2_gas },
358 { C::execution_prev_da_gas_used, ex_event.before_context_event.gas_used.da_gas },
361 { C::execution_prev_written_public_data_slots_tree_root,
362 ex_event.before_context_event.written_public_data_slots_tree_snapshot.root },
363 { C::execution_prev_written_public_data_slots_tree_size,
364 ex_event.before_context_event.written_public_data_slots_tree_snapshot.next_available_leaf_index },
365 { C::execution_written_public_data_slots_tree_root,
366 ex_event.after_context_event.written_public_data_slots_tree_snapshot.root },
367 { C::execution_written_public_data_slots_tree_size,
368 ex_event.after_context_event.written_public_data_slots_tree_snapshot.next_available_leaf_index },
370 { C::execution_prev_nullifier_tree_root,
371 ex_event.before_context_event.tree_states.nullifier_tree.tree.root },
372 { C::execution_prev_nullifier_tree_size,
373 ex_event.before_context_event.tree_states.nullifier_tree.tree.next_available_leaf_index },
374 { C::execution_prev_num_nullifiers_emitted,
375 ex_event.before_context_event.tree_states.nullifier_tree.counter },
376 { C::execution_nullifier_tree_root, ex_event.after_context_event.tree_states.nullifier_tree.tree.root },
377 { C::execution_nullifier_tree_size,
378 ex_event.after_context_event.tree_states.nullifier_tree.tree.next_available_leaf_index },
379 { C::execution_num_nullifiers_emitted,
380 ex_event.after_context_event.tree_states.nullifier_tree.counter },
382 { C::execution_prev_public_data_tree_root,
383 ex_event.before_context_event.tree_states.public_data_tree.tree.root },
384 { C::execution_prev_public_data_tree_size,
385 ex_event.before_context_event.tree_states.public_data_tree.tree.next_available_leaf_index },
386 { C::execution_public_data_tree_root,
387 ex_event.after_context_event.tree_states.public_data_tree.tree.root },
388 { C::execution_public_data_tree_size,
389 ex_event.after_context_event.tree_states.public_data_tree.tree.next_available_leaf_index },
391 { C::execution_prev_note_hash_tree_root,
392 ex_event.before_context_event.tree_states.note_hash_tree.tree.root },
393 { C::execution_prev_note_hash_tree_size,
394 ex_event.before_context_event.tree_states.note_hash_tree.tree.next_available_leaf_index },
395 { C::execution_prev_num_note_hashes_emitted,
396 ex_event.before_context_event.tree_states.note_hash_tree.counter },
397 { C::execution_note_hash_tree_root, ex_event.after_context_event.tree_states.note_hash_tree.tree.root },
398 { C::execution_note_hash_tree_size,
399 ex_event.after_context_event.tree_states.note_hash_tree.tree.next_available_leaf_index },
400 { C::execution_num_note_hashes_emitted,
401 ex_event.after_context_event.tree_states.note_hash_tree.counter },
403 { C::execution_l1_l2_tree_root,
404 ex_event.after_context_event.tree_states.l1_to_l2_message_tree.tree.root },
406 { C::execution_prev_retrieved_bytecodes_tree_root,
407 ex_event.before_context_event.retrieved_bytecodes_tree_snapshot.root },
408 { C::execution_prev_retrieved_bytecodes_tree_size,
409 ex_event.before_context_event.retrieved_bytecodes_tree_snapshot.next_available_leaf_index },
410 { C::execution_retrieved_bytecodes_tree_root,
411 ex_event.after_context_event.retrieved_bytecodes_tree_snapshot.root },
412 { C::execution_retrieved_bytecodes_tree_size,
413 ex_event.after_context_event.retrieved_bytecodes_tree_snapshot.next_available_leaf_index },
415 { C::execution_prev_num_unencrypted_log_fields, ex_event.before_context_event.numUnencryptedLogFields },
416 { C::execution_num_unencrypted_log_fields, ex_event.after_context_event.numUnencryptedLogFields },
417 { C::execution_prev_num_l2_to_l1_messages, ex_event.before_context_event.numL2ToL1Messages },
418 { C::execution_num_l2_to_l1_messages, ex_event.after_context_event.numL2ToL1Messages },
420 { C::execution_has_parent_ctx, has_parent ? 1 : 0 },
421 { C::execution_is_parent_id_inv, ex_event.after_context_event.parent_id },
428 const auto& internal_call_return_id = ex_event.before_context_event.internal_call_return_id;
431 { C::execution_internal_call_id, ex_event.before_context_event.internal_call_id },
432 { C::execution_internal_call_return_id, internal_call_return_id },
433 { C::execution_next_internal_call_id, ex_event.before_context_event.next_internal_call_id },
440 const bool bytecode_retrieval_failed = ex_event.error == ExecutionError::BYTECODE_RETRIEVAL;
441 const bool sel_first_row_in_context = prev_row_was_enter_call || is_first_event_in_enqueued_call;
444 { C::execution_sel_first_row_in_context, sel_first_row_in_context ? 1 : 0 },
445 { C::execution_sel_bytecode_retrieval_failure, bytecode_retrieval_failed ? 1 : 0 },
446 { C::execution_sel_bytecode_retrieval_success, !bytecode_retrieval_failed ? 1 : 0 },
447 { C::execution_bytecode_id, ex_event.after_context_event.bytecode_id },
456 const bool error_in_instruction_fetching = ex_event.error == ExecutionError::INSTRUCTION_FETCHING;
457 const bool instruction_fetching_success = !bytecode_retrieval_failed && !error_in_instruction_fetching;
458 trace.set(C::execution_sel_instruction_fetching_failure, row, error_in_instruction_fetching ? 1 : 0);
460 if (instruction_fetching_success) {
461 exec_opcode = ex_event.wire_instruction.get_exec_opcode();
472 { C::execution_next_pc,
473 static_cast<uint32_t
>(ex_event.before_context_event.pc +
474 ex_event.wire_instruction.size_in_bytes()) },
483 const bool addressing_failed = ex_event.error == ExecutionError::ADDRESSING;
492 const bool should_process_registers = instruction_fetching_success && !addressing_failed;
493 const bool register_processing_failed = ex_event.error == ExecutionError::REGISTER_READ;
494 if (should_process_registers) {
496 *exec_opcode, ex_event.inputs, ex_event.output,
registers, register_processing_failed,
trace, row);
503 const bool should_check_gas = should_process_registers && !register_processing_failed;
504 if (should_check_gas) {
511 uint32_t radix = ex_event.inputs[1].as<uint32_t>();
512 uint32_t num_limbs = ex_event.inputs[2].as<uint32_t>();
517 { C::execution_two_five_six, 256 },
518 { C::execution_sel_radix_gt_256, radix > 256 ? 1 : 0 },
519 { C::execution_sel_lookup_num_p_limbs, radix <= 256 ? 1 : 0 },
520 { C::execution_num_p_limbs, num_p_limbs },
521 { C::execution_sel_use_num_limbs, num_limbs > num_p_limbs ? 1 : 0 },
527 const bool oog = ex_event.error == ExecutionError::GAS;
532 const bool should_execute_opcode = should_check_gas && !oog;
536 bool sel_enter_call =
false;
537 bool sel_exit_call =
false;
538 bool should_execute_revert =
false;
540 const bool opcode_execution_failed = ex_event.error == ExecutionError::OPCODE_EXECUTION;
541 if (should_execute_opcode) {
546 { C::execution_sel_should_execute_opcode, 1 },
547 { C::execution_sel_opcode_error, opcode_execution_failed ? 1 : 0 },
553 trace.set(get_execution_opcode_selector(*exec_opcode), row, 1);
559 sel_enter_call =
true;
561 const Gas gas_left = ex_event.after_context_event.gas_limit - ex_event.after_context_event.gas_used;
563 uint32_t allocated_l2_gas =
registers[0].as<uint32_t>();
564 bool is_l2_gas_left_gt_allocated = gas_left.
l2_gas > allocated_l2_gas;
566 uint32_t allocated_da_gas =
registers[1].as<uint32_t>();
567 bool is_da_gas_left_gt_allocated = gas_left.
da_gas > allocated_da_gas;
571 { C::execution_sel_enter_call, 1 },
572 { C::execution_l2_gas_left, gas_left.
l2_gas },
573 { C::execution_da_gas_left, gas_left.
da_gas },
574 { C::execution_is_l2_gas_left_gt_allocated, is_l2_gas_left_gt_allocated ? 1 : 0 },
575 { C::execution_is_da_gas_left_gt_allocated, is_da_gas_left_gt_allocated ? 1 : 0 },
578 sel_exit_call =
true;
581 { C::execution_nested_return, has_parent ? 1 : 0 },
584 sel_exit_call =
true;
585 should_execute_revert =
true;
587 BB_ASSERT_EQ(ex_event.addressing_event.resolution_info.size(),
588 static_cast<size_t>(2),
589 "GETENVVAR should have exactly two resolved operands (envvar enum and output)");
591 Operand envvar_enum = ex_event.addressing_event.resolution_info[1].resolved_operand;
594 if (!opcode_execution_failed) {
596 trace.set(C::execution_internal_call_return_id_inv,
598 internal_call_return_id);
599 trace.set(C::execution_sel_read_unwind_call_stack, row, 1);
605 ex_event.before_context_event.tree_states.public_data_tree.counter;
609 { C::execution_max_data_writes_reached, remaining_data_writes == 0 },
610 { C::execution_remaining_data_writes_inv,
611 remaining_data_writes },
612 { C::execution_sel_write_public_data, !opcode_execution_failed },
615 uint64_t leaf_index =
registers[1].as<uint64_t>();
617 bool note_hash_leaf_in_range = leaf_index < note_hash_tree_leaf_count;
621 { C::execution_note_hash_leaf_in_range, note_hash_leaf_in_range },
622 { C::execution_note_hash_tree_leaf_count,
FF(note_hash_tree_leaf_count) },
625 uint32_t remaining_note_hashes =
630 { C::execution_sel_reached_max_note_hashes, remaining_note_hashes == 0 },
631 { C::execution_remaining_note_hashes_inv,
632 remaining_note_hashes },
633 { C::execution_sel_write_note_hash, !opcode_execution_failed },
636 uint64_t leaf_index =
registers[1].as<uint64_t>();
638 bool l1_to_l2_msg_leaf_in_range = leaf_index < l1_to_l2_msg_tree_leaf_count;
642 { C::execution_l1_to_l2_msg_leaf_in_range, l1_to_l2_msg_leaf_in_range },
643 { C::execution_l1_to_l2_msg_tree_leaf_count,
FF(l1_to_l2_msg_tree_leaf_count) },
648 uint32_t remaining_nullifiers =
653 { C::execution_sel_reached_max_nullifiers, remaining_nullifiers == 0 },
654 { C::execution_remaining_nullifiers_inv,
655 remaining_nullifiers },
656 { C::execution_sel_write_nullifier,
657 remaining_nullifiers != 0 && !ex_event.before_context_event.is_static },
660 uint32_t remaining_l2_to_l1_msgs =
664 { { { C::execution_sel_l2_to_l1_msg_limit_error, remaining_l2_to_l1_msgs == 0 },
665 { C::execution_remaining_l2_to_l1_msgs_inv,
666 remaining_l2_to_l1_msgs },
667 { C::execution_sel_write_l2_to_l1_msg, !opcode_execution_failed && !is_discarding() },
669 C::execution_public_inputs_index,
671 ex_event.before_context_event.numL2ToL1Messages,
680 const bool should_process_register_write = should_execute_opcode && !opcode_execution_failed;
681 if (should_process_register_write) {
689 const bool is_dying_context = ex_event.after_context_event.id == dying_context_id;
692 const FF dying_context_diff =
FF(ex_event.after_context_event.id) -
FF(dying_context_id);
696 const bool is_err = ex_event.error != ExecutionError::NONE;
697 sel_exit_call = sel_exit_call || is_err;
698 const bool is_failure = should_execute_revert || is_err;
699 const bool enqueued_call_end = sel_exit_call && !has_parent;
700 const bool nested_failure = is_failure && has_parent;
704 { C::execution_sel_exit_call, sel_exit_call ? 1 : 0 },
705 { C::execution_nested_failure, nested_failure ? 1 : 0 },
706 { C::execution_sel_error, is_err ? 1 : 0 },
707 { C::execution_sel_failure, is_failure ? 1 : 0 },
708 { C::execution_discard, is_discarding() ? 1 : 0 },
709 { C::execution_dying_context_id, dying_context_id },
710 { C::execution_dying_context_id_inv, dying_context_id },
711 { C::execution_is_dying_context, is_dying_context ? 1 : 0 },
712 { C::execution_dying_context_diff_inv, dying_context_diff },
713 { C::execution_enqueued_call_end, enqueued_call_end ? 1 : 0 },
719 const bool event_kills_dying_context = is_failure && is_dying_context;
721 if (event_kills_dying_context) {
723 dying_context_id = 0;
724 }
else if (sel_enter_call && !is_discarding() &&
725 failures.does_context_fail.contains(ex_event.next_context_id)) {
732 dying_context_id = ex_event.next_context_id;
740 is_first_event_in_enqueued_call = !has_parent && sel_exit_call;
743 prev_row_was_enter_call = sel_enter_call;
859 resolution_info_vec.size(),
static_cast<size_t>(
AVM_MAX_OPERANDS),
"Resolution info size is out of range");
878 uint8_t num_relative_operands = 0;
881 bool base_address_invalid = resolution_info_vec[0].error.has_value() &&
882 *resolution_info_vec[0].error == AddressingEventError::BASE_ADDRESS_INVALID;
883 bool do_base_check =
false;
888 const auto& resolution_info = resolution_info_vec[i];
890 relative_oob[i] = resolution_info.error.has_value() &&
891 *resolution_info.error == AddressingEventError::RELATIVE_COMPUTATION_OOB;
894 is_relative_effective[i] = op_is_address && is_relative[i];
895 is_indirect_effective[i] = op_is_address && is_indirect[i];
896 should_apply_indirection[i] = is_indirect_effective[i] && !relative_oob[i] && !base_address_invalid;
897 resolved_operand_tag[i] =
static_cast<uint8_t
>(resolution_info.resolved_operand.get_tag());
898 after_relative[i] = resolution_info.after_relative;
899 resolved_operand[i] = resolution_info.resolved_operand;
900 if (is_relative_effective[i]) {
901 do_base_check =
true;
902 num_relative_operands++;
906 BB_ASSERT(do_base_check || !base_address_invalid,
"Base address is invalid but we are not checking it.");
912 { OPERAND_IS_RELATIVE_WIRE_COLUMNS[i], is_relative[i] ? 1 : 0 },
913 { OPERAND_IS_INDIRECT_WIRE_COLUMNS[i], is_indirect[i] ? 1 : 0 },
914 { OPERAND_RELATIVE_OVERFLOW_COLUMNS[i], relative_oob[i] ? 1 : 0 },
915 { OPERAND_AFTER_RELATIVE_COLUMNS[i], after_relative[i] },
916 { OPERAND_SHOULD_APPLY_INDIRECTION_COLUMNS[i], should_apply_indirection[i] ? 1 : 0 },
917 { OPERAND_IS_RELATIVE_VALID_BASE_COLUMNS[i],
918 (is_relative_effective[i] && !base_address_invalid) ? 1 : 0 },
919 { RESOLVED_OPERAND_COLUMNS[i], resolved_operand[i] },
920 { RESOLVED_OPERAND_TAG_COLUMNS[i], resolved_operand_tag[i] },
931 { OPERAND_IS_RELATIVE_WIRE_COLUMNS[i], is_relative ? 1 : 0 },
932 { OPERAND_IS_INDIRECT_WIRE_COLUMNS[i], is_indirect ? 1 : 0 },
937 FF base_address_tag_diff = base_address_invalid ?
FF(
static_cast<uint8_t
>(addr_event.
base_address.
get_tag())) -
942 bool some_final_check_failed = std::ranges::any_of(addr_event.
resolution_info, [](
const auto&
info) {
943 return info.error.has_value() && *info.error == AddressingEventError::INVALID_ADDRESS_AFTER_INDIRECTION;
945 FF batched_tags_diff = 0;
946 if (some_final_check_failed) {
949 if (should_apply_indirection[i]) {
950 batched_tags_diff += power_of_2 * (
FF(resolved_operand_tag[i]) -
FF(
MEM_TAG_U32));
957 bool addressing_failed =
958 std::ranges::any_of(addr_event.
resolution_info, [](
const auto&
info) { return info.error.has_value(); });
959 FF addressing_error_collection =
963 (base_address_invalid ? 1 : 0) +
967 static_cast<uint32_t
>(0),
968 [](uint32_t acc,
const auto&
info) {
970 (
info.error.has_value() &&
971 *
info.error == AddressingEventError::RELATIVE_COMPUTATION_OOB
976 (some_final_check_failed ? 1 : 0))
982 { C::execution_sel_addressing_error, addressing_failed ? 1 : 0 },
983 { C::execution_addressing_error_collection_inv, addressing_error_collection },
985 { C::execution_base_address_tag,
static_cast<uint8_t
>(addr_event.
base_address.
get_tag()) },
986 { C::execution_base_address_tag_diff_inv, base_address_tag_diff },
987 { C::execution_batched_tags_diff_inv, batched_tags_diff },
988 { C::execution_sel_some_final_check_failed, some_final_check_failed ? 1 : 0 },
989 { C::execution_sel_base_address_failure, base_address_invalid ? 1 : 0 },
990 { C::execution_num_relative_operands_inv, num_relative_operands },
991 { C::execution_sel_do_base_check, do_base_check ? 1 : 0 },