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

feat: Ordering circuit in noir #2979

Merged
merged 3 commits into from
Oct 23, 2023
Merged
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
3 changes: 2 additions & 1 deletion yarn-project/noir-private-kernel/src/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ members = [
"crates/private-kernel-init-simulated",
"crates/private-kernel-inner",
"crates/private-kernel-inner-simulated",
# "crates/private-kernel-ordering",
"crates/private-kernel-ordering",
"crates/private-kernel-ordering-simulated",
]
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ mod public_data_update_request;
mod optionally_revealed_data;

mod combined_accumulated_data;
mod final_accumulated_data;

mod complete_address;

mod private_kernel;
mod kernel_circuit_public_inputs;
mod kernel_circuit_public_inputs_final;
mod previous_kernel_data;

mod call_stack_item;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,32 @@ struct CombinedAccumulatedData {
public_data_reads : [PublicDataRead; MAX_PUBLIC_DATA_READS_PER_TX],
}

struct FinalAccumulatedData{
aggregation_object : AggregationObject,

new_commitments : [Field; MAX_NEW_COMMITMENTS_PER_TX],
new_nullifiers : [Field; MAX_NEW_NULLIFIERS_PER_TX],
nullified_commitments : [Field; MAX_NEW_NULLIFIERS_PER_TX],

// For pending nullifiers, we have:
// nullifiedCommitments[j] != 0 <==> newNullifiers[j] nullifies nullifiedCommitments[j]

private_call_stack : [Field; MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX],
public_call_stack : [Field; MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX],
new_l2_to_l1_msgs : [Field; MAX_NEW_L2_TO_L1_MSGS_PER_TX],

encrypted_logs_hash : [Field; NUM_FIELDS_PER_SHA256],
unencrypted_logs_hash : [Field; NUM_FIELDS_PER_SHA256],

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

new_contracts : [NewContractData; MAX_NEW_CONTRACTS_PER_TX],
optionally_revealed_data : [OptionallyRevealedData; MAX_OPTIONALLY_REVEALED_DATA_LENGTH_PER_TX]
}

struct CombinedAccumulatedDataBuilder {
aggregation_object : AggregationObject,

Expand Down Expand Up @@ -112,4 +138,32 @@ impl CombinedAccumulatedDataBuilder {
public_data_reads: self.public_data_reads.storage,
}
}

pub fn to_final(self) -> FinalAccumulatedData {
assert_eq(self.read_requests.len, 0, "Final accumulated data: read requests not empty");
assert_eq(self.pending_read_requests.len, 0, "Final accumulated data: pending read requests not empty");
assert_eq(self.public_data_update_requests.len, 0, "Final accumulated data: public data update requests not empty");
assert_eq(self.public_data_reads.len, 0, "Final accumulated data: public data reads not empty");

FinalAccumulatedData {
aggregation_object: self.aggregation_object,

new_commitments: self.new_commitments.storage,
new_nullifiers: self.new_nullifiers.storage,
nullified_commitments: self.nullified_commitments.storage,

private_call_stack: self.private_call_stack.storage,
public_call_stack: self.public_call_stack.storage,
new_l2_to_l1_msgs: self.new_l2_to_l1_msgs.storage,

encrypted_logs_hash: self.encrypted_logs_hash,
unencrypted_logs_hash: self.unencrypted_logs_hash,

encrypted_log_preimages_length: self.encrypted_log_preimages_length,
unencrypted_log_preimages_length: self.unencrypted_log_preimages_length,

new_contracts: self.new_contracts.storage,
optionally_revealed_data: self.optionally_revealed_data,
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@

use crate::abis::combined_constant_data::CombinedConstantData;
use crate::abis::combined_accumulated_data::{
CombinedAccumulatedData,
FinalAccumulatedData,
CombinedAccumulatedDataBuilder,
};

struct KernelCircuitPublicInputs {

end : CombinedAccumulatedData,
constants : CombinedConstantData,

is_private : bool,
}

struct KernelCircuitPublicInputsFinal {
end : FinalAccumulatedData,
constants : CombinedConstantData,

// TODO(Jean): Left a note saying that this might need to be a witness
// In the context of Noir, this is already a witness.
is_private : bool,
}

struct KernelCircuitPublicInputsBuilder {
end : CombinedAccumulatedDataBuilder,
constants : CombinedConstantData,
Expand All @@ -28,4 +36,12 @@ impl KernelCircuitPublicInputsBuilder {
is_private : self.is_private,
}
}

pub fn to_final(self) -> KernelCircuitPublicInputsFinal {
KernelCircuitPublicInputsFinal {
end : self.end.to_final(),
constants : self.constants,
is_private : self.is_private,
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ struct NewContractData {
}

impl NewContractData {
fn is_empty(self) -> bool {
pub fn is_empty(self) -> bool {
(self.contract_address.to_field() == 0) &
(self.portal_contract_address.to_field() == 0) &
(self.function_tree_root ==0)
}

fn hash(self) -> Field {
pub fn default() -> Self {
Self {
contract_address : Address::default(),
portal_contract_address : EthAddress::default(),
function_tree_root : 0,
}
}

pub fn hash(self) -> Field {
if self.is_empty() {
0 // We want to return 0 here since the contract_address is zero
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::abis::private_circuit_public_inputs::PrivateCircuitPublicInputs;
use crate::abis::kernel_circuit_public_inputs::KernelCircuitPublicInputsBuilder;
use crate::utils::arrays;
use crate::abis::kernel_circuit_public_inputs_final::KernelCircuitPublicInputsFinal;
use crate::abis::previous_kernel_data::PreviousKernelData;
use crate::abis::private_kernel::private_call_data::PrivateCallData;
use crate::abis::combined_accumulated_data::{CombinedAccumulatedData, CombinedAccumulatedDataBuilder};
Expand Down Expand Up @@ -109,6 +108,7 @@ pub fn initialize_end_values(previous_kernel : PreviousKernelData, public_inputs
// functions within this circuit:
let start = previous_kernel.public_inputs.end;

public_inputs.end.read_requests = arrays::array_to_bounded_vec(start.read_requests, arrays::is_empty, 0);
public_inputs.end.new_commitments = arrays::array_to_bounded_vec(start.new_commitments, arrays::is_empty, 0);
public_inputs.end.new_nullifiers = arrays::array_to_bounded_vec(start.new_nullifiers, arrays::is_empty, 0);
public_inputs.end.nullified_commitments = arrays::array_to_bounded_vec(start.nullified_commitments, arrays::is_empty, 0);
Expand All @@ -124,34 +124,7 @@ pub fn initialize_end_values(previous_kernel : PreviousKernelData, public_inputs
public_inputs.end.unencrypted_log_preimages_length = start.unencrypted_log_preimages_length;

public_inputs.end.optionally_revealed_data = start.optionally_revealed_data;
}

// The cpp code avoids having two methods like we have by using templates.
//
// TODO(Kev): We can probably rewrite KernelCircuitPublicInputs and KernelCircuitPublicInputsFinal
// to use composition instead and have one method.
fn initialize_end_values_final(previous_kernel : PreviousKernelData, public_inputs : &mut KernelCircuitPublicInputsFinal) {
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:
let start = previous_kernel.public_inputs.end;

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

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

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

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

public_inputs.end.optionally_revealed_data = start.optionally_revealed_data;
public_inputs.end.new_contracts = arrays::array_to_bounded_vec(previous_kernel.public_inputs.end.new_contracts, |ncd: NewContractData| ncd.is_empty(), NewContractData::default());
}

pub fn update_end_values(private_call : PrivateCallData, public_inputs : &mut KernelCircuitPublicInputsBuilder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,14 @@ pub fn compute_contract_address_from_partial(point : Point, partial_address : Fi
Address::from_field(field)
}

fn compute_unique_commitment(nonce : Field, siloed_commitment : Field) -> Field {
pub fn compute_unique_commitment(nonce : Field, siloed_commitment : Field) -> Field {
dep::std::hash::pedersen_with_separator([
nonce,
siloed_commitment
], constants_gen::GENERATOR_INDEX__UNIQUE_COMMITMENT)[0]
}

fn compute_commitment_nonce(first_nullifier : Field, commitment_index : Field) -> Field {
pub fn compute_commitment_nonce(first_nullifier : Field, commitment_index : Field) -> Field {
dep::std::hash::pedersen_with_separator([
first_nullifier,
commitment_index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ mod interop_testing;

mod private_kernel_init;
mod private_kernel_inner;
mod private_kernel_ordering;

use abis::kernel_circuit_public_inputs::KernelCircuitPublicInputs;
use abis::kernel_circuit_public_inputs::{ KernelCircuitPublicInputs, KernelCircuitPublicInputsFinal };
use private_kernel_init::PrivateKernelInputsInit;
use private_kernel_inner::PrivateKernelInputsInner;
use private_kernel_ordering::PrivateKernelInputsOrdering;

// Hmm
// PrivateCircuitPublicInputs vs CombinedAccumulatedData (Duplicated fields)
// ?Que es la diferencia entre contract_address y storage_address?
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::abis::{
combined_constant_data::CombinedConstantData,
previous_kernel_data::PreviousKernelData,
private_kernel::private_call_data::PrivateCallData,
new_contract_data::NewContractData,
kernel_circuit_public_inputs::{KernelCircuitPublicInputs, KernelCircuitPublicInputsBuilder}
};
use crate::utils::{
Expand All @@ -20,16 +21,6 @@ struct PrivateKernelInputsInner {
}

impl PrivateKernelInputsInner {
fn initialize_end_values(self, public_inputs : &mut KernelCircuitPublicInputsBuilder) {
let previous_kernel = self.previous_kernel;

common::initialize_end_values(previous_kernel, public_inputs);

// Ensure the arrays are the same as previously, before we start pushing more data onto them in other
// functions within this circuit:
let start = previous_kernel.public_inputs.end;
public_inputs.end.read_requests = arrays::array_to_bounded_vec(start.read_requests, arrays::is_empty, 0);
}

// TODO: We can create a type alias for private call stack
fn pop_and_validate_this_private_call_hash(self, public_inputs : &mut KernelCircuitPublicInputsBuilder){
Expand Down Expand Up @@ -67,7 +58,7 @@ impl PrivateKernelInputsInner {
common::validate_previous_kernel_values(self.previous_kernel.public_inputs.end);

// Do this before any functions can modify the inputs.
self.initialize_end_values(&mut public_inputs);
common::initialize_end_values(self.previous_kernel, &mut public_inputs);

self.validate_inputs();

Expand Down
Loading