diff --git a/noir/noir_stdlib/src/uint128.nr b/noir/noir_stdlib/src/uint128.nr index 03f3e9ab0744..c8c6217de90f 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); 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 8f833310ebad..277865e99cfb 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 10eb323d07ff..3cb598849cfd 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,8 @@ 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 + // 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) 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 272e0f292b64..e8891679b207 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 bfd3690e5f2d..9188ff8abb64 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 e44769c9719c..1fca5912f628 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 @@ -136,7 +136,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; @@ -146,10 +145,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 0fd14eca1ffa..ddb4b34bbc68 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 e2f4d5dddda6..691ffb90a7bf 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 f65935ae8ff1..000000000000 --- 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 - } -}