323 const FailingContexts failures = preprocess_for_discard(ex_events);
327 uint32_t dying_context_id = 0;
330 auto is_discarding = [&dying_context_id]() {
return dying_context_id != 0; };
331 bool is_first_event_in_enqueued_call =
true;
332 bool prev_row_was_enter_call =
false;
334 for (
const auto& ex_event : ex_events) {
337 if (!is_discarding() && is_first_event_in_enqueued_call &&
338 is_phase_discarded(ex_event.after_context_event.phase, failures)) {
339 dying_context_id = dying_context_for_phase(ex_event.after_context_event.phase, failures);
342 const bool has_parent = ex_event.after_context_event.parent_id != 0;
351 { C::execution_sel, 1 },
352 { C::execution_clk, row },
355 { C::execution_enqueued_call_start, is_first_event_in_enqueued_call ? 1 : 0 },
357 { C::execution_context_id, ex_event.after_context_event.id },
358 { C::execution_parent_id, ex_event.after_context_event.parent_id },
360 { C::execution_pc, ex_event.before_context_event.pc },
361 { C::execution_msg_sender, ex_event.after_context_event.msg_sender },
362 { C::execution_contract_address, ex_event.after_context_event.contract_addr },
363 { C::execution_transaction_fee, ex_event.after_context_event.transaction_fee },
364 { C::execution_is_static, ex_event.after_context_event.is_static },
365 { C::execution_parent_calldata_addr, ex_event.after_context_event.parent_cd_addr },
366 { C::execution_parent_calldata_size, ex_event.after_context_event.parent_cd_size },
367 { C::execution_last_child_returndata_addr, ex_event.after_context_event.last_child_rd_addr },
368 { C::execution_last_child_returndata_size, ex_event.after_context_event.last_child_rd_size },
369 { C::execution_last_child_success, ex_event.after_context_event.last_child_success },
370 { C::execution_last_child_id, ex_event.after_context_event.last_child_id },
371 { C::execution_l2_gas_limit, ex_event.after_context_event.gas_limit.l2_gas },
372 { C::execution_da_gas_limit, ex_event.after_context_event.gas_limit.da_gas },
373 { C::execution_l2_gas_used, ex_event.after_context_event.gas_used.l2_gas },
374 { C::execution_da_gas_used, ex_event.after_context_event.gas_used.da_gas },
375 { C::execution_parent_l2_gas_limit, ex_event.after_context_event.parent_gas_limit.l2_gas },
376 { C::execution_parent_da_gas_limit, ex_event.after_context_event.parent_gas_limit.da_gas },
377 { C::execution_parent_l2_gas_used, ex_event.after_context_event.parent_gas_used.l2_gas },
378 { C::execution_parent_da_gas_used, ex_event.after_context_event.parent_gas_used.da_gas },
379 { C::execution_next_context_id, ex_event.next_context_id },
381 { C::execution_prev_l2_gas_used, ex_event.before_context_event.gas_used.l2_gas },
382 { C::execution_prev_da_gas_used, ex_event.before_context_event.gas_used.da_gas },
385 { C::execution_prev_written_public_data_slots_tree_root,
386 ex_event.before_context_event.written_public_data_slots_tree_snapshot.root },
387 { C::execution_prev_written_public_data_slots_tree_size,
388 ex_event.before_context_event.written_public_data_slots_tree_snapshot.next_available_leaf_index },
389 { C::execution_written_public_data_slots_tree_root,
390 ex_event.after_context_event.written_public_data_slots_tree_snapshot.root },
391 { C::execution_written_public_data_slots_tree_size,
392 ex_event.after_context_event.written_public_data_slots_tree_snapshot.next_available_leaf_index },
394 { C::execution_prev_nullifier_tree_root,
395 ex_event.before_context_event.tree_states.nullifier_tree.tree.root },
396 { C::execution_prev_nullifier_tree_size,
397 ex_event.before_context_event.tree_states.nullifier_tree.tree.next_available_leaf_index },
398 { C::execution_prev_num_nullifiers_emitted,
399 ex_event.before_context_event.tree_states.nullifier_tree.counter },
400 { C::execution_nullifier_tree_root, ex_event.after_context_event.tree_states.nullifier_tree.tree.root },
401 { C::execution_nullifier_tree_size,
402 ex_event.after_context_event.tree_states.nullifier_tree.tree.next_available_leaf_index },
403 { C::execution_num_nullifiers_emitted,
404 ex_event.after_context_event.tree_states.nullifier_tree.counter },
406 { C::execution_prev_public_data_tree_root,
407 ex_event.before_context_event.tree_states.public_data_tree.tree.root },
408 { C::execution_prev_public_data_tree_size,
409 ex_event.before_context_event.tree_states.public_data_tree.tree.next_available_leaf_index },
410 { C::execution_public_data_tree_root,
411 ex_event.after_context_event.tree_states.public_data_tree.tree.root },
412 { C::execution_public_data_tree_size,
413 ex_event.after_context_event.tree_states.public_data_tree.tree.next_available_leaf_index },
415 { C::execution_prev_note_hash_tree_root,
416 ex_event.before_context_event.tree_states.note_hash_tree.tree.root },
417 { C::execution_prev_note_hash_tree_size,
418 ex_event.before_context_event.tree_states.note_hash_tree.tree.next_available_leaf_index },
419 { C::execution_prev_num_note_hashes_emitted,
420 ex_event.before_context_event.tree_states.note_hash_tree.counter },
421 { C::execution_note_hash_tree_root, ex_event.after_context_event.tree_states.note_hash_tree.tree.root },
422 { C::execution_note_hash_tree_size,
423 ex_event.after_context_event.tree_states.note_hash_tree.tree.next_available_leaf_index },
424 { C::execution_num_note_hashes_emitted,
425 ex_event.after_context_event.tree_states.note_hash_tree.counter },
427 { C::execution_l1_l2_tree_root,
428 ex_event.after_context_event.tree_states.l1_to_l2_message_tree.tree.root },
430 { C::execution_prev_retrieved_bytecodes_tree_root,
431 ex_event.before_context_event.retrieved_bytecodes_tree_snapshot.root },
432 { C::execution_prev_retrieved_bytecodes_tree_size,
433 ex_event.before_context_event.retrieved_bytecodes_tree_snapshot.next_available_leaf_index },
434 { C::execution_retrieved_bytecodes_tree_root,
435 ex_event.after_context_event.retrieved_bytecodes_tree_snapshot.root },
436 { C::execution_retrieved_bytecodes_tree_size,
437 ex_event.after_context_event.retrieved_bytecodes_tree_snapshot.next_available_leaf_index },
439 { C::execution_prev_num_public_log_fields, ex_event.before_context_event.numPublicLogFields },
440 { C::execution_num_public_log_fields, ex_event.after_context_event.numPublicLogFields },
441 { C::execution_prev_num_l2_to_l1_messages, ex_event.before_context_event.numL2ToL1Messages },
442 { C::execution_num_l2_to_l1_messages, ex_event.after_context_event.numL2ToL1Messages },
444 { C::execution_has_parent_ctx, has_parent ? 1 : 0 },
445 { C::execution_is_parent_id_inv, ex_event.after_context_event.parent_id },
452 const auto& internal_call_return_id = ex_event.before_context_event.internal_call_return_id;
455 { C::execution_internal_call_id, ex_event.before_context_event.internal_call_id },
456 { C::execution_internal_call_return_id, internal_call_return_id },
457 { C::execution_next_internal_call_id, ex_event.before_context_event.next_internal_call_id },
464 const bool bytecode_retrieval_failed = ex_event.error == ExecutionError::BYTECODE_RETRIEVAL;
465 const bool sel_first_row_in_context = prev_row_was_enter_call || is_first_event_in_enqueued_call;
468 { C::execution_sel_first_row_in_context, sel_first_row_in_context ? 1 : 0 },
469 { C::execution_sel_bytecode_retrieval_failure, bytecode_retrieval_failed ? 1 : 0 },
470 { C::execution_sel_bytecode_retrieval_success, !bytecode_retrieval_failed ? 1 : 0 },
471 { C::execution_bytecode_id, ex_event.after_context_event.bytecode_id },
481 const bool error_in_instruction_fetching = ex_event.error == ExecutionError::INSTRUCTION_FETCHING;
483 const bool instruction_fetching_success = !bytecode_retrieval_failed && !error_in_instruction_fetching;
486 trace.set(C::execution_sel_instruction_fetching_failure, row, error_in_instruction_fetching ? 1 : 0);
488 if (instruction_fetching_success) {
489 exec_opcode = ex_event.wire_instruction.get_exec_opcode();
500 { C::execution_next_pc,
501 static_cast<uint32_t
>(ex_event.before_context_event.pc +
502 ex_event.wire_instruction.size_in_bytes()) },
511 const bool addressing_failed = ex_event.error == ExecutionError::ADDRESSING;
520 const bool do_process_registers = instruction_fetching_success && !addressing_failed;
521 const bool register_processing_failed = ex_event.error == ExecutionError::REGISTER_READ;
522 if (do_process_registers) {
524 *exec_opcode, ex_event.inputs, ex_event.output,
registers, register_processing_failed,
trace, row);
531 const bool check_gas = do_process_registers && !register_processing_failed;
539 uint32_t radix = ex_event.inputs[1].as<uint32_t>();
540 uint32_t num_limbs = ex_event.inputs[2].as<uint32_t>();
545 { C::execution_two_five_six, 256 },
546 { C::execution_sel_radix_gt_256, radix > 256 ? 1 : 0 },
547 { C::execution_sel_lookup_num_p_limbs, radix <= 256 ? 1 : 0 },
548 { C::execution_num_p_limbs, num_p_limbs },
549 { C::execution_sel_use_num_limbs, num_limbs > num_p_limbs ? 1 : 0 },
563 const bool oog = ex_event.error == ExecutionError::GAS;
568 const bool execute_opcode = check_gas && !oog;
572 bool sel_enter_call =
false;
573 bool sel_exit_call =
false;
574 bool execute_revert =
false;
576 const bool opcode_execution_failed = ex_event.error == ExecutionError::OPCODE_EXECUTION;
577 if (execute_opcode) {
582 { C::execution_sel_execute_opcode, 1 },
583 { C::execution_sel_opcode_error, opcode_execution_failed ? 1 : 0 },
589 trace.set(get_execution_opcode_selector(*exec_opcode), row, 1);
595 sel_enter_call =
true;
597 const Gas gas_left = ex_event.after_context_event.gas_limit - ex_event.after_context_event.gas_used;
599 uint32_t allocated_l2_gas =
registers[0].as<uint32_t>();
600 bool is_l2_gas_left_gt_allocated = gas_left.
l2_gas > allocated_l2_gas;
602 uint32_t allocated_da_gas =
registers[1].as<uint32_t>();
603 bool is_da_gas_left_gt_allocated = gas_left.
da_gas > allocated_da_gas;
607 { C::execution_sel_enter_call, 1 },
608 { C::execution_l2_gas_left, gas_left.
l2_gas },
609 { C::execution_da_gas_left, gas_left.
da_gas },
610 { C::execution_is_l2_gas_left_gt_allocated, is_l2_gas_left_gt_allocated ? 1 : 0 },
611 { C::execution_is_da_gas_left_gt_allocated, is_da_gas_left_gt_allocated ? 1 : 0 },
614 sel_exit_call =
true;
617 { C::execution_nested_return, has_parent ? 1 : 0 },
620 sel_exit_call =
true;
621 execute_revert =
true;
623 BB_ASSERT_EQ(ex_event.addressing_event.resolution_info.size(),
624 static_cast<size_t>(2),
625 "GETENVVAR should have exactly two resolved operands (envvar enum and output)");
627 Operand envvar_enum = ex_event.addressing_event.resolution_info[1].resolved_operand;
630 if (!opcode_execution_failed) {
632 trace.set(C::execution_internal_call_return_id_inv,
634 internal_call_return_id);
635 trace.set(C::execution_sel_read_unwind_call_stack, row, 1);
641 ex_event.before_context_event.tree_states.public_data_tree.counter;
645 { C::execution_max_data_writes_reached, (remaining_data_writes == 0) ? 1 : 0 },
646 { C::execution_remaining_data_writes_inv,
647 remaining_data_writes },
648 { C::execution_sel_write_public_data, opcode_execution_failed ? 0 : 1 },
653 uint64_t leaf_index =
registers[1].as<uint64_t>();
655 bool note_hash_leaf_in_range = leaf_index < note_hash_tree_leaf_count;
659 { C::execution_note_hash_leaf_in_range, note_hash_leaf_in_range ? 1 : 0 },
660 { C::execution_note_hash_tree_leaf_count,
FF(note_hash_tree_leaf_count) },
663 uint32_t remaining_note_hashes =
668 { C::execution_sel_reached_max_note_hashes, (remaining_note_hashes == 0) ? 1 : 0 },
669 { C::execution_remaining_note_hashes_inv,
670 remaining_note_hashes },
671 { C::execution_sel_write_note_hash, opcode_execution_failed ? 0 : 1 },
674 uint64_t leaf_index =
registers[1].as<uint64_t>();
676 bool l1_to_l2_msg_leaf_in_range = leaf_index < l1_to_l2_msg_tree_leaf_count;
680 { C::execution_l1_to_l2_msg_leaf_in_range, l1_to_l2_msg_leaf_in_range ? 1 : 0 },
681 { C::execution_l1_to_l2_msg_tree_leaf_count,
FF(l1_to_l2_msg_tree_leaf_count) },
690 uint32_t remaining_nullifiers =
694 { { { C::execution_sel_reached_max_nullifiers, (remaining_nullifiers == 0) ? 1 : 0 },
695 { C::execution_remaining_nullifiers_inv,
696 remaining_nullifiers },
697 { C::execution_sel_write_nullifier,
698 (remaining_nullifiers != 0 && !ex_event.before_context_event.is_static) ? 1 : 0 },
699 { C::execution_nullifier_pi_offset,
701 ex_event.before_context_event.tree_states.nullifier_tree.counter },
706 uint32_t remaining_l2_to_l1_msgs =
710 bool sel_too_large_recipient_error =
715 { { { C::execution_sel_l2_to_l1_msg_limit_error, (remaining_l2_to_l1_msgs == 0) ? 1 : 0 },
716 { C::execution_remaining_l2_to_l1_msgs_inv,
717 remaining_l2_to_l1_msgs },
719 { C::execution_sel_too_large_recipient_error, sel_too_large_recipient_error ? 1 : 0 },
720 { C::execution_sel_write_l2_to_l1_msg, (!opcode_execution_failed && !is_discarding()) ? 1 : 0 },
722 C::execution_public_inputs_index,
724 ex_event.before_context_event.numL2ToL1Messages,
733 const bool do_process_register_write = execute_opcode && !opcode_execution_failed;
734 if (do_process_register_write) {
742 const bool is_dying_context = ex_event.after_context_event.id == dying_context_id;
745 const FF dying_context_diff =
FF(ex_event.after_context_event.id) -
FF(dying_context_id);
749 const bool is_err = ex_event.error != ExecutionError::NONE;
750 sel_exit_call = sel_exit_call || is_err;
751 const bool is_failure = execute_revert || is_err;
752 const bool enqueued_call_end = sel_exit_call && !has_parent;
753 const bool nested_failure = is_failure && has_parent;
757 { C::execution_sel_exit_call, sel_exit_call ? 1 : 0 },
758 { C::execution_nested_failure, nested_failure ? 1 : 0 },
759 { C::execution_sel_error, is_err ? 1 : 0 },
760 { C::execution_sel_failure, is_failure ? 1 : 0 },
761 { C::execution_discard, is_discarding() ? 1 : 0 },
762 { C::execution_dying_context_id, dying_context_id },
763 { C::execution_dying_context_id_inv, dying_context_id },
764 { C::execution_is_dying_context, is_dying_context ? 1 : 0 },
765 { C::execution_dying_context_diff_inv, dying_context_diff },
766 { C::execution_enqueued_call_end, enqueued_call_end ? 1 : 0 },
772 const bool event_kills_dying_context = is_failure && is_dying_context;
774 if (event_kills_dying_context) {
776 dying_context_id = 0;
777 }
else if (sel_enter_call && !is_discarding() &&
778 failures.does_context_fail.contains(ex_event.next_context_id)) {
785 dying_context_id = ex_event.next_context_id;
793 is_first_event_in_enqueued_call = !has_parent && sel_exit_call;
796 prev_row_was_enter_call = sel_enter_call;
946 resolution_info_vec.size(),
static_cast<size_t>(
AVM_MAX_OPERANDS),
"Resolution info size is out of range");
965 uint8_t num_relative_operands = 0;
968 bool base_address_invalid = resolution_info_vec[0].error.has_value() &&
969 *resolution_info_vec[0].error == AddressingEventError::BASE_ADDRESS_INVALID;
970 bool do_base_check =
false;
975 const auto& resolution_info = resolution_info_vec[i];
977 relative_oob[i] = resolution_info.error.has_value() &&
978 *resolution_info.error == AddressingEventError::RELATIVE_COMPUTATION_OOB;
981 is_relative_effective[i] = op_is_address && is_relative[i];
982 is_indirect_effective[i] = op_is_address && is_indirect[i];
983 apply_indirection[i] = is_indirect_effective[i] && !relative_oob[i] && !base_address_invalid;
984 resolved_operand_tag[i] =
static_cast<uint8_t
>(resolution_info.resolved_operand.get_tag());
985 after_relative[i] = resolution_info.after_relative;
986 resolved_operand[i] = resolution_info.resolved_operand;
987 if (is_relative_effective[i]) {
988 do_base_check =
true;
989 num_relative_operands++;
993 BB_ASSERT(do_base_check || !base_address_invalid,
"Base address is invalid but we are not checking it.");
999 { OPERAND_IS_RELATIVE_WIRE_COLUMNS[i], is_relative[i] ? 1 : 0 },
1000 { OPERAND_IS_INDIRECT_WIRE_COLUMNS[i], is_indirect[i] ? 1 : 0 },
1001 { OPERAND_RELATIVE_OVERFLOW_COLUMNS[i], relative_oob[i] ? 1 : 0 },
1002 { OPERAND_AFTER_RELATIVE_COLUMNS[i], after_relative[i] },
1003 { OPERAND_APPLY_INDIRECTION_COLUMNS[i], apply_indirection[i] ? 1 : 0 },
1004 { OPERAND_IS_RELATIVE_VALID_BASE_COLUMNS[i],
1005 (is_relative_effective[i] && !base_address_invalid) ? 1 : 0 },
1006 { RESOLVED_OPERAND_COLUMNS[i], resolved_operand[i] },
1007 { RESOLVED_OPERAND_TAG_COLUMNS[i], resolved_operand_tag[i] },
1018 { OPERAND_IS_RELATIVE_WIRE_COLUMNS[i], is_relative ? 1 : 0 },
1019 { OPERAND_IS_INDIRECT_WIRE_COLUMNS[i], is_indirect ? 1 : 0 },
1024 FF base_address_tag_diff = base_address_invalid ?
FF(
static_cast<uint8_t
>(addr_event.
base_address.
get_tag())) -
1029 bool some_final_check_failed = std::ranges::any_of(addr_event.
resolution_info, [](
const auto&
info) {
1030 return info.error.has_value() && *info.error == AddressingEventError::INVALID_ADDRESS_AFTER_INDIRECTION;
1032 FF batched_tags_diff = 0;
1033 if (some_final_check_failed) {
1036 if (apply_indirection[i]) {
1037 batched_tags_diff += power_of_2 * (
FF(resolved_operand_tag[i]) -
FF(
MEM_TAG_U32));
1044 bool addressing_failed =
1045 std::ranges::any_of(addr_event.
resolution_info, [](
const auto&
info) { return info.error.has_value(); });
1046 FF addressing_error_collection =
1050 (base_address_invalid ? 1 : 0) +
1054 static_cast<uint32_t
>(0),
1055 [](uint32_t acc,
const auto&
info) {
1057 (
info.error.has_value() &&
1058 *
info.error == AddressingEventError::RELATIVE_COMPUTATION_OOB
1063 (some_final_check_failed ? 1 : 0))
1069 { C::execution_sel_addressing_error, addressing_failed ? 1 : 0 },
1070 { C::execution_addressing_error_collection_inv, addressing_error_collection },
1072 { C::execution_base_address_tag,
static_cast<uint8_t
>(addr_event.
base_address.
get_tag()) },
1073 { C::execution_base_address_tag_diff_inv, base_address_tag_diff },
1074 { C::execution_batched_tags_diff_inv, batched_tags_diff },
1075 { C::execution_sel_some_final_check_failed, some_final_check_failed ? 1 : 0 },
1076 { C::execution_sel_base_address_failure, base_address_invalid ? 1 : 0 },
1077 { C::execution_num_relative_operands_inv, num_relative_operands },
1078 { C::execution_sel_do_base_check, do_base_check ? 1 : 0 },