Skip to content

Commit

Permalink
feat: remove NUM_FIELDS_PER_SHA256 (#5392)
Browse files Browse the repository at this point in the history
Continuation of #5160

This PR removes all reference to NUM_FIELDS_PER_SHA256, as we are
truncating SHAs to 31 bytes inside the circuits/contracts, so they can
be represented as a single field.

It also tidies up `toTruncField(sha256(thing))` using a new TS method
`sha256ToField`. `toTruncField` now never actually truncates the number,
as it expects a truncated output from `sha256ToField` or directly from
Noir, which should solve any issues with tests where test values weren't
getting truncated correctly.

---------

Co-authored-by: sklppy88 <esau@aztecprotocol.com>
Co-authored-by: esau <152162806+sklppy88@users.noreply.github.com>
  • Loading branch information
3 people committed Mar 22, 2024
1 parent 4c56536 commit 86a181b
Show file tree
Hide file tree
Showing 61 changed files with 553 additions and 637 deletions.
1 change: 0 additions & 1 deletion l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ library Constants {
uint256 internal constant L1_TO_L2_MSG_SUBTREE_HEIGHT = 4;
uint256 internal constant L1_TO_L2_MSG_SUBTREE_SIBLING_PATH_LENGTH = 12;
uint256 internal constant FUNCTION_SELECTOR_NUM_BYTES = 4;
uint256 internal constant NUM_FIELDS_PER_SHA256 = 1;
uint256 internal constant ARGS_HASH_CHUNK_LENGTH = 32;
uint256 internal constant ARGS_HASH_CHUNK_COUNT = 32;
uint256 internal constant INITIALIZATION_SLOT_SEPARATOR = 1000_000_000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ contract FrontierMerkle is IFrontier {
uint256 level = _computeLevel(index);
bytes32 right = _leaf;
for (uint256 i = 0; i < level; i++) {
right = Hash.sha256ToField(bytes.concat(frontier[i], bytes32(right)));
right = Hash.sha256ToField(bytes.concat(frontier[i], right));
}
frontier[level] = right;

Expand Down
9 changes: 4 additions & 5 deletions noir-projects/aztec-nr/aztec/src/context/private_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ use dep::protocol_types::{
MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL, MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL,
MAX_PUBLIC_DATA_READS_PER_CALL, MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL,
MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, MAX_NULLIFIER_READ_REQUESTS_PER_CALL,
MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_CALL, MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_CALL,
NUM_FIELDS_PER_SHA256, RETURN_VALUES_LENGTH
MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_CALL, MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_CALL, RETURN_VALUES_LENGTH
},
contrakt::{storage_read::StorageRead, storage_update_request::StorageUpdateRequest},
grumpkin_private_key::GrumpkinPrivateKey, hash::hash_args, header::Header,
Expand Down Expand Up @@ -156,8 +155,8 @@ impl PrivateContext {

pub fn finish(self) -> 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_logs_hash = 0;
let unencrypted_logs_hash = 0;
let encrypted_log_preimages_length = 0;
let unencrypted_log_preimages_length = 0;

Expand Down Expand Up @@ -471,7 +470,7 @@ impl PrivateContext {
new_l2_to_l1_msgs: [L2ToL1Message::empty(); MAX_NEW_L2_TO_L1_MSGS_PER_CALL],
start_side_effect_counter: 0,
end_side_effect_counter: 0,
unencrypted_logs_hash: [0; NUM_FIELDS_PER_SHA256],
unencrypted_logs_hash: 0,
unencrypted_log_preimages_length: 0,
historical_header: Header::empty(),
prover_address: AztecAddress::zero(),
Expand Down
11 changes: 5 additions & 6 deletions noir-projects/aztec-nr/aztec/src/context/public_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use dep::protocol_types::{
MAX_NEW_NOTE_HASHES_PER_CALL, MAX_NEW_L2_TO_L1_MSGS_PER_CALL, MAX_NEW_NULLIFIERS_PER_CALL,
MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL, MAX_PUBLIC_DATA_READS_PER_CALL,
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL, MAX_NOTE_HASH_READ_REQUESTS_PER_CALL,
MAX_NULLIFIER_READ_REQUESTS_PER_CALL, MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_CALL,
NUM_FIELDS_PER_SHA256, RETURN_VALUES_LENGTH
MAX_NULLIFIER_READ_REQUESTS_PER_CALL, MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_CALL, RETURN_VALUES_LENGTH
},
contrakt::{storage_read::StorageRead, storage_update_request::StorageUpdateRequest},
hash::hash_args, header::Header, messaging::l2_to_l1_message::L2ToL1Message, utils::reader::Reader
Expand All @@ -39,8 +38,8 @@ struct PublicContext {
new_nullifiers: BoundedVec<SideEffectLinkedToNoteHash, MAX_NEW_NULLIFIERS_PER_CALL>,

new_l2_to_l1_msgs: BoundedVec<L2ToL1Message, MAX_NEW_L2_TO_L1_MSGS_PER_CALL>,
// TODO(Miranda): Remove arrays entirely as NUM_FIELDS_PER_SHA256 = 1
unencrypted_logs_hash: BoundedVec<Field, NUM_FIELDS_PER_SHA256>,

unencrypted_logs_hash: Field,
unencrypted_logs_preimages_length: Field,

// Header of a block whose state is used during public execution. Set by sequencer to be a header of a block
Expand All @@ -64,7 +63,7 @@ impl PublicContext {
new_note_hashes: BoundedVec::new(),
new_nullifiers: BoundedVec::new(),
new_l2_to_l1_msgs: BoundedVec::new(),
unencrypted_logs_hash: BoundedVec::new(),
unencrypted_logs_hash: 0,
unencrypted_logs_preimages_length: 0,
historical_header: inputs.historical_header,
prover_address: AztecAddress::zero() // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165)
Expand Down Expand Up @@ -121,7 +120,7 @@ impl PublicContext {

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

// Compute the public call stack hashes
Expand Down
10 changes: 4 additions & 6 deletions noir-projects/aztec-nr/aztec/src/oracle/logs.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dep::protocol_types::{address::AztecAddress, constants::NUM_FIELDS_PER_SHA256, grumpkin_point::GrumpkinPoint};
use dep::protocol_types::{address::AztecAddress, grumpkin_point::GrumpkinPoint};

// TODO: Should take encrypted data.
#[oracle(emitEncryptedLog)]
Expand All @@ -16,16 +16,14 @@ unconstrained pub fn emit_encrypted_log<N>(
note_type_id: Field,
encryption_pub_key: GrumpkinPoint,
preimage: [Field; N]
) -> [Field; NUM_FIELDS_PER_SHA256] {
[
) -> Field {
emit_encrypted_log_oracle(
contract_address,
storage_slot,
note_type_id,
encryption_pub_key,
preimage
)
]
}

#[oracle(emitUnencryptedLog)]
Expand All @@ -39,7 +37,7 @@ unconstrained pub fn emit_unencrypted_log<T>(
contract_address: AztecAddress,
event_selector: Field,
message: T
) -> [Field; NUM_FIELDS_PER_SHA256] {
) -> Field {
// https://github.com/AztecProtocol/aztec-packages/issues/885
[emit_unencrypted_log_oracle(contract_address, event_selector, message)]
emit_unencrypted_log_oracle(contract_address, event_selector, message)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<N> Sha256MerkleTree<N> {
leaves[2*i],
leaves[2*i+1]
]
)[0];
);
}

// hash the other layers
Expand All @@ -36,7 +36,7 @@ impl<N> Sha256MerkleTree<N> {
nodes[2*i],
nodes[2*i+1]
]
)[0];
);
}

Sha256MerkleTree { leaves, nodes }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ mod tests {
let mut builder = PrivateKernelInitInputsBuilder::new();

// Logs for the private call.
let encrypted_logs_hash = [16];
let encrypted_logs_hash = 16;
let encrypted_log_preimages_length = 100;
let unencrypted_logs_hash = [26];
let unencrypted_logs_hash = 26;
let unencrypted_log_preimages_length = 50;
builder.private_call.set_encrypted_logs(encrypted_logs_hash, encrypted_log_preimages_length);
builder.private_call.set_unencrypted_logs(unencrypted_logs_hash, unencrypted_log_preimages_length);
Expand All @@ -176,10 +176,10 @@ mod tests {
assert_eq(public_inputs.end.unencrypted_log_preimages_length, unencrypted_log_preimages_length);

// Logs hashes should be a sha256 hash of a 0 value (the previous log hash) and the `(un)encrypted_logs_hash` from private input
let expected_encrypted_logs_hash = compute_logs_hash([0], encrypted_logs_hash);
let expected_encrypted_logs_hash = compute_logs_hash(0, encrypted_logs_hash);
assert_eq(public_inputs.end.encrypted_logs_hash, expected_encrypted_logs_hash);

let expected_unencrypted_logs_hash = compute_logs_hash([0], unencrypted_logs_hash);
let expected_unencrypted_logs_hash = compute_logs_hash(0, unencrypted_logs_hash);
assert_eq(public_inputs.end.unencrypted_logs_hash, expected_unencrypted_logs_hash);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,17 +673,17 @@ mod tests {
let mut builder = PrivateKernelInnerInputsBuilder::new();

// Logs for the current call stack.
let encrypted_logs_hash = [16];
let encrypted_logs_hash = 16;
let encrypted_log_preimages_length = 100;
let unencrypted_logs_hash = [26];
let unencrypted_logs_hash = 26;
let unencrypted_log_preimages_length = 50;
builder.private_call.set_encrypted_logs(encrypted_logs_hash, encrypted_log_preimages_length);
builder.private_call.set_unencrypted_logs(unencrypted_logs_hash, unencrypted_log_preimages_length);

// Logs for the previous call stack.
let prev_encrypted_logs_hash = [80];
let prev_encrypted_logs_hash = 80;
let prev_encrypted_log_preimages_length = 13;
let prev_unencrypted_logs_hash = [956];
let prev_unencrypted_logs_hash = 956;
let prev_unencrypted_log_preimages_length = 24;
builder.previous_kernel.set_encrypted_logs(prev_encrypted_logs_hash, prev_encrypted_log_preimages_length);
builder.previous_kernel.set_unencrypted_logs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use dep::types::{
MAX_NEW_L2_TO_L1_MSGS_PER_CALL, MAX_NEW_NOTE_HASHES_PER_CALL, MAX_NEW_NULLIFIERS_PER_CALL,
MAX_NULLIFIER_READ_REQUESTS_PER_CALL, MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_CALL,
MAX_PUBLIC_DATA_READS_PER_TX, MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL,
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, MAX_PUBLIC_DATA_READS_PER_CALL, NUM_FIELDS_PER_SHA256,
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, MAX_PUBLIC_DATA_READS_PER_CALL,
MAX_REVERTIBLE_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
MAX_NON_REVERTIBLE_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX
},
Expand Down Expand Up @@ -433,16 +433,16 @@ fn propagate_new_l2_to_l1_messages(public_call: PublicCallData, public_inputs: &
*/
pub fn accumulate_unencrypted_logs(
public_call: PublicCallData,
previous_unencrypted_logs_hash: [Field; NUM_FIELDS_PER_SHA256],
previous_unencrypted_logs_hash: Field,
previous_unencrypted_log_preimages_length: Field,
public_inputs: &mut PublicKernelCircuitPublicInputsBuilder
) {
let public_call_public_inputs = public_call.call_stack_item.public_inputs;

let current_unencrypted_logs_hash = public_call_public_inputs.unencrypted_logs_hash;
public_inputs.end.unencrypted_logs_hash = accumulate_sha256([
previous_unencrypted_logs_hash[0],
current_unencrypted_logs_hash[0],
previous_unencrypted_logs_hash,
current_unencrypted_logs_hash,
]);

// Add log preimages lengths from current iteration to accumulated lengths
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,14 @@ mod tests {
fn circuit_outputs_should_be_correctly_populated_with_previous_public_kernel_logs() {
let mut builder = PublicKernelAppLogicCircuitPrivateInputsBuilder::new();
// Logs for the current call stack.
let unencrypted_logs_hash = [26];
let unencrypted_logs_hash = 26;
let unencrypted_log_preimages_length = 50;
builder.public_call.set_unencrypted_logs(unencrypted_logs_hash, unencrypted_log_preimages_length);

// Logs for the previous call stack.
let prev_encrypted_logs_hash = [80];
let prev_encrypted_logs_hash = 80;
let prev_encrypted_log_preimages_length = 13;
let prev_unencrypted_logs_hash = [956];
let prev_unencrypted_logs_hash = 956;
let prev_unencrypted_log_preimages_length = 24;
builder.previous_kernel.set_encrypted_logs(prev_encrypted_logs_hash, prev_encrypted_log_preimages_length);
builder.previous_kernel.set_unencrypted_logs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,14 +453,14 @@ mod tests {
let mut builder = PublicKernelSetupCircuitPrivateInputsBuilder::new();

// Logs for the current call stack.
let unencrypted_logs_hash = [26];
let unencrypted_logs_hash = 26;
let unencrypted_log_preimages_length = 50;
builder.public_call.set_unencrypted_logs(unencrypted_logs_hash, unencrypted_log_preimages_length);

// Logs for the previous call stack.
let prev_encrypted_logs_hash = [80];
let prev_encrypted_logs_hash = 80;
let prev_encrypted_log_preimages_length = 13;
let prev_unencrypted_logs_hash = [956];
let prev_unencrypted_logs_hash = 956;
let prev_unencrypted_log_preimages_length = 24;
builder.previous_kernel.set_encrypted_logs(prev_encrypted_logs_hash, prev_encrypted_log_preimages_length);
builder.previous_kernel.set_unencrypted_logs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,14 +343,14 @@ mod tests {
let mut builder = PublicKernelTeardownCircuitPrivateInputsBuilder::new();

// Logs for the current call stack.
let unencrypted_logs_hash = [26];
let unencrypted_logs_hash = 26;
let unencrypted_log_preimages_length = 50;
builder.public_call.set_unencrypted_logs(unencrypted_logs_hash, unencrypted_log_preimages_length);

// Logs for the previous call stack.
let prev_encrypted_logs_hash = [80];
let prev_encrypted_logs_hash = 80;
let prev_encrypted_log_preimages_length = 13;
let prev_unencrypted_logs_hash = [956];
let prev_unencrypted_logs_hash = 956;
let prev_unencrypted_log_preimages_length = 24;
builder.previous_kernel.set_encrypted_logs(prev_encrypted_logs_hash, prev_encrypted_log_preimages_length);
builder.previous_kernel.set_unencrypted_logs(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use dep::types::{
abis::append_only_tree_snapshot::AppendOnlyTreeSnapshot, constants::NUM_FIELDS_PER_SHA256,
abis::append_only_tree_snapshot::AppendOnlyTreeSnapshot,
partial_state_reference::PartialStateReference
};
use crate::abis::constant_rollup_data::ConstantRollupData;
Expand All @@ -26,6 +26,6 @@ struct BaseOrMergeRollupPublicInputs {
// So we want to constrain it when casting these fields to U128

// We hash public inputs to make them constant-sized (to then be unpacked on-chain)
txs_effects_hash : [Field; NUM_FIELDS_PER_SHA256],
out_hash : [Field; NUM_FIELDS_PER_SHA256],
txs_effects_hash : Field,
out_hash : Field,
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use dep::types::{
},
constants::{
NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH, NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH,
PUBLIC_DATA_TREE_HEIGHT, NOTE_HASH_SUBTREE_HEIGHT, NUM_FIELDS_PER_SHA256,
PUBLIC_DATA_TREE_HEIGHT, NOTE_HASH_SUBTREE_HEIGHT,
MAX_NEW_NOTE_HASHES_PER_TX, MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, MAX_PUBLIC_DATA_READS_PER_TX,
MAX_NEW_NULLIFIERS_PER_TX, NUM_ENCRYPTED_LOGS_HASHES_PER_TX, MAX_NEW_L2_TO_L1_MSGS_PER_TX,
NUM_UNENCRYPTED_LOGS_HASHES_PER_TX, NULLIFIER_SUBTREE_HEIGHT, NULLIFIER_TREE_HEIGHT,
Expand Down Expand Up @@ -407,7 +407,7 @@ mod tests {
MAX_NEW_NOTE_HASHES_PER_TX, MAX_NEW_NULLIFIERS_PER_TX, NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH,
NOTE_HASH_TREE_HEIGHT, NOTE_HASH_SUBTREE_HEIGHT, NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH,
NULLIFIER_TREE_HEIGHT, NULLIFIER_SUBTREE_HEIGHT, PUBLIC_DATA_TREE_HEIGHT,
PUBLIC_DATA_SUBTREE_HEIGHT, PUBLIC_DATA_SUBTREE_SIBLING_PATH_LENGTH, NUM_FIELDS_PER_SHA256,
PUBLIC_DATA_SUBTREE_HEIGHT, PUBLIC_DATA_SUBTREE_SIBLING_PATH_LENGTH,
MAX_NEW_L2_TO_L1_MSGS_PER_TX
},
contract_class_id::ContractClassId, partial_state_reference::PartialStateReference,
Expand Down Expand Up @@ -952,10 +952,8 @@ mod tests {

let hash_input_flattened = [0; TX_EFFECTS_HASH_INPUT_FIELDS * 32];
let sha_digest = dep::std::hash::sha256(hash_input_flattened);
let expected_tx_effects_hash = [field_from_bytes_32_trunc(sha_digest)];
for i in 0..NUM_FIELDS_PER_SHA256 {
assert_eq(outputs.txs_effects_hash[i], expected_tx_effects_hash[i]);
}
let expected_tx_effects_hash = field_from_bytes_32_trunc(sha_digest);
assert_eq(outputs.txs_effects_hash, expected_tx_effects_hash);
}

#[test]
Expand All @@ -964,10 +962,8 @@ mod tests {

let hash_input_flattened = [0; MAX_NEW_L2_TO_L1_MSGS_PER_TX * 32];
let sha_digest = dep::std::hash::sha256(hash_input_flattened);
let expected_out_hash = [field_from_bytes_32_trunc(sha_digest)];
for i in 0..NUM_FIELDS_PER_SHA256 {
assert_eq(outputs.out_hash[i], expected_out_hash[i]);
}
let expected_out_hash = field_from_bytes_32_trunc(sha_digest);
assert_eq(outputs.out_hash, expected_out_hash);
}

#[test]
Expand All @@ -980,11 +976,8 @@ mod tests {
let mut hash_input_flattened = [0; MAX_NEW_L2_TO_L1_MSGS_PER_TX * 32];
hash_input_flattened[MAX_NEW_L2_TO_L1_MSGS_PER_TX * 32 - 1] = 123;
let sha_digest = dep::std::hash::sha256(hash_input_flattened);
let expected_out_hash = [field_from_bytes_32_trunc(sha_digest)];

for i in 0..NUM_FIELDS_PER_SHA256 {
assert_eq(out_hash[i], expected_out_hash[i]);
}
let expected_out_hash = field_from_bytes_32_trunc(sha_digest);
assert_eq(out_hash, expected_out_hash);
}

#[test(should_fail_with = "membership check failed")]
Expand Down
Loading

0 comments on commit 86a181b

Please sign in to comment.