Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Nov 13, 2024
1 parent 934e558 commit ec2eb91
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 17 deletions.
2 changes: 2 additions & 0 deletions l1-contracts/test/fee_portal/depositToAztecPublic.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ contract DepositToAztecPublic is Test {
uint256 amount = 100 ether;
uint256 expectedIndex = 2 ** Constants.L1_TO_L2_MSG_SUBTREE_HEIGHT;

// The purpose of including the function selector is to make the message unique to that specific call. Note that
// it has nothing to do with calling the function.
DataStructures.L1ToL2Msg memory message = DataStructures.L1ToL2Msg({
sender: DataStructures.L1Actor(address(feeJuicePortal), block.chainid),
recipient: DataStructures.L2Actor(feeJuicePortal.L2_TOKEN_ADDRESS(), 1 + numberOfRollups),
Expand Down
4 changes: 4 additions & 0 deletions l1-contracts/test/portals/TokenPortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ contract TokenPortal {
DataStructures.L2Actor memory actor = DataStructures.L2Actor(l2Bridge, 1);

// Hash the message content to be reconstructed in the receiving contract
// The purpose of including the function selector is to make the message unique to that specific call. Note that
// it has nothing to do with calling the function.
bytes32 contentHash =
Hash.sha256ToField(abi.encodeWithSignature("mint_to_public(bytes32,uint256)", _to, _amount));

Expand Down Expand Up @@ -123,6 +125,8 @@ contract TokenPortal {
uint256 _leafIndex,
bytes32[] calldata _path
) external {
// The purpose of including the function selector is to make the message unique to that specific call. Note that
// it has nothing to do with calling the function.
DataStructures.L2ToL1Msg memory message = DataStructures.L2ToL1Msg({
sender: DataStructures.L2Actor(l2Bridge, 1),
recipient: DataStructures.L1Actor(address(this), block.chainid),
Expand Down
6 changes: 6 additions & 0 deletions l1-contracts/test/portals/TokenPortal.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ contract TokenPortalTest is Test {
view
returns (DataStructures.L1ToL2Msg memory)
{
// The purpose of including the function selector is to make the message unique to that specific call. Note that
// it has nothing to do with calling the function.
return DataStructures.L1ToL2Msg({
sender: DataStructures.L1Actor(address(tokenPortal), block.chainid),
recipient: DataStructures.L2Actor(l2TokenAddress, 1),
Expand All @@ -102,6 +104,8 @@ contract TokenPortalTest is Test {
view
returns (DataStructures.L1ToL2Msg memory)
{
// The purpose of including the function selector is to make the message unique to that specific call. Note that
// it has nothing to do with calling the function.
return DataStructures.L1ToL2Msg({
sender: DataStructures.L1Actor(address(tokenPortal), block.chainid),
recipient: DataStructures.L2Actor(l2TokenAddress, 1),
Expand Down Expand Up @@ -171,6 +175,8 @@ contract TokenPortalTest is Test {
internal
returns (bytes32, bytes32)
{
// The purpose of including the function selector is to make the message unique to that specific call. Note that
// it has nothing to do with calling the function.
bytes32 l2ToL1Message = Hash.sha256ToField(
DataStructures.L2ToL1Msg({
sender: DataStructures.L2Actor({actor: l2TokenAddress, version: 1}),
Expand Down
4 changes: 4 additions & 0 deletions l1-contracts/test/portals/UniswapPortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ contract UniswapPortal {

{
// prevent stack too deep errors
// The purpose of including the function selector is to make the message unique to that specific call. Note that
// it has nothing to do with calling the function.
vars.contentHash = Hash.sha256ToField(
abi.encodeWithSignature(
"swap_public(address,uint256,uint24,address,uint256,bytes32,bytes32,address)",
Expand Down Expand Up @@ -191,6 +193,8 @@ contract UniswapPortal {

{
// prevent stack too deep errors
// The purpose of including the function selector is to make the message unique to that specific call. Note that
// it has nothing to do with calling the function.
vars.contentHash = Hash.sha256ToField(
abi.encodeWithSignature(
"swap_private(address,uint256,uint24,address,uint256,bytes32,address)",
Expand Down
6 changes: 6 additions & 0 deletions l1-contracts/test/portals/UniswapPortal.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ contract UniswapPortalTest is Test {
view
returns (bytes32 l2ToL1MessageHash)
{
// The purpose of including the function selector is to make the message unique to that specific call. Note that
// it has nothing to do with calling the function.
DataStructures.L2ToL1Msg memory message = DataStructures.L2ToL1Msg({
sender: DataStructures.L2Actor(l2TokenAddress, 1),
recipient: DataStructures.L1Actor(address(daiTokenPortal), block.chainid),
Expand All @@ -116,6 +118,8 @@ contract UniswapPortalTest is Test {
view
returns (bytes32 l2ToL1MessageHash)
{
// The purpose of including the function selector is to make the message unique to that specific call. Note that
// it has nothing to do with calling the function.
DataStructures.L2ToL1Msg memory message = DataStructures.L2ToL1Msg({
sender: DataStructures.L2Actor(l2UniswapAddress, 1),
recipient: DataStructures.L1Actor(address(uniswapPortal), block.chainid),
Expand Down Expand Up @@ -143,6 +147,8 @@ contract UniswapPortalTest is Test {
* Set to address(0) if anyone can call.
*/
function _createUniswapSwapMessagePrivate(address _caller) internal view returns (bytes32) {
// The purpose of including the function selector is to make the message unique to that specific call. Note that
// it has nothing to do with calling the function.
DataStructures.L2ToL1Msg memory message = DataStructures.L2ToL1Msg({
sender: DataStructures.L2Actor(l2UniswapAddress, 1),
recipient: DataStructures.L1Actor(address(uniswapPortal), block.chainid),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ pub fn get_bridge_gas_msg_hash(owner: AztecAddress, amount: Field) -> Field {
hash_bytes[i + 36] = amount_bytes[i];
}

// Function selector: 0x63f44968 keccak256('claim(bytes32,uint256)')
hash_bytes[0] = 0x63;
hash_bytes[1] = 0xf4;
hash_bytes[2] = 0x49;
hash_bytes[3] = 0x68;
// The purpose of including the following selector is to make the message unique to that specific call. Note that
// it has nothing to do with calling the function.
let selector = comptime { std::hash::keccak256("claim(bytes32,uint256)".as_bytes(), 22) };

hash_bytes[0] = selector[0];
hash_bytes[1] = selector[1];
hash_bytes[2] = selector[2];
hash_bytes[3] = selector[3];

let content_hash = sha256_to_field(hash_bytes);
content_hash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ pub fn get_mint_to_public_content_hash(owner: AztecAddress, amount: Field) -> Fi
hash_bytes[i + 36] = amount_bytes[i];
}

let selector = comptime { std::hash::keccak256("mint_to_public(bytes32,uint256)".as_bytes(), 24) };
// The purpose of including the following selector is to make the message unique to that specific call. Note that
// it has nothing to do with calling the function.
let selector = comptime { std::hash::keccak256("mint_to_public(bytes32,uint256)".as_bytes(), 31) };

hash_bytes[0] = selector[0];
hash_bytes[1] = selector[1];
Expand All @@ -39,6 +41,8 @@ pub fn get_mint_to_private_content_hash(
hash_bytes[i + 4] = amount_bytes[i];
}

// The purpose of including the following selector is to make the message unique to that specific call. Note that
// it has nothing to do with calling the function.
let selector = comptime { std::hash::keccak256("mint_to_private(uint256)".as_bytes(), 24) };

hash_bytes[0] = selector[0];
Expand All @@ -63,7 +67,9 @@ pub fn get_withdraw_content_hash(recipient: EthAddress, amount: Field, caller_on
let amount_bytes: [u8; 32] = amount.to_be_bytes();
let caller_on_l1_bytes: [u8; 32] = caller_on_l1.to_field().to_be_bytes();

let selector = comptime { std::hash::keccak256("withdraw(address,uint256,address)".as_bytes(), 24) };
// The purpose of including the following selector is to make the message unique to that specific call. Note that
// it has nothing to do with calling the function.
let selector = comptime { std::hash::keccak256("withdraw(address,uint256,address)".as_bytes(), 33) };

hash_bytes[0] = selector[0];
hash_bytes[1] = selector[1];
Expand Down
36 changes: 26 additions & 10 deletions noir-projects/noir-contracts/contracts/uniswap_contract/src/util.nr
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@ pub fn compute_swap_public_content_hash(
secret_hash_for_L1_to_l2_message.to_be_bytes();
let caller_on_L1_bytes: [u8; 32] = caller_on_L1.to_field().to_be_bytes();

// function selector: 0xf18186d8 keccak256("swap_public(address,uint256,uint24,address,uint256,bytes32,bytes32,address)")
hash_bytes[0] = 0xf1;
hash_bytes[1] = 0x81;
hash_bytes[2] = 0x86;
hash_bytes[3] = 0xd8;
// The purpose of including the following selector is to make the message unique to that specific call. Note that
// it has nothing to do with calling the function.
let selector = comptime {
std::hash::keccak256(
"swap_public(address,uint256,uint24,address,uint256,bytes32,bytes32,address)".as_bytes(),
75,
)
};

hash_bytes[0] = selector[0];
hash_bytes[1] = selector[1];
hash_bytes[2] = selector[2];
hash_bytes[3] = selector[3];

for i in 0..32 {
hash_bytes[i + 4] = input_token_portal_bytes[i];
Expand Down Expand Up @@ -73,11 +81,19 @@ pub fn compute_swap_private_content_hash(
secret_hash_for_L1_to_l2_message.to_be_bytes();
let caller_on_L1_bytes: [u8; 32] = caller_on_L1.to_field().to_be_bytes();

// function selector: 0x84e55078 keccak256("swap_private(address,uint256,uint24,address,uint256,bytes32,address)")
hash_bytes[0] = 0x84;
hash_bytes[1] = 0xe5;
hash_bytes[2] = 0x50;
hash_bytes[3] = 0x78;
// The purpose of including the following selector is to make the message unique to that specific call. Note that
// it has nothing to do with calling the function.
let selector = comptime {
std::hash::keccak256(
"swap_private(address,uint256,uint24,address,uint256,bytes32,address)".as_bytes(),
68,
)
};

hash_bytes[0] = selector[0];
hash_bytes[1] = selector[1];
hash_bytes[2] = selector[2];
hash_bytes[3] = selector[3];

for i in 0..32 {
hash_bytes[i + 4] = input_token_portal_bytes[i];
Expand Down

0 comments on commit ec2eb91

Please sign in to comment.