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 },
329 { C::execution_clk, row },
332 { C::execution_enqueued_call_start, is_first_event_in_enqueued_call ? 1 : 0 },
334 { C::execution_context_id, ex_event.after_context_event.id },
335 { C::execution_parent_id, ex_event.after_context_event.parent_id },
337 { C::execution_pc, ex_event.before_context_event.pc },
338 { C::execution_msg_sender, ex_event.after_context_event.msg_sender },
339 { C::execution_contract_address, ex_event.after_context_event.contract_addr },
340 { C::execution_transaction_fee, ex_event.after_context_event.transaction_fee },
341 { C::execution_is_static, ex_event.after_context_event.is_static },
342 { C::execution_parent_calldata_addr, ex_event.after_context_event.parent_cd_addr },
343 { C::execution_parent_calldata_size, ex_event.after_context_event.parent_cd_size },
344 { C::execution_last_child_returndata_addr, ex_event.after_context_event.last_child_rd_addr },
345 { C::execution_last_child_returndata_size, ex_event.after_context_event.last_child_rd_size },
346 { C::execution_last_child_success, ex_event.after_context_event.last_child_success },
347 { C::execution_last_child_id, ex_event.after_context_event.last_child_id },
348 { C::execution_l2_gas_limit, ex_event.after_context_event.gas_limit.l2_gas },
349 { C::execution_da_gas_limit, ex_event.after_context_event.gas_limit.da_gas },
350 { C::execution_l2_gas_used, ex_event.after_context_event.gas_used.l2_gas },
351 { C::execution_da_gas_used, ex_event.after_context_event.gas_used.da_gas },
352 { C::execution_parent_l2_gas_limit, ex_event.after_context_event.parent_gas_limit.l2_gas },
353 { C::execution_parent_da_gas_limit, ex_event.after_context_event.parent_gas_limit.da_gas },
354 { C::execution_parent_l2_gas_used, ex_event.after_context_event.parent_gas_used.l2_gas },
355 { C::execution_parent_da_gas_used, ex_event.after_context_event.parent_gas_used.da_gas },
356 { C::execution_next_context_id, ex_event.next_context_id },
358 { C::execution_prev_l2_gas_used, ex_event.before_context_event.gas_used.l2_gas },
359 { C::execution_prev_da_gas_used, ex_event.before_context_event.gas_used.da_gas },
362 { C::execution_prev_written_public_data_slots_tree_root,
363 ex_event.before_context_event.written_public_data_slots_tree_snapshot.root },
364 { C::execution_prev_written_public_data_slots_tree_size,
365 ex_event.before_context_event.written_public_data_slots_tree_snapshot.next_available_leaf_index },
366 { C::execution_written_public_data_slots_tree_root,
367 ex_event.after_context_event.written_public_data_slots_tree_snapshot.root },
368 { C::execution_written_public_data_slots_tree_size,
369 ex_event.after_context_event.written_public_data_slots_tree_snapshot.next_available_leaf_index },
371 { C::execution_prev_nullifier_tree_root,
372 ex_event.before_context_event.tree_states.nullifier_tree.tree.root },
373 { C::execution_prev_nullifier_tree_size,
374 ex_event.before_context_event.tree_states.nullifier_tree.tree.next_available_leaf_index },
375 { C::execution_prev_num_nullifiers_emitted,
376 ex_event.before_context_event.tree_states.nullifier_tree.counter },
377 { C::execution_nullifier_tree_root, ex_event.after_context_event.tree_states.nullifier_tree.tree.root },
378 { C::execution_nullifier_tree_size,
379 ex_event.after_context_event.tree_states.nullifier_tree.tree.next_available_leaf_index },
380 { C::execution_num_nullifiers_emitted,
381 ex_event.after_context_event.tree_states.nullifier_tree.counter },
383 { C::execution_prev_public_data_tree_root,
384 ex_event.before_context_event.tree_states.public_data_tree.tree.root },
385 { C::execution_prev_public_data_tree_size,
386 ex_event.before_context_event.tree_states.public_data_tree.tree.next_available_leaf_index },
387 { C::execution_public_data_tree_root,
388 ex_event.after_context_event.tree_states.public_data_tree.tree.root },
389 { C::execution_public_data_tree_size,
390 ex_event.after_context_event.tree_states.public_data_tree.tree.next_available_leaf_index },
392 { C::execution_prev_note_hash_tree_root,
393 ex_event.before_context_event.tree_states.note_hash_tree.tree.root },
394 { C::execution_prev_note_hash_tree_size,
395 ex_event.before_context_event.tree_states.note_hash_tree.tree.next_available_leaf_index },
396 { C::execution_prev_num_note_hashes_emitted,
397 ex_event.before_context_event.tree_states.note_hash_tree.counter },
398 { C::execution_note_hash_tree_root, ex_event.after_context_event.tree_states.note_hash_tree.tree.root },
399 { C::execution_note_hash_tree_size,
400 ex_event.after_context_event.tree_states.note_hash_tree.tree.next_available_leaf_index },
401 { C::execution_num_note_hashes_emitted,
402 ex_event.after_context_event.tree_states.note_hash_tree.counter },
404 { C::execution_l1_l2_tree_root,
405 ex_event.after_context_event.tree_states.l1_to_l2_message_tree.tree.root },
407 { C::execution_prev_retrieved_bytecodes_tree_root,
408 ex_event.before_context_event.retrieved_bytecodes_tree_snapshot.root },
409 { C::execution_prev_retrieved_bytecodes_tree_size,
410 ex_event.before_context_event.retrieved_bytecodes_tree_snapshot.next_available_leaf_index },
411 { C::execution_retrieved_bytecodes_tree_root,
412 ex_event.after_context_event.retrieved_bytecodes_tree_snapshot.root },
413 { C::execution_retrieved_bytecodes_tree_size,
414 ex_event.after_context_event.retrieved_bytecodes_tree_snapshot.next_available_leaf_index },
416 { C::execution_prev_num_public_log_fields, ex_event.before_context_event.numPublicLogFields },
417 { C::execution_num_public_log_fields, ex_event.after_context_event.numPublicLogFields },
418 { C::execution_prev_num_l2_to_l1_messages, ex_event.before_context_event.numL2ToL1Messages },
419 { C::execution_num_l2_to_l1_messages, ex_event.after_context_event.numL2ToL1Messages },
421 { C::execution_has_parent_ctx, has_parent ? 1 : 0 },
422 { C::execution_is_parent_id_inv, ex_event.after_context_event.parent_id },
429 const auto& internal_call_return_id = ex_event.before_context_event.internal_call_return_id;
432 { C::execution_internal_call_id, ex_event.before_context_event.internal_call_id },
433 { C::execution_internal_call_return_id, internal_call_return_id },
434 { C::execution_next_internal_call_id, ex_event.before_context_event.next_internal_call_id },
441 const bool bytecode_retrieval_failed = ex_event.error == ExecutionError::BYTECODE_RETRIEVAL;
442 const bool sel_first_row_in_context = prev_row_was_enter_call || is_first_event_in_enqueued_call;
445 { C::execution_sel_first_row_in_context, sel_first_row_in_context ? 1 : 0 },
446 { C::execution_sel_bytecode_retrieval_failure, bytecode_retrieval_failed ? 1 : 0 },
447 { C::execution_sel_bytecode_retrieval_success, !bytecode_retrieval_failed ? 1 : 0 },
448 { C::execution_bytecode_id, ex_event.after_context_event.bytecode_id },
457 const bool error_in_instruction_fetching = ex_event.error == ExecutionError::INSTRUCTION_FETCHING;
458 const bool instruction_fetching_success = !bytecode_retrieval_failed && !error_in_instruction_fetching;
459 trace.set(C::execution_sel_instruction_fetching_failure, row, error_in_instruction_fetching ? 1 : 0);
461 if (instruction_fetching_success) {
462 exec_opcode = ex_event.wire_instruction.get_exec_opcode();
473 { C::execution_next_pc,
474 static_cast<uint32_t
>(ex_event.before_context_event.pc +
475 ex_event.wire_instruction.size_in_bytes()) },
484 const bool addressing_failed = ex_event.error == ExecutionError::ADDRESSING;
493 const bool should_process_registers = instruction_fetching_success && !addressing_failed;
494 const bool register_processing_failed = ex_event.error == ExecutionError::REGISTER_READ;
495 if (should_process_registers) {
497 *exec_opcode, ex_event.inputs, ex_event.output,
registers, register_processing_failed,
trace, row);
504 const bool should_check_gas = should_process_registers && !register_processing_failed;
505 if (should_check_gas) {
512 uint32_t radix = ex_event.inputs[1].as<uint32_t>();
513 uint32_t num_limbs = ex_event.inputs[2].as<uint32_t>();
518 { C::execution_two_five_six, 256 },
519 { C::execution_sel_radix_gt_256, radix > 256 ? 1 : 0 },
520 { C::execution_sel_lookup_num_p_limbs, radix <= 256 ? 1 : 0 },
521 { C::execution_num_p_limbs, num_p_limbs },
522 { C::execution_sel_use_num_limbs, num_limbs > num_p_limbs ? 1 : 0 },
535 const bool oog = ex_event.error == ExecutionError::GAS;
540 const bool should_execute_opcode = should_check_gas && !oog;
544 bool sel_enter_call =
false;
545 bool sel_exit_call =
false;
546 bool should_execute_revert =
false;
548 const bool opcode_execution_failed = ex_event.error == ExecutionError::OPCODE_EXECUTION;
549 if (should_execute_opcode) {
554 { C::execution_sel_should_execute_opcode, 1 },
555 { C::execution_sel_opcode_error, opcode_execution_failed ? 1 : 0 },
561 trace.set(get_execution_opcode_selector(*exec_opcode), row, 1);
567 sel_enter_call =
true;
569 const Gas gas_left = ex_event.after_context_event.gas_limit - ex_event.after_context_event.gas_used;
571 uint32_t allocated_l2_gas =
registers[0].as<uint32_t>();
572 bool is_l2_gas_left_gt_allocated = gas_left.
l2_gas > allocated_l2_gas;
574 uint32_t allocated_da_gas =
registers[1].as<uint32_t>();
575 bool is_da_gas_left_gt_allocated = gas_left.
da_gas > allocated_da_gas;
579 { C::execution_sel_enter_call, 1 },
580 { C::execution_l2_gas_left, gas_left.
l2_gas },
581 { C::execution_da_gas_left, gas_left.
da_gas },
582 { C::execution_is_l2_gas_left_gt_allocated, is_l2_gas_left_gt_allocated ? 1 : 0 },
583 { C::execution_is_da_gas_left_gt_allocated, is_da_gas_left_gt_allocated ? 1 : 0 },
586 sel_exit_call =
true;
589 { C::execution_nested_return, has_parent ? 1 : 0 },
592 sel_exit_call =
true;
593 should_execute_revert =
true;
595 BB_ASSERT_EQ(ex_event.addressing_event.resolution_info.size(),
596 static_cast<size_t>(2),
597 "GETENVVAR should have exactly two resolved operands (envvar enum and output)");
599 Operand envvar_enum = ex_event.addressing_event.resolution_info[1].resolved_operand;
602 if (!opcode_execution_failed) {
604 trace.set(C::execution_internal_call_return_id_inv,
606 internal_call_return_id);
607 trace.set(C::execution_sel_read_unwind_call_stack, row, 1);
613 ex_event.before_context_event.tree_states.public_data_tree.counter;
617 { C::execution_max_data_writes_reached, remaining_data_writes == 0 },
618 { C::execution_remaining_data_writes_inv,
619 remaining_data_writes },
620 { C::execution_sel_write_public_data, !opcode_execution_failed },
625 uint64_t leaf_index =
registers[1].as<uint64_t>();
627 bool note_hash_leaf_in_range = leaf_index < note_hash_tree_leaf_count;
631 { C::execution_note_hash_leaf_in_range, note_hash_leaf_in_range },
632 { C::execution_note_hash_tree_leaf_count,
FF(note_hash_tree_leaf_count) },
635 uint32_t remaining_note_hashes =
640 { C::execution_sel_reached_max_note_hashes, remaining_note_hashes == 0 },
641 { C::execution_remaining_note_hashes_inv,
642 remaining_note_hashes },
643 { C::execution_sel_write_note_hash, !opcode_execution_failed },
646 uint64_t leaf_index =
registers[1].as<uint64_t>();
648 bool l1_to_l2_msg_leaf_in_range = leaf_index < l1_to_l2_msg_tree_leaf_count;
652 { C::execution_l1_to_l2_msg_leaf_in_range, l1_to_l2_msg_leaf_in_range },
653 { C::execution_l1_to_l2_msg_tree_leaf_count,
FF(l1_to_l2_msg_tree_leaf_count) },
661 uint32_t remaining_nullifiers =
665 { { { C::execution_sel_reached_max_nullifiers, remaining_nullifiers == 0 },
666 { C::execution_remaining_nullifiers_inv,
667 remaining_nullifiers },
668 { C::execution_sel_write_nullifier,
669 remaining_nullifiers != 0 && !ex_event.before_context_event.is_static },
670 { C::execution_nullifier_pi_offset,
672 ex_event.before_context_event.tree_states.nullifier_tree.counter },
676 uint32_t remaining_l2_to_l1_msgs =
680 bool sel_too_large_recipient_error =
684 { { { C::execution_sel_l2_to_l1_msg_limit_error, remaining_l2_to_l1_msgs == 0 },
685 { C::execution_remaining_l2_to_l1_msgs_inv,
686 remaining_l2_to_l1_msgs },
688 { C::execution_sel_too_large_recipient_error, sel_too_large_recipient_error },
689 { C::execution_sel_write_l2_to_l1_msg, !opcode_execution_failed && !is_discarding() },
691 C::execution_public_inputs_index,
693 ex_event.before_context_event.numL2ToL1Messages,
702 const bool should_process_register_write = should_execute_opcode && !opcode_execution_failed;
703 if (should_process_register_write) {
711 const bool is_dying_context = ex_event.after_context_event.id == dying_context_id;
714 const FF dying_context_diff =
FF(ex_event.after_context_event.id) -
FF(dying_context_id);
718 const bool is_err = ex_event.error != ExecutionError::NONE;
719 sel_exit_call = sel_exit_call || is_err;
720 const bool is_failure = should_execute_revert || is_err;
721 const bool enqueued_call_end = sel_exit_call && !has_parent;
722 const bool nested_failure = is_failure && has_parent;
726 { C::execution_sel_exit_call, sel_exit_call ? 1 : 0 },
727 { C::execution_nested_failure, nested_failure ? 1 : 0 },
728 { C::execution_sel_error, is_err ? 1 : 0 },
729 { C::execution_sel_failure, is_failure ? 1 : 0 },
730 { C::execution_discard, is_discarding() ? 1 : 0 },
731 { C::execution_dying_context_id, dying_context_id },
732 { C::execution_dying_context_id_inv, dying_context_id },
733 { C::execution_is_dying_context, is_dying_context ? 1 : 0 },
734 { C::execution_dying_context_diff_inv, dying_context_diff },
735 { C::execution_enqueued_call_end, enqueued_call_end ? 1 : 0 },
741 const bool event_kills_dying_context = is_failure && is_dying_context;
743 if (event_kills_dying_context) {
745 dying_context_id = 0;
746 }
else if (sel_enter_call && !is_discarding() &&
747 failures.does_context_fail.contains(ex_event.next_context_id)) {
754 dying_context_id = ex_event.next_context_id;
762 is_first_event_in_enqueued_call = !has_parent && sel_exit_call;
765 prev_row_was_enter_call = sel_enter_call;
881 resolution_info_vec.size(),
static_cast<size_t>(
AVM_MAX_OPERANDS),
"Resolution info size is out of range");
900 uint8_t num_relative_operands = 0;
903 bool base_address_invalid = resolution_info_vec[0].error.has_value() &&
904 *resolution_info_vec[0].error == AddressingEventError::BASE_ADDRESS_INVALID;
905 bool do_base_check =
false;
910 const auto& resolution_info = resolution_info_vec[i];
912 relative_oob[i] = resolution_info.error.has_value() &&
913 *resolution_info.error == AddressingEventError::RELATIVE_COMPUTATION_OOB;
916 is_relative_effective[i] = op_is_address && is_relative[i];
917 is_indirect_effective[i] = op_is_address && is_indirect[i];
918 should_apply_indirection[i] = is_indirect_effective[i] && !relative_oob[i] && !base_address_invalid;
919 resolved_operand_tag[i] =
static_cast<uint8_t
>(resolution_info.resolved_operand.get_tag());
920 after_relative[i] = resolution_info.after_relative;
921 resolved_operand[i] = resolution_info.resolved_operand;
922 if (is_relative_effective[i]) {
923 do_base_check =
true;
924 num_relative_operands++;
928 BB_ASSERT(do_base_check || !base_address_invalid,
"Base address is invalid but we are not checking it.");
934 { OPERAND_IS_RELATIVE_WIRE_COLUMNS[i], is_relative[i] ? 1 : 0 },
935 { OPERAND_IS_INDIRECT_WIRE_COLUMNS[i], is_indirect[i] ? 1 : 0 },
936 { OPERAND_RELATIVE_OVERFLOW_COLUMNS[i], relative_oob[i] ? 1 : 0 },
937 { OPERAND_AFTER_RELATIVE_COLUMNS[i], after_relative[i] },
938 { OPERAND_SHOULD_APPLY_INDIRECTION_COLUMNS[i], should_apply_indirection[i] ? 1 : 0 },
939 { OPERAND_IS_RELATIVE_VALID_BASE_COLUMNS[i],
940 (is_relative_effective[i] && !base_address_invalid) ? 1 : 0 },
941 { RESOLVED_OPERAND_COLUMNS[i], resolved_operand[i] },
942 { RESOLVED_OPERAND_TAG_COLUMNS[i], resolved_operand_tag[i] },
953 { OPERAND_IS_RELATIVE_WIRE_COLUMNS[i], is_relative ? 1 : 0 },
954 { OPERAND_IS_INDIRECT_WIRE_COLUMNS[i], is_indirect ? 1 : 0 },
959 FF base_address_tag_diff = base_address_invalid ?
FF(
static_cast<uint8_t
>(addr_event.
base_address.
get_tag())) -
964 bool some_final_check_failed = std::ranges::any_of(addr_event.
resolution_info, [](
const auto&
info) {
965 return info.error.has_value() && *info.error == AddressingEventError::INVALID_ADDRESS_AFTER_INDIRECTION;
967 FF batched_tags_diff = 0;
968 if (some_final_check_failed) {
971 if (should_apply_indirection[i]) {
972 batched_tags_diff += power_of_2 * (
FF(resolved_operand_tag[i]) -
FF(
MEM_TAG_U32));
979 bool addressing_failed =
980 std::ranges::any_of(addr_event.
resolution_info, [](
const auto&
info) { return info.error.has_value(); });
981 FF addressing_error_collection =
985 (base_address_invalid ? 1 : 0) +
989 static_cast<uint32_t
>(0),
990 [](uint32_t acc,
const auto&
info) {
992 (
info.error.has_value() &&
993 *
info.error == AddressingEventError::RELATIVE_COMPUTATION_OOB
998 (some_final_check_failed ? 1 : 0))
1004 { C::execution_sel_addressing_error, addressing_failed ? 1 : 0 },
1005 { C::execution_addressing_error_collection_inv, addressing_error_collection },
1007 { C::execution_base_address_tag,
static_cast<uint8_t
>(addr_event.
base_address.
get_tag()) },
1008 { C::execution_base_address_tag_diff_inv, base_address_tag_diff },
1009 { C::execution_batched_tags_diff_inv, batched_tags_diff },
1010 { C::execution_sel_some_final_check_failed, some_final_check_failed ? 1 : 0 },
1011 { C::execution_sel_base_address_failure, base_address_invalid ? 1 : 0 },
1012 { C::execution_num_relative_operands_inv, num_relative_operands },
1013 { C::execution_sel_do_base_check, do_base_check ? 1 : 0 },