From fb480cab07e9c273189a60ac90348e510f1a883c Mon Sep 17 00:00:00 2001 From: thunkar Date: Thu, 4 Jan 2024 12:04:13 +0000 Subject: [PATCH 1/5] Eq trait for protocol-circuits --- noir/noir_stdlib/src/ec/montcurve.nr | 8 +++-- noir/noir_stdlib/src/ec/swcurve.nr | 36 +++++++++++-------- noir/noir_stdlib/src/ec/tecurve.nr | 34 ++++++++++-------- noir/noir_stdlib/src/ops.nr | 1 - .../src/abis/append_only_tree_snapshot.nr | 6 ++-- .../src/abis/constant_rollup_data.nr | 5 +-- .../rollup-lib/src/abis/global_variables.nr | 18 +++++----- .../src/crates/types/src/abis/call_context.nr | 27 +++++++------- .../src/crates/types/src/abis/call_request.nr | 19 +++++----- .../types/src/abis/function_selector.nr | 11 +++--- .../crates/types/src/abis/public_data_read.nr | 11 +++--- .../src/crates/types/src/abis/side_effect.nr | 27 ++++++++------ .../src/crates/types/src/address.nr | 21 ++++++----- .../src/contrakt/storage_update_request.nr | 17 +++++---- 14 files changed, 142 insertions(+), 99 deletions(-) diff --git a/noir/noir_stdlib/src/ec/montcurve.nr b/noir/noir_stdlib/src/ec/montcurve.nr index 82d22837b463..79bc16ff2a23 100644 --- a/noir/noir_stdlib/src/ec/montcurve.nr +++ b/noir/noir_stdlib/src/ec/montcurve.nr @@ -12,6 +12,7 @@ mod affine { use crate::ec::safe_inverse; use crate::ec::sqrt; use crate::ec::ZETA; + use crate::ops::Eq; // Curve specification struct Curve { // Montgomery Curve configuration (ky^2 = x^3 + j*x^2 + x) j: Field, @@ -26,6 +27,9 @@ mod affine { infty: bool // Indicator for point at infinity } + // Check for equality + impl Eq for Point { fn eq(self, p: Self) -> bool { (self.infty & p.infty) | (!self.infty & !p.infty & (self.x == p.x) & (self.y == p.y))}} + impl Point { // Point constructor pub fn new(x: Field, y: Field) -> Self { @@ -33,9 +37,7 @@ mod affine { } // Check for equality - fn eq(self, p: Self) -> bool { - (self.infty & p.infty) | (!self.infty & !p.infty & (self.x == p.x) & (self.y == p.y)) - } + // Check if zero pub fn is_zero(self) -> bool { diff --git a/noir/noir_stdlib/src/ec/swcurve.nr b/noir/noir_stdlib/src/ec/swcurve.nr index e9b6f661843e..c02323ff2eef 100644 --- a/noir/noir_stdlib/src/ec/swcurve.nr +++ b/noir/noir_stdlib/src/ec/swcurve.nr @@ -7,6 +7,7 @@ mod affine { use crate::ec::safe_inverse; use crate::ec::is_square; use crate::ec::sqrt; + use crate::ops::Eq; // Curve specification struct Curve { // Short Weierstraß curve // Coefficients in defining equation y^2 = x^3 + ax + b @@ -22,20 +23,22 @@ mod affine { infty: bool // Indicator for point at infinity } - impl Point { - // Point constructor - pub fn new(x: Field, y: Field) -> Self { - Self {x, y, infty: false} - } - - // Check for equality - fn eq(self, p: Point) -> bool { + // Check for equality + impl Eq for Point { + fn eq(self, p: Self) -> bool { let Self {x: x1, y: y1, infty: inf1} = self; let Self {x: x2, y: y2, infty: inf2} = p; (inf1 & inf2) | (!inf1 & !inf2 & (x1 == x2) & (y1 == y2)) } + } + + impl Point { + // Point constructor + pub fn new(x: Field, y: Field) -> Self { + Self {x, y, infty: false} + } // Check if zero pub fn is_zero(self) -> bool { @@ -182,6 +185,7 @@ mod curvegroup { // Points are represented by three-dimensional Jacobian coordinates. // See for details. use crate::ec::swcurve::affine; + use crate::ops::Eq; // Curve specification struct Curve { // Short Weierstraß curve // Coefficients in defining equation y^2 = x^3 + axz^4 + bz^6 @@ -197,20 +201,22 @@ mod curvegroup { z: Field // z = 0 corresponds to point at infinity. } - impl Point { - // Point constructor - pub fn new(x: Field, y: Field, z: Field) -> Self { - Self {x, y, z} - } - - // Check for equality + // Check for equality + impl Eq for Point { fn eq(self, p: Point) -> bool { let Self {x: x1, y: y1, z: z1} = self; let Self {x: x2, y: y2, z: z2} = p; ((z1 == 0) & (z2 == 0)) | ((z1 != 0) & (z2 != 0) & (x1*z2*z2 == x2*z1*z1) & (y1*z2*z2*z2 == y2*z1*z1*z1)) } + } + impl Point { + // Point constructor + pub fn new(x: Field, y: Field, z: Field) -> Self { + Self {x, y, z} + } + // Check if zero pub fn is_zero(self) -> bool { self.eq(Point::zero()) diff --git a/noir/noir_stdlib/src/ec/tecurve.nr b/noir/noir_stdlib/src/ec/tecurve.nr index 849b45ff0126..692dcb4f12e4 100644 --- a/noir/noir_stdlib/src/ec/tecurve.nr +++ b/noir/noir_stdlib/src/ec/tecurve.nr @@ -9,6 +9,7 @@ mod affine { use crate::ec::montcurve::affine::Point as MPoint; use crate::ec::swcurve::affine::Curve as SWCurve; use crate::ec::swcurve::affine::Point as SWPoint; + use crate::ops::Eq; // Curve specification struct Curve { // Twisted Edwards curve // Coefficients in defining equation ax^2 + y^2 = 1 + dx^2y^2 @@ -23,19 +24,21 @@ mod affine { y: Field } - impl Point { - // Point constructor - pub fn new(x: Field, y: Field) -> Self { - Self { x, y } - } - - // Check for equality + // Check for equality + impl Eq for Point { fn eq(self, p: Point) -> bool { let Self {x: x1, y: y1} = self; let Self {x: x2, y: y2} = p; (x1 == x2) & (y1 == y2) } + } + + impl Point { + // Point constructor + pub fn new(x: Field, y: Field) -> Self { + Self { x, y } + } // Check if zero pub fn is_zero(self) -> bool { @@ -198,6 +201,7 @@ mod curvegroup { use crate::ec::montcurve::curvegroup::Point as MPoint; use crate::ec::swcurve::curvegroup::Curve as SWCurve; use crate::ec::swcurve::curvegroup::Point as SWPoint; + use crate::ops::Eq; // Curve specification struct Curve { // Twisted Edwards curve // Coefficients in defining equation a(x^2 + y^2)z^2 = z^4 + dx^2y^2 @@ -214,19 +218,21 @@ mod curvegroup { z: Field } - impl Point { - // Point constructor - pub fn new(x: Field, y: Field, t: Field, z: Field) -> Self { - Self {x, y, t, z} - } - - // Check for equality + // Check for equality + impl Eq for Point { fn eq(self, p: Point) -> bool { let Self {x: x1, y: y1, t: _t1, z: z1} = self; let Self {x: x2, y: y2, t: _t2, z:z2} = p; (x1*z2 == x2*z1) & (y1*z2 == y2*z1) } + } + + impl Point { + // Point constructor + pub fn new(x: Field, y: Field, t: Field, z: Field) -> Self { + Self {x, y, t, z} + } // Check if zero pub fn is_zero(self) -> bool { diff --git a/noir/noir_stdlib/src/ops.nr b/noir/noir_stdlib/src/ops.nr index 23acc2f0e5df..2bf83cd0a70c 100644 --- a/noir/noir_stdlib/src/ops.nr +++ b/noir/noir_stdlib/src/ops.nr @@ -1,4 +1,3 @@ - trait Add { fn add(self, other: Self) -> Self; } diff --git a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/append_only_tree_snapshot.nr b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/append_only_tree_snapshot.nr index 9b0456ab60e0..4856f4465203 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/append_only_tree_snapshot.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/append_only_tree_snapshot.nr @@ -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) } } diff --git a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/constant_rollup_data.nr b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/constant_rollup_data.nr index 1871a0ec1ff8..8df873f0a3bb 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/constant_rollup_data.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/constant_rollup_data.nr @@ -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: @@ -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) & diff --git a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/global_variables.nr b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/global_variables.nr index ce4abc63541c..bcdef41c0b07 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/global_variables.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/global_variables.nr @@ -1,4 +1,5 @@ use dep::types::constants::GENERATOR_INDEX__GLOBAL_VARIABLES; +use dep::std::ops::Eq; struct GlobalVariables { chain_id : Field, @@ -7,8 +8,16 @@ 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) + } +} +impl GlobalVariables { pub fn hash(self) -> Field { dep::std::hash::pedersen_hash_with_separator([ self.chain_id, @@ -19,11 +28,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) - } } diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_context.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_context.nr index 241d2b08e311..2638f39da514 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_context.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_context.nr @@ -7,6 +7,7 @@ use crate::{ }, hash::pedersen_hash, }; +use dep::std::ops::Eq; // docs:start:call-context struct CallContext { @@ -24,6 +25,20 @@ 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 CallContext { fn serialize(self) -> [Field; CALL_CONTEXT_LENGTH] { [ @@ -53,16 +68,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) - } } diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_request.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_request.nr index c66972a53193..e06ebdc787d0 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_request.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_request.nr @@ -1,4 +1,5 @@ use crate::address::AztecAddress; +use dep::std::ops::Eq; struct CallerContext { msg_sender: AztecAddress, @@ -31,6 +32,16 @@ struct CallRequest { end_side_effect_counter: Field, } +impl Eq for CallRequest { + fn eq(self, call_request: CallRequest) -> bool { + (call_request.hash == self.hash) + & (call_request.caller_contract_address.eq(self.caller_contract_address)) + & (call_request.caller_context.eq(self.caller_context)) + & (call_request.start_side_effect_counter == self.start_side_effect_counter) + & (call_request.end_side_effect_counter == self.end_side_effect_counter) + } +} + impl CallRequest { pub fn empty() -> Self { Self { @@ -45,12 +56,4 @@ impl CallRequest { pub fn is_empty(self) -> bool { self.hash == 0 } - - pub fn eq(self, call_request: CallRequest) -> bool { - (call_request.hash == self.hash) - & (call_request.caller_contract_address.eq(self.caller_contract_address)) - & (call_request.caller_context.eq(self.caller_context)) - & (call_request.start_side_effect_counter == self.start_side_effect_counter) - & (call_request.end_side_effect_counter == self.end_side_effect_counter) - } } diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/function_selector.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/function_selector.nr index d166bbdf3f65..d076cc09bf85 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/function_selector.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/function_selector.nr @@ -1,4 +1,5 @@ use crate::utils::field::field_from_bytes; +use dep::std::ops::Eq; global SELECTOR_SIZE = 4; @@ -7,6 +8,12 @@ struct FunctionSelector { inner: u32, } +impl Eq for FunctionSelector { + fn eq(self, function_selector: FunctionSelector) -> bool { + function_selector.inner == self.inner + } +} + impl FunctionSelector { fn to_field(self) -> Field { self.inner as Field @@ -40,10 +47,6 @@ impl FunctionSelector { Self { inner: 0 } } - pub fn eq(self, function_selector: FunctionSelector) -> bool { - function_selector.inner == self.inner - } - pub fn serialize(self: Self) -> [Field; 1] { [self.inner as Field] } diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/public_data_read.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/public_data_read.nr index 8e3fc4bb497a..08ce9bb8e3cd 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/public_data_read.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/public_data_read.nr @@ -1,10 +1,17 @@ use crate::constants::GENERATOR_INDEX__PUBLIC_DATA_READ; +use dep::std::ops::Eq; struct PublicDataRead { leaf_slot : Field, value : Field, } +impl Eq for PublicDataRead { + fn eq(self, public_data_read: PublicDataRead) -> bool { + (public_data_read.leaf_slot == self.leaf_slot) & (public_data_read.value == self.value) + } +} + impl PublicDataRead { fn hash(self) -> Field { dep::std::hash::pedersen_hash_with_separator([ @@ -23,8 +30,4 @@ impl PublicDataRead { pub fn is_empty(self) -> bool { (self.leaf_slot == 0) & (self.value == 0) } - - pub fn eq(self, public_data_read: PublicDataRead) -> bool { - (public_data_read.leaf_slot == self.leaf_slot) & (public_data_read.value == self.value) - } } diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/side_effect.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/side_effect.nr index b9c53d233645..2e0fe02b954b 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/side_effect.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/side_effect.nr @@ -1,10 +1,18 @@ use crate::constants::{GENERATOR_INDEX__SIDE_EFFECT}; +use dep::std::ops::Eq; struct SideEffect{ value: Field, counter: Field, } +impl Eq for SideEffect { + fn eq(self, side_effect: SideEffect) -> bool { + (self.value == side_effect.value) + & (self.counter == side_effect.counter) + } +} + impl SideEffect { pub fn serialize(self) -> [Field; 2] { [self.value, self.counter] @@ -26,11 +34,6 @@ impl SideEffect { & (self.counter == 0) } - pub fn eq(self, side_effect: SideEffect) -> bool { - (self.value == side_effect.value) - & (self.counter == side_effect.counter) - } - pub fn empty() -> SideEffect { SideEffect { value: 0, @@ -45,6 +48,14 @@ struct SideEffectLinkedToNoteHash{ counter: Field, } +impl Eq for SideEffectLinkedToNoteHash { + fn eq(self, side_effect: SideEffectLinkedToNoteHash) -> bool { + (self.value == side_effect.value) + & (self.note_hash == side_effect.note_hash) + & (self.counter == side_effect.counter) + } +} + impl SideEffectLinkedToNoteHash{ pub fn serialize(self) -> [Field; 3] { [self.value, self.note_hash, self.counter] @@ -70,12 +81,6 @@ impl SideEffectLinkedToNoteHash{ & (self.counter == 0) } - pub fn eq(self, side_effect: SideEffectLinkedToNoteHash) -> bool { - (self.value == side_effect.value) - & (self.note_hash == side_effect.note_hash) - & (self.counter == side_effect.counter) - } - pub fn empty() -> SideEffectLinkedToNoteHash { SideEffectLinkedToNoteHash { value: 0, diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/address.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/address.nr index b60330d0c40a..ec9266ef4f13 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/address.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/address.nr @@ -7,12 +7,19 @@ use crate::{ utils, grumpkin_point::GrumpkinPoint, }; +use dep::std::ops::Eq; // Aztec address struct AztecAddress { inner : Field } +impl Eq for AztecAddress { + fn eq(self, other : Self) -> bool { + self.to_field() == other.to_field() + } +} + impl AztecAddress { pub fn zero() -> Self { Self { @@ -60,10 +67,6 @@ impl AztecAddress { } } - pub fn eq(self, other : Self) -> bool { - self.to_field() == other.to_field() - } - pub fn serialize(self: Self) -> [Field; 1] { [self.inner] } @@ -79,6 +82,12 @@ struct EthAddress{ inner : Field } +impl Eq for EthAddress { + fn eq(self, other : Self) -> bool { + self.to_field() == other.to_field() + } +} + impl EthAddress{ pub fn zero() -> Self { Self { @@ -117,10 +126,6 @@ impl EthAddress{ } } - pub fn eq(self, other : Self) -> bool { - self.to_field() == other.to_field() - } - pub fn serialize(self: Self) -> [Field; 1] { [self.inner] } diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/contrakt/storage_update_request.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/contrakt/storage_update_request.nr index 95f1d68bf4df..549645efcddc 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/contrakt/storage_update_request.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/contrakt/storage_update_request.nr @@ -5,6 +5,7 @@ use crate::{ }, hash::pedersen_hash, }; +use dep::std::ops::Eq; struct StorageUpdateRequest{ storage_slot : Field, @@ -12,6 +13,15 @@ struct StorageUpdateRequest{ new_value : Field, } +impl Eq for StorageUpdateRequest { + fn eq(self, request: Self) -> bool { + // TODO(https://github.com/AztecProtocol/aztec-packages/issues/3595) + (request.storage_slot == self.storage_slot) + & (request.old_value == self.old_value) + & (request.new_value == self.new_value) + } +} + impl StorageUpdateRequest { pub fn empty() -> Self { StorageUpdateRequest { @@ -32,11 +42,4 @@ impl StorageUpdateRequest { pub fn is_empty(self) -> bool { self.storage_slot == 0 } - - pub fn eq(self, request: Self) -> bool { - // TODO(https://github.com/AztecProtocol/aztec-packages/issues/3595) - (request.storage_slot == self.storage_slot) - & (request.old_value == self.old_value) - & (request.new_value == self.new_value) - } } From 218ef578069e488bc557a520eab2023f0520e6d3 Mon Sep 17 00:00:00 2001 From: thunkar Date: Thu, 4 Jan 2024 12:11:01 +0000 Subject: [PATCH 2/5] Reverted stdlib changes --- noir/noir_stdlib/src/ec/montcurve.nr | 8 +++---- noir/noir_stdlib/src/ec/swcurve.nr | 36 ++++++++++++---------------- noir/noir_stdlib/src/ec/tecurve.nr | 34 +++++++++++--------------- noir/noir_stdlib/src/ops.nr | 1 + 4 files changed, 33 insertions(+), 46 deletions(-) diff --git a/noir/noir_stdlib/src/ec/montcurve.nr b/noir/noir_stdlib/src/ec/montcurve.nr index 79bc16ff2a23..82d22837b463 100644 --- a/noir/noir_stdlib/src/ec/montcurve.nr +++ b/noir/noir_stdlib/src/ec/montcurve.nr @@ -12,7 +12,6 @@ mod affine { use crate::ec::safe_inverse; use crate::ec::sqrt; use crate::ec::ZETA; - use crate::ops::Eq; // Curve specification struct Curve { // Montgomery Curve configuration (ky^2 = x^3 + j*x^2 + x) j: Field, @@ -27,9 +26,6 @@ mod affine { infty: bool // Indicator for point at infinity } - // Check for equality - impl Eq for Point { fn eq(self, p: Self) -> bool { (self.infty & p.infty) | (!self.infty & !p.infty & (self.x == p.x) & (self.y == p.y))}} - impl Point { // Point constructor pub fn new(x: Field, y: Field) -> Self { @@ -37,7 +33,9 @@ mod affine { } // Check for equality - + fn eq(self, p: Self) -> bool { + (self.infty & p.infty) | (!self.infty & !p.infty & (self.x == p.x) & (self.y == p.y)) + } // Check if zero pub fn is_zero(self) -> bool { diff --git a/noir/noir_stdlib/src/ec/swcurve.nr b/noir/noir_stdlib/src/ec/swcurve.nr index c02323ff2eef..e9b6f661843e 100644 --- a/noir/noir_stdlib/src/ec/swcurve.nr +++ b/noir/noir_stdlib/src/ec/swcurve.nr @@ -7,7 +7,6 @@ mod affine { use crate::ec::safe_inverse; use crate::ec::is_square; use crate::ec::sqrt; - use crate::ops::Eq; // Curve specification struct Curve { // Short Weierstraß curve // Coefficients in defining equation y^2 = x^3 + ax + b @@ -23,22 +22,20 @@ mod affine { infty: bool // Indicator for point at infinity } - // Check for equality - impl Eq for Point { - fn eq(self, p: Self) -> bool { + impl Point { + // Point constructor + pub fn new(x: Field, y: Field) -> Self { + Self {x, y, infty: false} + } + + // Check for equality + fn eq(self, p: Point) -> bool { let Self {x: x1, y: y1, infty: inf1} = self; let Self {x: x2, y: y2, infty: inf2} = p; (inf1 & inf2) | (!inf1 & !inf2 & (x1 == x2) & (y1 == y2)) } - } - - impl Point { - // Point constructor - pub fn new(x: Field, y: Field) -> Self { - Self {x, y, infty: false} - } // Check if zero pub fn is_zero(self) -> bool { @@ -185,7 +182,6 @@ mod curvegroup { // Points are represented by three-dimensional Jacobian coordinates. // See for details. use crate::ec::swcurve::affine; - use crate::ops::Eq; // Curve specification struct Curve { // Short Weierstraß curve // Coefficients in defining equation y^2 = x^3 + axz^4 + bz^6 @@ -201,22 +197,20 @@ mod curvegroup { z: Field // z = 0 corresponds to point at infinity. } - // Check for equality - impl Eq for Point { + impl Point { + // Point constructor + pub fn new(x: Field, y: Field, z: Field) -> Self { + Self {x, y, z} + } + + // Check for equality fn eq(self, p: Point) -> bool { let Self {x: x1, y: y1, z: z1} = self; let Self {x: x2, y: y2, z: z2} = p; ((z1 == 0) & (z2 == 0)) | ((z1 != 0) & (z2 != 0) & (x1*z2*z2 == x2*z1*z1) & (y1*z2*z2*z2 == y2*z1*z1*z1)) } - } - impl Point { - // Point constructor - pub fn new(x: Field, y: Field, z: Field) -> Self { - Self {x, y, z} - } - // Check if zero pub fn is_zero(self) -> bool { self.eq(Point::zero()) diff --git a/noir/noir_stdlib/src/ec/tecurve.nr b/noir/noir_stdlib/src/ec/tecurve.nr index 692dcb4f12e4..849b45ff0126 100644 --- a/noir/noir_stdlib/src/ec/tecurve.nr +++ b/noir/noir_stdlib/src/ec/tecurve.nr @@ -9,7 +9,6 @@ mod affine { use crate::ec::montcurve::affine::Point as MPoint; use crate::ec::swcurve::affine::Curve as SWCurve; use crate::ec::swcurve::affine::Point as SWPoint; - use crate::ops::Eq; // Curve specification struct Curve { // Twisted Edwards curve // Coefficients in defining equation ax^2 + y^2 = 1 + dx^2y^2 @@ -24,21 +23,19 @@ mod affine { y: Field } - // Check for equality - impl Eq for Point { + impl Point { + // Point constructor + pub fn new(x: Field, y: Field) -> Self { + Self { x, y } + } + + // Check for equality fn eq(self, p: Point) -> bool { let Self {x: x1, y: y1} = self; let Self {x: x2, y: y2} = p; (x1 == x2) & (y1 == y2) } - } - - impl Point { - // Point constructor - pub fn new(x: Field, y: Field) -> Self { - Self { x, y } - } // Check if zero pub fn is_zero(self) -> bool { @@ -201,7 +198,6 @@ mod curvegroup { use crate::ec::montcurve::curvegroup::Point as MPoint; use crate::ec::swcurve::curvegroup::Curve as SWCurve; use crate::ec::swcurve::curvegroup::Point as SWPoint; - use crate::ops::Eq; // Curve specification struct Curve { // Twisted Edwards curve // Coefficients in defining equation a(x^2 + y^2)z^2 = z^4 + dx^2y^2 @@ -218,21 +214,19 @@ mod curvegroup { z: Field } - // Check for equality - impl Eq for Point { + impl Point { + // Point constructor + pub fn new(x: Field, y: Field, t: Field, z: Field) -> Self { + Self {x, y, t, z} + } + + // Check for equality fn eq(self, p: Point) -> bool { let Self {x: x1, y: y1, t: _t1, z: z1} = self; let Self {x: x2, y: y2, t: _t2, z:z2} = p; (x1*z2 == x2*z1) & (y1*z2 == y2*z1) } - } - - impl Point { - // Point constructor - pub fn new(x: Field, y: Field, t: Field, z: Field) -> Self { - Self {x, y, t, z} - } // Check if zero pub fn is_zero(self) -> bool { diff --git a/noir/noir_stdlib/src/ops.nr b/noir/noir_stdlib/src/ops.nr index 2bf83cd0a70c..23acc2f0e5df 100644 --- a/noir/noir_stdlib/src/ops.nr +++ b/noir/noir_stdlib/src/ops.nr @@ -1,3 +1,4 @@ + trait Add { fn add(self, other: Self) -> Self; } From b8a0b04b8bf4306eb71bb259833d42f3e18506c6 Mon Sep 17 00:00:00 2001 From: thunkar Date: Thu, 4 Jan 2024 14:00:02 +0000 Subject: [PATCH 3/5] Empty and eq traits --- .../aztec-nr/safe-math/src/safe_u120.nr | 18 +++--- .../crates/private-kernel-lib/src/common.nr | 2 +- .../crates/public-kernel-lib/src/common.nr | 2 +- .../rollup-lib/src/abis/global_variables.nr | 5 +- .../src/abis/public_data_tree_leaf.nr | 37 ++++++++---- .../rollup-lib/src/base/base_rollup_inputs.nr | 8 +-- .../src/crates/types/src/abis/block_header.nr | 11 ++-- .../src/crates/types/src/abis/call_context.nr | 11 ++-- .../src/crates/types/src/abis/call_request.nr | 29 +++++---- .../crates/types/src/abis/call_stack_item.nr | 7 ++- .../types/src/abis/function_leaf_preimage.nr | 3 +- .../types/src/abis/new_contract_data.nr | 33 ++++++---- .../types/src/abis/nullifier_leaf_preimage.nr | 20 ++++--- .../src/abis/private_circuit_public_inputs.nr | 7 ++- .../crates/types/src/abis/public_data_read.nr | 21 ++++--- .../src/abis/public_data_update_request.nr | 24 +++++--- .../src/crates/types/src/abis/side_effect.nr | 60 +++++++++++-------- .../src/crates/types/src/address.nr | 25 ++++---- .../types/src/contrakt/deployment_data.nr | 10 +++- .../crates/types/src/contrakt/storage_read.nr | 8 ++- .../src/contrakt/storage_update_request.nr | 17 ++++-- .../src/crates/types/src/lib.nr | 1 + .../src/crates/types/src/traits.nr | 7 +++ .../crates/types/src/transaction/context.nr | 3 +- .../crates/types/src/transaction/request.nr | 3 +- 25 files changed, 237 insertions(+), 135 deletions(-) create mode 100644 yarn-project/noir-protocol-circuits/src/crates/types/src/traits.nr diff --git a/yarn-project/aztec-nr/safe-math/src/safe_u120.nr b/yarn-project/aztec-nr/safe-math/src/safe_u120.nr index 8412f45a43a7..a7051457ce6c 100644 --- a/yarn-project/aztec-nr/safe-math/src/safe_u120.nr +++ b/yarn-project/aztec-nr/safe-math/src/safe_u120.nr @@ -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 { @@ -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 } diff --git a/yarn-project/noir-protocol-circuits/src/crates/private-kernel-lib/src/common.nr b/yarn-project/noir-protocol-circuits/src/crates/private-kernel-lib/src/common.nr index ef676202151e..069a232fd531 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/private-kernel-lib/src/common.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/private-kernel-lib/src/common.nr @@ -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) { diff --git a/yarn-project/noir-protocol-circuits/src/crates/public-kernel-lib/src/common.nr b/yarn-project/noir-protocol-circuits/src/crates/public-kernel-lib/src/common.nr index f80543eacf0b..115079acd9ae 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/public-kernel-lib/src/common.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/public-kernel-lib/src/common.nr @@ -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) { diff --git a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/global_variables.nr b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/global_variables.nr index bcdef41c0b07..d3a92124c2f5 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/global_variables.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/global_variables.nr @@ -1,5 +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, @@ -17,8 +18,8 @@ impl Eq for GlobalVariables { } } -impl GlobalVariables { - 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, diff --git a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/public_data_tree_leaf.nr b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/public_data_tree_leaf.nr index 05deed5b3950..e820f172175c 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/public_data_tree_leaf.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/public_data_tree_leaf.nr @@ -1,3 +1,6 @@ +use dep::std::ops::Eq; +use dep::types::traits::{Empty, Hash}; + struct PublicDataTreeLeafPreimage { slot : Field, value: Field, @@ -5,8 +8,8 @@ struct PublicDataTreeLeafPreimage { next_index : u32, } -impl PublicDataTreeLeafPreimage { - pub fn default() -> Self { +impl Empty for PublicDataTreeLeafPreimage { + fn empty() -> Self { Self { slot: 0, value: 0, @@ -14,12 +17,10 @@ impl PublicDataTreeLeafPreimage { 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 { @@ -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) - } } diff --git a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/base/base_rollup_inputs.nr b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/base/base_rollup_inputs.nr index 8bb893620484..afbb0e7a448b 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/base/base_rollup_inputs.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/base/base_rollup_inputs.nr @@ -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, @@ -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; } } @@ -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, @@ -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 { diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/block_header.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/block_header.nr index abcee9072410..f053fee0779e 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/block_header.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/block_header.nr @@ -5,6 +5,7 @@ use crate::{ }, hash::pedersen_hash, }; +use crate::traits::Empty; // docs:start:block-header struct BlockHeader { @@ -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); @@ -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]) - } } diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_context.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_context.nr index 2638f39da514..668dd8fce90e 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_context.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_context.nr @@ -8,6 +8,7 @@ use crate::{ hash::pedersen_hash, }; use dep::std::ops::Eq; +use crate::traits::Hash; // docs:start:call-context struct CallContext { @@ -39,6 +40,12 @@ impl Eq for CallContext { } } +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] { [ @@ -53,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); diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_request.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_request.nr index e06ebdc787d0..da97e8b6207a 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_request.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_request.nr @@ -1,27 +1,32 @@ use crate::address::AztecAddress; use dep::std::ops::Eq; +use crate::traits::Empty; struct CallerContext { msg_sender: AztecAddress, storage_contract_address: AztecAddress, } -impl CallerContext { - pub fn empty() -> Self { +impl Eq for CallerContext { + fn eq(self, caller_context: CallerContext) -> bool { + caller_context.msg_sender.eq(self.msg_sender) + & caller_context.storage_contract_address.eq(self.storage_contract_address) + } +} + +impl Empty for CallerContext { + fn empty() -> Self { CallerContext { msg_sender: AztecAddress::zero(), storage_contract_address: AztecAddress::zero(), } } +} +impl CallerContext { pub fn is_empty(self) -> bool { self.msg_sender.is_zero() & self.storage_contract_address.is_zero() } - - pub fn eq(self, caller_context: CallerContext) -> bool { - caller_context.msg_sender.eq(self.msg_sender) - & caller_context.storage_contract_address.eq(self.storage_contract_address) - } } struct CallRequest { @@ -42,17 +47,19 @@ impl Eq for CallRequest { } } -impl CallRequest { - pub fn empty() -> Self { - Self { +impl Empty for CallRequest { + fn empty() -> Self { + CallRequest { hash: 0, caller_contract_address: AztecAddress::zero(), - caller_context: dep::std::unsafe::zeroed(), + caller_context: CallerContext::empty(), start_side_effect_counter: 0, end_side_effect_counter: 0, } } +} +impl CallRequest { pub fn is_empty(self) -> bool { self.hash == 0 } diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_stack_item.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_stack_item.nr index 94aa0dce5326..45ebceaf0105 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_stack_item.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_stack_item.nr @@ -7,6 +7,7 @@ use crate::address::AztecAddress; use crate::constants::{ GENERATOR_INDEX__CALL_STACK_ITEM, }; +use crate::traits::Hash; struct PrivateCallStackItem { // This is the _actual_ contract address relating to where this function's code resides in the @@ -21,7 +22,7 @@ struct PrivateCallStackItem { is_execution_request: bool, } -impl PrivateCallStackItem { +impl Hash for PrivateCallStackItem { fn hash(self) -> Field { dep::std::hash::pedersen_hash_with_separator([ self.contract_address.to_field(), @@ -40,7 +41,7 @@ struct PublicCallStackItem { is_execution_request: bool, } -impl PublicCallStackItem { +impl Hash for PublicCallStackItem { fn hash(self) -> Field { let item = if self.is_execution_request { self.as_execution_request() @@ -54,7 +55,9 @@ impl PublicCallStackItem { item.public_inputs.hash(), ], GENERATOR_INDEX__CALL_STACK_ITEM) } +} +impl PublicCallStackItem { fn as_execution_request(self) -> Self { let public_inputs = self.public_inputs; let mut request_public_inputs: PublicCircuitPublicInputs = dep::std::unsafe::zeroed(); diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/function_leaf_preimage.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/function_leaf_preimage.nr index 2a66a161736f..fecfae27bd9f 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/function_leaf_preimage.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/function_leaf_preimage.nr @@ -1,5 +1,6 @@ use crate::abis::function_selector::FunctionSelector; use crate::constants::GENERATOR_INDEX__FUNCTION_LEAF; +use crate::traits::Hash; struct FunctionLeafPreimage { selector : FunctionSelector, @@ -9,7 +10,7 @@ struct FunctionLeafPreimage { acir_hash : Field } -impl FunctionLeafPreimage { +impl Hash for FunctionLeafPreimage { fn hash(self) -> Field { dep::std::hash::pedersen_hash_with_separator([ self.selector.to_field(), diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/new_contract_data.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/new_contract_data.nr index 89085c0e67f3..ece38501b6bc 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/new_contract_data.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/new_contract_data.nr @@ -1,5 +1,7 @@ use crate::address::{AztecAddress, EthAddress}; use crate::constants::GENERATOR_INDEX__CONTRACT_LEAF; +use dep::std::ops::Eq; +use crate::traits::{Empty, Hash}; struct NewContractData { contract_address: AztecAddress, @@ -7,28 +9,26 @@ struct NewContractData { function_tree_root: Field, } -impl NewContractData { - pub fn is_empty(self) -> bool { - (self.contract_address.to_field() == 0) & - (self.portal_contract_address.to_field() == 0) & - (self.function_tree_root ==0) - } - - pub fn eq(self, data: NewContractData) -> bool { +impl Eq for NewContractData { + fn eq(self, data: NewContractData) -> bool { data.contract_address.eq(self.contract_address) & data.portal_contract_address.eq(self.portal_contract_address) & (data.function_tree_root == self.function_tree_root) } +} - pub fn default() -> Self { +impl Empty for NewContractData { + fn empty() -> Self { Self { - contract_address : AztecAddress::default(), - portal_contract_address : EthAddress::default(), + contract_address : AztecAddress::empty(), + portal_contract_address : EthAddress::empty(), function_tree_root : 0, } } +} - pub fn hash(self) -> Field { +impl Hash for NewContractData { + fn hash(self) -> Field { if self.is_empty() { 0 // We want to return 0 here since the contract_address is zero } else { @@ -40,3 +40,12 @@ impl NewContractData { } } } + +impl NewContractData { + pub fn is_empty(self) -> bool { + (self.contract_address.to_field() == 0) & + (self.portal_contract_address.to_field() == 0) & + (self.function_tree_root ==0) + } + +} diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/nullifier_leaf_preimage.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/nullifier_leaf_preimage.nr index 681bd1156869..76ac2834bb38 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/nullifier_leaf_preimage.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/nullifier_leaf_preimage.nr @@ -1,31 +1,37 @@ global NULLIFIER_LEAF_PREIMAGE_LENGTH: Field = 3; +use crate::traits::{Empty, Hash}; + struct NullifierLeafPreimage { nullifier : Field, next_nullifier :Field, next_index : u32, } -impl NullifierLeafPreimage { - pub fn default() -> Self { +impl Empty for NullifierLeafPreimage { + fn empty() -> Self { Self { nullifier : 0, next_nullifier : 0, next_index : 0, } } +} - pub fn is_empty(self) -> bool { - (self.nullifier == 0) & (self.next_nullifier == 0) & (self.next_index == 0) - } - - pub fn hash(self) -> Field { +impl Hash for NullifierLeafPreimage { + fn hash(self) -> Field { if self.is_empty() { 0 } else { dep::std::hash::pedersen_hash(self.serialize()) } } +} + +impl NullifierLeafPreimage { + pub fn is_empty(self) -> bool { + (self.nullifier == 0) & (self.next_nullifier == 0) & (self.next_index == 0) + } pub fn serialize(self) -> [Field; NULLIFIER_LEAF_PREIMAGE_LENGTH] { [self.nullifier, self.next_nullifier, self.next_index as Field] diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/private_circuit_public_inputs.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/private_circuit_public_inputs.nr index 726868af6fe0..b788881c7685 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/private_circuit_public_inputs.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/private_circuit_public_inputs.nr @@ -23,6 +23,7 @@ use crate::constants::{ PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH, GENERATOR_INDEX__PRIVATE_CIRCUIT_PUBLIC_INPUTS, }; +use crate::traits::Hash; struct PrivateCircuitPublicInputs { call_context: CallContext, @@ -55,8 +56,8 @@ struct PrivateCircuitPublicInputs { version: Field, } -impl PrivateCircuitPublicInputs { - fn hash(self) -> Field { +impl Hash for PrivateCircuitPublicInputs { + fn hash(self) -> Field { // TODO(https://github.com/AztecProtocol/aztec-packages/issues/3595) let mut fields: BoundedVec = BoundedVec::new(0); fields.push(self.call_context.hash()); @@ -89,7 +90,9 @@ impl PrivateCircuitPublicInputs { pedersen_hash(fields.storage, GENERATOR_INDEX__PRIVATE_CIRCUIT_PUBLIC_INPUTS) } +} +impl PrivateCircuitPublicInputs { fn serialize(self) -> [Field; PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH] { let mut fields: BoundedVec = BoundedVec::new(0); fields.push_array(self.call_context.serialize()); diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/public_data_read.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/public_data_read.nr index 08ce9bb8e3cd..f009f6d4abc3 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/public_data_read.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/public_data_read.nr @@ -1,5 +1,6 @@ use crate::constants::GENERATOR_INDEX__PUBLIC_DATA_READ; use dep::std::ops::Eq; +use crate::traits::{Empty,Hash}; struct PublicDataRead { leaf_slot : Field, @@ -12,21 +13,25 @@ impl Eq for PublicDataRead { } } -impl PublicDataRead { +impl Empty for PublicDataRead { + fn empty() -> Self { + Self { + leaf_slot : 0, + value : 0, + } + } +} + +impl Hash for PublicDataRead { fn hash(self) -> Field { dep::std::hash::pedersen_hash_with_separator([ self.leaf_slot, self.value, ], GENERATOR_INDEX__PUBLIC_DATA_READ) } +} - pub fn empty() -> Self { - Self { - leaf_slot : 0, - value : 0, - } - } - +impl PublicDataRead { pub fn is_empty(self) -> bool { (self.leaf_slot == 0) & (self.value == 0) } diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/public_data_update_request.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/public_data_update_request.nr index 798f2fcc35e8..67ff120f086a 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/public_data_update_request.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/public_data_update_request.nr @@ -1,4 +1,6 @@ use crate::constants::GENERATOR_INDEX__PUBLIC_DATA_UPDATE_REQUEST; +use dep::std::ops::Eq; +use crate::traits::{Empty, Hash}; struct PublicDataUpdateRequest { leaf_slot : Field, @@ -6,28 +8,34 @@ struct PublicDataUpdateRequest { new_value : Field } -impl PublicDataUpdateRequest { - pub fn empty() -> Self { +impl Eq for PublicDataUpdateRequest { + fn eq(self, update_request: PublicDataUpdateRequest) -> bool { + (update_request.leaf_slot == self.leaf_slot) & (update_request.old_value == self.old_value) + & (update_request.new_value == self.new_value) + } +} + +impl Empty for PublicDataUpdateRequest { + fn empty() -> Self { Self { leaf_slot : 0, old_value : 0, new_value : 0 } } +} - pub fn eq(self, update_request: PublicDataUpdateRequest) -> bool { - (update_request.leaf_slot == self.leaf_slot) & (update_request.old_value == self.old_value) - & (update_request.new_value == self.new_value) - } - - pub fn hash(self) -> Field { +impl Hash for PublicDataUpdateRequest { + fn hash(self) -> Field { dep::std::hash::pedersen_hash_with_separator([ self.leaf_slot, self.old_value, self.new_value ], GENERATOR_INDEX__PUBLIC_DATA_UPDATE_REQUEST) } +} +impl PublicDataUpdateRequest { pub fn is_empty(self) -> bool { (self.leaf_slot == 0) & (self.old_value == 0) & (self.new_value == 0) } diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/side_effect.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/side_effect.nr index 2e0fe02b954b..da13d853fe91 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/side_effect.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/side_effect.nr @@ -1,5 +1,6 @@ use crate::constants::{GENERATOR_INDEX__SIDE_EFFECT}; use dep::std::ops::Eq; +use crate::traits::{Empty, Hash}; struct SideEffect{ value: Field, @@ -13,6 +14,22 @@ impl Eq for SideEffect { } } +impl Empty for SideEffect { + fn empty() -> Self { + SideEffect { + value: 0, + counter: 0, + } + } +} + +impl Hash for SideEffect { + fn hash(self) -> Field { + dep::std::hash::pedersen_hash_with_separator( + self.serialize() , GENERATOR_INDEX__SIDE_EFFECT) + } +} + impl SideEffect { pub fn serialize(self) -> [Field; 2] { [self.value, self.counter] @@ -24,22 +41,11 @@ impl SideEffect { counter: values[1], } } - pub fn hash(self) -> Field { - dep::std::hash::pedersen_hash_with_separator( - self.serialize() , GENERATOR_INDEX__SIDE_EFFECT) - } pub fn is_empty(self) -> bool { (self.value == 0) & (self.counter == 0) } - - pub fn empty() -> SideEffect { - SideEffect { - value: 0, - counter: 0, - } - } } struct SideEffectLinkedToNoteHash{ @@ -56,6 +62,24 @@ impl Eq for SideEffectLinkedToNoteHash { } } +impl Empty for SideEffectLinkedToNoteHash { + fn empty() -> Self { + SideEffectLinkedToNoteHash { + value: 0, + note_hash: 0, + counter: 0, + } + } +} + +impl Hash for SideEffectLinkedToNoteHash { + fn hash(self) -> Field { + dep::std::hash::pedersen_hash_with_separator( + self.serialize(), + GENERATOR_INDEX__SIDE_EFFECT) + } +} + impl SideEffectLinkedToNoteHash{ pub fn serialize(self) -> [Field; 3] { [self.value, self.note_hash, self.counter] @@ -69,24 +93,10 @@ impl SideEffectLinkedToNoteHash{ } } - pub fn hash(self) -> Field { - dep::std::hash::pedersen_hash_with_separator( - self.serialize(), - GENERATOR_INDEX__SIDE_EFFECT) - } - pub fn is_empty(self) -> bool { (self.value == 0) & (self.note_hash == 0) & (self.counter == 0) } - - pub fn empty() -> SideEffectLinkedToNoteHash { - SideEffectLinkedToNoteHash { - value: 0, - note_hash: 0, - counter: 0, - } - } } diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/address.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/address.nr index ec9266ef4f13..5bea56809f46 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/address.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/address.nr @@ -8,6 +8,7 @@ use crate::{ grumpkin_point::GrumpkinPoint, }; use dep::std::ops::Eq; +use crate::traits::Empty; // Aztec address struct AztecAddress { @@ -20,16 +21,18 @@ impl Eq for AztecAddress { } } -impl AztecAddress { - pub fn zero() -> Self { +impl Empty for AztecAddress { + fn empty() -> Self { Self { - inner: 0 + inner : 0 } } +} - pub fn default() -> Self { +impl AztecAddress { + pub fn zero() -> Self { Self { - inner : 0 + inner: 0 } } @@ -88,16 +91,18 @@ impl Eq for EthAddress { } } -impl EthAddress{ - pub fn zero() -> Self { +impl Empty for EthAddress { + fn empty() -> Self { Self { - inner: 0 + inner : 0 } } +} - pub fn default() -> Self { +impl EthAddress{ + pub fn zero() -> Self { Self { - inner : 0 + inner: 0 } } diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/contrakt/deployment_data.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/contrakt/deployment_data.nr index 5369de614fcb..a0ef079d69a2 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/contrakt/deployment_data.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/contrakt/deployment_data.nr @@ -5,6 +5,7 @@ use crate::constants::{ }; use crate::hash::pedersen_hash; use crate::grumpkin_point::GrumpkinPoint; +use crate::traits::Hash; // docs:start:contract-deployment-data struct ContractDeploymentData { @@ -16,6 +17,12 @@ struct ContractDeploymentData { } // docs:end:contract-deployment-data +impl Hash for ContractDeploymentData { + fn hash(self) -> Field { + pedersen_hash(self.serialize(), GENERATOR_INDEX__CONTRACT_DEPLOYMENT_DATA) + } +} + impl ContractDeploymentData { fn serialize(self) -> [Field; CONTRACT_DEPLOYMENT_DATA_LENGTH] { [ @@ -36,7 +43,4 @@ impl ContractDeploymentData { self.portal_contract_address.assert_is_zero(); } - fn hash(self) -> Field { - pedersen_hash(self.serialize(), GENERATOR_INDEX__CONTRACT_DEPLOYMENT_DATA) - } } diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/contrakt/storage_read.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/contrakt/storage_read.nr index 58aeaa552144..ffc0cbe43c78 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/contrakt/storage_read.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/contrakt/storage_read.nr @@ -5,19 +5,23 @@ use crate::{ }, hash::pedersen_hash, }; +use crate::traits::Empty; struct StorageRead { storage_slot: Field, current_value: Field, } -impl StorageRead { - pub fn empty() -> Self { +impl Empty for StorageRead { + fn empty() -> Self { Self { storage_slot: 0, current_value: 0, } } +} + +impl StorageRead { pub fn serialize(self) -> [Field; CONTRACT_STORAGE_READ_LENGTH] { [self.storage_slot, self.current_value] diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/contrakt/storage_update_request.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/contrakt/storage_update_request.nr index 549645efcddc..d9fab6779b2f 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/contrakt/storage_update_request.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/contrakt/storage_update_request.nr @@ -6,6 +6,7 @@ use crate::{ hash::pedersen_hash, }; use dep::std::ops::Eq; +use crate::traits::{Hash, Empty}; struct StorageUpdateRequest{ storage_slot : Field, @@ -22,21 +23,25 @@ impl Eq for StorageUpdateRequest { } } -impl StorageUpdateRequest { - pub fn empty() -> Self { +impl Empty for StorageUpdateRequest { + fn empty() -> Self { StorageUpdateRequest { storage_slot: 0, old_value: 0, new_value: 0, } } +} - pub fn serialize(self) -> [Field; CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH] { - [self.storage_slot, self.old_value, self.new_value] +impl Hash for StorageUpdateRequest { + fn hash(self) -> Field { + pedersen_hash(self.serialize(), GENERATOR_INDEX__PUBLIC_DATA_UPDATE_REQUEST) } +} - pub fn hash(self) -> Field { - pedersen_hash(self.serialize(), GENERATOR_INDEX__PUBLIC_DATA_UPDATE_REQUEST) +impl StorageUpdateRequest { + pub fn serialize(self) -> [Field; CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH] { + [self.storage_slot, self.old_value, self.new_value] } pub fn is_empty(self) -> bool { diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/lib.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/lib.nr index bde59b6156c5..7e22369a4628 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/lib.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/lib.nr @@ -15,5 +15,6 @@ mod hash; mod interop_testing; mod tests; +mod traits; use abis::kernel_circuit_public_inputs::{ KernelCircuitPublicInputs, KernelCircuitPublicInputsFinal }; diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/traits.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/traits.nr new file mode 100644 index 000000000000..7cc235c7ad84 --- /dev/null +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/traits.nr @@ -0,0 +1,7 @@ +trait Empty { + fn empty() -> Self; +} + +trait Hash { + fn hash(self) -> Field; +} diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/transaction/context.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/transaction/context.nr index 3715951ca78e..d34fe3e885bd 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/transaction/context.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/transaction/context.nr @@ -1,5 +1,6 @@ use crate::constants::GENERATOR_INDEX__TX_CONTEXT; use crate::contrakt::deployment_data::ContractDeploymentData; +use crate::traits::Hash; struct TxContext { is_fee_payment_tx : bool, @@ -12,7 +13,7 @@ struct TxContext { version : Field, } -impl TxContext { +impl Hash for TxContext { fn hash(self) -> Field { dep::std::hash::pedersen_hash_with_separator([ self.is_fee_payment_tx as Field, diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/transaction/request.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/transaction/request.nr index f659a81cebd2..88d1a8201432 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/transaction/request.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/transaction/request.nr @@ -2,6 +2,7 @@ use crate::constants::GENERATOR_INDEX__TX_REQUEST; use crate::address::AztecAddress; use crate::transaction::context::TxContext; use crate::abis::function_data::FunctionData; +use crate::traits::Hash; struct TxRequest { origin : AztecAddress, @@ -10,7 +11,7 @@ struct TxRequest { function_data : FunctionData } -impl TxRequest { +impl Hash for TxRequest { fn hash(self) -> Field { dep::std::hash::pedersen_hash_with_separator([ self.origin.to_field(), From bce9cd105f496b73f9b22bb2092e7a8dc5b7d2b3 Mon Sep 17 00:00:00 2001 From: thunkar Date: Thu, 4 Jan 2024 15:44:23 +0000 Subject: [PATCH 4/5] Added missing empty trait in aztec-nr --- yarn-project/aztec-nr/aztec/src/note/note_header.nr | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/yarn-project/aztec-nr/aztec/src/note/note_header.nr b/yarn-project/aztec-nr/aztec/src/note/note_header.nr index 306e92a4704c..b807deea79ac 100644 --- a/yarn-project/aztec-nr/aztec/src/note/note_header.nr +++ b/yarn-project/aztec-nr/aztec/src/note/note_header.nr @@ -1,4 +1,5 @@ use dep::protocol_types::address::AztecAddress; +use dep::protocol_types::traits::Empty; struct NoteHeader { contract_address: AztecAddress, @@ -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 } - } } From f9e90e79df44b890649b76ff46a85775f29d4b6a Mon Sep 17 00:00:00 2001 From: thunkar Date: Thu, 4 Jan 2024 20:21:18 +0000 Subject: [PATCH 5/5] Added comment --- .../noir-protocol-circuits/src/crates/types/src/traits.nr | 1 + 1 file changed, 1 insertion(+) diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/traits.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/traits.nr index 7cc235c7ad84..03ddb8d9bb8f 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/traits.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/traits.nr @@ -1,3 +1,4 @@ +// Preferred over Default trait to convey intent, as default doesn't necessarily mean empty. trait Empty { fn empty() -> Self; }