diff --git a/docs/docs/dev_docs/contracts/portals/main.md b/docs/docs/dev_docs/contracts/portals/main.md index e1450b98140..ac1a5ec8ed6 100644 --- a/docs/docs/dev_docs/contracts/portals/main.md +++ b/docs/docs/dev_docs/contracts/portals/main.md @@ -50,7 +50,8 @@ Computing the `content` must be done manually in its current form, as we are sti :::info The `content_hash` is a sha256 truncated to a field element (~ 254 bits). In Aztec-nr, you can use our `sha256_to_field()` to do a sha256 hash which fits in one field element: -#include_code mint_public_content_hash_nr /yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/util.nr rust +#include_code mint_public_content_hash_nr /yarn-project/noir-contracts/src/contracts/token_portal_content_hash_lib/src/lib.nr rust + In solidity, you can use our `Hash.sha256ToField()` method: diff --git a/yarn-project/noir-contracts/src/contracts/test_contract/Nargo.toml b/yarn-project/noir-contracts/src/contracts/test_contract/Nargo.toml index c4818f6ba32..afa5e911ac9 100644 --- a/yarn-project/noir-contracts/src/contracts/test_contract/Nargo.toml +++ b/yarn-project/noir-contracts/src/contracts/test_contract/Nargo.toml @@ -6,3 +6,4 @@ type = "contract" [dependencies] aztec = { path = "../../../../aztec-nr/aztec" } +token_portal_content_hash_lib = { path = "../token_portal_content_hash_lib" } diff --git a/yarn-project/noir-contracts/src/contracts/test_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/test_contract/src/main.nr index fd163e64771..76f4fd922b0 100644 --- a/yarn-project/noir-contracts/src/contracts/test_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/test_contract/src/main.nr @@ -1,5 +1,3 @@ -mod utils; - // A contract used for testing a random hodgepodge of small features from simulator and end-to-end tests. contract Test { use dep::aztec::{ @@ -15,7 +13,7 @@ contract Test { types::vec::BoundedVec, constants_gen::EMPTY_NULLIFIED_COMMITMENT, }; - use crate::utils::{get_mint_private_content_hash, get_mint_public_content_hash}; + use dep::token_portal_content_hash_lib::{get_mint_private_content_hash, get_mint_public_content_hash}; #[event] struct ExampleEvent { diff --git a/yarn-project/noir-contracts/src/contracts/test_contract/src/utils.nr b/yarn-project/noir-contracts/src/contracts/test_contract/src/utils.nr deleted file mode 100644 index d3200fc2117..00000000000 --- a/yarn-project/noir-contracts/src/contracts/test_contract/src/utils.nr +++ /dev/null @@ -1,50 +0,0 @@ -use dep::aztec::hash::{sha256_to_field}; - -// Computes a content hash of a deposit/mint_public message. -// Refer TokenPortal.sol for reference on L1. -pub fn get_mint_public_content_hash(amount: Field, owner_address: Field, canceller: Field) -> Field { - - let mut hash_bytes: [u8; 100] = [0; 100]; - let amount_bytes = amount.to_be_bytes(32); - let recipient_bytes = owner_address.to_be_bytes(32); - let canceller_bytes = canceller.to_be_bytes(32); - - for i in 0..32 { - hash_bytes[i + 4] = amount_bytes[i]; - hash_bytes[i + 36] = recipient_bytes[i]; - hash_bytes[i + 68] = canceller_bytes[i]; - } - - // Function selector: 0x63c9440d keccak256('mint_public(uint256,bytes32,address)') - hash_bytes[0] = 0x63; - hash_bytes[1] = 0xc9; - hash_bytes[2] = 0x44; - hash_bytes[3] = 0x0d; - - let content_hash = sha256_to_field(hash_bytes); - content_hash -} - -// Computes a content hash of a deposit/mint_private message. -// Refer TokenPortal.sol for reference on L1. -pub fn get_mint_private_content_hash(amount: Field, secret_hash_for_redeeming_minted_notes: Field, canceller: Field) -> Field { - let mut hash_bytes: [u8; 100] = [0; 100]; - let amount_bytes = amount.to_be_bytes(32); - let secret_hash_bytes = secret_hash_for_redeeming_minted_notes.to_be_bytes(32); - let canceller_bytes = canceller.to_be_bytes(32); - - for i in 0..32 { - hash_bytes[i + 4] = amount_bytes[i]; - hash_bytes[i + 36] = secret_hash_bytes[i]; - hash_bytes[i + 68] = canceller_bytes[i]; - } - - // Function selector: 0x25d46b0f keccak256('mint_private(uint256,bytes32,address)') - hash_bytes[0] = 0x25; - hash_bytes[1] = 0xd4; - hash_bytes[2] = 0x6b; - hash_bytes[3] = 0x0f; - - let content_hash = sha256_to_field(hash_bytes); - content_hash -} diff --git a/yarn-project/noir-contracts/src/contracts/token_bridge_contract/Nargo.toml b/yarn-project/noir-contracts/src/contracts/token_bridge_contract/Nargo.toml index cdad0512395..2dd5853eae1 100644 --- a/yarn-project/noir-contracts/src/contracts/token_bridge_contract/Nargo.toml +++ b/yarn-project/noir-contracts/src/contracts/token_bridge_contract/Nargo.toml @@ -5,4 +5,5 @@ compiler_version = "0.1" type = "contract" [dependencies] -aztec = { path = "../../../../aztec-nr/aztec" } \ No newline at end of file +aztec = { path = "../../../../aztec-nr/aztec" } +token_portal_content_hash_lib = { path = "../token_portal_content_hash_lib" } \ No newline at end of file diff --git a/yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/main.nr index 93dd19149a3..dad2866cdc9 100644 --- a/yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/main.nr @@ -1,4 +1,3 @@ -mod util; mod token_interface; // Minimal implementation of the token bridge that can move funds between L1 <> L2. @@ -17,9 +16,9 @@ contract TokenBridge { types::address::{AztecAddress, EthereumAddress}, selector::compute_selector, }; + use dep::token_portal_content_hash_lib::{get_mint_public_content_hash, get_mint_private_content_hash, get_withdraw_content_hash}; use crate::token_interface::Token; - use crate::util::{get_mint_public_content_hash, get_mint_private_content_hash, get_withdraw_content_hash}; // Storage structure, containing all storage, and specifying what slots they use. struct Storage { diff --git a/yarn-project/noir-contracts/src/contracts/token_portal_content_hash_lib/Nargo.toml b/yarn-project/noir-contracts/src/contracts/token_portal_content_hash_lib/Nargo.toml new file mode 100644 index 00000000000..f3dfa0e4f2a --- /dev/null +++ b/yarn-project/noir-contracts/src/contracts/token_portal_content_hash_lib/Nargo.toml @@ -0,0 +1,8 @@ +[package] +name = "token_portal_content_hash_lib" +authors = ["aztec-labs"] +compiler_version = "0.1" +type = "lib" + +[dependencies] +aztec = { path = "../../../../aztec-nr/aztec" } \ No newline at end of file diff --git a/yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/util.nr b/yarn-project/noir-contracts/src/contracts/token_portal_content_hash_lib/src/lib.nr similarity index 100% rename from yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/util.nr rename to yarn-project/noir-contracts/src/contracts/token_portal_content_hash_lib/src/lib.nr