Skip to content

Commit

Permalink
feat(aztec-noir): align public and private execution patterns (#1515)
Browse files Browse the repository at this point in the history
## Metadata
**fixes** 
- AztecProtocol/aztec-packages#1484

**Relevant discussions**
-
AztecProtocol/aztec-packages#1358 (comment)


## Overview
This PR aims to align the implementations of Public and Private state.
- Currently public circuits do not have a context object, this will
cause developers to have to learn more than one code path.


**Other work I am thinking of completing within this PR**
Right now, `PublicCircuitPublicInputs` (The object which is fed to the
public kernel after circuit completion) is not created within the
application circuit. It is created within the sequencer. If we zoom out,
this is not a direct security concern as execution is occuring within
the sequencer, however it does break away from the pattern seen within
`PrivateExecution` where the result of each execution is the
`PrivateCircuitPublicInputs` object.

We could leave this as is, (alot of this code will be dumped when the vm
arrives). However similar code paths will make building the library
easier. For example, for some library methods we have two
implementations, one for public and one for private. More and more
standardisation would allow these methods to take in a generic context
and utilise the same code paths. (A goal I think is worth pursuing).

# 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](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue)
this pull request to relevant issues (if any exist).
  • Loading branch information
superstar0402 committed Aug 15, 2023
1 parent 736b5da commit 59c0475
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use dep::value_note::{
};

use dep::aztec::{
context::Context,
context::PrivateContext,
log::emit_encrypted_log,
note::note_getter_options::NoteGetterOptions,
oracle::get_public_key::get_public_key,
Expand Down Expand Up @@ -39,7 +39,7 @@ impl EasyPrivateUint {
// Very similar to `send_note`.
fn add(
self,
context: &mut Context,
context: &mut PrivateContext,
addend: u120,
owner: Field,
) {
Expand All @@ -53,7 +53,7 @@ impl EasyPrivateUint {
let owner_key = get_public_key(owner);
emit_encrypted_log(
context,
context.inputs.call_context.storage_contract_address,
(*context).this_address(),
self.set.storage_slot,
owner_key,
addend_note.serialise(),
Expand All @@ -63,7 +63,7 @@ impl EasyPrivateUint {
// Very similar to `spend_note`.
fn sub(
self,
context: &mut Context,
context: &mut PrivateContext,
subtrahend: u120,
owner: Field,
) {
Expand Down Expand Up @@ -106,7 +106,7 @@ impl EasyPrivateUint {

emit_encrypted_log(
context,
context.inputs.call_context.storage_contract_address,
(*context).this_address(),
self.set.storage_slot,
owner_key,
encrypted_data,
Expand Down
38 changes: 19 additions & 19 deletions yarn-project/noir-libs/noir-aztec/src/abi.nr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::constants_gen::{
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL,
MAX_PUBLIC_DATA_READS_PER_CALL,
GENERATOR_INDEX__FUNCTION_ARGS,
CONSTANT_HISTORIC_BLOCK_DATA_LENGTH,
HISTORIC_BLOCK_DATA_LENGTH,
CONTRACT_DEPLOYMENT_DATA_LENGTH,
CALL_CONTEXT_LENGTH,
PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH,
Expand Down Expand Up @@ -132,27 +132,27 @@ struct HistoricBlockData {
contract_tree_root : Field,
l1_to_l2_messages_tree_root : Field,
blocks_tree_root: Field,
prev_global_variables_hash: Field,
public_data_tree_root: Field,
global_variables_hash: Field,
}

impl HistoricBlockData {
// NOTE: this order must match the order in `private_circuit_public_inputs.hpp`
fn serialize(self) -> [Field; CONSTANT_HISTORIC_BLOCK_DATA_LENGTH] {
fn serialize(self) -> [Field; HISTORIC_BLOCK_DATA_LENGTH] {
[
self.private_data_tree_root,
self.nullifier_tree_root,
self.contract_tree_root,
self.l1_to_l2_messages_tree_root,
self.blocks_tree_root,
self.prev_global_variables_hash,
self.public_data_tree_root
self.public_data_tree_root,
self.global_variables_hash,
]
}
}

fn empty_block_data() -> HistoricBlockData {
HistoricBlockData{ private_data_tree_root: 0, nullifier_tree_root: 0, contract_tree_root: 0, l1_to_l2_messages_tree_root: 0, blocks_tree_root: 0, prev_global_variables_hash: 0, public_data_tree_root: 0 }
fn empty() -> Self {
Self { private_data_tree_root: 0, nullifier_tree_root: 0, contract_tree_root: 0, l1_to_l2_messages_tree_root: 0, blocks_tree_root: 0, public_data_tree_root: 0, global_variables_hash: 0 }
}
}

struct FunctionData {
Expand Down Expand Up @@ -249,10 +249,6 @@ struct ContractStorageRead {
value: Field,
}

fn empty_contract_storage_read() -> ContractStorageRead {
ContractStorageRead { storage_slot: 0, value: 0 }
}

impl ContractStorageRead {
fn serialize(self) -> [Field; CONTRACT_STORAGE_READ_LENGTH] {
[self.storage_slot, self.value]
Expand All @@ -261,6 +257,10 @@ impl ContractStorageRead {
fn hash(self) -> Field {
dep::std::hash::pedersen_with_separator(self.serialize(), GENERATOR_INDEX__PUBLIC_DATA_READ)[0]
}

fn empty() -> Self {
Self { storage_slot: 0, value: 0 }
}
}

struct ContractStorageUpdateRequest {
Expand All @@ -277,10 +277,10 @@ impl ContractStorageUpdateRequest {
fn hash(self) -> Field {
dep::std::hash::pedersen_with_separator(self.serialize(), GENERATOR_INDEX__PUBLIC_DATA_UPDATE_REQUEST)[0]
}
}

fn empty_contract_storage_update_request() -> ContractStorageUpdateRequest {
ContractStorageUpdateRequest { storage_slot: 0, old_value: 0, new_value: 0 }
fn empty() -> Self {
Self { storage_slot: 0, old_value: 0, new_value: 0 }
}
}


Expand All @@ -297,8 +297,10 @@ struct PublicCircuitPublicInputs {
unencrypted_logs_hash: [Field; NUM_FIELDS_PER_SHA256],
unencrypted_log_preimages_length: Field,
block_data: HistoricBlockData,
historic_public_data_tree_root: Field,
prover_address: Field,

// TODO: include globals in here and check them elsewhere
// https://github.com/AztecProtocol/aztec-packages/issues/1567
}

impl PublicCircuitPublicInputs {
Expand All @@ -320,11 +322,10 @@ impl PublicCircuitPublicInputs {
inputs.push_array(self.new_l2_to_l1_msgs);

// We do not include block_data since it's not in the cpp hash
// inputs.push(self.block_data.hash()); see https://github.com/AztecProtocol/aztec-packages/issues/1473

inputs.push_array(self.unencrypted_logs_hash);
inputs.push(self.unencrypted_log_preimages_length);
inputs.push(self.historic_public_data_tree_root);
inputs.push_array(self.block_data.serialize()); // see https://github.com/AztecProtocol/aztec-packages/issues/1473
inputs.push(self.prover_address);

dep::std::hash::pedersen_with_separator(inputs.storage, GENERATOR_INDEX__PUBLIC_CIRCUIT_PUBLIC_INPUTS)[0]
Expand All @@ -348,7 +349,6 @@ impl PublicCircuitPublicInputs {
fields.push_array(self.unencrypted_logs_hash);
fields.push(self.unencrypted_log_preimages_length);
fields.push_array(self.block_data.serialize());
fields.push(self.historic_public_data_tree_root);
fields.push(self.prover_address);
fields.storage
}
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/noir-libs/noir-aztec/src/constants_gen.nr
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ global GET_NOTE_ORACLE_RETURN_LENGTH: comptime Field = 23;
global MAX_NOTES_PER_PAGE: comptime Field = 10;
global VIEW_NOTE_ORACLE_RETURN_LENGTH: comptime Field = 212;
global CALL_CONTEXT_LENGTH: comptime Field = 6;
global CONSTANT_HISTORIC_BLOCK_DATA_LENGTH: comptime Field = 7;
global HISTORIC_BLOCK_DATA_LENGTH: comptime Field = 7;
global FUNCTION_DATA_LENGTH: comptime Field = 4;
global CONTRACT_DEPLOYMENT_DATA_LENGTH: comptime Field = 6;
global PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH: comptime Field = 58;
Expand All @@ -60,7 +60,7 @@ global PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH: comptime Field = 77;
global GET_NOTES_ORACLE_RETURN_LENGTH: comptime Field = 86;
global EMPTY_NULLIFIED_COMMITMENT: comptime Field = 1000000;
global CALL_PRIVATE_FUNCTION_RETURN_SIZE: comptime Field = 64;
global PUBLIC_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH: comptime Field = 41;
global PUBLIC_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH: comptime Field = 47;
global PRIVATE_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH: comptime Field = 48;
global COMMITMENTS_NUM_BYTES_PER_BASE_ROLLUP: comptime Field = 1024;
global NULLIFIERS_NUM_BYTES_PER_BASE_ROLLUP: comptime Field = 1024;
Expand Down
Loading

0 comments on commit 59c0475

Please sign in to comment.