Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: uniswap private flow #2559

Merged
merged 5 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 42 additions & 39 deletions circuits/cpp/src/aztec3/circuits/rollup/base/.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -854,44 +854,47 @@ TEST_F(base_rollup_tests, native_multiple_public_state_read_writes)
// run_cbind(inputs, outputs);
}

TEST_F(base_rollup_tests, native_invalid_public_state_read)
{
DummyCircuitBuilder builder = DummyCircuitBuilder("base_rollup_tests__native_invalid_public_state_read");
MemoryStore private_data_tree_store;
MerkleTree private_data_tree(private_data_tree_store, PRIVATE_DATA_TREE_HEIGHT);

MemoryStore contract_tree_store;
MerkleTree contract_tree(contract_tree_store, CONTRACT_TREE_HEIGHT);

MemoryStore public_data_tree_store;
MerkleTree public_data_tree(public_data_tree_store, PUBLIC_DATA_TREE_HEIGHT);

MemoryStore l1_to_l2_messages_tree_store;
MerkleTree l1_to_l2_messages_tree(l1_to_l2_messages_tree_store, L1_TO_L2_MSG_TREE_HEIGHT);

auto data_read = abis::PublicDataRead<NT>{
.leaf_index = fr(1),
.value = fr(42),
};

std::array<PreviousKernelData<NT>, 2> kernel_data = { get_empty_kernel(), get_empty_kernel() };
kernel_data[0].public_inputs.end.public_data_reads[0] = data_read;
auto inputs = test_utils::utils::base_rollup_inputs_from_kernels(
kernel_data, private_data_tree, contract_tree, public_data_tree, l1_to_l2_messages_tree);

// We change the initial tree root so the read value does not match
public_data_tree.update_element(1, fr(43));
inputs.start_public_data_tree_root = public_data_tree.root();

BaseOrMergeRollupPublicInputs outputs =
aztec3::circuits::rollup::native_base_rollup::base_rollup_circuit(builder, inputs);

ASSERT_EQ(outputs.start_public_data_tree_root, inputs.start_public_data_tree_root);
ASSERT_EQ(outputs.end_public_data_tree_root, public_data_tree.root());
ASSERT_EQ(outputs.end_public_data_tree_root, outputs.start_public_data_tree_root);
ASSERT_TRUE(builder.failed());
// TODO(1998): see above
// run_cbind(inputs, outputs, true, false);
}
// TODO(#2521) - data read validation should happen against the current state of the tx and not the start state.
// https://aztecprotocol.slack.com/archives/C02M7VC7TN0/p1695809629015719?thread_ts=1695653252.007339&cid=C02M7VC7TN0

// TEST_F(base_rollup_tests, native_invalid_public_state_read)
// {
// DummyCircuitBuilder builder = DummyCircuitBuilder("base_rollup_tests__native_invalid_public_state_read");
// MemoryStore private_data_tree_store;
// MerkleTree private_data_tree(private_data_tree_store, PRIVATE_DATA_TREE_HEIGHT);

// MemoryStore contract_tree_store;
// MerkleTree contract_tree(contract_tree_store, CONTRACT_TREE_HEIGHT);

// MemoryStore public_data_tree_store;
// MerkleTree public_data_tree(public_data_tree_store, PUBLIC_DATA_TREE_HEIGHT);

// MemoryStore l1_to_l2_messages_tree_store;
// MerkleTree l1_to_l2_messages_tree(l1_to_l2_messages_tree_store, L1_TO_L2_MSG_TREE_HEIGHT);

// auto data_read = abis::PublicDataRead<NT>{
// .leaf_index = fr(1),
// .value = fr(42),
// };

// std::array<PreviousKernelData<NT>, 2> kernel_data = { get_empty_kernel(), get_empty_kernel() };
// kernel_data[0].public_inputs.end.public_data_reads[0] = data_read;
// auto inputs = test_utils::utils::base_rollup_inputs_from_kernels(
// kernel_data, private_data_tree, contract_tree, public_data_tree, l1_to_l2_messages_tree);

// // We change the initial tree root so the read value does not match
// public_data_tree.update_element(1, fr(43));
// inputs.start_public_data_tree_root = public_data_tree.root();

// BaseOrMergeRollupPublicInputs outputs =
// aztec3::circuits::rollup::native_base_rollup::base_rollup_circuit(builder, inputs);

// ASSERT_EQ(outputs.start_public_data_tree_root, inputs.start_public_data_tree_root);
// ASSERT_EQ(outputs.end_public_data_tree_root, public_data_tree.root());
// ASSERT_EQ(outputs.end_public_data_tree_root, outputs.start_public_data_tree_root);
// ASSERT_TRUE(builder.failed());
// // TODO(1998): see above
// // run_cbind(inputs, outputs, true, false);
// }

} // namespace aztec3::circuits::rollup::base::native_base_rollup_circuit
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,17 @@ void validate_public_data_reads(

fr validate_and_process_public_state(DummyBuilder& builder, BaseRollupInputs const& baseRollupInputs)
{
// TODO(#2521) - data read validation should happen against the current state of the tx and not the start state.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hunger Games Student GIF

// Blocks all interesting usecases that read and write to the same public state in the same tx.
// https://aztecprotocol.slack.com/archives/C02M7VC7TN0/p1695809629015719?thread_ts=1695653252.007339&cid=C02M7VC7TN0


// Process public data reads and public data update requests for left input
validate_public_data_reads(builder,
baseRollupInputs.start_public_data_tree_root,
baseRollupInputs.kernel_data[0].public_inputs.end.public_data_reads,
0,
baseRollupInputs.new_public_data_reads_sibling_paths);
// validate_public_data_reads(builder,
// baseRollupInputs.start_public_data_tree_root,
// baseRollupInputs.kernel_data[0].public_inputs.end.public_data_reads,
// 0,
// baseRollupInputs.new_public_data_reads_sibling_paths);

auto mid_public_data_tree_root = insert_public_data_update_requests(
builder,
Expand All @@ -432,13 +437,19 @@ fr validate_and_process_public_state(DummyBuilder& builder, BaseRollupInputs con
0,
baseRollupInputs.new_public_data_update_requests_sibling_paths);


// TODO(#2521) - data read validation should happen against the current state of the tx and not the start state.
// Blocks all interesting usecases that read and write to the same public state in the same tx.
// https://aztecprotocol.slack.com/archives/C02M7VC7TN0/p1695809629015719?thread_ts=1695653252.007339&cid=C02M7VC7TN0


// Process public data reads and public data update requests for right input using the resulting tree root from the
// left one
validate_public_data_reads(builder,
mid_public_data_tree_root,
baseRollupInputs.kernel_data[1].public_inputs.end.public_data_reads,
MAX_PUBLIC_DATA_READS_PER_TX,
baseRollupInputs.new_public_data_reads_sibling_paths);
// validate_public_data_reads(builder,
// mid_public_data_tree_root,
// baseRollupInputs.kernel_data[1].public_inputs.end.public_data_reads,
// MAX_PUBLIC_DATA_READS_PER_TX,
// baseRollupInputs.new_public_data_reads_sibling_paths);

auto end_public_data_tree_root = insert_public_data_update_requests(
builder,
Expand Down
28 changes: 14 additions & 14 deletions l1-contracts/test/portals/TokenPortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ contract TokenPortal {
// docs:start:deposit_public
/**
* @notice Deposit funds into the portal and adds an L2 message which can only be consumed publicly on Aztec
* @param _to - The aztec address of the recipient
* @param _amount - The amount to deposit
* @param _to - The aztec address of the recipient
* @param _canceller - The address that can cancel the L1 to L2 message
* @param _deadline - The timestamp after which the entry can be cancelled
* @param _secretHash - The hash of the secret consumable message. The hash should be 254 bits (so it can fit in a Field element)
* @param _canceller - The address that can cancel the L1 to L2 message
* @return The key of the entry in the Inbox
*/
function depositToAztecPublic(
bytes32 _to,
uint256 _amount,
bytes32 _to,
address _canceller,
uint32 _deadline,
bytes32 _secretHash,
address _canceller
bytes32 _secretHash
) external payable returns (bytes32) {
// Preamble
IInbox inbox = registry.getInbox();
Expand All @@ -59,18 +59,18 @@ contract TokenPortal {
/**
* @notice Deposit funds into the portal and adds an L2 message which can only be consumed privately on Aztec
* @param _amount - The amount to deposit
* @param _secretHashForRedeemingMintedNotes - The hash of the secret to redeem minted notes privately on Aztec. The hash should be 254 bits (so it can fit in a Field element)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It pains me a little that the ordering of arguments for the function is different than to the inner calls. The deadline and _secretHashForL2MessageConsumption are not part of the content, but they are still showed into the middle of it.

* @param _canceller - The address that can cancel the L1 to L2 message
* @param _deadline - The timestamp after which the entry can be cancelled
* @param _secretHashForL2MessageConsumption - The hash of the secret consumable L1 to L2 message. The hash should be 254 bits (so it can fit in a Field element)
* @param _secretHashForL2MessageConsumption - The hash of the secret to redeem minted notes privately on Aztec. The hash should be 254 bits (so it can fit in a Field element)
* @param _canceller - The address that can cancel the L1 to L2 message
* @return The key of the entry in the Inbox
*/
function depositToAztecPrivate(
uint256 _amount,
uint32 _deadline,
bytes32 _secretHashForL2MessageConsumption,
bytes32 _secretHashForRedeemingMintedNotes,
address _canceller
address _canceller,
uint32 _deadline,
bytes32 _secretHashForL2MessageConsumption
) external payable returns (bytes32) {
// Preamble
IInbox inbox = registry.getInbox();
Expand Down Expand Up @@ -99,16 +99,16 @@ contract TokenPortal {
/**
* @notice Cancel a public depositToAztec L1 to L2 message
* @dev only callable by the `canceller` of the message
* @param _to - The aztec address of the recipient in the original message
* @param _amount - The amount to deposit per the original message
* @param _to - The aztec address of the recipient in the original message
* @param _deadline - The timestamp after which the entry can be cancelled
* @param _secretHash - The hash of the secret consumable message in the original message
* @param _fee - The fee paid to the sequencer
* @return The key of the entry in the Inbox
*/
function cancelL1ToAztecMessagePublic(
bytes32 _to,
uint256 _amount,
bytes32 _to,
uint32 _deadline,
bytes32 _secretHash,
uint64 _fee
Expand Down Expand Up @@ -137,17 +137,17 @@ contract TokenPortal {
* @notice Cancel a private depositToAztec L1 to L2 message
* @dev only callable by the `canceller` of the message
* @param _amount - The amount to deposit per the original message
* @param _secretHashForRedeemingMintedNotes - The hash of the secret to redeem minted notes privately on Aztec
* @param _deadline - The timestamp after which the entry can be cancelled
* @param _secretHashForL2MessageConsumption - The hash of the secret consumable L1 to L2 message
* @param _secretHashForL2MessageConsumption - The hash of the secret to redeem minted notes privately on Aztec
* @param _fee - The fee paid to the sequencer
* @return The key of the entry in the Inbox
*/
function cancelL1ToAztecMessagePrivate(
uint256 _amount,
bytes32 _secretHashForRedeemingMintedNotes,
uint32 _deadline,
bytes32 _secretHashForL2MessageConsumption,
bytes32 _secretHashForRedeemingMintedNotes,
uint64 _fee
) external returns (bytes32) {
IInbox inbox = registry.getInbox();
Expand Down
20 changes: 10 additions & 10 deletions l1-contracts/test/portals/TokenPortal.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ contract TokenPortalTest is Test {
// Perform op
bytes32 entryKey = tokenPortal.depositToAztecPrivate{value: bid}(
amount,
deadline,
secretHashForL2MessageConsumption,
secretHashForRedeemingMintedNotes,
address(this)
address(this),
deadline,
secretHashForL2MessageConsumption
);

assertEq(entryKey, expectedEntryKey, "returned entry key and calculated entryKey should match");
Expand Down Expand Up @@ -185,7 +185,7 @@ contract TokenPortalTest is Test {

// Perform op
bytes32 entryKey = tokenPortal.depositToAztecPublic{value: bid}(
to, amount, deadline, secretHashForL2MessageConsumption, address(this)
amount, to, address(this), deadline, secretHashForL2MessageConsumption
);

assertEq(entryKey, expectedEntryKey, "returned entry key and calculated entryKey should match");
Expand All @@ -210,7 +210,7 @@ contract TokenPortalTest is Test {
abi.encodeWithSelector(Errors.Inbox__NothingToConsume.selector, expectedWrongEntryKey)
);
tokenPortal.cancelL1ToAztecMessagePublic(
to, amount, deadline, secretHashForL2MessageConsumption, bid
amount, to, deadline, secretHashForL2MessageConsumption, bid
);
vm.stopPrank();

Expand All @@ -221,7 +221,7 @@ contract TokenPortalTest is Test {
abi.encodeWithSelector(Errors.Inbox__NothingToConsume.selector, expectedWrongEntryKey)
);
tokenPortal.cancelL1ToAztecMessagePrivate(
amount, deadline, secretHashForL2MessageConsumption, secretHashForRedeemingMintedNotes, bid
amount, secretHashForRedeemingMintedNotes, deadline, secretHashForL2MessageConsumption, bid
);

// actually cancel the message
Expand All @@ -231,7 +231,7 @@ contract TokenPortalTest is Test {
emit L1ToL2MessageCancelled(expectedEntryKey);
// perform op
bytes32 entryKey = tokenPortal.cancelL1ToAztecMessagePublic(
to, amount, deadline, secretHashForL2MessageConsumption, bid
amount, to, deadline, secretHashForL2MessageConsumption, bid
);

assertEq(entryKey, expectedEntryKey, "returned entry key and calculated entryKey should match");
Expand All @@ -257,7 +257,7 @@ contract TokenPortalTest is Test {
abi.encodeWithSelector(Errors.Inbox__NothingToConsume.selector, expectedWrongEntryKey)
);
tokenPortal.cancelL1ToAztecMessagePrivate(
amount, deadline, secretHashForL2MessageConsumption, secretHashForRedeemingMintedNotes, bid
amount, secretHashForRedeemingMintedNotes, deadline, secretHashForL2MessageConsumption, bid
);
vm.stopPrank();

Expand All @@ -268,7 +268,7 @@ contract TokenPortalTest is Test {
abi.encodeWithSelector(Errors.Inbox__NothingToConsume.selector, expectedWrongEntryKey)
);
tokenPortal.cancelL1ToAztecMessagePublic(
to, amount, deadline, secretHashForL2MessageConsumption, bid
amount, to, deadline, secretHashForL2MessageConsumption, bid
);

// actually cancel the message
Expand All @@ -278,7 +278,7 @@ contract TokenPortalTest is Test {
emit L1ToL2MessageCancelled(expectedEntryKey);
// perform op
bytes32 entryKey = tokenPortal.cancelL1ToAztecMessagePrivate(
amount, deadline, secretHashForL2MessageConsumption, secretHashForRedeemingMintedNotes, bid
amount, secretHashForRedeemingMintedNotes, deadline, secretHashForL2MessageConsumption, bid
);

assertEq(entryKey, expectedEntryKey, "returned entry key and calculated entryKey should match");
Expand Down
Loading