Skip to content

Commit

Permalink
feat: non-hardcoded constants (#7736)
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan authored Aug 2, 2024
1 parent 0c1d98f commit 51d73ce
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 90 deletions.
44 changes: 22 additions & 22 deletions l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,7 @@ library Constants {
uint256 internal constant MAX_NOTE_ENCRYPTED_LOGS_PER_CALL = 16;
uint256 internal constant MAX_ENCRYPTED_LOGS_PER_CALL = 4;
uint256 internal constant MAX_UNENCRYPTED_LOGS_PER_CALL = 4;
uint256 internal constant MAX_NOTE_HASHES_PER_TX = 64;
uint256 internal constant MAX_NULLIFIERS_PER_TX = 64;
uint256 internal constant MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX = 8;
uint256 internal constant MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX = 32;
uint256 internal constant MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX = 63;
uint256 internal constant PROTOCOL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX = 1;
uint256 internal constant MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX = 64;
uint256 internal constant MAX_PUBLIC_DATA_READS_PER_TX = 64;
uint256 internal constant MAX_L2_TO_L1_MSGS_PER_TX = 8;
uint256 internal constant MAX_NOTE_HASH_READ_REQUESTS_PER_TX = 64;
uint256 internal constant MAX_NULLIFIER_READ_REQUESTS_PER_TX = 64;
uint256 internal constant MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_TX = 64;
uint256 internal constant MAX_L1_TO_L2_MSG_READ_REQUESTS_PER_TX = 64;
uint256 internal constant MAX_KEY_VALIDATION_REQUESTS_PER_TX = 64;
uint256 internal constant MAX_NOTE_ENCRYPTED_LOGS_PER_TX = 64;
uint256 internal constant MAX_ENCRYPTED_LOGS_PER_TX = 8;
uint256 internal constant MAX_UNENCRYPTED_LOGS_PER_TX = 8;
uint256 internal constant MAX_PUBLIC_DATA_HINTS = 128;
uint256 internal constant NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP = 16;
uint256 internal constant ARCHIVE_HEIGHT = 16;
uint256 internal constant VK_TREE_HEIGHT = 5;
uint256 internal constant FUNCTION_TREE_HEIGHT = 5;
uint256 internal constant NOTE_HASH_TREE_HEIGHT = 32;
Expand All @@ -63,14 +45,32 @@ library Constants {
uint256 internal constant L1_TO_L2_MESSAGE_TREE_ID = 3;
uint256 internal constant ARCHIVE_TREE_ID = 4;
uint256 internal constant NOTE_HASH_SUBTREE_HEIGHT = 6;
uint256 internal constant NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH = 26;
uint256 internal constant NULLIFIER_SUBTREE_HEIGHT = 6;
uint256 internal constant PUBLIC_DATA_SUBTREE_HEIGHT = 6;
uint256 internal constant ARCHIVE_HEIGHT = 16;
uint256 internal constant L1_TO_L2_MSG_SUBTREE_HEIGHT = 4;
uint256 internal constant NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH = 26;
uint256 internal constant NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH = 14;
uint256 internal constant PUBLIC_DATA_SUBTREE_SIBLING_PATH_LENGTH = 34;
uint256 internal constant L1_TO_L2_MSG_SUBTREE_HEIGHT = 4;
uint256 internal constant L1_TO_L2_MSG_SUBTREE_SIBLING_PATH_LENGTH = 12;
uint256 internal constant MAX_NOTE_HASHES_PER_TX = 64;
uint256 internal constant MAX_NULLIFIERS_PER_TX = 64;
uint256 internal constant MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX = 8;
uint256 internal constant MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX = 32;
uint256 internal constant PROTOCOL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX = 1;
uint256 internal constant MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX = 64;
uint256 internal constant MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX = 63;
uint256 internal constant MAX_PUBLIC_DATA_READS_PER_TX = 64;
uint256 internal constant MAX_L2_TO_L1_MSGS_PER_TX = 8;
uint256 internal constant MAX_NOTE_HASH_READ_REQUESTS_PER_TX = 64;
uint256 internal constant MAX_NULLIFIER_READ_REQUESTS_PER_TX = 64;
uint256 internal constant MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_TX = 64;
uint256 internal constant MAX_L1_TO_L2_MSG_READ_REQUESTS_PER_TX = 64;
uint256 internal constant MAX_KEY_VALIDATION_REQUESTS_PER_TX = 64;
uint256 internal constant MAX_NOTE_ENCRYPTED_LOGS_PER_TX = 64;
uint256 internal constant MAX_ENCRYPTED_LOGS_PER_TX = 8;
uint256 internal constant MAX_UNENCRYPTED_LOGS_PER_TX = 8;
uint256 internal constant MAX_PUBLIC_DATA_HINTS = 128;
uint256 internal constant NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP = 16;
uint256 internal constant PRIVATE_KERNEL_INIT_INDEX = 0;
uint256 internal constant PRIVATE_KERNEL_INNER_INDEX = 1;
uint256 internal constant PRIVATE_KERNEL_RESET_FULL_INDEX = 2;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
mod lib;

contract PrivateFPC {
use dep::aztec::protocol_types::{abis::log_hash::LogHash, address::AztecAddress, hash::poseidon2_hash};
use dep::aztec::state_vars::SharedImmutable;
use dep::aztec::{protocol_types::{address::AztecAddress, hash::poseidon2_hash}, state_vars::SharedImmutable};
use dep::token_with_refunds::TokenWithRefunds;
use crate::lib::emit_randomness_as_unencrypted_log;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ unconstrained fn setup_refund_success() {
utils::check_private_balance(token_contract_address, fee_payer, 1)
}

// TODO(#7694): Ideally we would check the error message here but it's currently not possible because TXE does not
// support checking messages of errors thrown in a public teardown function. Once this is supported, check the message
// here and delete the e2e test checking it.
// TODO(#7694): Ideally we would check the error message here but it's currently not supported by TXE. Once this
// is supported, check the message here and delete try deleting the corresponding e2e test.
// #[test(should_fail_with = "tx fee is higher than funded amount")]
#[test(should_fail)]
unconstrained fn setup_refund_insufficient_funded_amount() {
Expand Down
62 changes: 29 additions & 33 deletions noir-projects/noir-protocol-circuits/crates/types/src/constants.nr
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,39 @@ global MAX_NOTE_ENCRYPTED_LOGS_PER_CALL: u32 = 16;
global MAX_ENCRYPTED_LOGS_PER_CALL: u32 = 4; // If modifying, update DEPLOYER_CONTRACT_ADDRESS.
global MAX_UNENCRYPTED_LOGS_PER_CALL: u32 = 4; // If modifying, update DEPLOYER_CONTRACT_ADDRESS.

// TREES RELATED CONSTANTS
global ARCHIVE_HEIGHT: u32 = 16;
global VK_TREE_HEIGHT: u32 = 5;
global FUNCTION_TREE_HEIGHT: u32 = 5;
global NOTE_HASH_TREE_HEIGHT: u32 = 32;
global PUBLIC_DATA_TREE_HEIGHT: u32 = 40;
global NULLIFIER_TREE_HEIGHT: u32 = 20;
global L1_TO_L2_MSG_TREE_HEIGHT: u32 = 16;
global ARTIFACT_FUNCTION_TREE_MAX_HEIGHT = 5;
global NULLIFIER_TREE_ID = 0;
global NOTE_HASH_TREE_ID = 1;
global PUBLIC_DATA_TREE_ID = 2;
global L1_TO_L2_MESSAGE_TREE_ID = 3;
global ARCHIVE_TREE_ID = 4;

// SUB-TREES RELATED CONSTANTS
global NOTE_HASH_SUBTREE_HEIGHT: u32 = 6;
global NULLIFIER_SUBTREE_HEIGHT: u32 = 6;
global PUBLIC_DATA_SUBTREE_HEIGHT: u32 = 6;
global L1_TO_L2_MSG_SUBTREE_HEIGHT: u32 = 4;
global NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH: u32 = NOTE_HASH_TREE_HEIGHT - NOTE_HASH_SUBTREE_HEIGHT;
global NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH: u32 = NULLIFIER_TREE_HEIGHT - NULLIFIER_SUBTREE_HEIGHT;
global PUBLIC_DATA_SUBTREE_SIBLING_PATH_LENGTH: u32 = PUBLIC_DATA_TREE_HEIGHT - PUBLIC_DATA_SUBTREE_HEIGHT;
global L1_TO_L2_MSG_SUBTREE_SIBLING_PATH_LENGTH: u32 = L1_TO_L2_MSG_TREE_HEIGHT - L1_TO_L2_MSG_SUBTREE_HEIGHT;

// "PER TRANSACTION" CONSTANTS
global MAX_NOTE_HASHES_PER_TX: u32 = 64;
global MAX_NULLIFIERS_PER_TX: u32 = 64;
global MAX_NOTE_HASHES_PER_TX: u32 = (1 as u8 << NOTE_HASH_SUBTREE_HEIGHT as u8) as u32;
global MAX_NULLIFIERS_PER_TX: u32 = (1 as u8 << NULLIFIER_SUBTREE_HEIGHT as u8) as u32;
global MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX: u32 = 8;
global MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX: u32 = 32;
// If you touch any of the constants below don't forget to update MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX.
global MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX: u32 = 63;
global PROTOCOL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX: u32 = 1;
// We cannot do constant propagation below and instead we have to hardcode the value because the generated code in TS
// would then result in the type of the constant be a number and not a literal type. This would mess up the types.
// Pain.
global MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX: u32 = 64; // MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX + PROTOCOL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX;
global MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX: u32 = (1 as u8 << PUBLIC_DATA_SUBTREE_HEIGHT as u8) as u32;
global MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX: u32 = MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX - PROTOCOL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX;
global MAX_PUBLIC_DATA_READS_PER_TX: u32 = 64;
global MAX_L2_TO_L1_MSGS_PER_TX: u32 = 8;
global MAX_NOTE_HASH_READ_REQUESTS_PER_TX: u32 = 64;
Expand All @@ -73,31 +94,6 @@ global MAX_PUBLIC_DATA_HINTS: u32 = 128;
// ROLLUP CONTRACT CONSTANTS - constants used only in l1-contracts
global NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP: u32 = 16;

// TREES RELATED CONSTANTS
global VK_TREE_HEIGHT: u32 = 5;
global FUNCTION_TREE_HEIGHT: u32 = 5;
global NOTE_HASH_TREE_HEIGHT: u32 = 32;
global PUBLIC_DATA_TREE_HEIGHT: u32 = 40;
global NULLIFIER_TREE_HEIGHT: u32 = 20;
global L1_TO_L2_MSG_TREE_HEIGHT: u32 = 16;
global ARTIFACT_FUNCTION_TREE_MAX_HEIGHT = 5;
global NULLIFIER_TREE_ID = 0;
global NOTE_HASH_TREE_ID = 1;
global PUBLIC_DATA_TREE_ID = 2;
global L1_TO_L2_MESSAGE_TREE_ID = 3;
global ARCHIVE_TREE_ID = 4;

// SUB-TREES RELATED CONSTANTS
global NOTE_HASH_SUBTREE_HEIGHT: u32 = 6;
global NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH: u32 = 26;
global NULLIFIER_SUBTREE_HEIGHT: u32 = 6;
global PUBLIC_DATA_SUBTREE_HEIGHT: u32 = 6;
global ARCHIVE_HEIGHT: u32 = 16;
global NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH: u32 = 14;
global PUBLIC_DATA_SUBTREE_SIBLING_PATH_LENGTH: u32 = 34;
global L1_TO_L2_MSG_SUBTREE_HEIGHT: u32 = 4;
global L1_TO_L2_MSG_SUBTREE_SIBLING_PATH_LENGTH: u32 = 12;

// VK TREE CONSTANTS
global PRIVATE_KERNEL_INIT_INDEX: u32 = 0;
global PRIVATE_KERNEL_INNER_INDEX: u32 = 1;
Expand Down
1 change: 1 addition & 0 deletions yarn-project/aztec.js/src/contract/sent_tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class SentTx {
* The function internally awaits for the 'txHashPromise' to resolve, and then returns the resolved transaction hash.
*
* @returns A promise that resolves to the transaction hash of the SentTx instance.
* TODO(#7717): Don't throw here.
*/
public getTxHash(): Promise<TxHash> {
return this.txHashPromise;
Expand Down
44 changes: 22 additions & 22 deletions yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,7 @@ export const MAX_KEY_VALIDATION_REQUESTS_PER_CALL = 16;
export const MAX_NOTE_ENCRYPTED_LOGS_PER_CALL = 16;
export const MAX_ENCRYPTED_LOGS_PER_CALL = 4;
export const MAX_UNENCRYPTED_LOGS_PER_CALL = 4;
export const MAX_NOTE_HASHES_PER_TX = 64;
export const MAX_NULLIFIERS_PER_TX = 64;
export const MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX = 8;
export const MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX = 32;
export const MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX = 63;
export const PROTOCOL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX = 1;
export const MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX = 64;
export const MAX_PUBLIC_DATA_READS_PER_TX = 64;
export const MAX_L2_TO_L1_MSGS_PER_TX = 8;
export const MAX_NOTE_HASH_READ_REQUESTS_PER_TX = 64;
export const MAX_NULLIFIER_READ_REQUESTS_PER_TX = 64;
export const MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_TX = 64;
export const MAX_L1_TO_L2_MSG_READ_REQUESTS_PER_TX = 64;
export const MAX_KEY_VALIDATION_REQUESTS_PER_TX = 64;
export const MAX_NOTE_ENCRYPTED_LOGS_PER_TX = 64;
export const MAX_ENCRYPTED_LOGS_PER_TX = 8;
export const MAX_UNENCRYPTED_LOGS_PER_TX = 8;
export const MAX_PUBLIC_DATA_HINTS = 128;
export const NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP = 16;
export const ARCHIVE_HEIGHT = 16;
export const VK_TREE_HEIGHT = 5;
export const FUNCTION_TREE_HEIGHT = 5;
export const NOTE_HASH_TREE_HEIGHT = 32;
Expand All @@ -49,14 +31,32 @@ export const PUBLIC_DATA_TREE_ID = 2;
export const L1_TO_L2_MESSAGE_TREE_ID = 3;
export const ARCHIVE_TREE_ID = 4;
export const NOTE_HASH_SUBTREE_HEIGHT = 6;
export const NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH = 26;
export const NULLIFIER_SUBTREE_HEIGHT = 6;
export const PUBLIC_DATA_SUBTREE_HEIGHT = 6;
export const ARCHIVE_HEIGHT = 16;
export const L1_TO_L2_MSG_SUBTREE_HEIGHT = 4;
export const NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH = 26;
export const NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH = 14;
export const PUBLIC_DATA_SUBTREE_SIBLING_PATH_LENGTH = 34;
export const L1_TO_L2_MSG_SUBTREE_HEIGHT = 4;
export const L1_TO_L2_MSG_SUBTREE_SIBLING_PATH_LENGTH = 12;
export const MAX_NOTE_HASHES_PER_TX = 64;
export const MAX_NULLIFIERS_PER_TX = 64;
export const MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX = 8;
export const MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX = 32;
export const PROTOCOL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX = 1;
export const MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX = 64;
export const MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX = 63;
export const MAX_PUBLIC_DATA_READS_PER_TX = 64;
export const MAX_L2_TO_L1_MSGS_PER_TX = 8;
export const MAX_NOTE_HASH_READ_REQUESTS_PER_TX = 64;
export const MAX_NULLIFIER_READ_REQUESTS_PER_TX = 64;
export const MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_TX = 64;
export const MAX_L1_TO_L2_MSG_READ_REQUESTS_PER_TX = 64;
export const MAX_KEY_VALIDATION_REQUESTS_PER_TX = 64;
export const MAX_NOTE_ENCRYPTED_LOGS_PER_TX = 64;
export const MAX_ENCRYPTED_LOGS_PER_TX = 8;
export const MAX_UNENCRYPTED_LOGS_PER_TX = 8;
export const MAX_PUBLIC_DATA_HINTS = 128;
export const NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP = 16;
export const PRIVATE_KERNEL_INIT_INDEX = 0;
export const PRIVATE_KERNEL_INNER_INDEX = 1;
export const PRIVATE_KERNEL_RESET_FULL_INDEX = 2;
Expand Down
5 changes: 4 additions & 1 deletion yarn-project/circuits.js/src/scripts/constants.in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ function generateCppConstants({ constants }: ParsedContent, targetPath: string)
#pragma once
${processConstantsCpp(constants)}
\n`;
`;

fs.writeFileSync(targetPath, resultCpp);
}
Expand Down Expand Up @@ -322,6 +322,9 @@ function evaluateExpressions(expressions: [string, string][]): { [key: string]:
const prelude = expressions
.map(([name, rhs]) => {
const guardedRhs = rhs
// Remove 'as u8' and 'as u32' castings
.replaceAll(' as u8', '')
.replaceAll(' as u32', '')
// We make some space around the parentheses, so that constant numbers are still split.
.replace(/\(/g, '( ')
.replace(/\)/g, ' )')
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/end-to-end/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,4 @@
"testRegex": "./src/.*\\.test\\.(js|mjs|ts)$",
"rootDir": "./src"
}
}
}
8 changes: 4 additions & 4 deletions yarn-project/end-to-end/src/e2e_auth_contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ describe('e2e_auth_contract', () => {
expect(await contract.methods.get_authorized().simulate()).toEqual(AztecAddress.ZERO);
});

it('non-admin canoot set authorized', async () => {
await expect(
contract.withWallet(other).methods.set_authorized(authorized.getAddress()).send().wait(),
).rejects.toThrow('caller is not admin');
it('non-admin cannot set authorized', async () => {
await expect(contract.withWallet(other).methods.set_authorized(authorized.getAddress()).prove()).rejects.toThrow(
'caller is not admin',
);
});

it('admin sets authorized', async () => {
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/end-to-end/src/e2e_fees/fees_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ export class FeesTest {
await this.snapshotManager.snapshot(
'fund_alice',
async () => {
await this.mintPrivateBananas(BigInt(this.ALICE_INITIAL_BANANAS), this.aliceAddress);
await this.mintPrivateBananas(this.ALICE_INITIAL_BANANAS, this.aliceAddress);
await this.bananaCoin.methods.mint_public(this.aliceAddress, this.ALICE_INITIAL_BANANAS).send().wait();
},
() => Promise.resolve(),
Expand All @@ -361,7 +361,7 @@ export class FeesTest {
await this.snapshotManager.snapshot(
'fund_alice_with_tokens',
async () => {
await this.mintTokenWithRefunds(BigInt(this.ALICE_INITIAL_BANANAS));
await this.mintTokenWithRefunds(this.ALICE_INITIAL_BANANAS);
},
() => Promise.resolve(),
);
Expand Down

0 comments on commit 51d73ce

Please sign in to comment.