Skip to content

Commit

Permalink
Push processed public txs into the base rollup circuit (#319)
Browse files Browse the repository at this point in the history
* Update CombinedAccumulatedData to use new PublicDataWrite instead of StateTransition and StateRead

* Get processed public txs into the base rollup circuit

* Format
  • Loading branch information
spalladino authored Apr 21, 2023
1 parent 53e33f0 commit 54ad110
Show file tree
Hide file tree
Showing 20 changed files with 395 additions and 257 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#pragma once
#include "optionally_revealed_data.hpp"
#include "new_contract_data.hpp"
#include "state_transition.hpp"
#include "state_read.hpp"
#include "public_data_write.hpp"
#include "aztec3/constants.hpp"
#include <barretenberg/stdlib/recursion/aggregation_state/aggregation_state.hpp>
#include <barretenberg/common/map.hpp>
Expand Down Expand Up @@ -42,8 +41,7 @@ template <typename NCT> struct CombinedAccumulatedData {

std::array<OptionallyRevealedData<NCT>, KERNEL_OPTIONALLY_REVEALED_DATA_LENGTH> optionally_revealed_data{};

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

boolean operator==(CombinedAccumulatedData<NCT> const& other) const
{
Expand All @@ -52,7 +50,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_reads == other.state_reads;
state_transitions == other.state_transitions;
};

template <typename Composer>
Expand Down Expand Up @@ -87,7 +85,6 @@ 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 @@ -121,7 +118,6 @@ 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 @@ -146,7 +142,6 @@ 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 All @@ -173,15 +168,7 @@ template <typename NCT> struct CombinedAccumulatedData {
}
}

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

template <size_t SIZE> void set_array_public(std::array<StateRead<NCT>, SIZE>& arr)
template <size_t SIZE> void set_array_public(std::array<PublicDataWrite<NCT>, SIZE>& arr)
{
static_assert(!(std::is_same<NativeTypes, NCT>::value));
for (auto& e : arr) {
Expand All @@ -205,7 +192,6 @@ 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 @@ -223,7 +209,6 @@ 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 @@ -247,9 +232,7 @@ 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"
<< "state_reads:\n"
<< accum_data.state_reads << "\n";
<< accum_data.state_transitions << "\n";
}

} // namespace aztec3::circuits::abis
91 changes: 91 additions & 0 deletions circuits/cpp/src/aztec3/circuits/abis/public_data_write.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#pragma once
#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::utils::types::CircuitTypes;
using aztec3::utils::types::NativeTypes;
using plonk::stdlib::witness_t;

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

fr leaf_index = 0;
fr new_value = 0;

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

template <typename Composer> PublicDataWrite<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); };

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

return state_transition;
};

template <typename Composer> PublicDataWrite<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); };

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

return state_transition;
};

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

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

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

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

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

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

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

write(buf, state_transition.leaf_index);
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"
<< "new_value: " << state_transition.new_value << "\n";
}

} // namespace aztec3::circuits::abis
Original file line number Diff line number Diff line change
Expand Up @@ -90,28 +90,14 @@ called_from_l1: 1
called_from_public_l2: 0
]
state_transitions:
[ storage_slot: 0x801
old_value: 0x802
[ leaf_index: 0x801
new_value: 0x802
leaf_index: 0x802
new_value: 0x803
storage_slot: 0x802
old_value: 0x803
leaf_index: 0x803
new_value: 0x804
storage_slot: 0x803
old_value: 0x804
leaf_index: 0x804
new_value: 0x805
storage_slot: 0x804
old_value: 0x805
new_value: 0x806
]
state_reads:
[ storage_slot: 0x901
current_value: 0x902
storage_slot: 0x902
current_value: 0x903
storage_slot: 0x903
current_value: 0x904
storage_slot: 0x904
current_value: 0x905
]
constants:
Expand Down Expand Up @@ -264,28 +250,14 @@ called_from_l1: 1
called_from_public_l2: 0
]
state_transitions:
[ storage_slot: 0x1801
old_value: 0x1802
[ leaf_index: 0x1801
new_value: 0x1802
leaf_index: 0x1802
new_value: 0x1803
storage_slot: 0x1802
old_value: 0x1803
leaf_index: 0x1803
new_value: 0x1804
storage_slot: 0x1803
old_value: 0x1804
leaf_index: 0x1804
new_value: 0x1805
storage_slot: 0x1804
old_value: 0x1805
new_value: 0x1806
]
state_reads:
[ storage_slot: 0x1901
current_value: 0x1902
storage_slot: 0x1902
current_value: 0x1903
storage_slot: 0x1903
current_value: 0x1904
storage_slot: 0x1904
current_value: 0x1905
]
constants:
Expand Down Expand Up @@ -608,28 +580,14 @@ called_from_l1: 1
called_from_public_l2: 0
]
state_transitions:
[ storage_slot: 0x801
old_value: 0x802
[ leaf_index: 0x801
new_value: 0x802
leaf_index: 0x802
new_value: 0x803
storage_slot: 0x802
old_value: 0x803
leaf_index: 0x803
new_value: 0x804
storage_slot: 0x803
old_value: 0x804
leaf_index: 0x804
new_value: 0x805
storage_slot: 0x804
old_value: 0x805
new_value: 0x806
]
state_reads:
[ storage_slot: 0x901
current_value: 0x902
storage_slot: 0x902
current_value: 0x903
storage_slot: 0x903
current_value: 0x904
storage_slot: 0x904
current_value: 0x905
]
constants:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,54 @@ export class OptionallyRevealedData {
}
}

/**
* Read operations from the public state tree.
*/
export class PublicDataRead {
constructor(public readonly leafIndex: Fr, public readonly value: Fr) {}

static from(args: { storageSlot: Fr; value: Fr }) {
return new PublicDataRead(args.storageSlot, args.value);
}

toBuffer() {
return serializeToBuffer(this.leafIndex, this.value);
}

static fromBuffer(buffer: Buffer | BufferReader) {
const reader = BufferReader.asReader(buffer);
return new PublicDataRead(reader.readFr(), reader.readFr());
}

static empty() {
return new PublicDataRead(Fr.ZERO, Fr.ZERO);
}
}

/**
* Write operations on the public state tree.
*/
export class PublicDataWrite {
constructor(public readonly leafIndex: Fr, public readonly newValue: Fr) {}

static from(args: { storageSlot: Fr; oldValue: Fr; newValue: Fr }) {
return new PublicDataWrite(args.storageSlot, args.newValue);
}

toBuffer() {
return serializeToBuffer(this.leafIndex, this.newValue);
}

static fromBuffer(buffer: Buffer | BufferReader) {
const reader = BufferReader.asReader(buffer);
return new PublicDataWrite(reader.readFr(), reader.readFr());
}

static empty() {
return new PublicDataWrite(Fr.ZERO, Fr.ZERO);
}
}

export class CombinedAccumulatedData {
constructor(
public aggregationObject: AggregationObject,
Expand All @@ -126,8 +174,7 @@ export class CombinedAccumulatedData {

public optionallyRevealedData: OptionallyRevealedData[],

public stateTransitions: StateTransition[],
public stateReads: StateRead[],
public stateTransitions: PublicDataWrite[],
) {
assertLength(this, 'newCommitments', KERNEL_NEW_COMMITMENTS_LENGTH);
assertLength(this, 'newNullifiers', KERNEL_NEW_NULLIFIERS_LENGTH);
Expand All @@ -137,7 +184,6 @@ export class CombinedAccumulatedData {
assertLength(this, 'newContracts', KERNEL_NEW_CONTRACTS_LENGTH);
assertLength(this, 'optionallyRevealedData', KERNEL_OPTIONALLY_REVEALED_DATA_LENGTH);
assertLength(this, 'stateTransitions', STATE_TRANSITIONS_LENGTH);
assertLength(this, 'stateReads', STATE_READS_LENGTH);
}

toBuffer() {
Expand All @@ -153,7 +199,6 @@ export class CombinedAccumulatedData {
this.newContracts,
this.optionallyRevealedData,
this.stateTransitions,
this.stateReads,
);
}

Expand All @@ -174,8 +219,7 @@ export class CombinedAccumulatedData {
reader.readArray(KERNEL_L1_MSG_STACK_LENGTH, Fr),
reader.readArray(KERNEL_NEW_CONTRACTS_LENGTH, NewContractData),
reader.readArray(KERNEL_OPTIONALLY_REVEALED_DATA_LENGTH, OptionallyRevealedData),
reader.readArray(STATE_TRANSITIONS_LENGTH, StateTransition),
reader.readArray(STATE_READS_LENGTH, StateRead),
reader.readArray(STATE_TRANSITIONS_LENGTH, PublicDataWrite),
);
}

Expand All @@ -191,8 +235,7 @@ export class CombinedAccumulatedData {
times(KERNEL_L1_MSG_STACK_LENGTH, Fr.zero),
times(KERNEL_NEW_CONTRACTS_LENGTH, NewContractData.empty),
times(KERNEL_OPTIONALLY_REVEALED_DATA_LENGTH, OptionallyRevealedData.empty),
times(STATE_TRANSITIONS_LENGTH, StateTransition.empty),
times(STATE_READS_LENGTH, StateRead.empty),
times(STATE_TRANSITIONS_LENGTH, PublicDataWrite.empty),
);
}
}
Loading

0 comments on commit 54ad110

Please sign in to comment.