Skip to content

Commit

Permalink
uniswap private flow
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul-kothari committed Sep 27, 2023
1 parent fdf5fce commit deaead8
Show file tree
Hide file tree
Showing 20 changed files with 936 additions and 792 deletions.
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.
// 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
2 changes: 1 addition & 1 deletion l1-contracts/test/portals/TokenPortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ contract TokenPortal {
* @param _amount - The amount to deposit
* @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 _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)
* @param _canceller - The address that can cancel the L1 to L2 message
* @return The key of the entry in the Inbox
*/
Expand Down
43 changes: 32 additions & 11 deletions l1-contracts/test/portals/UniswapPortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ contract UniswapPortal {
* @param _uniswapFeeTier - The fee tier for the swap on UniswapV3
* @param _outputTokenPortal - The ethereum address of the output token portal
* @param _amountOutMinimum - The minimum amount of output assets to receive from the swap (slippage protection)
* @param _aztecRecipient - The aztec address to receive the output assets
* @param _secretHash - The hash of the secret consumable message
* @param _aztecRecipientOrSecretHashForRedeemingMintedNotes - If public flow, the aztec address to receive the output assets. If private, the hash of the secret to redeem minted notes privately on Aztec.
* @param _secretHashForL1ToL2Message - The hash of the secret consumable message
* @param _deadlineForL1ToL2Message - deadline for when the L1 to L2 message (to mint outpiut assets in L2) must be consumed by
* @param _canceller - The ethereum address that can cancel the deposit
* @param _withCaller - When true, using `msg.sender` as the caller, otherwise address(0)
* @param _isPrivateFlow - When true, the output assets will be minted privately on Aztec, otherwise publicly
* @return The entryKey of the deposit transaction in the Inbox
*/
function swap(
Expand All @@ -58,11 +59,12 @@ contract UniswapPortal {
uint24 _uniswapFeeTier,
address _outputTokenPortal,
uint256 _amountOutMinimum,
bytes32 _aztecRecipient,
bytes32 _secretHash,
bytes32 _aztecRecipientOrSecretHashForRedeemingMintedNotes,
bytes32 _secretHashForL1ToL2Message,
uint32 _deadlineForL1ToL2Message,
address _canceller,
bool _withCaller
bool _withCaller,
bool _isPrivateFlow
) public payable returns (bytes32) {
LocalSwapVars memory vars;

Expand All @@ -73,16 +75,23 @@ contract UniswapPortal {
TokenPortal(_inputTokenPortal).withdraw(_inAmount, address(this), true);
{
// prevent stack too deep errors

// having two different hashes mean you can't consume a message intended for private in public.
string memory functionSignature = _isPrivateFlow
?
"swap_private(address,uint256,uint24,address,uint256,bytes32,bytes32,uint32,address,address)"
: "swap_public(address,uint256,uint24,address,uint256,bytes32,bytes32,uint32,address,address)";

vars.contentHash = Hash.sha256ToField(
abi.encodeWithSignature(
"swap(address,uint256,uint24,address,uint256,bytes32,bytes32,uint32,address,address)",
functionSignature,
_inputTokenPortal,
_inAmount,
_uniswapFeeTier,
_outputTokenPortal,
_amountOutMinimum,
_aztecRecipient,
_secretHash,
_aztecRecipientOrSecretHashForRedeemingMintedNotes,
_secretHashForL1ToL2Message,
_deadlineForL1ToL2Message,
_canceller,
_withCaller ? msg.sender : address(0)
Expand Down Expand Up @@ -121,10 +130,22 @@ contract UniswapPortal {
// Note, safeApprove was deprecated from Oz
vars.outputAsset.approve(address(_outputTokenPortal), amountOut);

// Deposit the output asset to the L2 via its portal]
// TODO(2167) - Update UniswapPortal properly with new portal standard.
// Deposit the output asset to the L2 via its portal
if (_isPrivateFlow) {
return TokenPortal(_outputTokenPortal).depositToAztecPrivate{value: msg.value}(
amountOut,
_deadlineForL1ToL2Message,
_secretHashForL1ToL2Message,
_aztecRecipientOrSecretHashForRedeemingMintedNotes,
_canceller
);
}
return TokenPortal(_outputTokenPortal).depositToAztecPublic{value: msg.value}(
_aztecRecipient, amountOut, _deadlineForL1ToL2Message, _secretHash, _canceller
_aztecRecipientOrSecretHashForRedeemingMintedNotes,
amountOut,
_deadlineForL1ToL2Message,
_secretHashForL1ToL2Message,
_canceller
);
}
// docs:end:solidity_uniswap_swap
Expand Down
Loading

0 comments on commit deaead8

Please sign in to comment.