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

Global variables in public inputs #861

Closed
wants to merge 3 commits into from
Closed
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
12 changes: 8 additions & 4 deletions circuits/cpp/src/aztec3/circuits/abis/c_bind.test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "c_bind.h"

#include "function_leaf_preimage.hpp"
#include "tx_request.hpp"

Expand Down Expand Up @@ -66,8 +65,14 @@ TEST(abi_tests, hash_tx_request)
.function_data = FunctionData<NT>(),
.args_hash = NT::fr::random_element(),
.nonce = NT::fr::random_element(),
.tx_context = TxContext<NT>(),
.chain_id = NT::fr::random_element(),
.tx_context = {
.is_fee_payment_tx = static_cast<bool>(engine.get_random_uint8() & 1),
.is_rebate_payment_tx = static_cast<bool>(engine.get_random_uint8() & 1),
.is_contract_deployment_tx = false,
.contract_deployment_data = ContractDeploymentData<NT>(),
.chain_id = NT::fr::random_element(),
.version = NT::fr::random_element(),
},
};

// Write the tx request to a buffer and
Expand Down Expand Up @@ -325,7 +330,6 @@ TEST(abi_tests, compute_transaction_hash)
.args_hash = NT::fr::random_element(),
.nonce = NT::fr::random_element(),
.tx_context = TxContext<NT>(),
.chain_id = NT::fr::random_element(),
};

std::array<uint8_t, 32> const r{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "../append_only_tree_snapshot.hpp"

#include "aztec3/circuits/global_variables.hpp"

#include <barretenberg/barretenberg.hpp>

namespace aztec3::circuits::abis {
Expand All @@ -20,13 +22,17 @@ template <typename NCT> struct ConstantRollupData {
fr base_rollup_vk_hash = 0;
fr merge_rollup_vk_hash = 0;

// Global variables
GlobalVariables<NCT> global_variables{};

MSGPACK_FIELDS(start_tree_of_historic_private_data_tree_roots_snapshot,
start_tree_of_historic_contract_tree_roots_snapshot,
start_tree_of_historic_l1_to_l2_msg_tree_roots_snapshot,
private_kernel_vk_tree_root,
public_kernel_vk_tree_root,
base_rollup_vk_hash,
merge_rollup_vk_hash);
merge_rollup_vk_hash,
global_variables);

bool operator==(ConstantRollupData<NCT> const&) const = default;
};
Expand All @@ -42,6 +48,7 @@ template <typename NCT> void read(uint8_t const*& it, ConstantRollupData<NCT>& o
read(it, obj.public_kernel_vk_tree_root);
read(it, obj.base_rollup_vk_hash);
read(it, obj.merge_rollup_vk_hash);
read(it, obj.global_variables);
};

template <typename NCT> void write(std::vector<uint8_t>& buf, ConstantRollupData<NCT> const& obj)
Expand All @@ -55,6 +62,7 @@ template <typename NCT> void write(std::vector<uint8_t>& buf, ConstantRollupData
write(buf, obj.public_kernel_vk_tree_root);
write(buf, obj.base_rollup_vk_hash);
write(buf, obj.merge_rollup_vk_hash);
write(buf, obj.global_variables);
};

template <typename NCT> std::ostream& operator<<(std::ostream& os, ConstantRollupData<NCT> const& obj)
Expand All @@ -68,7 +76,8 @@ template <typename NCT> std::ostream& operator<<(std::ostream& os, ConstantRollu
<< "private_kernel_vk_tree_root: " << obj.private_kernel_vk_tree_root << "\n"
<< "public_kernel_vk_tree_root: " << obj.public_kernel_vk_tree_root << "\n"
<< "base_rollup_vk_hash: " << obj.base_rollup_vk_hash << "\n"
<< "merge_rollup_vk_hash: " << obj.merge_rollup_vk_hash << "\n";
<< "merge_rollup_vk_hash: " << obj.merge_rollup_vk_hash << "\n"
<< "global_variables: " << obj.global_variables << "\n";
}

} // namespace aztec3::circuits::abis
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ template <typename NCT> struct RootRollupPublicInputs {
using fr = typename NCT::fr;
using AggregationObject = typename NCT::AggregationObject;

// @todo @LHerskind: the global variables from constants should be included here.

// All below are shared between the base and merge rollups
AggregationObject end_aggregation_object;

Expand Down
30 changes: 27 additions & 3 deletions circuits/cpp/src/aztec3/circuits/abis/tx_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,23 @@ template <typename NCT> struct TxContext {

ContractDeploymentData<NCT> contract_deployment_data{};

fr chain_id = 0;
fr version = 0;

// for serialization: update up with new fields
MSGPACK_FIELDS(is_fee_payment_tx, is_rebate_payment_tx, is_contract_deployment_tx, contract_deployment_data);
MSGPACK_FIELDS(is_fee_payment_tx,
is_rebate_payment_tx,
is_contract_deployment_tx,
contract_deployment_data,
chain_id,
version);

boolean operator==(TxContext<NCT> const& other) const
{
return is_fee_payment_tx == other.is_fee_payment_tx && is_rebate_payment_tx == other.is_rebate_payment_tx &&
is_contract_deployment_tx == other.is_contract_deployment_tx &&
contract_deployment_data == other.contract_deployment_data;
contract_deployment_data == other.contract_deployment_data && chain_id == other.chain_id &&
version == other.version;
};

template <typename Composer> TxContext<CircuitTypes<Composer>> to_circuit_type(Composer& composer) const
Expand All @@ -46,6 +56,8 @@ template <typename NCT> struct TxContext {
to_ct(is_rebate_payment_tx),
to_ct(is_contract_deployment_tx),
contract_deployment_data.to_circuit_type(composer),
to_ct(chain_id),
to_ct(version),
};

return tx_context;
Expand All @@ -62,6 +74,8 @@ template <typename NCT> struct TxContext {
to_nt(is_rebate_payment_tx),
to_nt(is_contract_deployment_tx),
to_native_type(contract_deployment_data),
to_nt(chain_id),
to_nt(version),
};

return tx_context;
Expand All @@ -75,6 +89,8 @@ template <typename NCT> struct TxContext {
fr(is_rebate_payment_tx).set_public();
fr(is_contract_deployment_tx).set_public();
contract_deployment_data.set_public();
fr(chain_id).set_public();
fr(version).set_public();
}

fr hash() const
Expand All @@ -84,6 +100,8 @@ template <typename NCT> struct TxContext {
fr(is_rebate_payment_tx),
fr(is_contract_deployment_tx),
contract_deployment_data.hash(),
chain_id,
version,
};

return NCT::compress(inputs, GeneratorIndex::TX_CONTEXT);
Expand All @@ -98,6 +116,8 @@ template <typename NCT> void read(uint8_t const*& it, TxContext<NCT>& tx_context
read(it, tx_context.is_rebate_payment_tx);
read(it, tx_context.is_contract_deployment_tx);
read(it, tx_context.contract_deployment_data);
read(it, tx_context.chain_id);
read(it, tx_context.version);
};

template <typename NCT> void write(std::vector<uint8_t>& buf, TxContext<NCT> const& tx_context)
Expand All @@ -108,6 +128,8 @@ template <typename NCT> void write(std::vector<uint8_t>& buf, TxContext<NCT> con
write(buf, tx_context.is_rebate_payment_tx);
write(buf, tx_context.is_contract_deployment_tx);
write(buf, tx_context.contract_deployment_data);
write(buf, tx_context.chain_id);
write(buf, tx_context.version);
};

template <typename NCT> std::ostream& operator<<(std::ostream& os, TxContext<NCT> const& tx_context)
Expand All @@ -117,7 +139,9 @@ template <typename NCT> std::ostream& operator<<(std::ostream& os, TxContext<NCT
<< "is_contract_deployment_tx: " << tx_context.is_contract_deployment_tx << "\n"
<< "contract_deployment_data: "
<< "\n"
<< tx_context.contract_deployment_data;
<< tx_context.contract_deployment_data << "\n"
<< "chain_id: " << tx_context.chain_id << "\n"
<< "version: " << tx_context.version << "\n";
}

} // namespace aztec3::circuits::abis
11 changes: 2 additions & 9 deletions circuits/cpp/src/aztec3/circuits/abis/tx_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ template <typename NCT> struct TxRequest {
fr args_hash = 0;
fr nonce = 0;
TxContext<NCT> tx_context{};
fr chain_id = 0;

boolean operator==(TxContext<NCT> const& other) const
{
return from == other.from && to == other.to && function_data == other.function_data &&
args_hash == other.args && nonce == other.nonce && tx_context == other.tx_context &&
chain_id == other.chain_id;
args_hash == other.args && nonce == other.nonce && tx_context == other.tx_context;
};

template <typename Composer> TxRequest<CircuitTypes<Composer>> to_circuit_type(Composer& composer) const
Expand All @@ -45,7 +43,6 @@ template <typename NCT> struct TxRequest {
TxRequest<CircuitTypes<Composer>> tx_request = {
to_ct(from), to_ct(to), to_circuit_type(function_data),
to_ct(args_hash), to_ct(nonce), to_circuit_type(tx_context),
to_ct(chain_id),
};

return tx_request;
Expand All @@ -60,7 +57,6 @@ template <typename NCT> struct TxRequest {
inputs.push_back(args_hash);
inputs.push_back(nonce);
inputs.push_back(tx_context.hash());
inputs.push_back(chain_id);

return NCT::compress(inputs, GeneratorIndex::TX_REQUEST);
}
Expand All @@ -76,7 +72,6 @@ template <typename NCT> void read(uint8_t const*& it, TxRequest<NCT>& tx_request
read(it, tx_request.args_hash);
read(it, tx_request.nonce);
read(it, tx_request.tx_context);
read(it, tx_request.chain_id);
};

template <typename NCT> void write(std::vector<uint8_t>& buf, TxRequest<NCT> const& tx_request)
Expand All @@ -89,7 +84,6 @@ template <typename NCT> void write(std::vector<uint8_t>& buf, TxRequest<NCT> con
write(buf, tx_request.args_hash);
write(buf, tx_request.nonce);
write(buf, tx_request.tx_context);
write(buf, tx_request.chain_id);
};

template <typename NCT> std::ostream& operator<<(std::ostream& os, TxRequest<NCT> const& tx_request)
Expand All @@ -99,8 +93,7 @@ template <typename NCT> std::ostream& operator<<(std::ostream& os, TxRequest<NCT
<< "function_data: " << tx_request.function_data << "\n"
<< "args_hash: " << tx_request.args_hash << "\n"
<< "nonce: " << tx_request.nonce << "\n"
<< "tx_context: " << tx_request.tx_context << "\n"
<< "chain_id: " << tx_request.chain_id << "\n";
<< "tx_context: " << tx_request.tx_context << "\n";
}

} // namespace aztec3::circuits::abis
59 changes: 59 additions & 0 deletions circuits/cpp/src/aztec3/circuits/global_variables.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#pragma once

#include "aztec3/constants.hpp"
#include "aztec3/utils/msgpack_derived_equals.hpp"
#include "aztec3/utils/msgpack_derived_output.hpp"
#include "aztec3/utils/types/circuit_types.hpp"
#include "aztec3/utils/types/convert.hpp"
#include "aztec3/utils/types/native_types.hpp"

#include <barretenberg/barretenberg.hpp>

namespace aztec3::circuits::abis {

using aztec3::utils::types::CircuitTypes;
using aztec3::utils::types::NativeTypes;

template <typename NCT> struct GlobalVariables {
using fr = typename NCT::fr;

fr timestamp = 0;
fr block_number = 0;
fr chain_id = 0;
fr version = 0;

MSGPACK_FIELDS(timestamp, block_number, chain_id, version);

bool operator==(GlobalVariables<NCT> const&) const = default;
};

template <typename NCT> void read(uint8_t const*& it, GlobalVariables<NCT>& obj)
{
using serialize::read;

read(it, obj.timestamp);
read(it, obj.block_number);
read(it, obj.chain_id);
read(it, obj.version);
};

template <typename NCT> void write(std::vector<uint8_t>& buf, GlobalVariables<NCT> const& obj)
{
using serialize::write;

write(buf, obj.timestamp);
write(buf, obj.block_number);
write(buf, obj.chain_id);
write(buf, obj.version);
};

template <typename NCT> std::ostream& operator<<(std::ostream& os, GlobalVariables<NCT> const& obj)
{
os << "timestamp: " << obj.timestamp << std::endl;
os << "block_number: " << obj.block_number << std::endl;
os << "chain_id: " << obj.chain_id << std::endl;
os << "version: " << obj.version << std::endl;
return os;
};

} // namespace aztec3::circuits::abis
Original file line number Diff line number Diff line change
Expand Up @@ -399,14 +399,11 @@ PrivateKernelInputsInit<NT> do_private_call_get_kernel_inputs_init(bool const is
.function_data = private_call_data.call_stack_item.function_data,
.args_hash = compute_var_args_hash<NT>(args_vec),
.nonce = 0,
.tx_context =
TxContext<NT>{
.is_fee_payment_tx = false,
.is_rebate_payment_tx = false,
.is_contract_deployment_tx = is_constructor,
.contract_deployment_data = contract_deployment_data,
},
.chain_id = 1,
.tx_context = TxContext<NT>{ .is_fee_payment_tx = false,
.is_rebate_payment_tx = false,
.is_contract_deployment_tx = is_constructor,
.contract_deployment_data = contract_deployment_data,
.chain_id = 1 },
};

auto const signed_tx_request = SignedTxRequest<NT>{
Expand Down
2 changes: 1 addition & 1 deletion circuits/cpp/src/aztec3/circuits/kernel/public/.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ PublicKernelInputsNoPreviousKernel<NT> get_kernel_inputs_no_previous_kernel()
.is_rebate_payment_tx = false,
.is_contract_deployment_tx = false,
.contract_deployment_data = {},
.chain_id = 1,
},
.chain_id = 1,
};

auto const signed_tx_request = SignedTxRequest<NT>{
Expand Down
58 changes: 58 additions & 0 deletions circuits/cpp/src/aztec3/circuits/rollup/base/.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,64 @@ TEST_F(base_rollup_tests, native_no_new_contract_leafs)
run_cbind(emptyInputs, outputs);
}

TEST_F(base_rollup_tests, native_invalid_chainid)
{
DummyComposer composer = DummyComposer("base_rollup_tests__native_invalid_chainid");
BaseRollupInputs emptyInputs = base_rollup_inputs_from_kernels({ get_empty_kernel(), get_empty_kernel() });

// Setting chain id to 1 instead of 0 (which is the default)
emptyInputs.constants.global_variables.chain_id = 1;

auto empty_contract_tree = native_base_rollup::MerkleTree(CONTRACT_TREE_HEIGHT);

BaseOrMergeRollupPublicInputs const outputs =
aztec3::circuits::rollup::native_base_rollup::base_rollup_circuit(composer, emptyInputs);

AppendOnlyTreeSnapshot<NT> const expectedStartContractTreeSnapshot = {
.root = empty_contract_tree.root(),
.next_available_leaf_index = 0,
};
AppendOnlyTreeSnapshot<NT> const expectedEndContractTreeSnapshot = {
.root = empty_contract_tree.root(),
.next_available_leaf_index = 2,
};
ASSERT_EQ(outputs.start_contract_tree_snapshot, expectedStartContractTreeSnapshot);
ASSERT_EQ(outputs.end_contract_tree_snapshot, expectedEndContractTreeSnapshot);
ASSERT_EQ(outputs.start_contract_tree_snapshot, emptyInputs.start_contract_tree_snapshot);
ASSERT_TRUE(composer.failed());
ASSERT_EQ(composer.get_first_failure().message,
format("Chain id in tx context does not match ", fr(0), " ", fr(1)));
}


TEST_F(base_rollup_tests, native_invalid_version)
{
DummyComposer composer = DummyComposer("base_rollup_tests__native_invalid_version");
BaseRollupInputs emptyInputs = base_rollup_inputs_from_kernels({ get_empty_kernel(), get_empty_kernel() });

// Setting version to 1 instead of 0 (which is the default)
emptyInputs.constants.global_variables.version = 1;

auto empty_contract_tree = native_base_rollup::MerkleTree(CONTRACT_TREE_HEIGHT);

BaseOrMergeRollupPublicInputs const outputs =
aztec3::circuits::rollup::native_base_rollup::base_rollup_circuit(composer, emptyInputs);

AppendOnlyTreeSnapshot<NT> const expectedStartContractTreeSnapshot = {
.root = empty_contract_tree.root(),
.next_available_leaf_index = 0,
};
AppendOnlyTreeSnapshot<NT> const expectedEndContractTreeSnapshot = {
.root = empty_contract_tree.root(),
.next_available_leaf_index = 2,
};
ASSERT_EQ(outputs.start_contract_tree_snapshot, expectedStartContractTreeSnapshot);
ASSERT_EQ(outputs.end_contract_tree_snapshot, expectedEndContractTreeSnapshot);
ASSERT_EQ(outputs.start_contract_tree_snapshot, emptyInputs.start_contract_tree_snapshot);
ASSERT_TRUE(composer.failed());
ASSERT_EQ(composer.get_first_failure().message, format("Version in tx context does not match ", fr(0), " ", fr(1)));
}

TEST_F(base_rollup_tests, native_contract_leaf_inserted)
{
DummyComposer composer = DummyComposer("base_rollup_tests__native_contract_leaf_inserted");
Expand Down
Loading