Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(avm): kernel output opcodes #6416

Merged
merged 80 commits into from
May 28, 2024
Merged

feat(avm): kernel output opcodes #6416

merged 80 commits into from
May 28, 2024

Conversation

Maddiaa0
Copy link
Member

@Maddiaa0 Maddiaa0 commented May 15, 2024

Overview

This pr implements:

  • emit note hash
  • note hash exists (only positive case)
  • nullifier exists (wip)
  • emit nullifier
  • l1 to l2 msg exists
  • emit unencrypted log
  • emit l2 to l1 msg
  • sload
  • sstore

Currently Not Implemented (TODO)

… md/04-12-feat_example_caller_and_address_opcode
@AztecBot
Copy link
Collaborator

AztecBot commented May 15, 2024

Benchmark results

Metrics with a significant change:

  • protocol_circuit_simulation_time_in_ms (public-kernel-setup): 354 (-44%)
  • protocol_circuit_simulation_time_in_ms (public-kernel-teardown): 314 (-41%)
  • protocol_circuit_input_size_in_bytes (public-kernel-setup): 82,270 (-22%)
  • protocol_circuit_input_size_in_bytes (public-kernel-teardown): 82,979 (-21%)
  • protocol_circuit_output_size_in_bytes (public-kernel-setup): 68,469 (-21%)
  • protocol_circuit_output_size_in_bytes (public-kernel-teardown): 69,026 (-20%)
  • protocol_circuit_witness_generation_time_in_ms (base-parity): 765 (-37%)
  • protocol_circuit_witness_generation_time_in_ms (base-rollup): 5,426 (+147%)
  • protocol_circuit_witness_generation_time_in_ms (public-kernel-app-logic): 1,036 (-65%)
  • protocol_circuit_witness_generation_time_in_ms (root-parity): 166 (+144%)
  • protocol_circuit_witness_generation_time_in_ms (public-kernel-tail): 6,080 (-73%)
  • protocol_circuit_witness_generation_time_in_ms (root-rollup): 132 (+108%)
  • protocol_circuit_proving_time_in_ms (base-parity): 8,168 (+179%)
  • protocol_circuit_proving_time_in_ms (public-kernel-app-logic): 12,227 (-74%)
  • protocol_circuit_proving_time_in_ms (base-rollup): 132,636 (+79%)
  • protocol_circuit_proving_time_in_ms (root-parity): 114,652 (+158%)
  • protocol_circuit_proving_time_in_ms (public-kernel-tail): 38,783 (-75%)
  • protocol_circuit_proof_size_in_bytes (public-kernel-app-logic): 77,704 (-32%)
Detailed results

All benchmarks are run on txs on the Benchmarking contract on the repository. Each tx consists of a batch call to create_note and increment_balance, which guarantees that each tx has a private call, a nested private call, a public call, and a nested public call, as well as an emitted private note, an unencrypted log, and public storage read and write.

This benchmark source data is available in JSON format on S3 here.

Proof generation

Each column represents the number of threads used in proof generation.

Metric 1 threads 4 threads 16 threads 32 threads 64 threads
proof_construction_time_sha256 5,700 1,546 713 786 780

L2 block published to L1

Each column represents the number of txs on an L2 block published to L1.

Metric 8 txs 32 txs 64 txs
l1_rollup_calldata_size_in_bytes 1,412 1,412 1,412
l1_rollup_calldata_gas 9,464 9,476 9,476
l1_rollup_execution_gas 616,105 616,117 616,117
l2_block_processing_time_in_ms 1,293 4,823 9,558 (-1%)
l2_block_building_time_in_ms 44,521 (-1%) 176,481 352,937
l2_block_rollup_simulation_time_in_ms 44,351 (-1%) 175,804 351,689
l2_block_public_tx_process_time_in_ms 23,847 (-1%) 99,670 (-1%) 204,204 (-1%)

L2 chain processing

Each column represents the number of blocks on the L2 chain where each block has 16 txs.

Metric 3 blocks 5 blocks
node_history_sync_time_in_ms 9,478 (-1%) 14,509
node_database_size_in_bytes 14,491,728 21,373,008
pxe_database_size_in_bytes 18,071 29,868

Circuits stats

Stats on running time and I/O sizes collected for every kernel circuit run across all benchmarks.

Circuit simulation_time_in_ms witness_generation_time_in_ms proving_time_in_ms input_size_in_bytes output_size_in_bytes proof_size_in_bytes num_public_inputs size_in_gates
private-kernel-init 158 (-2%) 3,776 (+1%) 22,817 20,418 (-1%) 62,921 (-3%) 89,536 2,731 1,048,576
private-kernel-inner 607 (-2%) 4,340 (-2%) 45,944 (+7%) 90,048 (-2%) 62,597 (-3%) 89,536 2,731 2,097,152
private-kernel-tail 571 (+1%) 2,921 (-13%) 36,006 (-3%) 96,541 77,498 10,656 266 2,097,152
base-parity 7.16 (+9%) ⚠️ 765 (-37%) ⚠️ 8,168 (+179%) 128 64.0 2,208 2.00 131,072
root-parity 51.0 (+2%) ⚠️ 166 (+144%) ⚠️ 114,652 (+158%) 27,080 64.0 2,720 18.0 2,097,152
base-rollup 723 (-3%) ⚠️ 5,426 (+147%) ⚠️ 132,636 (+79%) 119,058 766 (+1%) 3,285 (-10%) 47.0 4,194,304
root-rollup 105 (-6%) ⚠️ 132 (+108%) 18,178 (-7%) 22,989 (-9%) 648 (+5%) 3,440 41.0 1,048,576
public-kernel-app-logic 510 (-2%) ⚠️ 1,036 (-65%) ⚠️ 12,227 (-74%) 103,785 (-1%) 85,392 (-1%) ⚠️ 77,704 (-32%) 3,520 2,097,152
public-kernel-tail 1,056 (-3%) ⚠️ 6,080 (-73%) ⚠️ 38,783 (-75%) 384,359 (-3%) 7,530 10,248 (-4%) 266 8,388,608
private-kernel-reset-small 594 (+1%) 2,182 (+4%) 45,391 (-1%) 120,733 64,614 89,536 2,731 2,097,152
private-kernel-ordering 685 N/A N/A 213,464 34,764 N/A N/A N/A
public-kernel-setup ⚠️ 354 (-44%) 276 405 ⚠️ 82,270 (-22%) ⚠️ 68,469 (-21%) 65,344 N/A N/A
public-kernel-teardown ⚠️ 314 (-41%) 310 345 ⚠️ 82,979 (-21%) ⚠️ 69,026 (-20%) 65,344 N/A N/A
merge-rollup 29.1 (+1%) 114 2,469 16,534 756 3,104 N/A N/A
private-kernel-tail-to-public N/A 8,899 (+4%) 92,991 (+2%) N/A N/A 114,784 3,520 4,194,304

Stats on running time collected for app circuits

Function input_size_in_bytes output_size_in_bytes witness_generation_time_in_ms proof_size_in_bytes proving_time_in_ms size_in_gates num_public_inputs
ContractClassRegisterer:register 1,344 9,944 466 (-1%) N/A N/A N/A N/A
ContractInstanceDeployer:deploy 1,408 9,944 41.9 (-1%) N/A N/A N/A N/A
MultiCallEntrypoint:entrypoint 1,920 9,944 1,437 N/A N/A N/A N/A
SchnorrAccount:constructor 1,312 9,944 989 N/A N/A N/A N/A
SchnorrAccount:entrypoint 2,304 9,944 2,091 16,768 51,429 2,097,152 457
Token:privately_mint_private_note 1,280 9,944 1,137 N/A N/A N/A N/A
Token:transfer 1,376 9,944 4,058 16,768 52,979 (+15%) 2,097,152 457
Benchmarking:create_note 1,312 9,944 951 N/A N/A N/A N/A
FPC:fee_entrypoint_public 1,344 9,944 219 N/A N/A N/A N/A
SchnorrAccount:spend_private_authwit 1,280 9,944 77.3 (-1%) N/A N/A N/A N/A
Token:unshield 1,376 9,944 3,248 N/A N/A N/A N/A
FPC:fee_entrypoint_private 1,376 9,944 4,014 (-1%) N/A N/A N/A N/A

Tree insertion stats

The duration to insert a fixed batch of leaves into each tree type.

Metric 1 leaves 16 leaves 64 leaves 128 leaves 512 leaves 1024 leaves 2048 leaves 4096 leaves 32 leaves
batch_insert_into_append_only_tree_16_depth_ms 10.5 (+1%) 17.1 (+1%) N/A N/A N/A N/A N/A N/A N/A
batch_insert_into_append_only_tree_16_depth_hash_count 16.7 31.8 N/A N/A N/A N/A N/A N/A N/A
batch_insert_into_append_only_tree_16_depth_hash_ms 0.611 (+1%) 0.525 (+1%) N/A N/A N/A N/A N/A N/A N/A
batch_insert_into_append_only_tree_32_depth_ms N/A N/A 48.8 76.0 (-2%) 245 474 (-1%) 928 1,837 (-1%) N/A
batch_insert_into_append_only_tree_32_depth_hash_count N/A N/A 95.9 159 543 1,055 2,079 4,127 N/A
batch_insert_into_append_only_tree_32_depth_hash_ms N/A N/A 0.498 0.468 (-2%) 0.444 0.442 (-1%) 0.440 0.439 N/A
batch_insert_into_indexed_tree_20_depth_ms N/A N/A 59.3 (+1%) 112 (-1%) 355 697 (-1%) 1,383 2,761 N/A
batch_insert_into_indexed_tree_20_depth_hash_count N/A N/A 107 (+1%) 208 692 1,363 2,707 5,395 N/A
batch_insert_into_indexed_tree_20_depth_hash_ms N/A N/A 0.510 0.501 (-1%) 0.481 0.478 (-1%) 0.478 0.479 N/A
batch_insert_into_indexed_tree_40_depth_ms N/A N/A N/A N/A N/A N/A N/A N/A 62.9 (+1%)
batch_insert_into_indexed_tree_40_depth_hash_count N/A N/A N/A N/A N/A N/A N/A N/A 107
batch_insert_into_indexed_tree_40_depth_hash_ms N/A N/A N/A N/A N/A N/A N/A N/A 0.554

Miscellaneous

Transaction sizes based on how many contract classes are registered in the tx.

Metric 0 registered classes 1 registered classes
tx_size_in_bytes 82,355 (-2%) 758,659 (+14%)

Transaction size based on fee payment method

| Metric | |
| - | |

@Maddiaa0 Maddiaa0 marked this pull request as ready for review May 16, 2024 19:48
Copy link
Contributor

@jeanmon jeanmon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work! Please look at my feedback before merging.


// When we encounter a state writing opcode
// We increment the side effect counter by 1
KERNEL_OUTPUT_SELECTORS * (avm_kernel.side_effect_counter' - (avm_kernel.side_effect_counter + 1)) = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this crucial to start at zero? (Probably, otherwise a malicious prover might choose a huge value and then you get overflow/wraparound. Not sure about what the kernel is expecting.)

If yes, then we should have a constraint to enforce the initial value to be zero. (maybe sthg like: first * avm_kernel.side_effect_counter =0 )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side effect counter will be required to be constrained to the value of the start_side_effect_counter in the public inputs - this can be done with a copy constraint or via a lookup

// OUTPUTS LOOKUPS
// Constrain the value of kernel_out_sel to be the correct offset for the operation being performed
#[NOTE_HASH_KERNEL_OUTPUT]
sel_op_note_hash_exists * (avm_kernel.kernel_out_sel - (avm_kernel.START_NOTE_HASH_EXISTS_WRITE_OFFSET + avm_kernel.note_hash_exist_write_offset)) = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avm_kernel.note_hash_exist_write_offset should be constrained to be initialized to zero. (maybe on first line?)

Same holds for all other offsets.

pol commit kernel_sel;
pol commit kernel_out_sel;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suffix "sel" makes us think it is a boolean. I think "offset" suffix might be better,

@@ -11,6 +11,17 @@ using Flavor = bb::AvmFlavor;
using FF = Flavor::FF;
using Row = bb::AvmFullRow<bb::fr>;

// There are 4 public input columns, 1 for context inputs, and 3 for emitting side effects
using VM_PUBLIC_INPUTS = std::tuple<std::array<FF, KERNEL_INPUTS_LENGTH>, // Input: Kernel context inputs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current notation in bb for type alias is camel case. We might need to rename "using VmPublicInputs = ..." to be consistent.

return result;
}

void AvmKernelTraceBuilder::perform_kernel_output_lookup(uint32_t write_offset, FF value, FF metadata)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value and metadata can be made "const &"

@Maddiaa0 Maddiaa0 enabled auto-merge (squash) May 23, 2024 11:43
@Maddiaa0 Maddiaa0 merged commit 0281b8f into master May 28, 2024
85 checks passed
@Maddiaa0 Maddiaa0 deleted the md/05-10-kernel_outputs branch May 28, 2024 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants