Skip to content

Commit

Permalink
refactor: moving hash functions to relevant classes (#4551)
Browse files Browse the repository at this point in the history
Fixes #4411
Partially addresses #4410 (will finish that in a separate PR since this
one is already huge)

\+ renamed `abis.ts` in circuits.js as `hash.ts`
\+ nuked useless `secrets.ts` in `aztec.js`
\+ ordered the Noir struct length constants since they were all over the
place
\+ added more length checks to `toFields()` methods (we don't need the
length tests when these checks are present - serialization ones are
enough)
  • Loading branch information
benesjan authored Feb 16, 2024
1 parent f144745 commit 731d7d0
Show file tree
Hide file tree
Showing 119 changed files with 1,911 additions and 1,830 deletions.
4 changes: 2 additions & 2 deletions l1-contracts/slither_output.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,9 @@ src/core/messagebridge/Inbox.sol#L148-L153
Impact: Informational
Confidence: Medium
- [ ] ID-41
Variable [Constants.L1_TO_L2_MESSAGE_LENGTH](src/core/libraries/ConstantsGen.sol#L80) is too similar to [Constants.L2_TO_L1_MESSAGE_LENGTH](src/core/libraries/ConstantsGen.sol#L81)
Variable [Constants.L1_TO_L2_MESSAGE_LENGTH](src/core/libraries/ConstantsGen.sol#L96) is too similar to [Constants.L2_TO_L1_MESSAGE_LENGTH](src/core/libraries/ConstantsGen.sol#L97)

src/core/libraries/ConstantsGen.sol#L80
src/core/libraries/ConstantsGen.sol#L96


- [ ] ID-42
Expand Down
28 changes: 18 additions & 10 deletions l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,34 @@ library Constants {
0xe7af816635466f128568edb04c9fa024f6c87fb9010fdbffa68b3d99;
uint256 internal constant DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE =
0x85864497636cf755ae7bde03f267ce01a520981c21c3682aaf82a631;
uint256 internal constant L1_TO_L2_MESSAGE_LENGTH = 8;
uint256 internal constant L2_TO_L1_MESSAGE_LENGTH = 2;
uint256 internal constant L1_TO_L2_MESSAGE_ORACLE_CALL_LENGTH = 25;
uint256 internal constant MAX_NOTE_FIELDS_LENGTH = 20;
uint256 internal constant GET_NOTE_ORACLE_RETURN_LENGTH = 23;
uint256 internal constant MAX_NOTES_PER_PAGE = 10;
uint256 internal constant VIEW_NOTE_ORACLE_RETURN_LENGTH = 212;
uint256 internal constant AZTEC_ADDRESS_LENGTH = 1;
uint256 internal constant CALL_CONTEXT_LENGTH = 8;
uint256 internal constant GLOBAL_VARIABLES_LENGTH = 6;
uint256 internal constant PARTIAL_STATE_REFERENCE_LENGTH = 8;
uint256 internal constant STATE_REFERENCE_LENGTH = 10;
uint256 internal constant CONTENT_COMMITMENT_LENGTH = 7;
uint256 internal constant HEADER_LENGTH = 25;
uint256 internal constant FUNCTION_DATA_LENGTH = 4;
uint256 internal constant CONTRACT_DEPLOYMENT_DATA_LENGTH = 6;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 214;
uint256 internal constant CONTRACT_STORAGE_READ_LENGTH = 2;
uint256 internal constant CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 2;
uint256 internal constant ETH_ADDRESS_LENGTH = 1;
uint256 internal constant FUNCTION_DATA_LENGTH = 4;
uint256 internal constant FUNCTION_LEAF_PREIMAGE_LENGTH = 5;
uint256 internal constant GLOBAL_VARIABLES_LENGTH = 6;
uint256 internal constant HEADER_LENGTH = 25;
uint256 internal constant L1_TO_L2_MESSAGE_LENGTH = 8;
uint256 internal constant L2_TO_L1_MESSAGE_LENGTH = 2;
uint256 internal constant NEW_CONTRACT_DATA_LENGTH = 3;
uint256 internal constant NULLIFIER_KEY_VALIDATION_REQUEST_LENGTH = 4;
uint256 internal constant NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH = 5;
uint256 internal constant PARTIAL_STATE_REFERENCE_LENGTH = 8;
uint256 internal constant PRIVATE_CALL_STACK_ITEM_LENGTH = 219;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 214;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 194;
uint256 internal constant CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 2;
uint256 internal constant CONTRACT_STORAGE_READ_LENGTH = 2;
uint256 internal constant STATE_REFERENCE_LENGTH = 10;
uint256 internal constant TX_CONTEXT_DATA_LENGTH = 11;
uint256 internal constant TX_REQUEST_LENGTH = 17;
uint256 internal constant GET_NOTES_ORACLE_RETURN_LENGTH = 674;
uint256 internal constant COMMITMENTS_NUM_BYTES_PER_BASE_ROLLUP = 2048;
uint256 internal constant NULLIFIERS_NUM_BYTES_PER_BASE_ROLLUP = 2048;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use dep::protocol_types::{abis::call_context::CallContext, contrakt::deployment_data::ContractDeploymentData, header::Header};
use dep::protocol_types::{
abis::call_context::CallContext, contrakt::contract_deployment_data::ContractDeploymentData,
header::Header
};
use crate::context::globals::private_global_variables::PrivateGlobalVariables;

// PrivateContextInputs are expected to be provided to each private function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,7 @@ contract Lending {
let last_updated_ts = context.timestamp() as u120;
let loan_to_value = loan_to_value as u120;

asset_loc.write(
Asset {
interest_accumulator: 1000000000,
last_updated_ts,
loan_to_value,
oracle
}
);
asset_loc.write(Asset { interest_accumulator: 1000000000, last_updated_ts, loan_to_value, oracle });

storage.collateral_asset.write(collateral_asset);
storage.stable_coin.write(stable_coin);
Expand Down
1 change: 1 addition & 0 deletions noir-projects/noir-contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@aztec/noir-contracts",
"version": "0.1.0",
"type": "module",
"license": "MIT",
"exports": {
"./target/*": "./target/*.json"
},
Expand Down
1 change: 1 addition & 0 deletions noir-projects/noir-protocol-circuits/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@aztec/noir-protocol-circuits",
"version": "0.1.0",
"type": "module",
"license": "MIT",
"exports": {
".": "./dest/index.js",
"./types": "./dest/types/index.js"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use dep::types::{
side_effect::{SideEffect, SideEffectLinkedToNoteHash}
},
address::{AztecAddress, EthAddress, PartialAddress, compute_initialization_hash},
contract_class::ContractClassId, contrakt::deployment_data::ContractDeploymentData,
contract_class::ContractClassId, contrakt::contract_deployment_data::ContractDeploymentData,
constants::{
MAX_NEW_NULLIFIERS_PER_CALL, MAX_NEW_L2_TO_L1_MSGS_PER_CALL, MAX_NEW_COMMITMENTS_PER_CALL,
MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL, MAX_READ_REQUESTS_PER_CALL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use dep::types::{
side_effect::{SideEffect, SideEffectLinkedToNoteHash}
},
address::{AztecAddress, PublicKeysHash, compute_initialization_hash},
mocked::{Proof, verify_previous_kernel_state}, transaction::request::TxRequest,
mocked::{Proof, verify_previous_kernel_state}, transaction::tx_request::TxRequest,
traits::is_empty_array
};

Expand Down Expand Up @@ -133,7 +133,7 @@ mod tests {
grumpkin_private_key::GrumpkinPrivateKey,
hash::{compute_constructor_hash, compute_logs_hash, stdlib_recursion_verification_key_compress_native_vk},
messaging::l2_to_l1_message::L2ToL1Message,
tests::private_call_data_builder::PrivateCallDataBuilder, transaction::request::TxRequest,
tests::private_call_data_builder::PrivateCallDataBuilder, transaction::tx_request::TxRequest,
utils::arrays::array_length
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod append_only_tree_snapshot;

mod contract_class_function_leaf_preimage;

mod function_selector;
mod function_data;
mod function_leaf_preimage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::transaction::context::TxContext;
use crate::transaction::tx_context::TxContext;
use crate::header::Header;

struct CombinedConstantData {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::abis::function_selector::FunctionSelector;
use crate::constants::GENERATOR_INDEX__FUNCTION_LEAF;
use crate::traits::Hash;

struct ContractClassFunctionLeafPreimage {
selector : FunctionSelector,
vk_hash : Field,
}

impl Hash for ContractClassFunctionLeafPreimage {
fn hash(self) -> Field {
dep::std::hash::pedersen_hash_with_separator([
self.selector.to_field(),
self.vk_hash,
], GENERATOR_INDEX__FUNCTION_LEAF)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ fn serialization_of_empty() {
assert(data.eq(deserialized));
}

// TODO(#4619): Hash non-empty
#[test]
fn empty_hash() {
let data: FunctionData = dep::std::unsafe::zeroed();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::abis::function_selector::FunctionSelector;
use crate::constants::GENERATOR_INDEX__FUNCTION_LEAF;
use crate::traits::Hash;
use crate::{
abis::function_selector::FunctionSelector,
constants::{GENERATOR_INDEX__FUNCTION_LEAF, FUNCTION_LEAF_PREIMAGE_LENGTH}, hash::pedersen_hash,
traits::{Serialize, Hash, Deserialize}
};

struct FunctionLeafPreimage {
selector : FunctionSelector,
Expand All @@ -10,28 +12,66 @@ struct FunctionLeafPreimage {
acir_hash : Field
}

impl Hash for FunctionLeafPreimage {
fn hash(self) -> Field {
dep::std::hash::pedersen_hash_with_separator([
impl Eq for FunctionLeafPreimage {
fn eq(self, other: Self) -> bool {
self.selector.eq(other.selector) &
(self.is_internal == other.is_internal) &
(self.is_private == other.is_private) &
(self.vk_hash == other.vk_hash) &
(self.acir_hash == other.acir_hash)
}
}

impl Serialize<FUNCTION_LEAF_PREIMAGE_LENGTH> for FunctionLeafPreimage {
fn serialize(self) -> [Field; FUNCTION_LEAF_PREIMAGE_LENGTH] {
[
self.selector.to_field(),
self.is_internal as Field,
self.is_private as Field,
self.vk_hash,
self.acir_hash
], GENERATOR_INDEX__FUNCTION_LEAF)
self.acir_hash,
]
}
}

struct ContractClassFunctionLeafPreimage {
selector : FunctionSelector,
vk_hash : Field,
impl Deserialize<FUNCTION_LEAF_PREIMAGE_LENGTH> for FunctionLeafPreimage {
fn deserialize(serialized: [Field; FUNCTION_LEAF_PREIMAGE_LENGTH]) -> Self {
Self {
selector: FunctionSelector::from_field(serialized[0]),
is_internal: serialized[1] as bool,
is_private: serialized[2] as bool,
vk_hash: serialized[3],
acir_hash: serialized[4],
}
}
}

impl Hash for ContractClassFunctionLeafPreimage {
impl Hash for FunctionLeafPreimage {
fn hash(self) -> Field {
dep::std::hash::pedersen_hash_with_separator([
self.selector.to_field(),
self.vk_hash,
], GENERATOR_INDEX__FUNCTION_LEAF)
pedersen_hash(self.serialize(), GENERATOR_INDEX__FUNCTION_LEAF)
}
}

#[test]
fn serialization_of_empty() {
let data: FunctionLeafPreimage = dep::std::unsafe::zeroed();
let serialized = data.serialize();
let deserialized = FunctionLeafPreimage::deserialize(serialized);
assert(data.eq(deserialized));
}

#[test]
fn empty_hash() {
let data: FunctionLeafPreimage = dep::std::unsafe::zeroed();
let hash = data.hash();

// Value from function_leaf_preimage.test.ts "computes a function leaf" test
assert_eq(hash, 0x1f2e3193c7187347a099ee7cb5d6ac077da6b18706fe5508e658a3d0a05494f7);
}

#[test]
fn compute_function_leaf() {
let leaf = FunctionLeafPreimage { selector: FunctionSelector::from_u32(27), is_internal: false, is_private: true, vk_hash: 1, acir_hash: 2 };

assert_eq(leaf.hash(), 0x1ad8ece7f40e63d011ae47c6ce6cdaf31d632a23f5cf35bbeaaf69c8302afdbc);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::address::{AztecAddress, EthAddress};
use crate::contract_class::ContractClassId;
use crate::constants::GENERATOR_INDEX__CONTRACT_LEAF;
use crate::constants::{GENERATOR_INDEX__CONTRACT_LEAF, NEW_CONTRACT_DATA_LENGTH};
use dep::std::cmp::Eq;
use crate::traits::{Empty, Hash};
use crate::traits::{Empty, Serialize, Hash, Deserialize};
use crate::hash::pedersen_hash;

struct NewContractData {
contract_address: AztecAddress,
Expand All @@ -18,6 +19,26 @@ impl Eq for NewContractData {
}
}

impl Serialize<NEW_CONTRACT_DATA_LENGTH> for NewContractData {
fn serialize(self) -> [Field; NEW_CONTRACT_DATA_LENGTH] {
[
self.contract_address.to_field(),
self.portal_contract_address.to_field(),
self.contract_class_id.to_field(),
]
}
}

impl Deserialize<NEW_CONTRACT_DATA_LENGTH> for NewContractData {
fn deserialize(serialized: [Field; NEW_CONTRACT_DATA_LENGTH]) -> Self {
Self {
contract_address: AztecAddress::from_field(serialized[0]),
portal_contract_address: EthAddress::from_field(serialized[1]),
contract_class_id: ContractClassId::from_field(serialized[2]),
}
}
}

impl Empty for NewContractData {
fn empty() -> Self {
Self {
Expand All @@ -33,11 +54,7 @@ impl Hash for NewContractData {
if self.is_empty() {
0 // We want to return 0 here since the contract_address is zero
} else {
dep::std::hash::pedersen_hash_with_separator([
self.contract_address.to_field(),
self.portal_contract_address.to_field(),
self.contract_class_id.to_field(),
], GENERATOR_INDEX__CONTRACT_LEAF)
pedersen_hash(self.serialize(), GENERATOR_INDEX__CONTRACT_LEAF)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use dep::std::cmp::Eq;
use crate::{
address::AztecAddress, traits::{Empty, Serialize, Deserialize}, grumpkin_point::GrumpkinPoint,
address::AztecAddress,
constants::{NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH, NULLIFIER_KEY_VALIDATION_REQUEST_LENGTH},
traits::{Empty, Serialize, Deserialize}, grumpkin_point::GrumpkinPoint,
grumpkin_private_key::GrumpkinPrivateKey
};

global NULLIFIER_KEY_VALIDATION_REQUEST_SERIALIZED_LEN = 4;
global NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_SERIALIZED_LEN = 5;

struct NullifierKeyValidationRequest {
public_key: GrumpkinPoint,
secret_key: GrumpkinPrivateKey,
Expand All @@ -28,8 +27,8 @@ impl Empty for NullifierKeyValidationRequest {
}
}

impl Serialize<NULLIFIER_KEY_VALIDATION_REQUEST_SERIALIZED_LEN> for NullifierKeyValidationRequest {
fn serialize(self) -> [Field; NULLIFIER_KEY_VALIDATION_REQUEST_SERIALIZED_LEN] {
impl Serialize<NULLIFIER_KEY_VALIDATION_REQUEST_LENGTH> for NullifierKeyValidationRequest {
fn serialize(self) -> [Field; NULLIFIER_KEY_VALIDATION_REQUEST_LENGTH] {
[
self.public_key.x,
self.public_key.y,
Expand All @@ -39,8 +38,8 @@ impl Serialize<NULLIFIER_KEY_VALIDATION_REQUEST_SERIALIZED_LEN> for NullifierKey
}
}

impl Deserialize<NULLIFIER_KEY_VALIDATION_REQUEST_SERIALIZED_LEN> for NullifierKeyValidationRequest {
fn deserialize(fields: [Field; NULLIFIER_KEY_VALIDATION_REQUEST_SERIALIZED_LEN]) -> Self {
impl Deserialize<NULLIFIER_KEY_VALIDATION_REQUEST_LENGTH> for NullifierKeyValidationRequest {
fn deserialize(fields: [Field; NULLIFIER_KEY_VALIDATION_REQUEST_LENGTH]) -> Self {
Self {
public_key: GrumpkinPoint::new(fields[0], fields[1]),
secret_key: GrumpkinPrivateKey::new(fields[2], fields[3]),
Expand Down Expand Up @@ -78,8 +77,8 @@ impl Empty for NullifierKeyValidationRequestContext {
}
}

impl Serialize<NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_SERIALIZED_LEN> for NullifierKeyValidationRequestContext {
fn serialize(self) -> [Field; NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_SERIALIZED_LEN] {
impl Serialize<NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH> for NullifierKeyValidationRequestContext {
fn serialize(self) -> [Field; NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH] {
[
self.public_key.x,
self.public_key.y,
Expand All @@ -90,8 +89,8 @@ impl Serialize<NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_SERIALIZED_LEN> for Null
}
}

impl Deserialize<NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_SERIALIZED_LEN> for NullifierKeyValidationRequestContext {
fn deserialize(fields: [Field; NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_SERIALIZED_LEN]) -> Self {
impl Deserialize<NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH> for NullifierKeyValidationRequestContext {
fn deserialize(fields: [Field; NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH]) -> Self {
Self {
public_key: GrumpkinPoint::new(fields[0], fields[1]),
secret_key: GrumpkinPrivateKey::new(fields[2], fields[3]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ fn serialization_of_empty() {
assert(item.eq(deserialized));
}

// TODO(#4619): Hash non-empty
#[test]
fn empty_hash() {
let mut item: PrivateCallStackItem = dep::std::unsafe::zeroed();
item.function_data.is_private = true;
let hash = item.hash();

// Value from private_call_stack_item.test.ts "computes empty item hash" test
assert_eq(hash, 0x1c3b67cab2bc3dc2106cfeddd8ea68b8d445849f20ed3b9286ad684542aae25d);
assert_eq(hash, 0x07151bc7440eb0b1c4c61a0bcfacca82cf2aed402c20d3742557179f7fedb635);
}
Loading

0 comments on commit 731d7d0

Please sign in to comment.