Skip to content

Commit

Permalink
Public data in L2Block (#360)
Browse files Browse the repository at this point in the history
* docs: explained presence of nextAvailableLeaf

* feat: public data in block

* test: updated synchroniser test

* Rename current public data write to public data transition

* Introduce public data writes and add to L2 block

* Assemble public state date in circuit block builder

* refactor: updated ordering in a block

* fix: Rollup bytecode without most of the decoding and checks

---------

Co-authored-by: Santiago Palladino <[email protected]>
  • Loading branch information
benesjan and spalladino authored Apr 28, 2023
1 parent b6998a8 commit 11ae6db
Show file tree
Hide file tree
Showing 23 changed files with 199 additions and 109 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include "optionally_revealed_data.hpp"
#include "new_contract_data.hpp"
#include "public_data_write.hpp"
#include "public_data_transition.hpp"
#include "public_data_read.hpp"
#include "aztec3/constants.hpp"
#include <barretenberg/stdlib/recursion/aggregation_state/aggregation_state.hpp>
Expand Down Expand Up @@ -42,7 +42,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<PublicDataTransition<NCT>, STATE_TRANSITIONS_LENGTH> state_transitions{};
std::array<PublicDataRead<NCT>, STATE_READS_LENGTH> state_reads{};

boolean operator==(CombinedAccumulatedData<NCT> const& other) const
Expand Down Expand Up @@ -172,7 +172,7 @@ template <typename NCT> struct CombinedAccumulatedData {
}
}

template <size_t SIZE> void set_array_public(std::array<PublicDataWrite<NCT>, SIZE>& arr)
template <size_t SIZE> void set_array_public(std::array<PublicDataTransition<NCT>, SIZE>& arr)
{
static_assert(!(std::is_same<NativeTypes, NCT>::value));
for (auto& e : arr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ using aztec3::GeneratorIndex;
using aztec3::utils::types::CircuitTypes;
using aztec3::utils::types::NativeTypes;

template <typename NCT> struct PublicDataWrite {
template <typename NCT> struct PublicDataTransition {
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;
bool operator==(PublicDataTransition<NCT> const&) const = default;

template <typename Composer> PublicDataWrite<CircuitTypes<Composer>> to_circuit_type(Composer& composer) const
template <typename Composer> PublicDataTransition<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 = {
PublicDataTransition<CircuitTypes<Composer>> state_transition = {
to_ct(leaf_index),
to_ct(old_value),
to_ct(new_value),
Expand All @@ -37,13 +37,13 @@ template <typename NCT> struct PublicDataWrite {
return state_transition;
};

template <typename Composer> PublicDataWrite<NativeTypes> to_native_type() const
template <typename Composer> PublicDataTransition<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 = {
PublicDataTransition<NativeTypes> state_transition = {
to_nt(leaf_index),
to_nt(old_value),
to_nt(new_value),
Expand Down Expand Up @@ -75,7 +75,7 @@ template <typename NCT> struct PublicDataWrite {
boolean is_empty() const { return leaf_index == 0; }
};

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

Expand All @@ -84,7 +84,7 @@ template <typename NCT> void read(uint8_t const*& it, PublicDataWrite<NCT>& stat
read(it, state_transition.new_value);
};

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

Expand All @@ -93,7 +93,7 @@ template <typename NCT> void write(std::vector<uint8_t>& buf, PublicDataWrite<NC
write(buf, state_transition.new_value);
};

template <typename NCT> std::ostream& operator<<(std::ostream& os, PublicDataWrite<NCT> const& state_transition)
template <typename NCT> std::ostream& operator<<(std::ostream& os, PublicDataTransition<NCT> const& state_transition)
{
return os << "leaf_index: " << state_transition.leaf_index << "\n"
<< "old_value: " << state_transition.old_value << "\n"
Expand Down
6 changes: 3 additions & 3 deletions circuits/cpp/src/aztec3/circuits/kernel/public/.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ TEST(public_kernel_tests, circuit_outputs_should_be_correctly_populated)
const auto contract_address = inputs.public_call.call_stack_item.contract_address;
for (size_t i = 0; i < STATE_TRANSITIONS_LENGTH; i++) {
const auto& st = inputs.public_call.call_stack_item.public_inputs.state_transitions[i];
const auto public_write = PublicDataWrite<NT>{
const auto public_write = PublicDataTransition<NT>{
.leaf_index = compute_public_data_tree_index<NT>(contract_address, st.storage_slot),
.new_value = compute_public_data_tree_value<NT>(st.new_value),
};
Expand Down Expand Up @@ -369,11 +369,11 @@ TEST(public_kernel_tests, only_valid_state_transitions_should_be_propagated)

// only the 2 valid transitions should have been propagated
const auto contract_address = inputs.public_call.call_stack_item.contract_address;
const auto public_write_1 = PublicDataWrite<NT>{
const auto public_write_1 = PublicDataTransition<NT>{
.leaf_index = compute_public_data_tree_index<NT>(contract_address, first_valid.storage_slot),
.new_value = compute_public_data_tree_value<NT>(first_valid.new_value),
};
const auto public_write_2 = PublicDataWrite<NT>{
const auto public_write_2 = PublicDataTransition<NT>{
.leaf_index = compute_public_data_tree_index<NT>(contract_address, second_valid.storage_slot),
.new_value = compute_public_data_tree_value<NT>(second_valid.new_value),
};
Expand Down
6 changes: 3 additions & 3 deletions circuits/cpp/src/aztec3/circuits/kernel/public/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
#include <aztec3/circuits/abis/kernel_circuit_public_inputs.hpp>
#include <aztec3/circuits/abis/state_read.hpp>
#include <aztec3/circuits/abis/state_transition.hpp>
#include <aztec3/circuits/abis/public_data_write.hpp>
#include <aztec3/circuits/abis/public_data_transition.hpp>
#include <aztec3/utils/dummy_composer.hpp>
#include <aztec3/utils/array.hpp>
#include <aztec3/circuits/hash.hpp>

using NT = aztec3::utils::types::NativeTypes;
using aztec3::circuits::abis::KernelCircuitPublicInputs;
using aztec3::circuits::abis::PublicDataRead;
using aztec3::circuits::abis::PublicDataWrite;
using aztec3::circuits::abis::PublicDataTransition;
using aztec3::circuits::abis::StateRead;
using aztec3::circuits::abis::StateTransition;
using aztec3::circuits::abis::public_kernel::PublicKernelInputs;
Expand Down Expand Up @@ -132,7 +132,7 @@ void propagate_valid_state_transitions(KernelInput const& public_kernel_inputs,
if (state_transition.is_empty()) {
continue;
}
const auto new_write = PublicDataWrite<NT>{
const auto new_write = PublicDataTransition<NT>{
.leaf_index = compute_public_data_tree_index<NT>(contract_address, state_transition.storage_slot),
.new_value = compute_public_data_tree_value<NT>(state_transition.new_value),
};
Expand Down
2 changes: 1 addition & 1 deletion circuits/cpp/src/aztec3/circuits/rollup/base/.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ TEST_F(base_rollup_tests, native_single_public_state_write)
stdlib::merkle_tree::MemoryStore public_data_tree_store;
native_base_rollup::SparseTree public_data_tree(public_data_tree_store, PUBLIC_DATA_TREE_HEIGHT);

auto data_write = abis::PublicDataWrite<NT>{
auto data_write = abis::PublicDataTransition<NT>{
.leaf_index = fr(1),
.old_value = fr(2),
.new_value = fr(42),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "aztec3/circuits/abis/membership_witness.hpp"
#include "aztec3/circuits/abis/public_data_read.hpp"
#include "aztec3/circuits/abis/public_data_write.hpp"
#include "aztec3/circuits/abis/public_data_transition.hpp"
#include "aztec3/circuits/hash.hpp"
#include "aztec3/constants.hpp"
#include "aztec3/utils/circuit_errors.hpp"
Expand Down Expand Up @@ -434,7 +434,7 @@ AppendOnlySnapshot check_nullifier_tree_non_membership_and_insert_to_tree(DummyC
fr insert_state_transitions(
DummyComposer& composer,
fr tree_root,
std::array<abis::PublicDataWrite<NT>, STATE_TRANSITIONS_LENGTH> const& state_transitions,
std::array<abis::PublicDataTransition<NT>, STATE_TRANSITIONS_LENGTH> const& state_transitions,
size_t witnesses_offset,
std::array<abis::MembershipWitness<NT, PUBLIC_DATA_TREE_HEIGHT>, 2 * STATE_TRANSITIONS_LENGTH> const& witnesses)
{
Expand Down
6 changes: 3 additions & 3 deletions circuits/cpp/src/aztec3/circuits/rollup/test_utils/utils.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include "aztec3/circuits/abis/public_data_write.hpp"
#include "aztec3/circuits/abis/public_data_transition.hpp"
#include "barretenberg/numeric/uint256/uint256.hpp"
#include "nullifier_tree_testing_harness.hpp"
#include "init.hpp"
Expand Down Expand Up @@ -101,9 +101,9 @@ void set_kernel_nullifiers(KernelData& kernel_data, std::array<fr, KERNEL_NEW_NU

MergeRollupInputs get_merge_rollup_inputs(utils::DummyComposer& composer, std::array<KernelData, 4> kernel_data);

inline abis::PublicDataWrite<NT> make_public_write(fr leaf_index, fr old_value, fr new_value)
inline abis::PublicDataTransition<NT> make_public_write(fr leaf_index, fr old_value, fr new_value)
{
return abis::PublicDataWrite<NT>{
return abis::PublicDataTransition<NT>{
.leaf_index = leaf_index,
.old_value = old_value,
.new_value = new_value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ export class PublicDataRead {
}

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

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

toBuffer() {
Expand All @@ -147,11 +147,11 @@ export class PublicDataWrite {

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

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

Expand All @@ -173,7 +173,7 @@ export class CombinedAccumulatedData {

public optionallyRevealedData: OptionallyRevealedData[],

public stateTransitions: PublicDataWrite[],
public stateTransitions: PublicDataTransition[],
public stateReads: PublicDataRead[],
) {
assertLength(this, 'newCommitments', KERNEL_NEW_COMMITMENTS_LENGTH);
Expand Down Expand Up @@ -221,7 +221,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, PublicDataWrite),
reader.readArray(STATE_TRANSITIONS_LENGTH, PublicDataTransition),
reader.readArray(STATE_READS_LENGTH, PublicDataRead),
);
}
Expand All @@ -238,7 +238,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, PublicDataWrite.empty),
times(STATE_TRANSITIONS_LENGTH, PublicDataTransition.empty),
times(STATE_READS_LENGTH, PublicDataRead.empty),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import { serializeToBuffer } from '../../utils/serialize.js';
import { UInt32 } from '../shared.js';

export class AppendOnlyTreeSnapshot {
/**
* Constructs a new append only tree snapshot.
* @param root - Root of the append only tree.
* @param nextAvailableLeafIndex - Index of the next available leaf in the append only tree.
* Note: We include the next available leaf index in the snapshot so that the snapshot can be used to verify that
* the insertion was performed at the correct place. If we only verified tree root then it could happen that
* some leaves would get overwritten and the tree root check would still pass.
* TLDR: We need to store the next available leaf index to ensure that the "append only" property was
* preserved when verifying state transitions.
*/
constructor(public root: Fr, public nextAvailableLeafIndex: UInt32) {}

toBuffer() {
Expand Down
8 changes: 4 additions & 4 deletions yarn-project/circuits.js/src/tests/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
PublicCallData,
PublicCircuitPublicInputs,
PublicDataRead,
PublicDataWrite,
PublicDataTransition,
PublicKernelInputs,
PublicKernelInputsNoPreviousKernel,
RootRollupInputs,
Expand Down Expand Up @@ -93,8 +93,8 @@ export function makeSelector(seed: number) {
return buffer;
}

export function makePublicDataWrite(seed = 1) {
return new PublicDataWrite(fr(seed), fr(seed + 1), fr(seed + 2));
export function makePublicDataTransition(seed = 1) {
return new PublicDataTransition(fr(seed), fr(seed + 1), fr(seed + 2));
}

export function makePublicDataRead(seed = 1) {
Expand All @@ -121,7 +121,7 @@ export function makeAccumulatedData(seed = 1): CombinedAccumulatedData {
range(KERNEL_L1_MSG_STACK_LENGTH, seed + 0x500).map(fr),
range(KERNEL_NEW_CONTRACTS_LENGTH, seed + 0x600).map(makeNewContractData),
range(KERNEL_OPTIONALLY_REVEALED_DATA_LENGTH, seed + 0x700).map(makeOptionallyRevealedData),
range(STATE_TRANSITIONS_LENGTH, seed + 0x800).map(makePublicDataWrite),
range(STATE_TRANSITIONS_LENGTH, seed + 0x800).map(makePublicDataTransition),
range(STATE_READS_LENGTH, seed + 0x900).map(makePublicDataRead),
);
}
Expand Down
Loading

0 comments on commit 11ae6db

Please sign in to comment.