From 7a62c2f9dfc35080a3051c518fa63c26f86977d7 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Mon, 4 Mar 2024 16:33:24 +0000 Subject: [PATCH] chore: remove unnecessary casts (#4906) --- .../contracts/card_game_contract/src/cards.nr | 2 +- .../src/private_kernel_tail.nr | 8 ++++---- .../rollup-lib/src/base/base_rollup_inputs.nr | 11 ++++------- .../types/src/tests/kernel_data_builder.nr | 14 +++++++------- .../crates/types/src/tests/merkle_tree_utils.nr | 6 +++--- .../types/src/tests/public_call_data_builder.nr | 16 ++++++++-------- .../crates/types/src/utils/arrays.nr | 2 +- .../crates/types/src/utils/field.nr | 2 +- 8 files changed, 29 insertions(+), 32 deletions(-) diff --git a/noir-projects/noir-contracts/contracts/card_game_contract/src/cards.nr b/noir-projects/noir-contracts/contracts/card_game_contract/src/cards.nr index 59208aa986e..4f66b724ff6 100644 --- a/noir-projects/noir-contracts/contracts/card_game_contract/src/cards.nr +++ b/noir-projects/noir-contracts/contracts/card_game_contract/src/cards.nr @@ -169,7 +169,7 @@ pub fn get_pack_cards(seed: Field, owner: AztecAddress, context: &mut PrivateCon let mut cards = [Card::from_field(0); PACK_CARDS]; // we generate PACK_CARDS cards - assert((PACK_CARDS as u64) < 8, "Cannot generate more than 8 cards"); + assert(PACK_CARDS < 8, "Cannot generate more than 8 cards"); for i in 0..PACK_CARDS { let strength = (random_bytes[i] as u32) + (random_bytes[i + 1] as u32) * 256; let points = (random_bytes[i + 2] as u32) + (random_bytes[i + 3] as u32) * 256; diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_tail.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_tail.nr index 1787a725bc3..a5d939506c9 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_tail.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_tail.nr @@ -94,7 +94,7 @@ impl PrivateKernelTailCircuitPrivateInputs { // match reads to commitments from the previous call(s) for rr_idx in 0..MAX_NOTE_HASH_READ_REQUESTS_PER_TX { let read_request = read_requests.get_unchecked(rr_idx); - let read_commitment_hint = self.read_commitment_hints[rr_idx] as u64; + let read_commitment_hint = self.read_commitment_hints[rr_idx]; if (read_request.value != 0) { let hash = new_note_hashes.get_unchecked(read_commitment_hint); @@ -160,7 +160,7 @@ impl PrivateKernelTailCircuitPrivateInputs { let nullifier = new_nullifiers[n_idx]; // TODO - should not be able to squash the first nullifier. let nullified_note_hash = nullifier.note_hash; - let hint_pos = self.nullifier_commitment_hints[n_idx] as u64; + let hint_pos = self.nullifier_commitment_hints[n_idx]; // Nullified_commitment of value `0` implies non-transient (persistable) // nullifier in which case no attempt will be made to match it to a hash. @@ -168,7 +168,7 @@ impl PrivateKernelTailCircuitPrivateInputs { // 0-valued nullified_note_hash is empty and will be ignored if nullified_note_hash != 0 { assert( - hint_pos < MAX_NEW_NOTE_HASHES_PER_TX as u64, "New nullifier is transient but hint is invalid" + hint_pos < MAX_NEW_NOTE_HASHES_PER_TX, "New nullifier is transient but hint is invalid" ); let hash = new_note_hashes[hint_pos]; assert_eq(nullified_note_hash, hash.value, "Hinted hash does not match"); @@ -179,7 +179,7 @@ impl PrivateKernelTailCircuitPrivateInputs { // squash both the nullifier and the hash // (set to 0 here and then rearrange array after loop) new_note_hashes[hint_pos] = SideEffect::empty(); - new_nullifiers[n_idx as u64] = SideEffectLinkedToNoteHash::empty(); + new_nullifiers[n_idx] = SideEffectLinkedToNoteHash::empty(); } // non-transient (persistable) nullifiers are just kept in new_nullifiers array and forwarded // to public inputs (used later by base rollup circuit) diff --git a/noir-projects/noir-protocol-circuits/crates/rollup-lib/src/base/base_rollup_inputs.nr b/noir-projects/noir-protocol-circuits/crates/rollup-lib/src/base/base_rollup_inputs.nr index fd70d6535b4..d239cc82b2d 100644 --- a/noir-projects/noir-protocol-circuits/crates/rollup-lib/src/base/base_rollup_inputs.nr +++ b/noir-projects/noir-protocol-circuits/crates/rollup-lib/src/base/base_rollup_inputs.nr @@ -488,7 +488,7 @@ fn validate_public_data_reads( #[test] fn consistent_not_hash_subtree_width() { assert_eq( - MAX_NEW_NOTE_HASHES_PER_TX, 2.pow_32(NOTE_HASH_SUBTREE_HEIGHT as Field) as u64, "note hash subtree width is incorrect" + MAX_NEW_NOTE_HASHES_PER_TX as Field, 2.pow_32(NOTE_HASH_SUBTREE_HEIGHT as Field), "note hash subtree width is incorrect" ); } @@ -644,7 +644,7 @@ mod tests { ); for i in 0..MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX { - if (i as u64) < MAX_PUBLIC_DATA_WRITES_PER_TEST { + if i < MAX_PUBLIC_DATA_WRITES_PER_TEST { let (low_leaf_index, leaf): (u64, PublicDataTreeLeaf) = sorted_write_tuples[i].value; sorted_public_data_writes[i] = leaf; @@ -765,7 +765,7 @@ mod tests { let mut pre_existing_nullifiers = self.pre_existing_nullifiers; for i in 0..MAX_NEW_NULLIFIERS_PER_TEST { - if (i as u64) < (self.new_nullifiers.len()) { + if i < self.new_nullifiers.len() { let sorted_tuple = sorted_new_nullifier_tuples[i]; let new_nullifier = sorted_tuple.value; let original_index = sorted_tuple.original_index; @@ -1034,10 +1034,7 @@ mod tests { assert(outputs.start.note_hash_tree.eq(expected_start_note_hash_tree_snapshot)); for i in 0..new_note_hashes.len() { - expected_commitments_tree.update_leaf( - (i as u64) + (MAX_NEW_NOTE_HASHES_PER_TX as u64), - new_note_hashes[i] - ); + expected_commitments_tree.update_leaf(i + MAX_NEW_NOTE_HASHES_PER_TX, new_note_hashes[i]); } let expected_end_note_hash_tree_snapshot = AppendOnlyTreeSnapshot { root: expected_commitments_tree.get_root(), diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/tests/kernel_data_builder.nr b/noir-projects/noir-protocol-circuits/crates/types/src/tests/kernel_data_builder.nr index a4126dff9b7..357248be829 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/tests/kernel_data_builder.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/tests/kernel_data_builder.nr @@ -78,10 +78,10 @@ impl PreviousKernelDataBuilder { *self } - pub fn append_public_data_update_requests(&mut self, num_updates: Field) { + pub fn append_public_data_update_requests(&mut self, num_updates: u64) { let value_offset = self.end.public_data_update_requests.len(); for i in 0..MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX { - if i as u64 < num_updates as u64 { + if i < num_updates { let update_request = PublicDataUpdateRequest { // The default leaf index is its index + 23. leaf_slot: (value_offset + i + 23) as Field, @@ -93,10 +93,10 @@ impl PreviousKernelDataBuilder { } } - pub fn append_public_data_read_requests(&mut self, num_reads: Field) { + pub fn append_public_data_read_requests(&mut self, num_reads: u64) { let value_offset = self.end.public_data_reads.len(); for i in 0..MAX_PUBLIC_DATA_READS_PER_TX { - if i as u64 < num_reads as u64 { + if i < num_reads { let read_request = PublicDataRead { // The default leaf index is its index + 34. leaf_slot: (value_offset + i + 34) as Field, @@ -145,7 +145,7 @@ impl PreviousKernelDataBuilder { // so the tx nullifier is in `end` let index_offset = self.end.new_nullifiers.len(); for i in 0..MAX_NEW_NULLIFIERS_PER_TX { - if i as u64 < num_extra_nullifier as u64 { + if i < num_extra_nullifier { let mock_value = self.get_mock_nullifier_value(index_offset + i); self.end.new_nullifiers.push( SideEffectLinkedToNoteHash { @@ -161,7 +161,7 @@ impl PreviousKernelDataBuilder { pub fn append_new_nullifiers_from_public(&mut self, num_extra_nullifier: u64) { let index_offset = self.end.new_nullifiers.len(); for i in 1..MAX_NEW_NULLIFIERS_PER_TX { - if i as u64 <= num_extra_nullifier as u64 { + if i <= num_extra_nullifier { self.end.new_nullifiers.push( SideEffectLinkedToNoteHash { value: self.get_mock_nullifier_value(index_offset + i), @@ -176,7 +176,7 @@ impl PreviousKernelDataBuilder { pub fn append_new_nullifiers_non_revertible(&mut self, num_extra_nullifier: u64) { let index_offset = self.end_non_revertible.new_nullifiers.len(); for i in 1..MAX_NON_REVERTIBLE_NULLIFIERS_PER_TX { - if i as u64 <= num_extra_nullifier as u64 { + if i <= num_extra_nullifier { self.end_non_revertible.new_nullifiers.push( SideEffectLinkedToNoteHash { value: self.get_mock_nullifier_value(index_offset + i), diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/tests/merkle_tree_utils.nr b/noir-projects/noir-protocol-circuits/crates/types/src/tests/merkle_tree_utils.nr index 5857980ea7b..b2edc7ee736 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/tests/merkle_tree_utils.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/tests/merkle_tree_utils.nr @@ -105,8 +105,8 @@ impl NonEmptyMerkl pub fn get_sibling_path(self, leaf_index: u64) -> [Field; TREE_HEIGHT] { let mut path = [0; TREE_HEIGHT]; - let mut current_index = leaf_index as u64; - let mut subtree_width = SUBTREE_ITEMS as u64; + let mut current_index = leaf_index; + let mut subtree_width = SUBTREE_ITEMS; let mut sibling_index = MerkleTree::sibling_index(current_index); @@ -139,7 +139,7 @@ impl NonEmptyMerkl } pub fn update_leaf(&mut self, index: u64, value: Field) { - assert(index < SUBTREE_ITEMS as u64, "index must be less than the number of leaves in the subtree"); + assert(index < SUBTREE_ITEMS, "index must be less than the number of leaves in the subtree"); self.subtree.update_leaf(index, value, [0; SUBTREE_HEIGHT]); diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/tests/public_call_data_builder.nr b/noir-projects/noir-protocol-circuits/crates/types/src/tests/public_call_data_builder.nr index d12da8ff1b6..9350c46ce10 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/tests/public_call_data_builder.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/tests/public_call_data_builder.nr @@ -111,10 +111,10 @@ impl PublicCallDataBuilder { } } - pub fn append_public_data_read_requests(&mut self, num_reads: Field) { + pub fn append_public_data_read_requests(&mut self, num_reads: u64) { let value_offset = self.public_inputs.contract_storage_reads.len(); for i in 0..MAX_PUBLIC_DATA_READS_PER_CALL { - if i as u64 < num_reads as u64 { + if i < num_reads { let read_request = StorageRead { // The default storage slot is its index + 1. storage_slot: (value_offset + i + 1) as Field, @@ -126,18 +126,18 @@ impl PublicCallDataBuilder { } } - pub fn append_empty_public_data_read_requests(&mut self, num_reads: Field) { + pub fn append_empty_public_data_read_requests(&mut self, num_reads: u64) { for i in 0..MAX_PUBLIC_DATA_READS_PER_CALL { - if i as u64 < num_reads as u64 { + if i < num_reads { self.public_inputs.contract_storage_reads.push(StorageRead::empty()); } } } - pub fn append_update_requests(&mut self, num_updates: Field) { + pub fn append_update_requests(&mut self, num_updates: u64) { let value_offset = self.public_inputs.contract_storage_update_requests.len(); for i in 0..MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL { - if i as u64 < num_updates as u64 { + if i < num_updates { let update_request = StorageUpdateRequest { // The default storage slot is its index + 1. storage_slot: (value_offset + i + 1) as Field, @@ -149,9 +149,9 @@ impl PublicCallDataBuilder { } } - pub fn append_empty_update_requests(&mut self, num_updates: Field) { + pub fn append_empty_update_requests(&mut self, num_updates: u64) { for i in 0..MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL { - if i as u64 < num_updates as u64 { + if i < num_updates { self.public_inputs.contract_storage_update_requests.push(StorageUpdateRequest::empty()); } } diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/utils/arrays.nr b/noir-projects/noir-protocol-circuits/crates/types/src/utils/arrays.nr index 4cd8dd3e196..f9ead080ee0 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/utils/arrays.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/utils/arrays.nr @@ -30,7 +30,7 @@ pub fn validate_array(array: [T; N]) where T: Empty + Eq { first_zero_pos = i; } } - assert((last_non_zero_pos as u64) <= (first_zero_pos as u64), "invalid array"); + assert(last_non_zero_pos <= first_zero_pos, "invalid array"); } // Helper method to determine the number of non-zero/empty elements in a validated array (ie, validate_array(array) diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/utils/field.nr b/noir-projects/noir-protocol-circuits/crates/types/src/utils/field.nr index d5033666f1e..68dbc41e81a 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/utils/field.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/utils/field.nr @@ -1,5 +1,5 @@ pub fn field_from_bytes(bytes: [u8; N], big_endian: bool) -> Field { - assert(bytes.len() as u32 < 32, "field_from_bytes: N must be less than 32"); + assert(bytes.len() < 32, "field_from_bytes: N must be less than 32"); let mut as_field = 0; let mut offset = 1; for i in 0..N {