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!: call stack validation optimisation. #3387

Merged
merged 27 commits into from
Nov 27, 2023
Merged

Conversation

LeilaWang
Copy link
Contributor

@LeilaWang LeilaWang commented Nov 21, 2023

Closes #3060

Before

We passed call stack preimages to the kernel. The kernel then hashed the preimages and check them against the hashes in the app circuits' public inputs. The public kernel also checked the addresses and contexts in the preimages were valid for the types of calls they were making (private kernel should've done the same check, but was not present in the cpp code). The hashes would be aggregated to the accumulated data.
A nested execution would pop the hash from the accumulated data, and checked that it's the same as its own hash.

Now

We pass call requests to the kernel. The kernel checks that the addresses and contexts in those requests match the current call. The requests are aggregated to the accumulated data.
A nested execution pops the call request from the accumulated data, checks that the hash is the same as its own hash, and validates the addresses and contexts are correct for the type of call.

Other changes

  • Remove unnecessary checks in the protocol circuits:
    • Array lengths when they were already fixed sizes.
    • Format of the arrays from previous kernel.
  • Check the function exist in the tree using contract address instead of storage contract address.
  • Removing yarn:remake-bindings: Stop generating types and utils from cpp.
  • Deleting some types and utils generated from cpp that are not used anywhere. (A thorough cleanup will be in another PR.)

Checklist:

Remove the checklist to signal you've completed it. Enable auto-merge if the PR is ready to merge.

  • If the pull request requires a cryptography review (e.g. cryptographic algorithm implementations) I have added the 'crypto' tag.
  • I have reviewed my diff in github, line by line and removed unexpected formatting changes, testing logs, or commented-out code.
  • Every change is related to the PR description.
  • I have linked this pull request to relevant issues (if any exist).

@LeilaWang LeilaWang marked this pull request as draft November 22, 2023 13:34
@AztecBot
Copy link
Collaborator

AztecBot commented Nov 23, 2023

Benchmark results

Metrics with a significant change:

  • circuit_simulation_time_in_ms (private-kernel-init): 381 (-50%)
  • circuit_simulation_time_in_ms (base-rollup): 4,091 (+133%)
  • circuit_simulation_time_in_ms (public-kernel-private-input): 397 (-31%)
  • circuit_input_size_in_bytes (private-kernel-init): 43,077 (-30%)
  • circuit_input_size_in_bytes (private-kernel-inner): 64,484 (-21%)
  • circuit_input_size_in_bytes (public-kernel-private-input): 25,203 (-39%)
  • circuit_input_size_in_bytes (public-kernel-non-first-iteration): 25,245 (-39%)
  • circuit_output_size_in_bytes (private-kernel-ordering): 9,689 (+19%)
  • tx_size_in_bytes (0): 10,323 (+17%)
  • l2_block_building_time_in_ms (8): 28,826 (+40%)
  • l2_block_building_time_in_ms (32): 113,834 (+39%)
  • l2_block_building_time_in_ms (128): 453,852 (+38%)
  • l2_block_rollup_simulation_time_in_ms (8): 21,564 (+81%)
  • l2_block_rollup_simulation_time_in_ms (32): 85,254 (+80%)
  • l2_block_rollup_simulation_time_in_ms (128): 338,588 (+79%)
  • l2_block_public_tx_process_time_in_ms (8): 7,217 (-16%)
  • l2_block_public_tx_process_time_in_ms (32): 28,436 (-17%)
  • l2_block_public_tx_process_time_in_ms (128): 114,721 (-17%)
  • note_trial_decrypting_time_in_ms (8): 98.5 (+1610%)
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.

Values are compared against data from master at commit 2001d474 and shown if the difference exceeds 1%.

L2 block published to L1

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

Metric 8 txs 32 txs 128 txs
l1_rollup_calldata_size_in_bytes 45,444 179,588 716,132
l1_rollup_calldata_gas 222,984 868,172 3,449,528
l1_rollup_execution_gas 842,071 3,595,280 22,204,897
l2_block_processing_time_in_ms 2,027 7,661 (-1%) 29,953
note_successful_decrypting_time_in_ms 296 (-1%) 881 3,185 (-1%)
note_trial_decrypting_time_in_ms ⚠️ 98.5 (+1610%) 92.0 141 (+1%)
l2_block_building_time_in_ms ⚠️ 28,826 (+40%) ⚠️ 113,834 (+39%) ⚠️ 453,852 (+38%)
l2_block_rollup_simulation_time_in_ms ⚠️ 21,564 (+81%) ⚠️ 85,254 (+80%) ⚠️ 338,588 (+79%)
l2_block_public_tx_process_time_in_ms ⚠️ 7,217 (-16%) ⚠️ 28,436 (-17%) ⚠️ 114,721 (-17%)

L2 chain processing

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

Metric 5 blocks 10 blocks
node_history_sync_time_in_ms 21,879 42,500
note_history_successful_decrypting_time_in_ms 2,045 3,978
note_history_trial_decrypting_time_in_ms 127 (+1%) 156 (+1%)
node_database_size_in_bytes 1,630,605 1,095,535
pxe_database_size_in_bytes 29,748 59,307

Circuits stats

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

Circuit circuit_simulation_time_in_ms circuit_input_size_in_bytes circuit_output_size_in_bytes
private-kernel-init ⚠️ 381 (-50%) ⚠️ 43,077 (-30%) 20,441 (+8%)
private-kernel-ordering 121 (-5%) 25,833 (+6%) ⚠️ 9,689 (+19%)
base-rollup ⚠️ 4,091 (+133%) 659,500 873
root-rollup 142 (-15%) 4,072 1,097
private-kernel-inner 788 (-1%) ⚠️ 64,484 (-21%) 20,441 (+8%)
public-kernel-private-input ⚠️ 397 (-31%) ⚠️ 25,203 (-39%) 20,441 (+8%)
public-kernel-non-first-iteration 390 (-5%) ⚠️ 25,245 (-39%) 20,441 (+8%)
merge-rollup 16.1 (+5%) 2,592 873

Miscellaneous

Transaction sizes based on how many contracts are deployed in the tx.

Metric 0 deployed contracts 1 deployed contracts
tx_size_in_bytes ⚠️ 10,323 (+17%) 29,083 (+6%)

@iAmMichaelConnor
Copy link
Contributor

Nice speed improvements with this!! Why does the tx size (the output of the final ordering circuit) increase so significantly? (I'm looking at the automated benchmarks for this PR)

@LeilaWang
Copy link
Contributor Author

It's now outputting an array of CallRequests instead of fields. CallRequest has a hash, contract address, storage contract address, and msg_sender. So that's 3 more fields per request and 16 * 3 fields in total.

@iAmMichaelConnor
Copy link
Contributor

Ah, yes that makes sense!

@@ -2,7 +2,7 @@ import { expectReserializeToMatchObject, expectSerializeToMatchSnapshot } from '
import { makeBaseOrMergeRollupPublicInputs, makeBaseRollupInputs } from '../../tests/factories.js';
import { BaseOrMergeRollupPublicInputs } from './base_or_merge_rollup_public_inputs.js';

describe('structs/base_rollup', () => {
describe.skip('structs/base_rollup', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tests and utils using circuits.wasm will be deleted and rewritten in another PR.

@@ -157,14 +159,6 @@ describe('Kernel Prover', () => {
}
});

it('should throw if call stack is too deep', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't have to check in the prover. It will fail to simulate and to create the proof.

@@ -325,14 +325,6 @@ pub fn contract_logic(private_call : PrivateCallData, public_inputs : &mut Kerne
* - Compute the contract_leaf: hash(contract_address, portal_contract_address, function_tree_root)
* - Hash the contract_leaf with the contract_leaf's sibling_path to get the contract_tree_root
*/

// Ensures that if the function is internal, only the contract itself can call it
if private_call.call_stack_item.function_data.is_internal {
Copy link
Contributor Author

@LeilaWang LeilaWang Nov 23, 2023

Choose a reason for hiding this comment

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

Moved it to validate_call_stack and only checks for nested calls. The first call can't be internal, which is checked in the init kernel.

Copy link

New dependencies detected. Learn more about Socket for GitHub ↗︎

Packages Version New capabilities Transitives Size Publisher
@types/lodash.flatmap 4.5.7 None +0 3.8 kB types
@types/lodash.upperfirst 4.3.7 None +0 3.84 kB types
vite 4.4.8 eval, network, filesystem, shell, environment +1 12.6 MB vitebot
unzipit 1.4.3 network +0 168 kB greggman
tsc-multi 1.1.0 None +0 59.3 kB tommy351
@types/koa__cors 4.0.0 None +0 5.51 kB types
@tsconfig/docusaurus 1.0.6 None +0 2.69 kB typescript-deploys
formik 2.4.3 environment +0 581 kB jaredpalmer
@types/node 20.6.3 None +0 3.86 MB types
@libp2p/interface 0.1.2 None +1 273 kB npm-service-account-libp2p
ts-loader 9.5.1 None +0 258 kB johnnyreilly

@LeilaWang LeilaWang marked this pull request as ready for review November 23, 2023 17:02
Copy link
Contributor

@sirasistant sirasistant left a comment

Choose a reason for hiding this comment

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

Nice optimization and cleanup! 🥳

validate_array(end.public_call_stack);
validate_array(end.new_l2_to_l1_msgs);
}
pub fn validate_call_stack(private_call: PrivateCallData, request: CallRequest) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we could name this validate_call_against_request or something like that? It was not clear to me that this was checking the current call being processed because it said stack


// Pops the current function execution from the stack and validates it against the call stack item

pub fn validate_call_stack(public_call: PublicCallData, request: CallRequest) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as with the private one!

/* eslint-disable */
// GENERATED FILE DO NOT EDIT, RUN yarn remake-bindings
/* eslint-disable camelcase,jsdoc/require-jsdoc */
// TODO: Remove this file as we no longer generate types from cpp.
Copy link
Contributor

Choose a reason for hiding this comment

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

letsgo! 🎉

@LeilaWang LeilaWang merged commit d06d5db into master Nov 27, 2023
82 checks passed
@LeilaWang LeilaWang deleted the lw/kernel_refactor branch November 27, 2023 13:55
kevaundray pushed a commit that referenced this pull request Nov 27, 2023
🤖 I have created a release *beep* *boop*
---


<details><summary>aztec-packages: 0.16.0</summary>

##
[0.16.0](aztec-packages-v0.15.1...aztec-packages-v0.16.0)
(2023-11-27)


### ⚠ BREAKING CHANGES

* deprecate circuits/cpp
([#3421](#3421))
* call stack validation optimisation.
([#3387](#3387))

### Features

* Base rollup in noir
([#3257](#3257))
([4a1e9c3](4a1e9c3))
* Call stack validation optimisation.
([#3387](#3387))
([d06d5db](d06d5db))
* Goblin proof construction
([#3332](#3332))
([6a7ebb6](6a7ebb6))
* More logs relevant for debugging failures of 2 pixies test
([#3370](#3370))
([683a0f3](683a0f3))
* Noir subrepo.
([#3369](#3369))
([d94d88b](d94d88b))
* Noir_wasm compilation of noir programs
([#3272](#3272))
([f9981d5](f9981d5))
* Rollback public state changes on failure
([#3393](#3393))
([0e276fb](0e276fb))


### Bug Fixes

* **docs:** Doc explaining noir debug_log
([#3322](#3322))
([eed023d](eed023d))
* Naming inconsistency in private kernel
([#3384](#3384))
([4743486](4743486))
* Race condition in `PXE.getTxReceipt(...)`
([#3411](#3411))
([9557a66](9557a66))


### Miscellaneous

* Deprecate circuits/cpp
([#3421](#3421))
([4973cfb](4973cfb))
* Deterministically deduplicate
`cached_partial_non_native_field_multiplication` across wasm32 and
native compilations
([#3425](#3425))
([5524933](5524933))
* **docs:** Common patterns and anti patterns in aztec.nr
([#3413](#3413))
([65bd855](65bd855))
* Fix and reenable e2e quick start
([#3403](#3403))
([112740e](112740e)),
closes
[#3356](#3356)
* Fix intermittent failures for block-building e2e test
([#3404](#3404))
([e76e2d4](e76e2d4)),
closes
[#3358](#3358)
* Formatted `noir-contracts` and `aztec-nr`
([a73c4aa](a73c4aa))
* Initial clone of noir to subrepo
([#3409](#3409))
([8f1cb83](8f1cb83))
* **noir-contracts:** Remove redundant return value of 1
([#3415](#3415))
([2001d47](2001d47)),
closes
[#2615](#2615)
* Plumbs noir subrepo into yarn-project.
([#3420](#3420))
([63173c4](63173c4))
* Remove pxe / node /p2p-bootstrap docker images
([#3396](#3396))
([c236143](c236143))
* Skip artifacts for prettier
([#3399](#3399))
([98d9e04](98d9e04))
* Update path to acir artifacts
([#3426](#3426))
([f56f88d](f56f88d))
</details>

<details><summary>barretenberg.js: 0.16.0</summary>

##
[0.16.0](barretenberg.js-v0.15.1...barretenberg.js-v0.16.0)
(2023-11-27)


### Miscellaneous

* Plumbs noir subrepo into yarn-project.
([#3420](#3420))
([63173c4](63173c4))
</details>

<details><summary>barretenberg: 0.16.0</summary>

##
[0.16.0](barretenberg-v0.15.1...barretenberg-v0.16.0)
(2023-11-27)


### Features

* Goblin proof construction
([#3332](#3332))
([6a7ebb6](6a7ebb6))
* Noir subrepo.
([#3369](#3369))
([d94d88b](d94d88b))


### Miscellaneous

* Deterministically deduplicate
`cached_partial_non_native_field_multiplication` across wasm32 and
native compilations
([#3425](#3425))
([5524933](5524933))
* Plumbs noir subrepo into yarn-project.
([#3420](#3420))
([63173c4](63173c4))
* Update path to acir artifacts
([#3426](#3426))
([f56f88d](f56f88d))
</details>

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
AztecBot added a commit to AztecProtocol/barretenberg that referenced this pull request Nov 28, 2023
🤖 I have created a release *beep* *boop*
---


<details><summary>aztec-packages: 0.16.0</summary>

##
[0.16.0](AztecProtocol/aztec-packages@aztec-packages-v0.15.1...aztec-packages-v0.16.0)
(2023-11-27)


### ⚠ BREAKING CHANGES

* deprecate circuits/cpp
([#3421](AztecProtocol/aztec-packages#3421))
* call stack validation optimisation.
([#3387](AztecProtocol/aztec-packages#3387))

### Features

* Base rollup in noir
([#3257](AztecProtocol/aztec-packages#3257))
([4a1e9c3](AztecProtocol/aztec-packages@4a1e9c3))
* Call stack validation optimisation.
([#3387](AztecProtocol/aztec-packages#3387))
([d06d5db](AztecProtocol/aztec-packages@d06d5db))
* Goblin proof construction
([#3332](AztecProtocol/aztec-packages#3332))
([6a7ebb6](AztecProtocol/aztec-packages@6a7ebb6))
* More logs relevant for debugging failures of 2 pixies test
([#3370](AztecProtocol/aztec-packages#3370))
([683a0f3](AztecProtocol/aztec-packages@683a0f3))
* Noir subrepo.
([#3369](AztecProtocol/aztec-packages#3369))
([d94d88b](AztecProtocol/aztec-packages@d94d88b))
* Noir_wasm compilation of noir programs
([#3272](AztecProtocol/aztec-packages#3272))
([f9981d5](AztecProtocol/aztec-packages@f9981d5))
* Rollback public state changes on failure
([#3393](AztecProtocol/aztec-packages#3393))
([0e276fb](AztecProtocol/aztec-packages@0e276fb))


### Bug Fixes

* **docs:** Doc explaining noir debug_log
([#3322](AztecProtocol/aztec-packages#3322))
([eed023d](AztecProtocol/aztec-packages@eed023d))
* Naming inconsistency in private kernel
([#3384](AztecProtocol/aztec-packages#3384))
([4743486](AztecProtocol/aztec-packages@4743486))
* Race condition in `PXE.getTxReceipt(...)`
([#3411](AztecProtocol/aztec-packages#3411))
([9557a66](AztecProtocol/aztec-packages@9557a66))


### Miscellaneous

* Deprecate circuits/cpp
([#3421](AztecProtocol/aztec-packages#3421))
([4973cfb](AztecProtocol/aztec-packages@4973cfb))
* Deterministically deduplicate
`cached_partial_non_native_field_multiplication` across wasm32 and
native compilations
([#3425](AztecProtocol/aztec-packages#3425))
([5524933](AztecProtocol/aztec-packages@5524933))
* **docs:** Common patterns and anti patterns in aztec.nr
([#3413](AztecProtocol/aztec-packages#3413))
([65bd855](AztecProtocol/aztec-packages@65bd855))
* Fix and reenable e2e quick start
([#3403](AztecProtocol/aztec-packages#3403))
([112740e](AztecProtocol/aztec-packages@112740e)),
closes
[#3356](AztecProtocol/aztec-packages#3356)
* Fix intermittent failures for block-building e2e test
([#3404](AztecProtocol/aztec-packages#3404))
([e76e2d4](AztecProtocol/aztec-packages@e76e2d4)),
closes
[#3358](AztecProtocol/aztec-packages#3358)
* Formatted `noir-contracts` and `aztec-nr`
([a73c4aa](AztecProtocol/aztec-packages@a73c4aa))
* Initial clone of noir to subrepo
([#3409](AztecProtocol/aztec-packages#3409))
([8f1cb83](AztecProtocol/aztec-packages@8f1cb83))
* **noir-contracts:** Remove redundant return value of 1
([#3415](AztecProtocol/aztec-packages#3415))
([2001d47](AztecProtocol/aztec-packages@2001d47)),
closes
[#2615](AztecProtocol/aztec-packages#2615)
* Plumbs noir subrepo into yarn-project.
([#3420](AztecProtocol/aztec-packages#3420))
([63173c4](AztecProtocol/aztec-packages@63173c4))
* Remove pxe / node /p2p-bootstrap docker images
([#3396](AztecProtocol/aztec-packages#3396))
([c236143](AztecProtocol/aztec-packages@c236143))
* Skip artifacts for prettier
([#3399](AztecProtocol/aztec-packages#3399))
([98d9e04](AztecProtocol/aztec-packages@98d9e04))
* Update path to acir artifacts
([#3426](AztecProtocol/aztec-packages#3426))
([f56f88d](AztecProtocol/aztec-packages@f56f88d))
</details>

<details><summary>barretenberg.js: 0.16.0</summary>

##
[0.16.0](AztecProtocol/aztec-packages@barretenberg.js-v0.15.1...barretenberg.js-v0.16.0)
(2023-11-27)


### Miscellaneous

* Plumbs noir subrepo into yarn-project.
([#3420](AztecProtocol/aztec-packages#3420))
([63173c4](AztecProtocol/aztec-packages@63173c4))
</details>

<details><summary>barretenberg: 0.16.0</summary>

##
[0.16.0](AztecProtocol/aztec-packages@barretenberg-v0.15.1...barretenberg-v0.16.0)
(2023-11-27)


### Features

* Goblin proof construction
([#3332](AztecProtocol/aztec-packages#3332))
([6a7ebb6](AztecProtocol/aztec-packages@6a7ebb6))
* Noir subrepo.
([#3369](AztecProtocol/aztec-packages#3369))
([d94d88b](AztecProtocol/aztec-packages@d94d88b))


### Miscellaneous

* Deterministically deduplicate
`cached_partial_non_native_field_multiplication` across wasm32 and
native compilations
([#3425](AztecProtocol/aztec-packages#3425))
([5524933](AztecProtocol/aztec-packages@5524933))
* Plumbs noir subrepo into yarn-project.
([#3420](AztecProtocol/aztec-packages#3420))
([63173c4](AztecProtocol/aztec-packages@63173c4))
* Update path to acir artifacts
([#3426](AztecProtocol/aztec-packages#3426))
([f56f88d](AztecProtocol/aztec-packages@f56f88d))
</details>

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Remove validate_call_stack and the private_call_stack_preimages
4 participants