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 },
528 const bool oog = ex_event.error == ExecutionError::GAS;
533 const bool should_execute_opcode = should_check_gas && !oog;
537 bool sel_enter_call =
false;
538 bool sel_exit_call =
false;
539 bool should_execute_revert =
false;
541 const bool opcode_execution_failed = ex_event.error == ExecutionError::OPCODE_EXECUTION;
542 if (should_execute_opcode) {
547 { C::execution_sel_should_execute_opcode, 1 },
548 { C::execution_sel_opcode_error, opcode_execution_failed ? 1 : 0 },
554 trace.set(get_execution_opcode_selector(*exec_opcode), row, 1);
560 sel_enter_call =
true;
562 const Gas gas_left = ex_event.after_context_event.gas_limit - ex_event.after_context_event.gas_used;
564 uint32_t allocated_l2_gas =
registers[0].as<uint32_t>();
565 bool is_l2_gas_left_gt_allocated = gas_left.
l2_gas > allocated_l2_gas;
567 uint32_t allocated_da_gas =
registers[1].as<uint32_t>();
568 bool is_da_gas_left_gt_allocated = gas_left.
da_gas > allocated_da_gas;
572 { C::execution_sel_enter_call, 1 },
573 { C::execution_l2_gas_left, gas_left.
l2_gas },
574 { C::execution_da_gas_left, gas_left.
da_gas },
575 { C::execution_is_l2_gas_left_gt_allocated, is_l2_gas_left_gt_allocated ? 1 : 0 },
576 { C::execution_is_da_gas_left_gt_allocated, is_da_gas_left_gt_allocated ? 1 : 0 },
579 sel_exit_call =
true;
582 { C::execution_nested_return, has_parent ? 1 : 0 },
585 sel_exit_call =
true;
586 should_execute_revert =
true;
588 BB_ASSERT_EQ(ex_event.addressing_event.resolution_info.size(),
589 static_cast<size_t>(2),
590 "GETENVVAR should have exactly two resolved operands (envvar enum and output)");
592 Operand envvar_enum = ex_event.addressing_event.resolution_info[1].resolved_operand;
595 if (!opcode_execution_failed) {
597 trace.set(C::execution_internal_call_return_id_inv,
599 internal_call_return_id);
600 trace.set(C::execution_sel_read_unwind_call_stack, row, 1);
606 ex_event.before_context_event.tree_states.public_data_tree.counter;
610 { C::execution_max_data_writes_reached, remaining_data_writes == 0 },
611 { C::execution_remaining_data_writes_inv,
612 remaining_data_writes },
613 { C::execution_sel_write_public_data, !opcode_execution_failed },
616 uint64_t leaf_index =
registers[1].as<uint64_t>();
618 bool note_hash_leaf_in_range = leaf_index < note_hash_tree_leaf_count;
622 { C::execution_note_hash_leaf_in_range, note_hash_leaf_in_range },
623 { C::execution_note_hash_tree_leaf_count,
FF(note_hash_tree_leaf_count) },
626 uint32_t remaining_note_hashes =
631 { C::execution_sel_reached_max_note_hashes, remaining_note_hashes == 0 },
632 { C::execution_remaining_note_hashes_inv,
633 remaining_note_hashes },
634 { C::execution_sel_write_note_hash, !opcode_execution_failed },
637 uint64_t leaf_index =
registers[1].as<uint64_t>();
639 bool l1_to_l2_msg_leaf_in_range = leaf_index < l1_to_l2_msg_tree_leaf_count;
643 { C::execution_l1_to_l2_msg_leaf_in_range, l1_to_l2_msg_leaf_in_range },
644 { C::execution_l1_to_l2_msg_tree_leaf_count,
FF(l1_to_l2_msg_tree_leaf_count) },
649 uint32_t remaining_nullifiers =
654 { C::execution_sel_reached_max_nullifiers, remaining_nullifiers == 0 },
655 { C::execution_remaining_nullifiers_inv,
656 remaining_nullifiers },
657 { C::execution_sel_write_nullifier,
658 remaining_nullifiers != 0 && !ex_event.before_context_event.is_static },
661 uint32_t remaining_l2_to_l1_msgs =
665 bool sel_too_large_recipient_error =
669 { { { C::execution_sel_l2_to_l1_msg_limit_error, remaining_l2_to_l1_msgs == 0 },
670 { C::execution_remaining_l2_to_l1_msgs_inv,
671 remaining_l2_to_l1_msgs },
673 { C::execution_sel_too_large_recipient_error, sel_too_large_recipient_error },
674 { C::execution_sel_write_l2_to_l1_msg, !opcode_execution_failed && !is_discarding() },
676 C::execution_public_inputs_index,
678 ex_event.before_context_event.numL2ToL1Messages,
687 const bool should_process_register_write = should_execute_opcode && !opcode_execution_failed;
688 if (should_process_register_write) {
696 const bool is_dying_context = ex_event.after_context_event.id == dying_context_id;
699 const FF dying_context_diff =
FF(ex_event.after_context_event.id) -
FF(dying_context_id);
703 const bool is_err = ex_event.error != ExecutionError::NONE;
704 sel_exit_call = sel_exit_call || is_err;
705 const bool is_failure = should_execute_revert || is_err;
706 const bool enqueued_call_end = sel_exit_call && !has_parent;
707 const bool nested_failure = is_failure && has_parent;
711 { C::execution_sel_exit_call, sel_exit_call ? 1 : 0 },
712 { C::execution_nested_failure, nested_failure ? 1 : 0 },
713 { C::execution_sel_error, is_err ? 1 : 0 },
714 { C::execution_sel_failure, is_failure ? 1 : 0 },
715 { C::execution_discard, is_discarding() ? 1 : 0 },
716 { C::execution_dying_context_id, dying_context_id },
717 { C::execution_dying_context_id_inv, dying_context_id },
718 { C::execution_is_dying_context, is_dying_context ? 1 : 0 },
719 { C::execution_dying_context_diff_inv, dying_context_diff },
720 { C::execution_enqueued_call_end, enqueued_call_end ? 1 : 0 },
726 const bool event_kills_dying_context = is_failure && is_dying_context;
728 if (event_kills_dying_context) {
730 dying_context_id = 0;
731 }
else if (sel_enter_call && !is_discarding() &&
732 failures.does_context_fail.contains(ex_event.next_context_id)) {
739 dying_context_id = ex_event.next_context_id;
747 is_first_event_in_enqueued_call = !has_parent && sel_exit_call;
750 prev_row_was_enter_call = sel_enter_call;
866 resolution_info_vec.size(),
static_cast<size_t>(
AVM_MAX_OPERANDS),
"Resolution info size is out of range");
885 uint8_t num_relative_operands = 0;
888 bool base_address_invalid = resolution_info_vec[0].error.has_value() &&
889 *resolution_info_vec[0].error == AddressingEventError::BASE_ADDRESS_INVALID;
890 bool do_base_check =
false;
895 const auto& resolution_info = resolution_info_vec[i];
897 relative_oob[i] = resolution_info.error.has_value() &&
898 *resolution_info.error == AddressingEventError::RELATIVE_COMPUTATION_OOB;
901 is_relative_effective[i] = op_is_address && is_relative[i];
902 is_indirect_effective[i] = op_is_address && is_indirect[i];
903 should_apply_indirection[i] = is_indirect_effective[i] && !relative_oob[i] && !base_address_invalid;
904 resolved_operand_tag[i] =
static_cast<uint8_t
>(resolution_info.resolved_operand.get_tag());
905 after_relative[i] = resolution_info.after_relative;
906 resolved_operand[i] = resolution_info.resolved_operand;
907 if (is_relative_effective[i]) {
908 do_base_check =
true;
909 num_relative_operands++;
913 BB_ASSERT(do_base_check || !base_address_invalid,
"Base address is invalid but we are not checking it.");
919 { OPERAND_IS_RELATIVE_WIRE_COLUMNS[i], is_relative[i] ? 1 : 0 },
920 { OPERAND_IS_INDIRECT_WIRE_COLUMNS[i], is_indirect[i] ? 1 : 0 },
921 { OPERAND_RELATIVE_OVERFLOW_COLUMNS[i], relative_oob[i] ? 1 : 0 },
922 { OPERAND_AFTER_RELATIVE_COLUMNS[i], after_relative[i] },
923 { OPERAND_SHOULD_APPLY_INDIRECTION_COLUMNS[i], should_apply_indirection[i] ? 1 : 0 },
924 { OPERAND_IS_RELATIVE_VALID_BASE_COLUMNS[i],
925 (is_relative_effective[i] && !base_address_invalid) ? 1 : 0 },
926 { RESOLVED_OPERAND_COLUMNS[i], resolved_operand[i] },
927 { RESOLVED_OPERAND_TAG_COLUMNS[i], resolved_operand_tag[i] },
938 { OPERAND_IS_RELATIVE_WIRE_COLUMNS[i], is_relative ? 1 : 0 },
939 { OPERAND_IS_INDIRECT_WIRE_COLUMNS[i], is_indirect ? 1 : 0 },
944 FF base_address_tag_diff = base_address_invalid ?
FF(
static_cast<uint8_t
>(addr_event.
base_address.
get_tag())) -
949 bool some_final_check_failed = std::ranges::any_of(addr_event.
resolution_info, [](
const auto&
info) {
950 return info.error.has_value() && *info.error == AddressingEventError::INVALID_ADDRESS_AFTER_INDIRECTION;
952 FF batched_tags_diff = 0;
953 if (some_final_check_failed) {
956 if (should_apply_indirection[i]) {
957 batched_tags_diff += power_of_2 * (
FF(resolved_operand_tag[i]) -
FF(
MEM_TAG_U32));
964 bool addressing_failed =
965 std::ranges::any_of(addr_event.
resolution_info, [](
const auto&
info) { return info.error.has_value(); });
966 FF addressing_error_collection =
970 (base_address_invalid ? 1 : 0) +
974 static_cast<uint32_t
>(0),
975 [](uint32_t acc,
const auto&
info) {
977 (
info.error.has_value() &&
978 *
info.error == AddressingEventError::RELATIVE_COMPUTATION_OOB
983 (some_final_check_failed ? 1 : 0))
989 { C::execution_sel_addressing_error, addressing_failed ? 1 : 0 },
990 { C::execution_addressing_error_collection_inv, addressing_error_collection },
992 { C::execution_base_address_tag,
static_cast<uint8_t
>(addr_event.
base_address.
get_tag()) },
993 { C::execution_base_address_tag_diff_inv, base_address_tag_diff },
994 { C::execution_batched_tags_diff_inv, batched_tags_diff },
995 { C::execution_sel_some_final_check_failed, some_final_check_failed ? 1 : 0 },
996 { C::execution_sel_base_address_failure, base_address_invalid ? 1 : 0 },
997 { C::execution_num_relative_operands_inv, num_relative_operands },
998 { C::execution_sel_do_base_check, do_base_check ? 1 : 0 },