Skip to content

Commit 88fcf8d

Browse files
authored
chore: use traits in noir-protocol-circuits (AztecProtocol#3832)
Simple use of traits in `noir-protocol-circuits` and `aztec-nr` when possible. Missing: Serialize/Deserialize due to: noir-lang/noir#3471 Renamed ::default() to ::empty(), since it better conveys meaning for our use cases (open to discussion).
1 parent 1fe63b2 commit 88fcf8d

29 files changed

+335
-199
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use dep::protocol_types::address::AztecAddress;
2+
use dep::protocol_types::traits::Empty;
23

34
struct NoteHeader {
45
contract_address: AztecAddress,
@@ -9,12 +10,14 @@ struct NoteHeader {
910
is_transient: bool,
1011
}
1112

13+
impl Empty for NoteHeader {
14+
fn empty() -> Self {
15+
NoteHeader { contract_address: AztecAddress::zero(), nonce: 0, storage_slot: 0, is_transient: false }
16+
}
17+
}
18+
1219
impl NoteHeader {
1320
pub fn new(contract_address: AztecAddress, nonce: Field, storage_slot: Field) -> Self {
1421
NoteHeader { contract_address, nonce, storage_slot, is_transient: false }
1522
}
16-
17-
pub fn empty() -> Self {
18-
NoteHeader { contract_address: AztecAddress::zero(), nonce: 0, storage_slot: 0, is_transient: false }
19-
}
2023
}

yarn-project/aztec-nr/safe-math/src/safe_u120.nr

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1+
use dep::std::ops::Eq;
2+
13
struct SafeU120 {
24
value: u120,
35
}
46

7+
impl Eq for SafeU120 {
8+
fn eq(
9+
self: Self,
10+
other: Self
11+
) -> bool {
12+
self.value == other.value
13+
}
14+
}
15+
516
impl SafeU120 {
617
pub fn min() -> Self {
718
Self {
@@ -34,13 +45,6 @@ impl SafeU120 {
3445
self.value == 0
3546
}
3647

37-
pub fn eq(
38-
self: Self,
39-
other: Self
40-
) -> bool {
41-
self.value == other.value
42-
}
43-
4448
pub fn lt(self: Self, other: Self) -> bool {
4549
self.value < other.value
4650
}

yarn-project/noir-protocol-circuits/src/crates/private-kernel-lib/src/common.nr

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ pub fn initialize_end_values(
130130
public_inputs.end.unencrypted_log_preimages_length = start.unencrypted_log_preimages_length;
131131

132132
public_inputs.end.optionally_revealed_data = start.optionally_revealed_data;
133-
public_inputs.end.new_contracts = struct_array_to_bounded_vec(start.new_contracts, |ncd: NewContractData| ncd.is_empty(), NewContractData::default());
133+
public_inputs.end.new_contracts = struct_array_to_bounded_vec(start.new_contracts, |ncd: NewContractData| ncd.is_empty(), NewContractData::empty());
134134
}
135135

136136
fn perform_static_call_checks(private_call: PrivateCallData) {

yarn-project/noir-protocol-circuits/src/crates/public-kernel-lib/src/common.nr

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub fn initialize_end_values(
8282
circuit_outputs.end.encrypted_logs_hash = start.encrypted_logs_hash;
8383
circuit_outputs.end.encrypted_log_preimages_length = start.encrypted_log_preimages_length;
8484

85-
circuit_outputs.end.new_contracts = struct_array_to_bounded_vec(previous_kernel.public_inputs.end.new_contracts, |ncd: NewContractData| ncd.is_empty(), NewContractData::default());
85+
circuit_outputs.end.new_contracts = struct_array_to_bounded_vec(previous_kernel.public_inputs.end.new_contracts, |ncd: NewContractData| ncd.is_empty(), NewContractData::empty());
8686
}
8787

8888
fn perform_static_call_checks(public_call: PublicCallData) {
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use dep::std::ops::Eq;
2+
13
struct AppendOnlyTreeSnapshot {
24
root : Field,
35
next_available_leaf_index : u32
46
}
57

6-
impl AppendOnlyTreeSnapshot{
7-
pub fn eq(self, other : AppendOnlyTreeSnapshot) -> bool {
8+
impl Eq for AppendOnlyTreeSnapshot {
9+
fn eq(self, other : AppendOnlyTreeSnapshot) -> bool {
810
(self.root == other.root) & (self.next_available_leaf_index == other.next_available_leaf_index)
911
}
1012
}

yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/constant_rollup_data.nr

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::abis::global_variables::GlobalVariables;
22
use crate::abis::append_only_tree_snapshot::AppendOnlyTreeSnapshot;
3+
use dep::std::ops::Eq;
34

45
struct ConstantRollupData {
56
// The very latest roots as at the very beginning of the entire rollup:
@@ -14,8 +15,8 @@ struct ConstantRollupData {
1415
global_variables : GlobalVariables,
1516
}
1617

17-
impl ConstantRollupData {
18-
pub fn eq(self, other : ConstantRollupData) -> bool {
18+
impl Eq for ConstantRollupData {
19+
fn eq(self, other : ConstantRollupData) -> bool {
1920
self.archive_snapshot.eq(other.archive_snapshot) &
2021
self.global_variables.eq(other.global_variables) &
2122
(self.private_kernel_vk_tree_root == other.private_kernel_vk_tree_root) &
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use dep::types::constants::GENERATOR_INDEX__GLOBAL_VARIABLES;
2+
use dep::std::ops::Eq;
3+
use dep::types::traits::Hash;
24

35
struct GlobalVariables {
46
chain_id : Field,
@@ -7,9 +9,17 @@ struct GlobalVariables {
79
timestamp : Field,
810
}
911

10-
impl GlobalVariables {
12+
impl Eq for GlobalVariables {
13+
fn eq(self, other : GlobalVariables) -> bool {
14+
(self.chain_id == other.chain_id) &
15+
(self.version == other.version) &
16+
(self.block_number == other.block_number) &
17+
(self.timestamp == other.timestamp)
18+
}
19+
}
1120

12-
pub fn hash(self) -> Field {
21+
impl Hash for GlobalVariables {
22+
fn hash(self) -> Field {
1323
dep::std::hash::pedersen_hash_with_separator([
1424
self.chain_id,
1525
self.version,
@@ -19,11 +29,4 @@ impl GlobalVariables {
1929
GENERATOR_INDEX__GLOBAL_VARIABLES,
2030
)
2131
}
22-
23-
pub fn eq(self, other : GlobalVariables) -> bool {
24-
(self.chain_id == other.chain_id) &
25-
(self.version == other.version) &
26-
(self.block_number == other.block_number) &
27-
(self.timestamp == other.timestamp)
28-
}
2932
}
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1+
use dep::std::ops::Eq;
2+
use dep::types::traits::{Empty, Hash};
3+
14
struct PublicDataTreeLeafPreimage {
25
slot : Field,
36
value: Field,
47
next_slot :Field,
58
next_index : u32,
69
}
710

8-
impl PublicDataTreeLeafPreimage {
9-
pub fn default() -> Self {
11+
impl Empty for PublicDataTreeLeafPreimage {
12+
fn empty() -> Self {
1013
Self {
1114
slot: 0,
1215
value: 0,
1316
next_slot: 0,
1417
next_index: 0,
1518
}
1619
}
20+
}
1721

18-
pub fn is_empty(self) -> bool {
19-
(self.slot == 0) & (self.value == 0) & (self.next_slot == 0) & (self.next_index == 0)
20-
}
21-
22-
pub fn hash(self) -> Field {
22+
impl Hash for PublicDataTreeLeafPreimage {
23+
fn hash(self) -> Field {
2324
if self.is_empty() {
2425
0
2526
} else {
@@ -28,24 +29,34 @@ impl PublicDataTreeLeafPreimage {
2829
}
2930
}
3031

32+
impl PublicDataTreeLeafPreimage {
33+
pub fn is_empty(self) -> bool {
34+
(self.slot == 0) & (self.value == 0) & (self.next_slot == 0) & (self.next_index == 0)
35+
}
36+
}
37+
3138
struct PublicDataTreeLeaf {
3239
slot: Field,
3340
value: Field,
3441
}
3542

36-
impl PublicDataTreeLeaf {
37-
pub fn default() -> Self {
43+
impl Eq for PublicDataTreeLeaf {
44+
fn eq(self, other: Self) -> bool {
45+
(self.slot == other.slot) & (self.value == other.value)
46+
}
47+
}
48+
49+
impl Empty for PublicDataTreeLeaf {
50+
fn empty() -> Self {
3851
Self {
3952
slot: 0,
4053
value: 0,
4154
}
4255
}
56+
}
4357

58+
impl PublicDataTreeLeaf {
4459
pub fn is_empty(self) -> bool {
4560
(self.slot == 0) & (self.value == 0)
4661
}
47-
48-
pub fn eq(self, other: Self) -> bool {
49-
(self.slot == other.slot) & (self.value == other.value)
50-
}
5162
}

yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/base/base_rollup_inputs.nr

+4-4
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ fn insert_public_data_update_requests(
438438
|write: PublicDataTreeLeaf, low_preimage: PublicDataTreeLeafPreimage| { // Build insertion leaf
439439
let is_update = low_preimage.slot == write.slot;
440440
if is_update {
441-
PublicDataTreeLeafPreimage::default()
441+
PublicDataTreeLeafPreimage::empty()
442442
}else {
443443
PublicDataTreeLeafPreimage {
444444
slot: write.slot,
@@ -706,7 +706,7 @@ mod tests {
706706
);
707707
}
708708
} else {
709-
sorted_public_data_writes[i] = PublicDataTreeLeaf::default();
709+
sorted_public_data_writes[i] = PublicDataTreeLeaf::empty();
710710
sorted_public_data_writes_indexes[i] = i as u32;
711711
}
712712
}
@@ -1089,7 +1089,7 @@ mod tests {
10891089
};
10901090

10911091
builder.new_nullifiers.push(NullifierInsertion { existing_index: 0, value: 1 });
1092-
let mut tree_nullifiers = [NullifierLeafPreimage::default(); MAX_NEW_NULLIFIERS_PER_TX * 2];
1092+
let mut tree_nullifiers = [NullifierLeafPreimage::empty(); MAX_NEW_NULLIFIERS_PER_TX * 2];
10931093
tree_nullifiers[0] = NullifierLeafPreimage {
10941094
nullifier : 0,
10951095
next_nullifier : 1,
@@ -1139,7 +1139,7 @@ mod tests {
11391139
}
11401140

11411141
let output = builder.execute();
1142-
let mut tree_nullifiers = [NullifierLeafPreimage::default(); MAX_NEW_NULLIFIERS_PER_TX * 2];
1142+
let mut tree_nullifiers = [NullifierLeafPreimage::empty(); MAX_NEW_NULLIFIERS_PER_TX * 2];
11431143
tree_nullifiers[0] = builder.pre_existing_nullifiers[0];
11441144

11451145
tree_nullifiers[1] = NullifierLeafPreimage {

yarn-project/noir-protocol-circuits/src/crates/types/src/abis/block_header.nr

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
},
66
hash::pedersen_hash,
77
};
8+
use crate::traits::Empty;
89

910
// docs:start:block-header
1011
struct BlockHeader {
@@ -18,6 +19,12 @@ struct BlockHeader {
1819
}
1920
// docs:end:block-header
2021

22+
impl Empty for BlockHeader {
23+
fn empty() -> Self {
24+
BlockHeader::deserialize([0; BLOCK_HEADER_LENGTH])
25+
}
26+
}
27+
2128
impl BlockHeader {
2229
pub fn assert_is_zero(self) {
2330
assert(self.note_hash_tree_root == 0);
@@ -77,8 +84,4 @@ impl BlockHeader {
7784
self.public_data_tree_root,
7885
], GENERATOR_INDEX__BLOCK_HASH)
7986
}
80-
81-
pub fn empty() -> Self {
82-
BlockHeader::deserialize([0; BLOCK_HEADER_LENGTH])
83-
}
8487
}

yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_context.nr

+22-16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use crate::{
77
},
88
hash::pedersen_hash,
99
};
10+
use dep::std::ops::Eq;
11+
use crate::traits::Hash;
1012

1113
// docs:start:call-context
1214
struct CallContext {
@@ -24,6 +26,26 @@ struct CallContext {
2426
}
2527
// docs:end:call-context
2628

29+
impl Eq for CallContext {
30+
fn eq(self, call_context: CallContext) -> bool {
31+
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/3595)
32+
call_context.msg_sender.eq(self.msg_sender)
33+
& call_context.storage_contract_address.eq(self.storage_contract_address)
34+
& call_context.portal_contract_address.eq(self.portal_contract_address)
35+
& call_context.function_selector.eq(self.function_selector)
36+
& (call_context.is_delegate_call == self.is_delegate_call)
37+
& (call_context.is_static_call == self.is_static_call)
38+
& (call_context.is_contract_deployment == self.is_contract_deployment)
39+
& (call_context.start_side_effect_counter == self.start_side_effect_counter)
40+
}
41+
}
42+
43+
impl Hash for CallContext {
44+
fn hash(self) -> Field {
45+
pedersen_hash(self.serialize(), GENERATOR_INDEX__CALL_CONTEXT)
46+
}
47+
}
48+
2749
impl CallContext {
2850
fn serialize(self) -> [Field; CALL_CONTEXT_LENGTH] {
2951
[
@@ -38,10 +60,6 @@ impl CallContext {
3860
]
3961
}
4062

41-
fn hash(self) -> Field {
42-
pedersen_hash(self.serialize(), GENERATOR_INDEX__CALL_CONTEXT)
43-
}
44-
4563
fn assert_is_zero(self) {
4664
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/3595)
4765
assert(self.msg_sender.to_field() == 0);
@@ -53,16 +71,4 @@ impl CallContext {
5371
assert(self.is_contract_deployment == false);
5472
assert(self.start_side_effect_counter == 0);
5573
}
56-
57-
fn eq(self, call_context: CallContext) -> bool {
58-
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/3595)
59-
call_context.msg_sender.eq(self.msg_sender)
60-
& call_context.storage_contract_address.eq(self.storage_contract_address)
61-
& call_context.portal_contract_address.eq(self.portal_contract_address)
62-
& call_context.function_selector.eq(self.function_selector)
63-
& (call_context.is_delegate_call == self.is_delegate_call)
64-
& (call_context.is_static_call == self.is_static_call)
65-
& (call_context.is_contract_deployment == self.is_contract_deployment)
66-
& (call_context.start_side_effect_counter == self.start_side_effect_counter)
67-
}
6874
}

0 commit comments

Comments
 (0)