Skip to content

Commit

Permalink
chore(1074): remove read request data from final private kernel circu…
Browse files Browse the repository at this point in the history
…it public inputs (#1840)

Resolves #1074

Refactor task consisting in defining a specific struct for the public
inputs of final ordering private kernel circuit. This new struct is
named: KernelCircuitPublicInputsFinal
This struct is identical to KernelCircuitPublicInputs except that we
trimmed the 4 following members from CombinedAccumulatedData (a struct
named FinalAccumulatedData was introduced for this purpose):

- read_requests
- read_request_membership_witnesses
- public_data_update_requests
- public_data_reads

In addition, one commit which resolves #1865 was added.


# Checklist:
Remove the checklist to signal you've completed it. Enable auto-merge if
the PR is ready to merge.
- [x] If the pull request requires a cryptography review (e.g.
cryptographic algorithm implementations) I have added the 'crypto' tag.
- [x] I have reviewed my diff in github, line by line and removed
unexpected formatting changes, testing logs, or commented-out code.
- [x] Every change is related to the PR description.
- [x] I have
[linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue)
this pull request to relevant issues (if any exist).
  • Loading branch information
jeanmon authored Aug 30, 2023
1 parent 9a603e0 commit c61557a
Show file tree
Hide file tree
Showing 32 changed files with 1,105 additions and 143 deletions.
13 changes: 13 additions & 0 deletions circuits/cpp/src/aztec3/circuits/abis/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "function_data.hpp"
#include "function_leaf_preimage.hpp"
#include "kernel_circuit_public_inputs.hpp"
#include "kernel_circuit_public_inputs_final.hpp"
#include "previous_kernel_data.hpp"
#include "private_circuit_public_inputs.hpp"
#include "tx_context.hpp"
Expand All @@ -16,6 +17,7 @@
#include "rollup/root/root_rollup_public_inputs.hpp"

#include "aztec3/circuits/abis/combined_accumulated_data.hpp"
#include "aztec3/circuits/abis/final_accumulated_data.hpp"
#include "aztec3/circuits/abis/new_contract_data.hpp"
#include "aztec3/circuits/abis/packers.hpp"
#include "aztec3/circuits/abis/point.hpp"
Expand Down Expand Up @@ -593,6 +595,11 @@ WASM_EXPORT const char* abis__test_roundtrip_serialize_combined_accumulated_data
return as_string_output<aztec3::circuits::abis::CombinedAccumulatedData<NT>>(input, size);
}

WASM_EXPORT const char* abis__test_roundtrip_serialize_final_accumulated_data(uint8_t const* input, uint32_t* size)
{
return as_string_output<aztec3::circuits::abis::FinalAccumulatedData<NT>>(input, size);
}

WASM_EXPORT const char* abis__test_roundtrip_serialize_signature(uint8_t const* input, uint32_t* size)
{
return as_string_output<NT::schnorr_signature>(input, size);
Expand All @@ -614,6 +621,12 @@ WASM_EXPORT const char* abis__test_roundtrip_serialize_kernel_circuit_public_inp
return as_string_output<aztec3::circuits::abis::KernelCircuitPublicInputs<NT>>(input, size);
}

WASM_EXPORT const char* abis__test_roundtrip_serialize_kernel_circuit_public_inputs_final(uint8_t const* input,
uint32_t* size)
{
return as_string_output<aztec3::circuits::abis::KernelCircuitPublicInputsFinal<NT>>(input, size);
}

WASM_EXPORT const char* abis__test_roundtrip_serialize_public_kernel_inputs(uint8_t const* input, uint32_t* size)
{
return as_string_output<aztec3::circuits::abis::public_kernel::PublicKernelInputs<NT>>(input, size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "aztec3/circuits/abis/historic_block_data.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>
Expand All @@ -27,7 +26,7 @@ template <typename NCT> struct CombinedConstantData {
MSGPACK_FIELDS(block_data, tx_context);
boolean operator==(CombinedConstantData<NCT> const& other) const
{
return block_data == other.block_data && tx_context == other.tx_context;
return msgpack_derived_equals<boolean>(*this, other);
}

template <typename Builder> CombinedConstantData<CircuitTypes<Builder>> to_circuit_type(Builder& builder) const
Expand Down
194 changes: 194 additions & 0 deletions circuits/cpp/src/aztec3/circuits/abis/final_accumulated_data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
#pragma once
#include "new_contract_data.hpp"
#include "optionally_revealed_data.hpp"
#include "public_data_read.hpp"
#include "public_data_update_request.hpp"

#include "aztec3/circuits/abis/membership_witness.hpp"
#include "aztec3/circuits/abis/read_request_membership_witness.hpp"
#include "aztec3/constants.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>

#include <array>
#include <cstddef>

namespace aztec3::circuits::abis {

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

template <typename NCT> struct FinalAccumulatedData {
using fr = typename NCT::fr;
using uint32 = typename NCT::uint32;
using boolean = typename NCT::boolean;
using AggregationObject = typename NCT::AggregationObject;

AggregationObject aggregation_object{};

std::array<fr, MAX_NEW_COMMITMENTS_PER_TX> new_commitments{};
std::array<fr, MAX_NEW_NULLIFIERS_PER_TX> new_nullifiers{};
std::array<fr, MAX_NEW_NULLIFIERS_PER_TX> nullified_commitments{};
// For pending nullifiers, we have:
// nullifiedCommitments[j] != 0 <==> newNullifiers[j] nullifies nullifiedCommitments[j]

std::array<fr, MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX> private_call_stack{};
std::array<fr, MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX> public_call_stack{};
std::array<fr, MAX_NEW_L2_TO_L1_MSGS_PER_TX> new_l2_to_l1_msgs{};

std::array<fr, NUM_FIELDS_PER_SHA256> encrypted_logs_hash{};
std::array<fr, NUM_FIELDS_PER_SHA256> unencrypted_logs_hash{};

// Here so that the gas cost of this request can be measured by circuits, without actually needing to feed in the
// variable-length data.
fr encrypted_log_preimages_length = 0;
fr unencrypted_log_preimages_length = 0;

std::array<NewContractData<NCT>, MAX_NEW_CONTRACTS_PER_TX> new_contracts{};

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

// for serialization, update with new fields
MSGPACK_FIELDS(aggregation_object,
new_commitments,
new_nullifiers,
nullified_commitments,
private_call_stack,
public_call_stack,
new_l2_to_l1_msgs,
encrypted_logs_hash,
unencrypted_logs_hash,
encrypted_log_preimages_length,
unencrypted_log_preimages_length,
new_contracts,
optionally_revealed_data);
boolean operator==(FinalAccumulatedData<NCT> const& other) const
{
return msgpack_derived_equals<boolean>(*this, other);
};

template <typename Builder> FinalAccumulatedData<CircuitTypes<Builder>> to_circuit_type(Builder& builder) const
{
typedef CircuitTypes<Builder> CT;
static_assert((std::is_same<NativeTypes, NCT>::value));

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

FinalAccumulatedData<CT> acc_data = {
typename CT::AggregationObject{
to_ct(aggregation_object.P0),
to_ct(aggregation_object.P1),
to_ct(aggregation_object.public_inputs),
aggregation_object.proof_witness_indices,
aggregation_object.has_data,
},

to_ct(new_commitments),
to_ct(new_nullifiers),
to_ct(nullified_commitments),

to_ct(private_call_stack),
to_ct(public_call_stack),
to_ct(new_l2_to_l1_msgs),

to_ct(encrypted_logs_hash),
to_ct(unencrypted_logs_hash),

to_ct(encrypted_log_preimages_length),
to_ct(unencrypted_log_preimages_length),

map(new_contracts, to_circuit_type),
map(optionally_revealed_data, to_circuit_type),
};

return acc_data;
};

template <typename Builder> FinalAccumulatedData<NativeTypes> to_native_type() const
{
static_assert(std::is_same<CircuitTypes<Builder>, NCT>::value);
auto to_nt = [&](auto& e) { return aztec3::utils::types::to_nt<Builder>(e); };
auto to_native_type = []<typename T>(T& e) { return e.template to_native_type<Builder>(); };

FinalAccumulatedData<NativeTypes> acc_data = {
typename NativeTypes::AggregationObject{
to_nt(aggregation_object.P0),
to_nt(aggregation_object.P1),
to_nt(aggregation_object.public_inputs),
aggregation_object.proof_witness_indices,
aggregation_object.has_data,
},

to_nt(new_commitments),
to_nt(new_nullifiers),
to_nt(nullified_commitments),

to_nt(private_call_stack),
to_nt(public_call_stack),
to_nt(new_l2_to_l1_msgs),

to_nt(encrypted_logs_hash),
to_nt(unencrypted_logs_hash),

to_nt(encrypted_log_preimages_length),
to_nt(unencrypted_log_preimages_length),

map(new_contracts, to_native_type),
map(optionally_revealed_data, to_native_type),
};
return acc_data;
}

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

aggregation_object.add_proof_outputs_as_public_inputs();

set_array_public(new_commitments);
set_array_public(new_nullifiers);
set_array_public(nullified_commitments);

set_array_public(private_call_stack);
set_array_public(public_call_stack);
set_array_public(new_l2_to_l1_msgs);

set_array_public(encrypted_logs_hash);
set_array_public(unencrypted_logs_hash);

set_array_public(new_contracts);
set_array_public(optionally_revealed_data);
}

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

template <size_t SIZE> void set_array_public(std::array<OptionallyRevealedData<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<NewContractData<NCT>, SIZE>& arr)
{
static_assert(!(std::is_same<NativeTypes, NCT>::value));
for (auto& e : arr) {
e.set_public();
}
}
};

} // namespace aztec3::circuits::abis
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ template <typename NCT> struct KernelCircuitPublicInputs {

boolean operator==(KernelCircuitPublicInputs<NCT> const& other) const
{
return end == other.end && constants == other.constants && is_private == other.is_private;
return msgpack_derived_equals<boolean>(*this, other);
};

template <typename Builder> KernelCircuitPublicInputs<CircuitTypes<Builder>> to_circuit_type(Builder& builder) const
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#pragma once
#include "combined_constant_data.hpp"
#include "final_accumulated_data.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;
using std::is_same;

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

FinalAccumulatedData<NCT> end{};
CombinedConstantData<NCT> constants{};

boolean is_private = true; // TODO: might need to instantiate from witness!

// for serialization, update with new fields
MSGPACK_FIELDS(end, constants, is_private);

boolean operator==(KernelCircuitPublicInputsFinal<NCT> const& other) const
{
return msgpack_derived_equals<boolean>(*this, other);
}

template <typename Builder>
KernelCircuitPublicInputsFinal<CircuitTypes<Builder>> to_circuit_type(Builder& builder) const
{
static_assert((std::is_same<NativeTypes, NCT>::value));

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

KernelCircuitPublicInputsFinal<CircuitTypes<Builder>> private_inputs = {
end.to_circuit_type(builder),
constants.to_circuit_type(builder),

to_ct(is_private),
};

return private_inputs;
};

template <typename Builder> KernelCircuitPublicInputsFinal<NativeTypes> to_native_type() const
{
static_assert(std::is_same<CircuitTypes<Builder>, NCT>::value);
auto to_nt = [&](auto& e) { return aztec3::utils::types::to_nt<Builder>(e); };
auto to_native_type = []<typename T>(T& e) { return e.template to_native_type<Builder>(); };

KernelCircuitPublicInputsFinal<NativeTypes> pis = {
to_native_type(end),
to_native_type(constants),

to_nt(is_private),
};

return pis;
};

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

end.set_public();
constants.set_public();

fr(is_private).set_public();
}
};

} // namespace aztec3::circuits::abis
1 change: 0 additions & 1 deletion circuits/cpp/src/aztec3/circuits/abis/tx_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "function_data.hpp"
#include "tx_context.hpp"

#include "aztec3/utils/array.hpp"
#include "aztec3/utils/types/circuit_types.hpp"
#include "aztec3/utils/types/convert.hpp"
#include "aztec3/utils/types/native_types.hpp"
Expand Down
29 changes: 0 additions & 29 deletions circuits/cpp/src/aztec3/circuits/kernel/private/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "aztec3/circuits/abis/function_data.hpp"
#include "aztec3/circuits/abis/kernel_circuit_public_inputs.hpp"
#include "aztec3/circuits/abis/new_contract_data.hpp"
#include "aztec3/circuits/abis/previous_kernel_data.hpp"
#include "aztec3/circuits/abis/private_kernel/private_call_data.hpp"
#include "aztec3/circuits/abis/read_request_membership_witness.hpp"
#include "aztec3/circuits/hash.hpp"
Expand All @@ -21,7 +20,6 @@ using aztec3::circuits::abis::ContractLeafPreimage;
using aztec3::circuits::abis::FunctionData;
using aztec3::circuits::abis::KernelCircuitPublicInputs;
using aztec3::circuits::abis::NewContractData;
using aztec3::circuits::abis::PreviousKernelData;
using aztec3::circuits::abis::ReadRequestMembershipWitness;

using aztec3::utils::array_length;
Expand Down Expand Up @@ -423,31 +421,4 @@ void common_contract_logic(DummyBuilder& builder,
}
}

void common_initialise_end_values(PreviousKernelData<NT> const& previous_kernel,
KernelCircuitPublicInputs<NT>& public_inputs)
{
public_inputs.constants = previous_kernel.public_inputs.constants;

// Ensure the arrays are the same as previously, before we start pushing more data onto them in other
// functions within this circuit:
auto& end = public_inputs.end;
const auto& start = previous_kernel.public_inputs.end;

end.new_commitments = start.new_commitments;
end.new_nullifiers = start.new_nullifiers;
end.nullified_commitments = start.nullified_commitments;

end.private_call_stack = start.private_call_stack;
end.public_call_stack = start.public_call_stack;
end.new_l2_to_l1_msgs = start.new_l2_to_l1_msgs;

end.encrypted_logs_hash = start.encrypted_logs_hash;
end.unencrypted_logs_hash = start.unencrypted_logs_hash;

end.encrypted_log_preimages_length = start.encrypted_log_preimages_length;
end.unencrypted_log_preimages_length = start.unencrypted_log_preimages_length;

end.optionally_revealed_data = start.optionally_revealed_data;
}

} // namespace aztec3::circuits::kernel::private_kernel
Loading

0 comments on commit c61557a

Please sign in to comment.