From 5946cafb9ab5a822d3e727a14e1c58fb893e322c Mon Sep 17 00:00:00 2001 From: Tom French Date: Thu, 11 Jan 2024 15:11:44 +0000 Subject: [PATCH 1/6] chore: replace `AztecU128` with `U128` --- noir/noir_stdlib/src/uint128.nr | 16 +++++++++- .../crates/public-kernel-lib/src/common.nr | 9 +++--- .../base_or_merge_rollup_public_inputs.nr | 2 -- .../src/crates/rollup-lib/src/components.nr | 9 +++--- .../src/merge/merge_rollup_inputs.nr | 9 +++--- .../src/crates/rollup-lib/src/root.nr | 9 +++--- .../src/crates/types/src/hash.nr | 13 ++++---- .../src/crates/types/src/utils.nr | 1 - .../src/crates/types/src/utils/uint128.nr | 31 ------------------- 9 files changed, 37 insertions(+), 62 deletions(-) delete mode 100644 yarn-project/noir-protocol-circuits/src/crates/types/src/utils/uint128.nr diff --git a/noir/noir_stdlib/src/uint128.nr b/noir/noir_stdlib/src/uint128.nr index 4a58b3868be..db2958cab07 100644 --- a/noir/noir_stdlib/src/uint128.nr +++ b/noir/noir_stdlib/src/uint128.nr @@ -42,6 +42,17 @@ impl U128 { } } + pub fn to_be_bytes(self: Self) -> [u8; 16] { + let lo = self.lo.to_be_bytes(8); + let hi = self.hi.to_be_bytes(8); + let mut bytes = [0;16]; + for i in 0..8 { + bytes[i] = hi[i]; + bytes[i+8] = lo[i]; + } + bytes + } + pub fn to_le_bytes(self: Self) -> [u8; 16] { let lo = self.lo.to_le_bytes(8); let hi = self.hi.to_le_bytes(8); @@ -115,6 +126,9 @@ impl U128 { } pub fn from_integer(i: T) -> U128 { + // TODO(Kev): Apply 128 bit range constraint and fail if this is not the case. + // We can expose a `apply_range_constraint` method from Noir which can take a field + // and return a Field. let f = crate::as_field(i); let lo = f as u64 as Field; let hi = (f-lo) / pow64; @@ -289,4 +303,4 @@ impl Shr for U128 { } self / U128::from_integer(y) } -} \ No newline at end of file +} 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 8f833310eba..277865e99cf 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 @@ -30,7 +30,6 @@ use dep::types::{ utils::{ arrays::{array_length, array_to_bounded_vec}, bounded_vec::BoundedVec, - uint128::AztecU128, }, traits::is_empty_array }; @@ -280,10 +279,10 @@ pub fn accumulate_unencrypted_logs( let current_unencrypted_logs_hash = public_call_public_inputs.unencrypted_logs_hash; public_inputs.end.unencrypted_logs_hash = accumulate_sha256([ - AztecU128::from_field(previous_unencrypted_logs_hash[0]), - AztecU128::from_field(previous_unencrypted_logs_hash[1]), - AztecU128::from_field(current_unencrypted_logs_hash[0]), - AztecU128::from_field(current_unencrypted_logs_hash[1]) + U128::from_integer(previous_unencrypted_logs_hash[0]), + U128::from_integer(previous_unencrypted_logs_hash[1]), + U128::from_integer(current_unencrypted_logs_hash[0]), + U128::from_integer(current_unencrypted_logs_hash[1]) ]); // Add log preimages lengths from current iteration to accumulated lengths diff --git a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr index 10eb323d07f..a3b8a92753b 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr @@ -24,8 +24,6 @@ struct BaseOrMergeRollupPublicInputs { end: PartialStateReference, // We hash public inputs to make them constant-sized (to then be unpacked on-chain) - // AztecU128 isn't safe if it's an input to the circuit (it won't automatically constrain the witness) - // So we want to constrain it when casting these fields to AztecU128 // TODO(#3938): split this to txs_hash and out_hash // We hash public inputs to make them constant-sized (to then be unpacked on-chain) diff --git a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/components.nr b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/components.nr index 272e0f292b6..e8891679b20 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/components.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/components.nr @@ -1,7 +1,6 @@ use crate::abis::base_or_merge_rollup_public_inputs::BaseOrMergeRollupPublicInputs; use dep::types::mocked::AggregationObject; use dep::types::hash::{accumulate_sha256, assert_check_membership, root_from_sibling_path}; -use dep::types::utils::uint128::AztecU128; use dep::types::constants::NUM_FIELDS_PER_SHA256; use crate::abis::previous_rollup_data::PreviousRollupData; use dep::types::abis::append_only_tree_snapshot::AppendOnlyTreeSnapshot; @@ -86,10 +85,10 @@ pub fn assert_prev_rollups_follow_on_from_each_other( pub fn compute_calldata_hash(previous_rollup_data: [PreviousRollupData; 2]) -> [Field; NUM_FIELDS_PER_SHA256] { accumulate_sha256( [ - AztecU128::from_field(previous_rollup_data[0].base_or_merge_rollup_public_inputs.calldata_hash[0]), - AztecU128::from_field(previous_rollup_data[0].base_or_merge_rollup_public_inputs.calldata_hash[1]), - AztecU128::from_field(previous_rollup_data[1].base_or_merge_rollup_public_inputs.calldata_hash[0]), - AztecU128::from_field(previous_rollup_data[1].base_or_merge_rollup_public_inputs.calldata_hash[1]) + U128::from_integer(previous_rollup_data[0].base_or_merge_rollup_public_inputs.calldata_hash[0]), + U128::from_integer(previous_rollup_data[0].base_or_merge_rollup_public_inputs.calldata_hash[1]), + U128::from_integer(previous_rollup_data[1].base_or_merge_rollup_public_inputs.calldata_hash[0]), + U128::from_integer(previous_rollup_data[1].base_or_merge_rollup_public_inputs.calldata_hash[1]) ] ) } diff --git a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/merge/merge_rollup_inputs.nr b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/merge/merge_rollup_inputs.nr index bfd3690e5f2..9188ff8abb6 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/merge/merge_rollup_inputs.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/merge/merge_rollup_inputs.nr @@ -48,7 +48,6 @@ mod tests { tests::merge_rollup_inputs::default_merge_rollup_inputs, }; use dep::types::hash::accumulate_sha256; - use dep::types::utils::uint128::AztecU128; #[test(should_fail_with="input proofs are of different rollup types")] fn different_rollup_type_fails() { @@ -141,10 +140,10 @@ mod tests { let mut inputs = default_merge_rollup_inputs(); let expected_calldata_hash = accumulate_sha256( [ - AztecU128::from_field(0), - AztecU128::from_field(1), - AztecU128::from_field(2), - AztecU128::from_field(3) + U128::from_integer(0), + U128::from_integer(1), + U128::from_integer(2), + U128::from_integer(3) ] ); let outputs = inputs.merge_rollup_circuit(); diff --git a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/root.nr b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/root.nr index 0fff41cc8fe..d0f6924747e 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/root.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/root.nr @@ -141,7 +141,6 @@ mod tests { }, tests::root_rollup_inputs::default_root_rollup_inputs, }; - use dep::types::utils::uint128::AztecU128; use dep::types::utils::uint256::U256; use dep::types::hash::accumulate_sha256; @@ -151,10 +150,10 @@ mod tests { let expected_calldata_hash = accumulate_sha256( [ - AztecU128::from_field(0), - AztecU128::from_field(1), - AztecU128::from_field(2), - AztecU128::from_field(3) + U128::from_integer(0), + U128::from_integer(1), + U128::from_integer(2), + U128::from_integer(3) ] ); diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/hash.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/hash.nr index 0fd14eca1ff..ddb4b34bbc6 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/hash.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/hash.nr @@ -5,7 +5,6 @@ use crate::abis::function_leaf_preimage::FunctionLeafPreimage; use crate::abis::new_contract_data::NewContractData as ContractLeafPreimage; use crate::abis::function_data::FunctionData; use crate::abis::side_effect::{SideEffect}; -use crate::utils::uint128::AztecU128; use crate::utils::uint256::U256; use crate::utils::bounded_vec::BoundedVec; use crate::constants::{ @@ -243,9 +242,9 @@ pub fn compute_constructor_hash( // Returning a Field would be desirable because then this can be replaced with // poseidon without changing the rest of the code // -pub fn accumulate_sha256(input: [AztecU128; 4]) -> [Field; NUM_FIELDS_PER_SHA256] { +pub fn accumulate_sha256(input: [U128; 4]) -> [Field; NUM_FIELDS_PER_SHA256] { // This is a note about the cpp code, since it takes an array of Fields - // instead of a AztecU128. + // instead of a U128. // 4 Field elements when converted to bytes will usually // occupy 4 * 32 = 128 bytes. // However, this function is making the assumption that each Field @@ -273,10 +272,10 @@ pub fn compute_logs_hash( ) -> [Field; NUM_FIELDS_PER_SHA256] { accumulate_sha256( [ - AztecU128::from_field(previous_log_hash[0]), - AztecU128::from_field(previous_log_hash[1]), - AztecU128::from_field(current_log_hash[0]), - AztecU128::from_field(current_log_hash[1]) + U128::from_integer(previous_log_hash[0]), + U128::from_integer(previous_log_hash[1]), + U128::from_integer(current_log_hash[0]), + U128::from_integer(current_log_hash[1]) ] ) } diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/utils.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/utils.nr index e2f4d5dddda..691ffb90a7b 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/utils.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/utils.nr @@ -5,7 +5,6 @@ mod arrays; mod bounded_vec; mod field; -mod uint128; mod uint256; // if predicate == true then return lhs, else return rhs diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/utils/uint128.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/utils/uint128.nr deleted file mode 100644 index f65935ae8ff..00000000000 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/utils/uint128.nr +++ /dev/null @@ -1,31 +0,0 @@ -// This is a diversion from the cpp code. -// The cpp code uses Fields for log_hashes -// whereas we are using u128 to make sure that it is really a u128. -struct AztecU128 { - inner : Field -} - -impl AztecU128 { - pub fn to_field(self) -> Field { - self.inner as Field - } - - pub fn from_field(value : Field) -> AztecU128 { - // TODO(Kev): Apply 128 bit range constraint and fail if this is not the case. - // We can expose a `apply_range_constraint` method from Noir which can take a field - // and return a Field. - // It won't be type-safe, but thats fine. We may then be able to implement - // u128 in the stdlib and have it be called automatically when a user - // does `let x :u128 = 0;` We will require traits to make operations nice. - AztecU128{inner : value} - } - - pub fn to_be_bytes(self) -> [u8;16] { - let slice = self.inner.to_be_bytes(16); - let mut arr = [0;16]; - for i in 0..16 { - arr[i] = slice[i]; - } - arr - } -} From d69266f1503222e2775fb87f2b3ed03d79eb296e Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Fri, 12 Jan 2024 11:26:22 +0000 Subject: [PATCH 2/6] Update yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr --- .../rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr index a3b8a92753b..e4f680853fe 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr @@ -24,7 +24,8 @@ struct BaseOrMergeRollupPublicInputs { end: PartialStateReference, // We hash public inputs to make them constant-sized (to then be unpacked on-chain) - + // U128 isn't safe if it's an input to the circuit (it won't automatically constrain the witness) + // So we want to constrain it when casting these fields to U128 // TODO(#3938): split this to txs_hash and out_hash // We hash public inputs to make them constant-sized (to then be unpacked on-chain) calldata_hash : [Field; 2], From 20d8dc36023b553cedb5d09bf1aecd0b61462538 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Fri, 12 Jan 2024 11:26:53 +0000 Subject: [PATCH 3/6] Update yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr --- .../rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr | 1 + 1 file changed, 1 insertion(+) diff --git a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr index e4f680853fe..169da3b8d10 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr @@ -27,6 +27,7 @@ struct BaseOrMergeRollupPublicInputs { // U128 isn't safe if it's an input to the circuit (it won't automatically constrain the witness) // So we want to constrain it when casting these fields to U128 // TODO(#3938): split this to txs_hash and out_hash + // We hash public inputs to make them constant-sized (to then be unpacked on-chain) calldata_hash : [Field; 2], } From 62ea8350a0cee2c40414a078128741aafeb6a3f0 Mon Sep 17 00:00:00 2001 From: Tom French Date: Fri, 12 Jan 2024 11:27:50 +0000 Subject: [PATCH 4/6] chore: fix whitespace --- .../rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr index 169da3b8d10..91a5e930627 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr @@ -26,8 +26,8 @@ struct BaseOrMergeRollupPublicInputs { // We hash public inputs to make them constant-sized (to then be unpacked on-chain) // U128 isn't safe if it's an input to the circuit (it won't automatically constrain the witness) // So we want to constrain it when casting these fields to U128 + // TODO(#3938): split this to txs_hash and out_hash - // We hash public inputs to make them constant-sized (to then be unpacked on-chain) calldata_hash : [Field; 2], } From 7f3347974b26f6117a1dcf12a00897acaa31c2e2 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Mon, 15 Jan 2024 16:10:09 +0000 Subject: [PATCH 5/6] Update noir/noir_stdlib/src/uint128.nr --- noir/noir_stdlib/src/uint128.nr | 3 --- 1 file changed, 3 deletions(-) diff --git a/noir/noir_stdlib/src/uint128.nr b/noir/noir_stdlib/src/uint128.nr index 2c14576ea9c..c8c6217de90 100644 --- a/noir/noir_stdlib/src/uint128.nr +++ b/noir/noir_stdlib/src/uint128.nr @@ -126,9 +126,6 @@ impl U128 { } pub fn from_integer(i: T) -> U128 { - // TODO(Kev): Apply 128 bit range constraint and fail if this is not the case. - // We can expose a `apply_range_constraint` method from Noir which can take a field - // and return a Field. let f = crate::as_field(i); // Reject values which would overflow a u128 f.assert_max_bit_size(128); From 4f2241353c4d2878cc37fae26079c13ba6f9e00e Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Mon, 15 Jan 2024 16:10:33 +0000 Subject: [PATCH 6/6] Update yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr --- .../rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr index 91a5e930627..3cb598849cf 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/base_or_merge_rollup_public_inputs.nr @@ -26,7 +26,7 @@ struct BaseOrMergeRollupPublicInputs { // We hash public inputs to make them constant-sized (to then be unpacked on-chain) // U128 isn't safe if it's an input to the circuit (it won't automatically constrain the witness) // So we want to constrain it when casting these fields to U128 - + // TODO(#3938): split this to txs_hash and out_hash // We hash public inputs to make them constant-sized (to then be unpacked on-chain) calldata_hash : [Field; 2],