Skip to content

Commit

Permalink
refactor: moving Noir constants to constants.hpp (#1072)
Browse files Browse the repository at this point in the history
* refactor: more Noir consts in cpp

* refactor: more Noir consts in cpp 2

* refactor: less terrible naming

* fixes after rebase + recompiled contracts

* docs: removed unnecessary comments

* chore: recompiled contracts
  • Loading branch information
benesjan authored Jul 14, 2023
1 parent 567bbf3 commit eede42b
Show file tree
Hide file tree
Showing 18 changed files with 136 additions and 99 deletions.
16 changes: 15 additions & 1 deletion circuits/cpp/src/aztec3/circuits/abis/packers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,21 @@ struct ConstantsPacker {
L1_TO_L2_MSG_SUBTREE_SIBLING_PATH_LENGTH,
FUNCTION_SELECTOR_NUM_BYTES,
MAPPING_SLOT_PEDERSEN_SEPARATOR,
NUM_FIELDS_PER_SHA256)); // <-- Add names of new constants here
NUM_FIELDS_PER_SHA256,
L1_TO_L2_MESSAGE_LENGTH,
L1_TO_L2_MESSAGE_ORACLE_CALL_LENGTH,
MAX_NOTE_FIELDS_LENGTH,
GET_NOTE_ORACLE_RETURN_LENGTH,
MAX_NOTES_PER_PAGE,
VIEW_NOTE_ORACLE_RETURN_LENGTH,
CALL_CONTEXT_LENGTH,
COMMITMENT_TREES_ROOTS_LENGTH,
FUNCTION_DATA_LENGTH,
CONTRACT_DEPLOYMENT_DATA_LENGTH,
PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH,
CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH,
CONTRACT_STORAGE_READ_LENGTH,
PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH)); // <-- Add names of new constants here
}
};

Expand Down
44 changes: 44 additions & 0 deletions circuits/cpp/src/aztec3/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,48 @@ enum PrivateStateNoteGeneratorIndex {
// Note: When modifying, modify `PrivateStateTypePacker` in packer.hpp accordingly.
enum PrivateStateType { PARTITIONED = 1, WHOLE };

////////////////////////////////////////////////////////////////////////////////
// NOIR CONSTANTS - constants used only in yarn-packages/noir-contracts
// --> Here because Noir doesn't yet support globals referencing other globals yet and doing so in C++ seems to be the
// best thing to do for now. Move these constants to a noir file once the issue bellow is resolved:
// https://github.com/noir-lang/noir/issues/1734
constexpr size_t L1_TO_L2_MESSAGE_LENGTH = 8;
// message length + sibling path (same size as tree height) + 1 field for root + 1 field for index
constexpr size_t L1_TO_L2_MESSAGE_ORACLE_CALL_LENGTH = L1_TO_L2_MESSAGE_LENGTH + L1_TO_L2_MSG_TREE_HEIGHT + 1 + 1;

// TODO: Remove these when nested array is supported.
constexpr size_t MAX_NOTE_FIELDS_LENGTH = 20;
// + 2 for EXTRA_DATA: [number_of_return_notes, contract_address]
constexpr size_t GET_NOTE_ORACLE_RETURN_LENGTH = MAX_READ_REQUESTS_PER_CALL * MAX_NOTE_FIELDS_LENGTH + 2;
constexpr size_t MAX_NOTES_PER_PAGE = 10;
// + 2 for EXTRA_DATA: [number_of_return_notes, contract_address]
constexpr size_t VIEW_NOTE_ORACLE_RETURN_LENGTH = MAX_NOTES_PER_PAGE * MAX_NOTE_FIELDS_LENGTH + 2;

constexpr size_t CALL_CONTEXT_LENGTH = 6;
constexpr size_t COMMITMENT_TREES_ROOTS_LENGTH = 4;
constexpr size_t FUNCTION_DATA_LENGTH = 3;
constexpr size_t CONTRACT_DEPLOYMENT_DATA_LENGTH = 4;

// Change this ONLY if you have changed the PrivateCircuitPublicInputs structure in C++.
// In other words, if the structure/size of the public inputs of a function call changes then we
// should change this constant as well as the offsets in private_call_stack_item.nr
constexpr size_t PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH =
CALL_CONTEXT_LENGTH + 1 // +1 for args_hash
+ RETURN_VALUES_LENGTH + MAX_READ_REQUESTS_PER_CALL + MAX_NEW_COMMITMENTS_PER_CALL + MAX_NEW_NULLIFIERS_PER_CALL +
MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL + MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL + MAX_NEW_L2_TO_L1_MSGS_PER_CALL +
NUM_FIELDS_PER_SHA256 + NUM_FIELDS_PER_SHA256 + 2 // + 2 for logs preimage lengths
+ COMMITMENT_TREES_ROOTS_LENGTH + CONTRACT_DEPLOYMENT_DATA_LENGTH + 2; // + 2 for chain_id and version


constexpr size_t CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 3;
constexpr size_t CONTRACT_STORAGE_READ_LENGTH = 2;

// Change this ONLY if you have changed the PublicCircuitPublicInputs structure in C++.
constexpr size_t PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH =
CALL_CONTEXT_LENGTH + 1 + RETURN_VALUES_LENGTH + // + 1 for args_hash
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL * CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH +
MAX_PUBLIC_DATA_READS_PER_CALL * CONTRACT_STORAGE_READ_LENGTH + MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL +
MAX_NEW_COMMITMENTS_PER_CALL + MAX_NEW_NULLIFIERS_PER_CALL + MAX_NEW_L2_TO_L1_MSGS_PER_CALL +
COMMITMENT_TREES_ROOTS_LENGTH + 2; // + 2 for chain_id and version

} // namespace aztec3
14 changes: 14 additions & 0 deletions yarn-project/circuits.js/src/cbind/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ export const L1_TO_L2_MSG_SUBTREE_SIBLING_PATH_LENGTH = 12;
export const FUNCTION_SELECTOR_NUM_BYTES = 4;
export const MAPPING_SLOT_PEDERSEN_SEPARATOR = 4;
export const NUM_FIELDS_PER_SHA256 = 2;
export const L1_TO_L2_MESSAGE_LENGTH = 8;
export const L1_TO_L2_MESSAGE_ORACLE_CALL_LENGTH = 26;
export const MAX_NOTE_FIELDS_LENGTH = 20;
export const GET_NOTE_ORACLE_RETURN_LENGTH = 82;
export const MAX_NOTES_PER_PAGE = 10;
export const VIEW_NOTE_ORACLE_RETURN_LENGTH = 202;
export const CALL_CONTEXT_LENGTH = 6;
export const COMMITMENT_TREES_ROOTS_LENGTH = 4;
export const FUNCTION_DATA_LENGTH = 3;
export const CONTRACT_DEPLOYMENT_DATA_LENGTH = 4;
export const PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 49;
export const CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 3;
export const CONTRACT_STORAGE_READ_LENGTH = 2;
export const PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 51;
export enum GeneratorIndex {
COMMITMENT = 1,
COMMITMENT_PLACEHOLDER = 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ contract PendingCommitments {
}
};
use dep::custom_notes::{
constants::MAX_NOTE_FIELDS_SIZE,
utils::compute_note_hash_and_nullifier,
};

Expand All @@ -32,6 +31,7 @@ contract PendingCommitments {
use dep::aztec::state_vars::set::Set;
use dep::aztec::log::emit_encrypted_log;
use dep::aztec::log::emit_unencrypted_log;
use dep::aztec::constants_gen::MAX_NOTE_FIELDS_LENGTH;
use dep::aztec::private_call_stack_item::PrivateCallStackItem;

// TODO(dbanks12): consolidate code into internal helper functions
Expand Down Expand Up @@ -232,7 +232,7 @@ contract PendingCommitments {
//}

/// ABI stev type "unconstrained"
fn stev(contract_address: Field, storage_slot: Field, preimage: [Field; MAX_NOTE_FIELDS_SIZE]) -> pub [Field; 2] {
fn stev(contract_address: Field, storage_slot: Field, preimage: [Field; MAX_NOTE_FIELDS_LENGTH]) -> pub [Field; 2] {
compute_note_hash_and_nullifier(contract_address, storage_slot, ValueNoteInterface, preimage)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ contract ZkToken {
};
use dep::custom_notes::{
claim_note::{ClaimNote, ClaimNoteInterface},
constants::MAX_NOTE_FIELDS_SIZE,
utils::compute_note_hash_and_nullifier,
};

Expand All @@ -24,6 +23,7 @@ contract ZkToken {
use dep::aztec::state_vars::set::Set;
use dep::aztec::log::emit_encrypted_log;
use dep::aztec::log::emit_unencrypted_log;
use dep::aztec::constants_gen::MAX_NOTE_FIELDS_LENGTH;

use crate::storage::Storage;

Expand Down Expand Up @@ -167,7 +167,7 @@ contract ZkToken {
}

/// ABI stev type "unconstrained"
fn stev(contract_address: Field, storage_slot: Field, preimage: [Field; MAX_NOTE_FIELDS_SIZE]) -> pub [Field; 2] {
fn stev(contract_address: Field, storage_slot: Field, preimage: [Field; MAX_NOTE_FIELDS_LENGTH]) -> pub [Field; 2] {
if (storage_slot == 2) {
compute_note_hash_and_nullifier(contract_address, storage_slot, ClaimNoteInterface, preimage)
} else {
Expand Down

Large diffs are not rendered by default.

Loading

0 comments on commit eede42b

Please sign in to comment.