Skip to content

Commit

Permalink
Update structs for processing public function data in rollup circuits (
Browse files Browse the repository at this point in the history
…#357)

* Add missing fields to rollup public inputs

* Set public data tree snapshots in merge and rollup circuits

* Update root rollup public inputs snapshots

* Update structs with new fields for public data checks and updates

* Remove witnessed public call data and public data tree root

* Update inputs to public kernel and base rollup circuits from sequencer

* Format
  • Loading branch information
spalladino authored Apr 26, 2023
1 parent 9730b9b commit 1117f2f
Show file tree
Hide file tree
Showing 30 changed files with 461 additions and 336 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "optionally_revealed_data.hpp"
#include "new_contract_data.hpp"
#include "public_data_write.hpp"
#include "public_data_read.hpp"
#include "aztec3/constants.hpp"
#include <barretenberg/stdlib/recursion/aggregation_state/aggregation_state.hpp>
#include <barretenberg/common/map.hpp>
Expand Down Expand Up @@ -42,6 +43,7 @@ template <typename NCT> struct CombinedAccumulatedData {
std::array<OptionallyRevealedData<NCT>, KERNEL_OPTIONALLY_REVEALED_DATA_LENGTH> optionally_revealed_data{};

std::array<PublicDataWrite<NCT>, STATE_TRANSITIONS_LENGTH> state_transitions{};
std::array<PublicDataRead<NCT>, STATE_READS_LENGTH> state_reads{};

boolean operator==(CombinedAccumulatedData<NCT> const& other) const
{
Expand All @@ -50,7 +52,7 @@ template <typename NCT> struct CombinedAccumulatedData {
new_nullifiers == other.new_nullifiers && private_call_stack == other.private_call_stack &&
public_call_stack == other.public_call_stack && l1_msg_stack == other.l1_msg_stack &&
new_contracts == other.new_contracts && optionally_revealed_data == other.optionally_revealed_data &&
state_transitions == other.state_transitions;
state_transitions == other.state_transitions && state_reads == other.state_reads;
};

template <typename Composer>
Expand Down Expand Up @@ -85,6 +87,7 @@ template <typename NCT> struct CombinedAccumulatedData {
map(new_contracts, to_circuit_type),
map(optionally_revealed_data, to_circuit_type),
map(state_transitions, to_circuit_type),
map(state_reads, to_circuit_type),
};

return acc_data;
Expand Down Expand Up @@ -118,6 +121,7 @@ template <typename NCT> struct CombinedAccumulatedData {
map(new_contracts, to_native_type),
map(optionally_revealed_data, to_native_type),
map(state_transitions, to_native_type),
map(state_reads, to_native_type),
};
return acc_data;
}
Expand All @@ -141,6 +145,7 @@ template <typename NCT> struct CombinedAccumulatedData {
set_array_public(new_contracts);
set_array_public(optionally_revealed_data);
set_array_public(state_transitions);
set_array_public(state_reads);
}

template <typename T, size_t SIZE> void set_array_public(std::array<T, SIZE>& arr)
Expand Down Expand Up @@ -174,6 +179,14 @@ template <typename NCT> struct CombinedAccumulatedData {
e.set_public();
}
}

template <size_t SIZE> void set_array_public(std::array<PublicDataRead<NCT>, SIZE>& arr)
{
static_assert(!(std::is_same<NativeTypes, NCT>::value));
for (auto& e : arr) {
e.set_public();
}
}
};

template <typename NCT> void read(uint8_t const*& it, CombinedAccumulatedData<NCT>& accum_data)
Expand All @@ -191,6 +204,7 @@ template <typename NCT> void read(uint8_t const*& it, CombinedAccumulatedData<NC
read(it, accum_data.new_contracts);
read(it, accum_data.optionally_revealed_data);
read(it, accum_data.state_transitions);
read(it, accum_data.state_reads);
};

template <typename NCT> void write(std::vector<uint8_t>& buf, CombinedAccumulatedData<NCT> const& accum_data)
Expand All @@ -208,6 +222,7 @@ template <typename NCT> void write(std::vector<uint8_t>& buf, CombinedAccumulate
write(buf, accum_data.new_contracts);
write(buf, accum_data.optionally_revealed_data);
write(buf, accum_data.state_transitions);
write(buf, accum_data.state_reads);
};

template <typename NCT> std::ostream& operator<<(std::ostream& os, CombinedAccumulatedData<NCT> const& accum_data)
Expand All @@ -231,7 +246,9 @@ template <typename NCT> std::ostream& operator<<(std::ostream& os, CombinedAccum
<< "optionally_revealed_data:\n"
<< accum_data.optionally_revealed_data << "\n"
<< "state_transitions:\n"
<< accum_data.state_transitions << "\n";
<< accum_data.state_transitions << "\n"
<< "state_reads:\n"
<< accum_data.state_reads << "\n";
}

} // namespace aztec3::circuits::abis
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ template <typename NCT> struct CombinedHistoricTreeRoots {
typedef typename NCT::boolean boolean;

PrivateHistoricTreeRoots<NCT> private_historic_tree_roots{};
fr public_data_tree_root = 0;

boolean operator==(CombinedHistoricTreeRoots<NCT> const& other) const
{
return private_historic_tree_roots == other.private_historic_tree_roots &&
public_data_tree_root == other.public_data_tree_root;
return private_historic_tree_roots == other.private_historic_tree_roots;
};

template <typename Composer>
Expand All @@ -32,12 +30,10 @@ template <typename NCT> struct CombinedHistoricTreeRoots {
static_assert((std::is_same<NativeTypes, NCT>::value));

// Capture the composer:
auto to_ct = [&](auto& e) { return aztec3::utils::types::to_ct(composer, e); };
auto to_circuit_type = [&](auto& e) { return e.to_circuit_type(composer); };

CombinedHistoricTreeRoots<CircuitTypes<Composer>> data = {
to_circuit_type(private_historic_tree_roots),
to_ct(public_data_tree_root),
};

return data;
Expand All @@ -46,12 +42,10 @@ template <typename NCT> struct CombinedHistoricTreeRoots {
template <typename Composer> CombinedHistoricTreeRoots<NativeTypes> to_native_type() const
{
static_assert(std::is_same<CircuitTypes<Composer>, NCT>::value);
auto to_nt = [&](auto& e) { return aztec3::utils::types::to_nt<Composer>(e); };
auto to_native_type = [&]<typename T>(T& e) { return e.template to_native_type<Composer>(); };

CombinedHistoricTreeRoots<NativeTypes> data = {
to_native_type(private_historic_tree_roots),
to_nt(public_data_tree_root),
};

return data;
Expand All @@ -62,7 +56,6 @@ template <typename NCT> struct CombinedHistoricTreeRoots {
static_assert(!(std::is_same<NativeTypes, NCT>::value));

private_historic_tree_roots.set_public();
public_data_tree_root.set_public();
}
};

Expand All @@ -71,22 +64,19 @@ template <typename NCT> void read(uint8_t const*& it, CombinedHistoricTreeRoots<
using serialize::read;

read(it, historic_tree_roots.private_historic_tree_roots);
read(it, historic_tree_roots.public_data_tree_root);
};

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

write(buf, historic_tree_roots.private_historic_tree_roots);
write(buf, historic_tree_roots.public_data_tree_root);
};

template <typename NCT>
std::ostream& operator<<(std::ostream& os, CombinedHistoricTreeRoots<NCT> const& historic_tree_roots)
{
return os << "private_historic_tree_roots: " << historic_tree_roots.private_historic_tree_roots << "\n"
<< "public_data_tree_root: " << historic_tree_roots.public_data_tree_root << "\n";
return os << "private_historic_tree_roots: " << historic_tree_roots.private_historic_tree_roots << "\n";
}

} // namespace aztec3::circuits::abis
95 changes: 95 additions & 0 deletions circuits/cpp/src/aztec3/circuits/abis/public_data_read.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#pragma once
#include "aztec3/constants.hpp"
#include <barretenberg/stdlib/primitives/witness/witness.hpp>
#include <aztec3/utils/types/native_types.hpp>
#include <aztec3/utils/types/convert.hpp>
#include <aztec3/utils/types/circuit_types.hpp>

namespace aztec3::circuits::abis {

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

template <typename NCT> struct PublicDataRead {
typedef typename NCT::fr fr;
typedef typename NCT::boolean boolean;

fr leaf_index = 0;
fr value = 0;

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

template <typename Composer> PublicDataRead<CircuitTypes<Composer>> to_circuit_type(Composer& composer) const
{
static_assert((std::is_same<NativeTypes, NCT>::value));

// Capture the composer:
auto to_ct = [&](auto& e) { return aztec3::utils::types::to_ct(composer, e); };

PublicDataRead<CircuitTypes<Composer>> state_transition = {
to_ct(leaf_index),
to_ct(value),
};

return state_transition;
};

template <typename Composer> PublicDataRead<NativeTypes> to_native_type() const
{
static_assert((std::is_same<CircuitTypes<Composer>, NCT>::value));

auto to_nt = [&](auto& e) { return aztec3::utils::types::to_nt<Composer>(e); };

PublicDataRead<NativeTypes> state_transition = {
to_nt(leaf_index),
to_nt(value),
};

return state_transition;
};

fr hash() const
{
std::vector<fr> inputs = {
leaf_index,
value,
};

return NCT::compress(inputs, GeneratorIndex::STATE_READ);
}

void set_public()
{
static_assert(!(std::is_same<NativeTypes, NCT>::value));

leaf_index.set_public();
value.set_public();
}

boolean is_empty() const { return leaf_index == 0; }
};

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

read(it, state_transition.leaf_index);
read(it, state_transition.value);
};

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

write(buf, state_transition.leaf_index);
write(buf, state_transition.value);
};

template <typename NCT> std::ostream& operator<<(std::ostream& os, PublicDataRead<NCT> const& state_transition)
{
return os << "leaf_index: " << state_transition.leaf_index << "\n"
<< "value: " << state_transition.value << "\n";
}

} // namespace aztec3::circuits::abis
11 changes: 10 additions & 1 deletion circuits/cpp/src/aztec3/circuits/abis/public_data_write.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
#pragma once
#include "aztec3/constants.hpp"
#include <barretenberg/stdlib/primitives/witness/witness.hpp>
#include <aztec3/utils/types/native_types.hpp>
#include <aztec3/utils/types/convert.hpp>
#include <aztec3/utils/types/circuit_types.hpp>

namespace aztec3::circuits::abis {

using aztec3::GeneratorIndex;
using aztec3::utils::types::CircuitTypes;
using aztec3::utils::types::NativeTypes;
using plonk::stdlib::witness_t;

template <typename NCT> struct PublicDataWrite {
typedef typename NCT::fr fr;
typedef typename NCT::boolean boolean;

fr leaf_index = 0;
fr old_value = 0;
fr new_value = 0;

bool operator==(PublicDataWrite<NCT> const&) const = default;
Expand All @@ -28,6 +30,7 @@ template <typename NCT> struct PublicDataWrite {

PublicDataWrite<CircuitTypes<Composer>> state_transition = {
to_ct(leaf_index),
to_ct(old_value),
to_ct(new_value),
};

Expand All @@ -42,6 +45,7 @@ template <typename NCT> struct PublicDataWrite {

PublicDataWrite<NativeTypes> state_transition = {
to_nt(leaf_index),
to_nt(old_value),
to_nt(new_value),
};

Expand All @@ -52,6 +56,7 @@ template <typename NCT> struct PublicDataWrite {
{
std::vector<fr> inputs = {
leaf_index,
old_value,
new_value,
};

Expand All @@ -63,6 +68,7 @@ template <typename NCT> struct PublicDataWrite {
static_assert(!(std::is_same<NativeTypes, NCT>::value));

leaf_index.set_public();
old_value.set_public();
new_value.set_public();
}

Expand All @@ -74,6 +80,7 @@ template <typename NCT> void read(uint8_t const*& it, PublicDataWrite<NCT>& stat
using serialize::read;

read(it, state_transition.leaf_index);
read(it, state_transition.old_value);
read(it, state_transition.new_value);
};

Expand All @@ -82,12 +89,14 @@ template <typename NCT> void write(std::vector<uint8_t>& buf, PublicDataWrite<NC
using serialize::write;

write(buf, state_transition.leaf_index);
write(buf, state_transition.old_value);
write(buf, state_transition.new_value);
};

template <typename NCT> std::ostream& operator<<(std::ostream& os, PublicDataWrite<NCT> const& state_transition)
{
return os << "leaf_index: " << state_transition.leaf_index << "\n"
<< "old_value: " << state_transition.old_value << "\n"
<< "new_value: " << state_transition.new_value << "\n";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "../previous_kernel_data.hpp"
#include "public_call_data.hpp"
#include "../signed_tx_request.hpp"
#include "witnessed_public_call_data.hpp"

#include <barretenberg/stdlib/primitives/witness/witness.hpp>
#include <aztec3/utils/types/native_types.hpp>
Expand All @@ -21,7 +20,7 @@ template <typename NCT> struct PublicKernelInputs {
typedef typename NCT::boolean boolean;

PreviousKernelData<NCT> previous_kernel{};
WitnessedPublicCallData<NCT> public_call{};
PublicCallData<NCT> public_call{};

boolean operator==(PublicKernelInputs<NCT> const& other) const
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include "../previous_kernel_data.hpp"
#include "../signed_tx_request.hpp"
#include "public_call_data.hpp"
#include "witnessed_public_call_data.hpp"

#include <barretenberg/stdlib/primitives/witness/witness.hpp>
#include <aztec3/utils/types/native_types.hpp>
Expand All @@ -20,7 +20,7 @@ template <typename NCT> struct PublicKernelInputsNoPreviousKernel {
typedef typename NCT::boolean boolean;

SignedTxRequest<NCT> signed_tx_request{};
WitnessedPublicCallData<NCT> public_call{};
PublicCallData<NCT> public_call{};

boolean operator==(PublicKernelInputsNoPreviousKernel<NCT> const& other) const
{
Expand Down
Loading

0 comments on commit 1117f2f

Please sign in to comment.