Skip to content

Commit

Permalink
chore: use traits in noir-protocol-circuits (#3832)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
Thunkar authored Jan 4, 2024
1 parent 1fe63b2 commit 88fcf8d
Show file tree
Hide file tree
Showing 29 changed files with 335 additions and 199 deletions.
11 changes: 7 additions & 4 deletions yarn-project/aztec-nr/aztec/src/note/note_header.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use dep::protocol_types::address::AztecAddress;
use dep::protocol_types::traits::Empty;

struct NoteHeader {
contract_address: AztecAddress,
Expand All @@ -9,12 +10,14 @@ struct NoteHeader {
is_transient: bool,
}

impl Empty for NoteHeader {
fn empty() -> Self {
NoteHeader { contract_address: AztecAddress::zero(), nonce: 0, storage_slot: 0, is_transient: false }
}
}

impl NoteHeader {
pub fn new(contract_address: AztecAddress, nonce: Field, storage_slot: Field) -> Self {
NoteHeader { contract_address, nonce, storage_slot, is_transient: false }
}

pub fn empty() -> Self {
NoteHeader { contract_address: AztecAddress::zero(), nonce: 0, storage_slot: 0, is_transient: false }
}
}
18 changes: 11 additions & 7 deletions yarn-project/aztec-nr/safe-math/src/safe_u120.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
use dep::std::ops::Eq;

struct SafeU120 {
value: u120,
}

impl Eq for SafeU120 {
fn eq(
self: Self,
other: Self
) -> bool {
self.value == other.value
}
}

impl SafeU120 {
pub fn min() -> Self {
Self {
Expand Down Expand Up @@ -34,13 +45,6 @@ impl SafeU120 {
self.value == 0
}

pub fn eq(
self: Self,
other: Self
) -> bool {
self.value == other.value
}

pub fn lt(self: Self, other: Self) -> bool {
self.value < other.value
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub fn initialize_end_values(
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 = struct_array_to_bounded_vec(start.new_contracts, |ncd: NewContractData| ncd.is_empty(), NewContractData::default());
public_inputs.end.new_contracts = struct_array_to_bounded_vec(start.new_contracts, |ncd: NewContractData| ncd.is_empty(), NewContractData::empty());
}

fn perform_static_call_checks(private_call: PrivateCallData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn initialize_end_values(
circuit_outputs.end.encrypted_logs_hash = start.encrypted_logs_hash;
circuit_outputs.end.encrypted_log_preimages_length = start.encrypted_log_preimages_length;

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

fn perform_static_call_checks(public_call: PublicCallData) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use dep::std::ops::Eq;

struct AppendOnlyTreeSnapshot {
root : Field,
next_available_leaf_index : u32
}

impl AppendOnlyTreeSnapshot{
pub fn eq(self, other : AppendOnlyTreeSnapshot) -> bool {
impl Eq for AppendOnlyTreeSnapshot {
fn eq(self, other : AppendOnlyTreeSnapshot) -> bool {
(self.root == other.root) & (self.next_available_leaf_index == other.next_available_leaf_index)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::abis::global_variables::GlobalVariables;
use crate::abis::append_only_tree_snapshot::AppendOnlyTreeSnapshot;
use dep::std::ops::Eq;

struct ConstantRollupData {
// The very latest roots as at the very beginning of the entire rollup:
Expand All @@ -14,8 +15,8 @@ struct ConstantRollupData {
global_variables : GlobalVariables,
}

impl ConstantRollupData {
pub fn eq(self, other : ConstantRollupData) -> bool {
impl Eq for ConstantRollupData {
fn eq(self, other : ConstantRollupData) -> bool {
self.archive_snapshot.eq(other.archive_snapshot) &
self.global_variables.eq(other.global_variables) &
(self.private_kernel_vk_tree_root == other.private_kernel_vk_tree_root) &
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use dep::types::constants::GENERATOR_INDEX__GLOBAL_VARIABLES;
use dep::std::ops::Eq;
use dep::types::traits::Hash;

struct GlobalVariables {
chain_id : Field,
Expand All @@ -7,9 +9,17 @@ struct GlobalVariables {
timestamp : Field,
}

impl GlobalVariables {
impl Eq for GlobalVariables {
fn eq(self, other : GlobalVariables) -> bool {
(self.chain_id == other.chain_id) &
(self.version == other.version) &
(self.block_number == other.block_number) &
(self.timestamp == other.timestamp)
}
}

pub fn hash(self) -> Field {
impl Hash for GlobalVariables {
fn hash(self) -> Field {
dep::std::hash::pedersen_hash_with_separator([
self.chain_id,
self.version,
Expand All @@ -19,11 +29,4 @@ impl GlobalVariables {
GENERATOR_INDEX__GLOBAL_VARIABLES,
)
}

pub fn eq(self, other : GlobalVariables) -> bool {
(self.chain_id == other.chain_id) &
(self.version == other.version) &
(self.block_number == other.block_number) &
(self.timestamp == other.timestamp)
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
use dep::std::ops::Eq;
use dep::types::traits::{Empty, Hash};

struct PublicDataTreeLeafPreimage {
slot : Field,
value: Field,
next_slot :Field,
next_index : u32,
}

impl PublicDataTreeLeafPreimage {
pub fn default() -> Self {
impl Empty for PublicDataTreeLeafPreimage {
fn empty() -> Self {
Self {
slot: 0,
value: 0,
next_slot: 0,
next_index: 0,
}
}
}

pub fn is_empty(self) -> bool {
(self.slot == 0) & (self.value == 0) & (self.next_slot == 0) & (self.next_index == 0)
}

pub fn hash(self) -> Field {
impl Hash for PublicDataTreeLeafPreimage {
fn hash(self) -> Field {
if self.is_empty() {
0
} else {
Expand All @@ -28,24 +29,34 @@ impl PublicDataTreeLeafPreimage {
}
}

impl PublicDataTreeLeafPreimage {
pub fn is_empty(self) -> bool {
(self.slot == 0) & (self.value == 0) & (self.next_slot == 0) & (self.next_index == 0)
}
}

struct PublicDataTreeLeaf {
slot: Field,
value: Field,
}

impl PublicDataTreeLeaf {
pub fn default() -> Self {
impl Eq for PublicDataTreeLeaf {
fn eq(self, other: Self) -> bool {
(self.slot == other.slot) & (self.value == other.value)
}
}

impl Empty for PublicDataTreeLeaf {
fn empty() -> Self {
Self {
slot: 0,
value: 0,
}
}
}

impl PublicDataTreeLeaf {
pub fn is_empty(self) -> bool {
(self.slot == 0) & (self.value == 0)
}

pub fn eq(self, other: Self) -> bool {
(self.slot == other.slot) & (self.value == other.value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ fn insert_public_data_update_requests(
|write: PublicDataTreeLeaf, low_preimage: PublicDataTreeLeafPreimage| { // Build insertion leaf
let is_update = low_preimage.slot == write.slot;
if is_update {
PublicDataTreeLeafPreimage::default()
PublicDataTreeLeafPreimage::empty()
}else {
PublicDataTreeLeafPreimage {
slot: write.slot,
Expand Down Expand Up @@ -706,7 +706,7 @@ mod tests {
);
}
} else {
sorted_public_data_writes[i] = PublicDataTreeLeaf::default();
sorted_public_data_writes[i] = PublicDataTreeLeaf::empty();
sorted_public_data_writes_indexes[i] = i as u32;
}
}
Expand Down Expand Up @@ -1089,7 +1089,7 @@ mod tests {
};

builder.new_nullifiers.push(NullifierInsertion { existing_index: 0, value: 1 });
let mut tree_nullifiers = [NullifierLeafPreimage::default(); MAX_NEW_NULLIFIERS_PER_TX * 2];
let mut tree_nullifiers = [NullifierLeafPreimage::empty(); MAX_NEW_NULLIFIERS_PER_TX * 2];
tree_nullifiers[0] = NullifierLeafPreimage {
nullifier : 0,
next_nullifier : 1,
Expand Down Expand Up @@ -1139,7 +1139,7 @@ mod tests {
}

let output = builder.execute();
let mut tree_nullifiers = [NullifierLeafPreimage::default(); MAX_NEW_NULLIFIERS_PER_TX * 2];
let mut tree_nullifiers = [NullifierLeafPreimage::empty(); MAX_NEW_NULLIFIERS_PER_TX * 2];
tree_nullifiers[0] = builder.pre_existing_nullifiers[0];

tree_nullifiers[1] = NullifierLeafPreimage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
},
hash::pedersen_hash,
};
use crate::traits::Empty;

// docs:start:block-header
struct BlockHeader {
Expand All @@ -18,6 +19,12 @@ struct BlockHeader {
}
// docs:end:block-header

impl Empty for BlockHeader {
fn empty() -> Self {
BlockHeader::deserialize([0; BLOCK_HEADER_LENGTH])
}
}

impl BlockHeader {
pub fn assert_is_zero(self) {
assert(self.note_hash_tree_root == 0);
Expand Down Expand Up @@ -77,8 +84,4 @@ impl BlockHeader {
self.public_data_tree_root,
], GENERATOR_INDEX__BLOCK_HASH)
}

pub fn empty() -> Self {
BlockHeader::deserialize([0; BLOCK_HEADER_LENGTH])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::{
},
hash::pedersen_hash,
};
use dep::std::ops::Eq;
use crate::traits::Hash;

// docs:start:call-context
struct CallContext {
Expand All @@ -24,6 +26,26 @@ struct CallContext {
}
// docs:end:call-context

impl Eq for CallContext {
fn eq(self, call_context: CallContext) -> bool {
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/3595)
call_context.msg_sender.eq(self.msg_sender)
& call_context.storage_contract_address.eq(self.storage_contract_address)
& call_context.portal_contract_address.eq(self.portal_contract_address)
& call_context.function_selector.eq(self.function_selector)
& (call_context.is_delegate_call == self.is_delegate_call)
& (call_context.is_static_call == self.is_static_call)
& (call_context.is_contract_deployment == self.is_contract_deployment)
& (call_context.start_side_effect_counter == self.start_side_effect_counter)
}
}

impl Hash for CallContext {
fn hash(self) -> Field {
pedersen_hash(self.serialize(), GENERATOR_INDEX__CALL_CONTEXT)
}
}

impl CallContext {
fn serialize(self) -> [Field; CALL_CONTEXT_LENGTH] {
[
Expand All @@ -38,10 +60,6 @@ impl CallContext {
]
}

fn hash(self) -> Field {
pedersen_hash(self.serialize(), GENERATOR_INDEX__CALL_CONTEXT)
}

fn assert_is_zero(self) {
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/3595)
assert(self.msg_sender.to_field() == 0);
Expand All @@ -53,16 +71,4 @@ impl CallContext {
assert(self.is_contract_deployment == false);
assert(self.start_side_effect_counter == 0);
}

fn eq(self, call_context: CallContext) -> bool {
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/3595)
call_context.msg_sender.eq(self.msg_sender)
& call_context.storage_contract_address.eq(self.storage_contract_address)
& call_context.portal_contract_address.eq(self.portal_contract_address)
& call_context.function_selector.eq(self.function_selector)
& (call_context.is_delegate_call == self.is_delegate_call)
& (call_context.is_static_call == self.is_static_call)
& (call_context.is_contract_deployment == self.is_contract_deployment)
& (call_context.start_side_effect_counter == self.start_side_effect_counter)
}
}
Loading

0 comments on commit 88fcf8d

Please sign in to comment.