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

[DO_NOT_MERGE] feat: optimizing private_set.nr #7798

Closed
wants to merge 7 commits into from

Conversation

benesjan
Copy link
Contributor

@benesjan benesjan commented Aug 6, 2024

We unnecessarily compute note_hash_for_read_request twice when removing a note: once when getting it via get_notes and the second time in PrivateSet::remove. This is expensive. This PRs removes the redundant hash computation by passing the hashes from get_notes.

Gate count change of Token transfer:
Before 93k gates.

After:

image

Gate count change: 93k-89k=4k gates savings.

Note for reviewer

I initially had 2 get notes functions: get_notes and get_notes_and_hashes. As the name implies get_notes returned "pure notes" and get_notes_and_hashes returned both the note and the hash. When I checked usage of the get_notes method it was only used in test contracts and hence would not consider them a real use case. So it generally seems that whenever we obtain a note we will most likely also want to destroy it. Hence I merged the 2 functions and now there is only 1 get_notes func.

Copy link
Contributor Author

benesjan commented Aug 6, 2024

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @benesjan and the rest of your teammates on Graphite Graphite

@AztecBot
Copy link
Collaborator

AztecBot commented Aug 6, 2024

Benchmark results

Metrics with a significant change:

  • app_circuit_witness_generation_time_in_ms (Token:unshield): 883 (+16%)
  • avm_simulation_time_ms (Token:transfer_public): 24.5 (-47%)
  • avm_simulation_time_ms (Token:_increase_public_balance): 9.48 (-88%)
  • avm_simulation_time_ms (FPC:pay_refund_with_shielded_rebate): 141 (+92%)
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_ms 5,765 (+1%) 1,555 (+1%) 718 (+2%) 755 (+1%) 772
proof_construction_time_sha256_30_ms 11,957 (+2%) 3,194 (+2%) 1,432 (+2%) 1,441 (+1%) 1,486 (+1%)
proof_construction_time_sha256_100_ms 44,344 (+1%) 12,645 (+2%) 5,824 (+3%) 5,677 (+2%) 5,541 (+1%)
proof_construction_time_poseidon_hash_ms 78.0 34.0 34.0 58.0 (-3%) 88.0
proof_construction_time_poseidon_hash_30_ms 1,530 (+1%) 420 (+1%) 201 (+1%) 222 271 (+3%)
proof_construction_time_poseidon_hash_100_ms 5,663 (+1%) 1,530 (+1%) 678 740 (+3%) 753 (+1%)

L2 block published to L1

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

Metric 4 txs 8 txs 16 txs
l1_rollup_calldata_size_in_bytes 740 740 740
l1_rollup_calldata_gas 6,716 6,710 6,704
l1_rollup_execution_gas 611,983 612,128 612,273
l2_block_processing_time_in_ms 257 (+7%) 434 (-3%) 838 (+6%)
l2_block_building_time_in_ms 9,819 (+1%) 18,815 (+1%) 38,456 (+4%)
l2_block_rollup_simulation_time_in_ms 9,818 (+1%) 18,815 (+1%) 38,456 (+4%)
l2_block_public_tx_process_time_in_ms 8,179 16,964 (+1%) 36,525 (+4%)

L2 chain processing

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

Metric 3 blocks 5 blocks
node_history_sync_time_in_ms 2,610 (-6%) 3,507 (-4%)
node_database_size_in_bytes 12,722,256 16,846,928
pxe_database_size_in_bytes 16,254 26,813

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 input_size_in_bytes output_size_in_bytes proving_time_in_ms proof_size_in_bytes num_public_inputs size_in_gates
private-kernel-init 97.5 (+1%) 390 (-1%) 21,846 44,858 N/A N/A N/A N/A
private-kernel-inner 176 (+1%) 694 (-1%) 72,545 45,005 N/A N/A N/A N/A
private-kernel-tail 685 808 53,216 50,574 N/A N/A N/A N/A
base-parity 5.90 (+1%) 592 (-2%) 160 96.0 1,722 13,188 19.0 65,536
root-parity 110 130 (+2%) 69,084 96.0 31,047 13,188 19.0 4,194,304
base-rollup 3,064 (+2%) 5,305 (+2%) 182,736 664 98,144 (+2%) 14,020 45.0 16,777,216
root-rollup 97.6 116 (-4%) 54,525 716 28,066 (-1%) 13,988 44.0 4,194,304
public-kernel-setup 90.0 (-4%) 2,807 103,513 70,710 17,309 (-6%) 128,708 3,629 2,097,152
public-kernel-app-logic 107 (+2%) 4,203 (+2%) 103,513 70,710 10,217 128,708 3,629 1,048,576
public-kernel-tail 580 (+1%) 29,351 (-5%) 403,238 11,326 101,819 (-6%) 27,204 457 16,777,216
private-kernel-reset-tiny 185 (+1%) 829 68,621 44,750 N/A N/A N/A N/A
private-kernel-tail-to-public 3,356 1,532 (-2%) 942,862 1,697 N/A N/A N/A N/A
public-kernel-teardown 85.6 (-2%) 4,127 (+1%) 103,513 70,710 18,878 (+1%) 128,708 3,629 2,097,152
merge-rollup 60.7 (+1%) N/A 35,742 664 N/A N/A N/A N/A
undefined N/A N/A N/A N/A 69,480 (-1%) N/A N/A N/A

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
ContractClassRegisterer:register 1,344 11,731 341 N/A N/A
ContractInstanceDeployer:deploy 1,408 11,731 18.2 N/A N/A
MultiCallEntrypoint:entrypoint 1,920 11,731 480 N/A N/A
FeeJuice:deploy 1,376 11,731 386 (-1%) N/A N/A
SchnorrAccount:constructor 1,312 11,731 178 N/A N/A
SchnorrAccount:entrypoint 2,304 11,731 548 (+1%) N/A N/A
Token:privately_mint_private_note 1,280 11,731 222 N/A N/A
FPC:fee_entrypoint_public 1,344 11,731 28.7 (-9%) N/A N/A
Token:transfer 1,312 11,731 478 (-2%) N/A N/A
AuthRegistry:set_authorized (avm) 18,491 N/A N/A 147,456 2,480 (+1%)
FPC:prepare_fee (avm) 22,958 N/A N/A 147,520 3,150
Token:transfer_public (avm) 61,614 N/A N/A 147,520 17,776 (+3%)
AuthRegistry:consume (avm) 41,719 N/A N/A 147,488 7,707 (+5%)
FPC:pay_refund (avm) 26,227 N/A N/A 147,488 7,403 (+1%)
Benchmarking:create_note 1,344 11,731 173 (+4%) N/A N/A
SchnorrAccount:verify_private_authwit 1,280 11,731 27.4 (-3%) N/A N/A
Token:unshield 1,376 11,731 ⚠️ 883 (+16%) N/A N/A
FPC:fee_entrypoint_private 1,376 11,731 1,134 (+4%) N/A N/A

AVM Simulation

Time to simulate various public functions in the AVM.

Function time_ms bytecode_size_in_bytes
FeeJuice:_increase_public_balance 92.4 (-4%) 8,139
FeeJuice:set_portal 11.4 (+13%) 2,362
Token:constructor 120 (-1%) 31,107
FPC:constructor 91.5 (-6%) 22,380
FeeJuice:mint_public 81.0 (+2%) 6,150
Token:mint_public 95.4 (+14%) 11,720
Token:assert_minter_and_mint 84.3 (-6%) 8,028
AuthRegistry:set_authorized 10.5 (+16%) 4,537
FPC:prepare_fee 282 (-9%) 8,812
Token:transfer_public ⚠️ 24.5 (-47%) 47,374
FPC:pay_refund 65.9 (-4%) 12,114
Benchmarking:increment_balance 1,028 (+2%) 7,450
Token:_increase_public_balance ⚠️ 9.48 (-88%) 8,960
FPC:pay_refund_with_shielded_rebate ⚠️ 141 (+92%) 12,663

Public DB Access

Time to access various public DBs.

Function time_ms
get-nullifier-index 0.159 (-3%)

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 256 leaves 512 leaves 1024 leaves
batch_insert_into_append_only_tree_16_depth_ms 2.17 3.87 N/A N/A N/A N/A N/A
batch_insert_into_append_only_tree_16_depth_hash_count 16.8 31.7 N/A N/A N/A N/A N/A
batch_insert_into_append_only_tree_16_depth_hash_ms 0.112 0.109 (-1%) N/A N/A N/A N/A N/A
batch_insert_into_append_only_tree_32_depth_ms N/A N/A 11.0 (-1%) 18.2 32.0 (+5%) 59.0 117 (+5%)
batch_insert_into_append_only_tree_32_depth_hash_count N/A N/A 95.9 159 287 543 1,055
batch_insert_into_append_only_tree_32_depth_hash_ms N/A N/A 0.105 (-2%) 0.106 0.103 (+4%) 0.102 0.104 (+4%)
batch_insert_into_indexed_tree_20_depth_ms N/A N/A 14.3 25.7 (+1%) 45.5 (+5%) 82.0 (+1%) 173 (+9%)
batch_insert_into_indexed_tree_20_depth_hash_count N/A N/A 109 207 355 691 1,363
batch_insert_into_indexed_tree_20_depth_hash_ms N/A N/A 0.107 0.104 (+1%) 0.110 (+5%) 0.102 (+1%) 0.109 (+6%)
batch_insert_into_indexed_tree_40_depth_ms N/A N/A 16.6 (+1%) N/A N/A N/A N/A
batch_insert_into_indexed_tree_40_depth_hash_count N/A N/A 132 N/A N/A N/A N/A
batch_insert_into_indexed_tree_40_depth_hash_ms N/A N/A 0.106 N/A N/A N/A N/A

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 63,593 665,008

Transaction size based on fee payment method

| Metric | |
| - | |

@benesjan benesjan force-pushed the 08-06-feat_optimizing_private_set.nr branch from d53fe8c to 34acf74 Compare August 7, 2024 08:41
@benesjan benesjan changed the title feat: optimizing private_set.nr feat: optimizing private_set.nr Aug 7, 2024
@@ -120,42 +121,46 @@ impl Deck<&mut PrivateContext> {
inserted_cards
}

pub fn get_cards<N>(&mut self, cards: [Card; N]) -> [CardNote; N] {
pub fn get_cards_and_note_hashes<N>(&mut self, cards: [Card; N]) -> ([CardNote; N], [Field; N]) {
Copy link
Contributor Author

@benesjan benesjan Aug 7, 2024

Choose a reason for hiding this comment

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

This function became a bit ugly with the notes and hashes because it didn't work well with the custom CardNote struct (made it difficult to couple the note and hash in the NoteAndHash struct). But I decided against doing a larger refactor since the ugliness is mostly just in this contract.

@benesjan benesjan force-pushed the 08-06-feat_optimizing_private_set.nr branch from e399ddf to 5804783 Compare August 7, 2024 10:42
@benesjan benesjan requested a review from nventuro August 7, 2024 12:50
@benesjan benesjan marked this pull request as ready for review August 7, 2024 12:58
@benesjan benesjan force-pushed the 08-06-feat_optimizing_private_set.nr branch from 5804783 to b79e4e4 Compare August 7, 2024 13:56
@benesjan benesjan marked this pull request as draft August 7, 2024 15:49
@benesjan benesjan removed the request for review from nventuro August 7, 2024 15:49
@benesjan benesjan changed the title feat: optimizing private_set.nr [DO_NOT_MERGE] feat: optimizing private_set.nr Aug 7, 2024
@benesjan
Copy link
Contributor Author

benesjan commented Aug 8, 2024

Closed in favor of #7834

@benesjan benesjan closed this Aug 8, 2024
@benesjan benesjan deleted the 08-06-feat_optimizing_private_set.nr branch August 8, 2024 11:06
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.

2 participants