Skip to content

Commit

Permalink
refactor(noir-contracts): necessary changes for hashing logs in Noir (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan authored Jul 25, 2023
1 parent f57f903 commit f17d881
Show file tree
Hide file tree
Showing 22 changed files with 132 additions and 114 deletions.
1 change: 1 addition & 0 deletions circuits/cpp/src/aztec3/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ constexpr size_t PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH =
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL * CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH +
MAX_PUBLIC_DATA_READS_PER_CALL * CONTRACT_STORAGE_READ_LENGTH + MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL +
MAX_NEW_COMMITMENTS_PER_CALL + MAX_NEW_NULLIFIERS_PER_CALL + MAX_NEW_L2_TO_L1_MSGS_PER_CALL +
NUM_FIELDS_PER_SHA256 + 1 + // + 1 for unencrypted logs preimage length
COMMITMENT_TREES_ROOTS_LENGTH + 2; // + 2 for chain_id and version

// Size of the return value of a private function call,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export class PrivateFunctionExecution {

const wasm = await CircuitsWasm.get();

// TODO(#1347): Noir fails with too many unknowns error when public inputs struct contains too many members.
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) --> set this in Noir
publicInputs.encryptedLogsHash = to2Fields(encryptedLogs.hash());
publicInputs.encryptedLogPreimagesLength = new Fr(encryptedLogs.getSerializedLength());
publicInputs.unencryptedLogsHash = to2Fields(unencryptedLogs.hash());
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuits.js/src/cbind/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const CONTRACT_DEPLOYMENT_DATA_LENGTH = 6;
export const PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 55;
export const CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 3;
export const CONTRACT_STORAGE_READ_LENGTH = 2;
export const PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 51;
export const PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 54;
export const GET_NOTES_ORACLE_RETURN_LENGTH = 86;
export const EMPTY_NULLIFIED_COMMITMENT = 1000000;
export const CALL_PRIVATE_FUNCTION_RETURN_SIZE = 60;
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions yarn-project/noir-contracts/src/artifacts/zk_token_contract.json

Large diffs are not rendered by default.

42 changes: 24 additions & 18 deletions yarn-project/noir-libs/noir-aztec/src/abi.nr
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
use crate::constants_gen::RETURN_VALUES_LENGTH;
use crate::constants_gen::MAX_READ_REQUESTS_PER_CALL;
use crate::constants_gen::MAX_NEW_COMMITMENTS_PER_CALL;
use crate::constants_gen::MAX_NEW_NULLIFIERS_PER_CALL;
use crate::constants_gen::MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL;
use crate::constants_gen::MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL;
use crate::constants_gen::MAX_NEW_L2_TO_L1_MSGS_PER_CALL;
use crate::constants_gen::NUM_FIELDS_PER_SHA256;
use crate::constants_gen::MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL;
use crate::constants_gen::MAX_PUBLIC_DATA_READS_PER_CALL;
use crate::constants_gen::GENERATOR_INDEX__FUNCTION_ARGS;
use crate::constants_gen::COMMITMENT_TREES_ROOTS_LENGTH;
use crate::constants_gen::CONTRACT_DEPLOYMENT_DATA_LENGTH;
use crate::constants_gen::CALL_CONTEXT_LENGTH;
use crate::constants_gen::PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH;
use crate::constants_gen::CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH;
use crate::constants_gen::CONTRACT_STORAGE_READ_LENGTH;
use crate::constants_gen::PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH;
use crate::constants_gen::{
RETURN_VALUES_LENGTH,
MAX_READ_REQUESTS_PER_CALL,
MAX_NEW_COMMITMENTS_PER_CALL,
MAX_NEW_NULLIFIERS_PER_CALL,
MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL,
MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL,
MAX_NEW_L2_TO_L1_MSGS_PER_CALL,
NUM_FIELDS_PER_SHA256,
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL,
MAX_PUBLIC_DATA_READS_PER_CALL,
GENERATOR_INDEX__FUNCTION_ARGS,
COMMITMENT_TREES_ROOTS_LENGTH,
CONTRACT_DEPLOYMENT_DATA_LENGTH,
CALL_CONTEXT_LENGTH,
PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH,
CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH,
CONTRACT_STORAGE_READ_LENGTH,
PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH,
};
use crate::types::vec::BoundedVec;
use crate::types::point::Point;

Expand Down Expand Up @@ -245,6 +247,8 @@ struct PublicCircuitPublicInputs {
new_commitments: [Field; MAX_NEW_COMMITMENTS_PER_CALL],
new_nullifiers: [Field; crate::abi::MAX_NEW_NULLIFIERS_PER_CALL],
new_l2_to_l1_msgs: [Field; crate::abi::MAX_NEW_L2_TO_L1_MSGS_PER_CALL],
unencrypted_logs_hash: [Field; NUM_FIELDS_PER_SHA256],
unencrypted_log_preimages_length: Field,
commitment_trees_roots: CommitmentTreesRoots,
historic_public_data_tree_root: Field,
prover_address: Field,
Expand All @@ -271,6 +275,8 @@ impl PublicCircuitPublicInputs {
fields = fields.push_array(self.new_commitments);
fields = fields.push_array(self.new_nullifiers);
fields = fields.push_array(self.new_l2_to_l1_msgs);
fields = fields.push_array(self.unencrypted_logs_hash);
fields = fields.push(self.unencrypted_log_preimages_length);
fields = fields.push_array(self.commitment_trees_roots.serialize());
fields = fields.push(self.historic_public_data_tree_root);
fields = fields.push(self.prover_address);
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/noir-libs/noir-aztec/src/constants_gen.nr
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ global CONTRACT_DEPLOYMENT_DATA_LENGTH: comptime Field = 6;
global PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH: comptime Field = 55;
global CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH: comptime Field = 3;
global CONTRACT_STORAGE_READ_LENGTH: comptime Field = 2;
global PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH: comptime Field = 51;
global PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH: comptime Field = 54;
global GET_NOTES_ORACLE_RETURN_LENGTH: comptime Field = 86;
global EMPTY_NULLIFIED_COMMITMENT: comptime Field = 1000000;
global CALL_PRIVATE_FUNCTION_RETURN_SIZE: comptime Field = 60;
Expand Down
79 changes: 43 additions & 36 deletions yarn-project/noir-libs/noir-aztec/src/context.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
use crate::types::vec::BoundedVec;
use crate::constants_gen;
use crate::constants_gen::{
RETURN_VALUES_LENGTH,
MAX_READ_REQUESTS_PER_CALL,
MAX_NEW_COMMITMENTS_PER_CALL,
MAX_NEW_NULLIFIERS_PER_CALL,
MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL,
MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL,
MAX_NEW_L2_TO_L1_MSGS_PER_CALL,
EMPTY_NULLIFIED_COMMITMENT,
NUM_FIELDS_PER_SHA256,
};
use crate::abi;

// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165)
// use dep::std::collections::vec::Vec;

// l1 to l2 messaging
use crate::messaging::process_l1_to_l2_message;

Expand All @@ -12,22 +25,21 @@ struct Context {
oracle_connector: Field, // for forcing dependencies between oracle calls

args_hash : Field,
return_values : BoundedVec<Field, constants_gen::RETURN_VALUES_LENGTH>,
return_values : BoundedVec<Field, RETURN_VALUES_LENGTH>,

read_requests: BoundedVec<Field, constants_gen::MAX_READ_REQUESTS_PER_CALL>,
read_requests: BoundedVec<Field, MAX_READ_REQUESTS_PER_CALL>,

new_commitments: BoundedVec<Field, constants_gen::MAX_NEW_COMMITMENTS_PER_CALL>,
new_nullifiers: BoundedVec<Field, constants_gen::MAX_NEW_NULLIFIERS_PER_CALL>,
nullified_commitments: BoundedVec<Field, constants_gen::MAX_NEW_NULLIFIERS_PER_CALL>,
new_commitments: BoundedVec<Field, MAX_NEW_COMMITMENTS_PER_CALL>,
new_nullifiers: BoundedVec<Field, MAX_NEW_NULLIFIERS_PER_CALL>,
nullified_commitments: BoundedVec<Field, MAX_NEW_NULLIFIERS_PER_CALL>,

private_call_stack : BoundedVec<Field, constants_gen::MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL>,
public_call_stack : BoundedVec<Field, constants_gen::MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL>,
new_l2_to_l1_msgs : BoundedVec<Field, constants_gen::MAX_NEW_L2_TO_L1_MSGS_PER_CALL>,
private_call_stack : BoundedVec<Field, MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL>,
public_call_stack : BoundedVec<Field, MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL>,
new_l2_to_l1_msgs : BoundedVec<Field, MAX_NEW_L2_TO_L1_MSGS_PER_CALL>,

encrypted_logs_hash : [Field; constants_gen::NUM_FIELDS_PER_SHA256],
unencrypted_logs_hash : [Field; constants_gen::NUM_FIELDS_PER_SHA256],
encrypted_log_preimages_length : Field,
unencrypted_log_preimages_length : Field,
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165)
// encrypted_logs_preimages: Vec<Field>,
// unencrypted_logs_preimages: Vec<Field>,
}

impl Context {
Expand All @@ -50,14 +62,19 @@ impl Context {
public_call_stack: BoundedVec::new(0),
new_l2_to_l1_msgs: BoundedVec::new(0),

encrypted_logs_hash: [0; constants_gen::NUM_FIELDS_PER_SHA256],
unencrypted_logs_hash: [0; constants_gen::NUM_FIELDS_PER_SHA256],
encrypted_log_preimages_length: 0,
unencrypted_log_preimages_length: 0,
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165)
// encrypted_logs_preimages: Vec::new(),
// unencrypted_logs_preimages: Vec::new(),
}
}

fn finish(self) -> abi::PrivateCircuitPublicInputs {
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165)
let encrypted_logs_hash = [0; NUM_FIELDS_PER_SHA256];
let unencrypted_logs_hash = [0; NUM_FIELDS_PER_SHA256];
let encrypted_log_preimages_length = 0;
let unencrypted_log_preimages_length = 0;

let priv_circuit_pub_inputs = abi::PrivateCircuitPublicInputs {
call_context: self.inputs.call_context,
args_hash: self.args_hash,
Expand All @@ -69,10 +86,10 @@ impl Context {
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,
encrypted_logs_hash: encrypted_logs_hash,
unencrypted_logs_hash: unencrypted_logs_hash,
encrypted_log_preimages_length: encrypted_log_preimages_length,
unencrypted_log_preimages_length: unencrypted_log_preimages_length,
commitment_trees_roots: self.inputs.roots,
contract_deployment_data: self.inputs.contract_deployment_data,
chain_id: self.inputs.private_global_variables.chain_id,
Expand Down Expand Up @@ -108,26 +125,16 @@ impl Context {
let nullifier = process_l1_to_l2_message(inputs.roots.l1_to_l2_messages_tree_root, inputs.call_context.storage_contract_address, msg_key, content, secret);

// Push nullifier (and the "commitment" corresponding to this can be "empty")
self.push_new_nullifier(nullifier, constants_gen::EMPTY_NULLIFIED_COMMITMENT)
}

fn set_encrypted_logs_hash(mut self: Self, hash: [Field; constants_gen::NUM_FIELDS_PER_SHA256]) -> Self {
self.encrypted_logs_hash = hash;
self
}

fn set_encrypted_log_preimages_length(mut self: Self, len: Field) -> Self {
self.encrypted_log_preimages_length = len;
self
self.push_new_nullifier(nullifier, EMPTY_NULLIFIED_COMMITMENT)
}

fn set_unencrypted_logs_hash(mut self: Self, hash: [Field; constants_gen::NUM_FIELDS_PER_SHA256]) -> Self {
self.unencrypted_logs_hash = hash;
fn accumulate_encrypted_logs<N>(mut self: Self, log: [Field; N]) -> Self {
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165)
self
}

fn set_unencrypted_log_preimages_length(mut self: Self, len: Field) -> Self {
self.unencrypted_log_preimages_length = len;
fn accumulate_unencrypted_logs<T>(mut self: Self, log: T) -> Self {
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165)
self
}
}
17 changes: 4 additions & 13 deletions yarn-project/noir-libs/noir-aztec/src/log.nr
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,16 @@ fn emit_encrypted_log<N>(
encryption_pub_key: Point,
log: [Field; N],
) -> Context {
let hash = oracle::logs::emit_encrypted_log(contract_address, storage_slot, owner, encryption_pub_key, log);
let mut len = log.len();
if (hash[0] == 0) & (hash[1] == 0) {
len = 0;
};
context = context.set_encrypted_logs_hash(hash);
context = context.set_encrypted_log_preimages_length(len);
let _ = oracle::logs::emit_encrypted_log(contract_address, storage_slot, owner, encryption_pub_key, log);
context = context.accumulate_encrypted_logs(log);
context
}

fn emit_unencrypted_log<T>(
mut context: Context,
log: T,
) -> Context {
let hash = oracle::logs::emit_unencrypted_log(log);
// TODO
let len = 0;
// let len = log.len();
context = context.set_unencrypted_logs_hash(hash);
context = context.set_unencrypted_log_preimages_length(len);
let _ = oracle::logs::emit_unencrypted_log(log);
context = context.accumulate_unencrypted_logs(log);
context
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::types::point::Point;
use crate::messaging::l1_to_l2_message_getter_data::L1ToL2MessageGetterData;
use crate::messaging::l1_to_l2_message_getter_data::make_l1_to_l2_message_getter_data;
use crate::constants_gen::L1_TO_L2_MESSAGE_LENGTH;
use crate::constants_gen::GENERATOR_INDEX__NULLIFIER;
use crate::constants_gen::GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET;
use crate::constants_gen::{
L1_TO_L2_MESSAGE_LENGTH,
GENERATOR_INDEX__NULLIFIER,
GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET,
};

struct L1ToL2Message {
sender: Field,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::messaging::l1_to_l2_message::L1ToL2Message;
use crate::constants_gen::L1_TO_L2_MSG_TREE_HEIGHT;
use crate::constants_gen::L1_TO_L2_MESSAGE_LENGTH;
use crate::constants_gen::{
L1_TO_L2_MSG_TREE_HEIGHT,
L1_TO_L2_MESSAGE_LENGTH,
};
use crate::utils::arr_copy_slice;

struct L1ToL2MessageGetterData {
Expand Down
19 changes: 12 additions & 7 deletions yarn-project/noir-libs/noir-aztec/src/public_call_stack_item.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ use crate::abi::PublicCircuitPublicInputs;
use crate::abi::FunctionData;
use crate::types::vec::BoundedVec;
use crate::utils::arr_copy_slice;
use crate::constants_gen::RETURN_VALUES_LENGTH;
use crate::constants_gen::MAX_NEW_COMMITMENTS_PER_CALL;
use crate::constants_gen::MAX_NEW_NULLIFIERS_PER_CALL;
use crate::constants_gen::MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL;
use crate::constants_gen::MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL;
use crate::constants_gen::MAX_PUBLIC_DATA_READS_PER_CALL;
use crate::constants_gen::MAX_NEW_L2_TO_L1_MSGS_PER_CALL;
use crate::constants_gen::{
RETURN_VALUES_LENGTH,
MAX_NEW_COMMITMENTS_PER_CALL,
MAX_NEW_NULLIFIERS_PER_CALL,
MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL,
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL,
MAX_PUBLIC_DATA_READS_PER_CALL,
MAX_NEW_L2_TO_L1_MSGS_PER_CALL,
NUM_FIELDS_PER_SHA256,
};

// oracles
use crate::oracle::enqueue_public_function_call::enqueue_public_function_call_internal;
Expand Down Expand Up @@ -68,6 +71,8 @@ impl PublicCallStackItem {
new_commitments: [0; MAX_NEW_COMMITMENTS_PER_CALL],
new_nullifiers: [0; MAX_NEW_NULLIFIERS_PER_CALL],
new_l2_to_l1_msgs:[0; MAX_NEW_L2_TO_L1_MSGS_PER_CALL],
unencrypted_logs_hash:[0; NUM_FIELDS_PER_SHA256],
unencrypted_log_preimages_length: 0,
commitment_trees_roots: crate::abi::empty_commitment_trees_roots(),
historic_public_data_tree_root: 0,
prover_address: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use crate::note::lifecycle::create_note;
use crate::note::note_getter::get_note;
use crate::note::note_interface::NoteInterface;
use crate::oracle;
use crate::constants_gen::GENERATOR_INDEX__INITIALISATION_NULLIFIER;
use crate::constants_gen::EMPTY_NULLIFIED_COMMITMENT;
use crate::constants_gen::{
GENERATOR_INDEX__INITIALISATION_NULLIFIER,
EMPTY_NULLIFIED_COMMITMENT,
};

struct ImmutableSingleton<Note, N> {
storage_slot: Field,
Expand Down
6 changes: 4 additions & 2 deletions yarn-project/noir-libs/noir-aztec/src/state_vars/singleton.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use crate::note::lifecycle::create_note;
use crate::note::lifecycle::destroy_note;
use crate::note::note_getter::get_note;
use crate::note::note_interface::NoteInterface;
use crate::constants_gen::GENERATOR_INDEX__INITIALISATION_NULLIFIER;
use crate::constants_gen::EMPTY_NULLIFIED_COMMITMENT;
use crate::constants_gen::{
GENERATOR_INDEX__INITIALISATION_NULLIFIER,
EMPTY_NULLIFIED_COMMITMENT,
};

struct Singleton<Note, N> {
storage_slot: Field,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class PublicProcessor {
item.isEmpty() ? Fr.zero() : computeCallStackItemHash(wasm, item),
);

// TODO(#1347): Noir fails with too many unknowns error when public inputs struct contains too many members.
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) --> set this in Noir
const unencryptedLogsHash = to2Fields(result.unencryptedLogs.hash());
const unencryptedLogPreimagesLength = new Fr(result.unencryptedLogs.getSerializedLength());

Expand Down

0 comments on commit f17d881

Please sign in to comment.