297 FailingContexts failures = preprocess_for_discard(ex_events);
299 uint32_t last_seen_parent_id = 0;
304 uint32_t dying_context_id = 0;
305 FF dying_context_id_inv = 0;
306 bool is_first_event_in_enqueued_call =
true;
307 bool prev_row_was_enter_call =
false;
309 for (
const auto& ex_event : ex_events) {
312 if (
discard == 0 && is_first_event_in_enqueued_call &&
313 is_phase_discarded(ex_event.after_context_event.phase, failures)) {
315 dying_context_id = dying_context_for_phase(ex_event.after_context_event.phase, failures);
316 dying_context_id_inv = dying_context_id;
320 bool has_parent = ex_event.after_context_event.parent_id != 0;
321 if (last_seen_parent_id != ex_event.after_context_event.parent_id) {
322 last_seen_parent_id = ex_event.after_context_event.parent_id;
332 { C::execution_sel, 1 },
335 { C::execution_enqueued_call_start, is_first_event_in_enqueued_call ? 1 : 0 },
337 { C::execution_context_id, ex_event.after_context_event.id },
338 { C::execution_parent_id, ex_event.after_context_event.parent_id },
339 { C::execution_pc, ex_event.before_context_event.pc },
340 { C::execution_msg_sender, ex_event.after_context_event.msg_sender },
341 { C::execution_contract_address, ex_event.after_context_event.contract_addr },
342 { C::execution_transaction_fee, ex_event.after_context_event.transaction_fee },
343 { C::execution_is_static, ex_event.after_context_event.is_static },
344 { C::execution_parent_calldata_addr, ex_event.after_context_event.parent_cd_addr },
345 { C::execution_parent_calldata_size, ex_event.after_context_event.parent_cd_size },
346 { C::execution_last_child_returndata_addr, ex_event.after_context_event.last_child_rd_addr },
347 { C::execution_last_child_returndata_size, ex_event.after_context_event.last_child_rd_size },
348 { C::execution_last_child_success, ex_event.after_context_event.last_child_success },
349 { C::execution_last_child_id, ex_event.after_context_event.last_child_id },
350 { C::execution_l2_gas_limit, ex_event.after_context_event.gas_limit.l2Gas },
351 { C::execution_da_gas_limit, ex_event.after_context_event.gas_limit.daGas },
352 { C::execution_l2_gas_used, ex_event.after_context_event.gas_used.l2Gas },
353 { C::execution_da_gas_used, ex_event.after_context_event.gas_used.daGas },
354 { C::execution_parent_l2_gas_limit, ex_event.after_context_event.parent_gas_limit.l2Gas },
355 { C::execution_parent_da_gas_limit, ex_event.after_context_event.parent_gas_limit.daGas },
356 { C::execution_parent_l2_gas_used, ex_event.after_context_event.parent_gas_used.l2Gas },
357 { C::execution_parent_da_gas_used, ex_event.after_context_event.parent_gas_used.daGas },
358 { C::execution_next_context_id, ex_event.next_context_id },
360 { C::execution_prev_l2_gas_used, ex_event.before_context_event.gas_used.l2Gas },
361 { C::execution_prev_da_gas_used, ex_event.before_context_event.gas_used.daGas },
364 { C::execution_prev_written_public_data_slots_tree_root,
365 ex_event.before_context_event.written_public_data_slots_tree_snapshot.root },
366 { C::execution_prev_written_public_data_slots_tree_size,
367 ex_event.before_context_event.written_public_data_slots_tree_snapshot.nextAvailableLeafIndex },
368 { C::execution_written_public_data_slots_tree_root,
369 ex_event.after_context_event.written_public_data_slots_tree_snapshot.root },
370 { C::execution_written_public_data_slots_tree_size,
371 ex_event.after_context_event.written_public_data_slots_tree_snapshot.nextAvailableLeafIndex },
372 { C::execution_prev_public_data_tree_root,
373 ex_event.before_context_event.tree_states.publicDataTree.tree.root },
374 { C::execution_prev_public_data_tree_size,
375 ex_event.before_context_event.tree_states.publicDataTree.tree.nextAvailableLeafIndex },
377 { C::execution_prev_nullifier_tree_root,
378 ex_event.before_context_event.tree_states.nullifierTree.tree.root },
379 { C::execution_prev_nullifier_tree_size,
380 ex_event.before_context_event.tree_states.nullifierTree.tree.nextAvailableLeafIndex },
381 { C::execution_prev_num_nullifiers_emitted,
382 ex_event.before_context_event.tree_states.nullifierTree.counter },
383 { C::execution_nullifier_tree_root, ex_event.after_context_event.tree_states.nullifierTree.tree.root },
384 { C::execution_nullifier_tree_size,
385 ex_event.after_context_event.tree_states.nullifierTree.tree.nextAvailableLeafIndex },
386 { C::execution_num_nullifiers_emitted, ex_event.after_context_event.tree_states.nullifierTree.counter },
388 { C::execution_public_data_tree_root,
389 ex_event.after_context_event.tree_states.publicDataTree.tree.root },
390 { C::execution_public_data_tree_size,
391 ex_event.after_context_event.tree_states.publicDataTree.tree.nextAvailableLeafIndex },
393 { C::execution_prev_note_hash_tree_root,
394 ex_event.before_context_event.tree_states.noteHashTree.tree.root },
395 { C::execution_prev_note_hash_tree_size,
396 ex_event.before_context_event.tree_states.noteHashTree.tree.nextAvailableLeafIndex },
397 { C::execution_prev_num_note_hashes_emitted,
398 ex_event.before_context_event.tree_states.noteHashTree.counter },
399 { C::execution_note_hash_tree_root, ex_event.after_context_event.tree_states.noteHashTree.tree.root },
400 { C::execution_note_hash_tree_size,
401 ex_event.after_context_event.tree_states.noteHashTree.tree.nextAvailableLeafIndex },
402 { C::execution_num_note_hashes_emitted, ex_event.after_context_event.tree_states.noteHashTree.counter },
404 { C::execution_l1_l2_tree_root, ex_event.after_context_event.tree_states.l1ToL2MessageTree.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.nextAvailableLeafIndex },
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.nextAvailableLeafIndex },
415 { C::execution_prev_num_unencrypted_log_fields,
416 ex_event.before_context_event.side_effect_states.numUnencryptedLogFields },
417 { C::execution_num_unencrypted_log_fields,
418 ex_event.after_context_event.side_effect_states.numUnencryptedLogFields },
419 { C::execution_prev_num_l2_to_l1_messages,
420 ex_event.before_context_event.side_effect_states.numL2ToL1Messages },
421 { C::execution_num_l2_to_l1_messages,
422 ex_event.after_context_event.side_effect_states.numL2ToL1Messages },
424 { C::execution_has_parent_ctx, has_parent ? 1 : 0 },
425 { C::execution_is_parent_id_inv, has_parent ? last_seen_parent_id : 0 },
431 { C::execution_internal_call_id, ex_event.before_context_event.internal_call_id },
432 { C::execution_internal_call_return_id, ex_event.before_context_event.internal_call_return_id },
433 { C::execution_next_internal_call_id, ex_event.before_context_event.next_internal_call_id },
440 bool bytecode_retrieval_failed = ex_event.error == ExecutionError::BYTECODE_RETRIEVAL;
443 { C::execution_sel_bytecode_retrieval_failure, bytecode_retrieval_failed ? 1 : 0 },
444 { C::execution_sel_bytecode_retrieval_success, !bytecode_retrieval_failed ? 1 : 0 },
445 { C::execution_bytecode_id, ex_event.after_context_event.bytecode_id },
454 bool error_in_instruction_fetching = ex_event.error == ExecutionError::INSTRUCTION_FETCHING;
455 bool instruction_fetching_success = !bytecode_retrieval_failed && !error_in_instruction_fetching;
456 trace.set(C::execution_sel_instruction_fetching_failure, row, error_in_instruction_fetching ? 1 : 0);
458 if (instruction_fetching_success) {
459 exec_opcode = ex_event.wire_instruction.get_exec_opcode();
464 { C::execution_next_pc,
465 ex_event.before_context_event.pc + ex_event.wire_instruction.size_in_bytes() },
474 bool addressing_failed = ex_event.error == ExecutionError::ADDRESSING;
483 bool should_process_registers = instruction_fetching_success && !addressing_failed;
484 bool register_processing_failed = ex_event.error == ExecutionError::REGISTER_READ;
485 if (should_process_registers) {
493 bool should_check_gas = should_process_registers && !register_processing_failed;
494 bool oog = ex_event.error == ExecutionError::GAS;
495 trace.set(C::execution_sel_should_check_gas, row, should_check_gas ? 1 : 0);
496 if (should_check_gas) {
501 uint32_t radix = ex_event.inputs[1].as<uint32_t>();
502 uint32_t num_limbs = ex_event.inputs[2].as<uint32_t>();
507 { C::execution_two_five_six, 256 },
508 { C::execution_sel_radix_gt_256, radix > 256 ? 1 : 0 },
509 { C::execution_sel_lookup_num_p_limbs, radix <= 256 ? 1 : 0 },
510 { C::execution_num_p_limbs, num_p_limbs },
511 { C::execution_sel_use_num_limbs, num_limbs > num_p_limbs ? 1 : 0 },
527 bool should_execute_opcode = should_check_gas && !oog;
528 bool should_execute_call =
530 bool should_execute_static_call =
532 bool should_execute_return =
534 bool should_execute_revert =
537 bool is_err = ex_event.error != ExecutionError::NONE;
538 bool is_failure = should_execute_revert || is_err;
539 bool sel_enter_call = should_execute_call || should_execute_static_call;
541 bool sel_exit_call = should_execute_return || should_execute_revert || is_err;
548 { C::execution_sel_execute_return, should_execute_return ? 1 : 0 },
549 { C::execution_sel_execute_revert, should_execute_revert ? 1 : 0 },
550 { C::execution_sel_exit_call, 1 },
551 { C::execution_nested_return, should_execute_return && has_parent ? 1 : 0 },
553 { C::execution_enqueued_call_end, !has_parent ? 1 : 0 },
554 { C::execution_nested_exit_call, has_parent ? 1 : 0 },
558 bool opcode_execution_failed = ex_event.error == ExecutionError::OPCODE_EXECUTION;
559 if (should_execute_opcode) {
564 { C::execution_sel_should_execute_opcode, 1 },
565 { C::execution_sel_opcode_error, opcode_execution_failed ? 1 : 0 },
571 trace.set(get_execution_opcode_selector(*exec_opcode), row, 1);
575 if (sel_enter_call) {
576 Gas gas_left = ex_event.after_context_event.gas_limit - ex_event.after_context_event.gas_used;
578 uint32_t allocated_l2_gas =
registers[0].as<uint32_t>();
579 bool is_l2_gas_allocated_lt_left = allocated_l2_gas < gas_left.
l2Gas;
581 uint32_t allocated_da_gas =
registers[1].as<uint32_t>();
582 bool is_da_gas_allocated_lt_left = allocated_da_gas < gas_left.
daGas;
586 { C::execution_sel_enter_call, sel_enter_call ? 1 : 0 },
587 { C::execution_sel_execute_call, should_execute_call ? 1 : 0 },
588 { C::execution_sel_execute_static_call, should_execute_static_call ? 1 : 0 },
589 { C::execution_l2_gas_left, gas_left.
l2Gas },
590 { C::execution_da_gas_left, gas_left.
daGas },
591 { C::execution_call_is_l2_gas_allocated_lt_left, is_l2_gas_allocated_lt_left },
592 { C::execution_call_is_da_gas_allocated_lt_left, is_da_gas_allocated_lt_left },
600 assert(ex_event.addressing_event.resolution_info.size() == 2 &&
601 "GETENVVAR should have exactly two resolved operands (envvar enum and output)");
603 TaggedValue envvar_enum = ex_event.addressing_event.resolution_info[1].resolved_operand;
606 trace.set(C::execution_internal_call_return_id_inv,
608 ex_event.before_context_event.internal_call_return_id);
611 ex_event.before_context_event.tree_states.publicDataTree.counter;
615 { C::execution_max_data_writes_reached, remaining_data_writes == 0 },
616 { C::execution_remaining_data_writes_inv,
617 remaining_data_writes },
618 { C::execution_sel_write_public_data, !opcode_execution_failed },
621 uint64_t leaf_index =
registers[1].as<uint64_t>();
623 bool note_hash_leaf_in_range = leaf_index < note_hash_tree_leaf_count;
627 { C::execution_note_hash_leaf_in_range, note_hash_leaf_in_range },
628 { C::execution_note_hash_tree_leaf_count,
FF(note_hash_tree_leaf_count) },
631 uint32_t remaining_note_hashes =
636 { C::execution_sel_reached_max_note_hashes, remaining_note_hashes == 0 },
637 { C::execution_remaining_note_hashes_inv,
638 remaining_note_hashes },
639 { C::execution_sel_write_note_hash, !opcode_execution_failed },
642 uint64_t leaf_index =
registers[1].as<uint64_t>();
644 bool l1_to_l2_msg_leaf_in_range = leaf_index < l1_to_l2_msg_tree_leaf_count;
648 { C::execution_l1_to_l2_msg_leaf_in_range, l1_to_l2_msg_leaf_in_range },
649 { C::execution_l1_to_l2_msg_tree_leaf_count,
FF(l1_to_l2_msg_tree_leaf_count) },
654 uint32_t remaining_nullifiers =
659 { C::execution_sel_reached_max_nullifiers, remaining_nullifiers == 0 },
660 { C::execution_remaining_nullifiers_inv,
661 remaining_nullifiers },
662 { C::execution_sel_write_nullifier,
663 remaining_nullifiers != 0 && !ex_event.before_context_event.is_static },
666 uint32_t remaining_l2_to_l1_msgs =
670 { { { C::execution_sel_l2_to_l1_msg_limit_error, remaining_l2_to_l1_msgs == 0 },
671 { C::execution_remaining_l2_to_l1_msgs_inv,
672 remaining_l2_to_l1_msgs },
673 { C::execution_sel_write_l2_to_l1_msg, !opcode_execution_failed && !
discard },
675 C::execution_public_inputs_index,
677 ex_event.before_context_event.side_effect_states.numL2ToL1Messages,
686 bool should_process_register_write = should_execute_opcode && !opcode_execution_failed;
687 if (should_process_register_write) {
695 bool is_dying_context =
discard == 1 && (ex_event.after_context_event.id == dying_context_id);
698 FF dying_context_diff_inv = 0;
699 if (!is_dying_context) {
701 FF diff =
FF(ex_event.after_context_event.id) -
FF(dying_context_id);
702 dying_context_diff_inv = diff;
706 bool sel_first_row_in_context = prev_row_was_enter_call || is_first_event_in_enqueued_call;
708 bool enqueued_call_end = sel_exit_call && !has_parent;
709 bool resolves_dying_context = is_failure && is_dying_context;
710 bool nested_call_rom_undiscarded_context = sel_enter_call &&
discard == 0;
711 bool propagate_discard = !enqueued_call_end && !resolves_dying_context && !nested_call_rom_undiscarded_context;
715 bool rollback_context = (should_execute_revert || is_err) && has_parent;
722 { C::execution_sel_exit_call, sel_exit_call ? 1 : 0 },
723 { C::execution_rollback_context, rollback_context ? 1 : 0 },
724 { C::execution_sel_error, is_err ? 1 : 0 },
725 { C::execution_sel_failure, is_failure ? 1 : 0 },
726 { C::execution_discard,
discard },
727 { C::execution_dying_context_id, dying_context_id },
728 { C::execution_dying_context_id_inv, dying_context_id_inv },
729 { C::execution_is_dying_context, is_dying_context ? 1 : 0 },
730 { C::execution_dying_context_diff_inv, dying_context_diff_inv },
731 { C::execution_enqueued_call_end, enqueued_call_end ? 1 : 0 },
732 { C::execution_sel_first_row_in_context, sel_first_row_in_context ? 1 : 0 },
733 { C::execution_resolves_dying_context, resolves_dying_context ? 1 : 0 },
734 { C::execution_nested_call_from_undiscarded_context, nested_call_rom_undiscarded_context ? 1 : 0 },
735 { C::execution_propagate_discard, propagate_discard ? 1 : 0 },
740 bool event_kills_dying_context =
741 discard == 1 && is_failure && ex_event.after_context_event.id == dying_context_id;
743 if (event_kills_dying_context) {
745 dying_context_id = 0;
746 dying_context_id_inv = 0;
748 }
else if (sel_enter_call &&
discard == 0 && !is_err &&
749 failures.does_context_fail.contains(ex_event.next_context_id)) {
754 dying_context_id = ex_event.next_context_id;
755 dying_context_id_inv = dying_context_id;
764 is_first_event_in_enqueued_call = ex_event.after_context_event.parent_id == 0 && sel_exit_call;
767 prev_row_was_enter_call = sel_enter_call;
772 if (!ex_events.empty()) {
773 trace.set(C::execution_last, row - 1, 1);