@aztec/protocol-contracts 3.0.0-nightly.20251113 → 3.0.0-nightly.20251114
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"transpiled": true,
|
|
3
|
-
"noir_version": "1.0.0-beta.15+
|
|
3
|
+
"noir_version": "1.0.0-beta.15+b34828f17e2b52b3137fca8f7881abaf91b74ad4-aztec",
|
|
4
4
|
"name": "ContractClassRegistry",
|
|
5
5
|
"functions": [
|
|
6
6
|
{
|
|
@@ -8145,7 +8145,7 @@
|
|
|
8145
8145
|
},
|
|
8146
8146
|
"376": {
|
|
8147
8147
|
"path": "/home/aztec-dev/aztec-packages/noir-projects/noir-protocol-circuits/crates/types/src/utils/arrays.nr",
|
|
8148
|
-
"source": "pub(crate) mod assert_trailing_zeros;\npub(crate) mod find_index;\npub(crate) mod get_sorted_tuples;\n\n// Re-exports.\npub use assert_trailing_zeros::assert_trailing_zeros;\npub use find_index::{find_first_index, find_last_index};\npub use get_sorted_tuples::{get_sorted_tuples, SortedTuple};\n\nuse crate::traits::{Deserialize, Empty, Serialize};\nuse super::for_loop::{for_i_in_0_, for_i_only_in_0_};\n\n//**********************************************************************************\n// ARRAY\n//**********************************************************************************\n\n// TODO: Consider making this a part of the noir stdlib.\n/// Helper fn to create a subarray from a given array.\npub fn subarray<T, let N: u32, let M: u32>(array: [T; N], offset: u32) -> [T; M]\nwhere\n T: Empty,\n{\n let mut result: [T; M] = [T::empty(); M];\n for i in 0..M {\n result[i] = array[offset + i];\n }\n result\n}\n\n// Helper function to find the index of the first element in an array that satisfies a given predicate.\n// If the element is not found, the function returns Option::none.\n// TODO: Consider making this a part of the noir stdlib.\npub unconstrained fn find_index_hint<T, let N: u32, Env>(\n array: [T; N],\n find: fn[Env](T) -> bool,\n) -> Option<u32> {\n let mut index: Option<u32> = Option::none();\n for i in 0..N {\n if find(array[i]) {\n index = Option::some(i);\n break;\n }\n }\n index\n}\n\n// Helper function to find the index of the first element (starting from the back) of an array that satisfies a given predicate.\n// If the element is not found, the function returns Option::none.\n// TODO: Consider making this a part of the noir stdlib.\npub unconstrained fn find_index_hint_in_reverse<T, let N: u32, Env>(\n array: [T; N],\n find: fn[Env](T) -> bool,\n) -> Option<u32> {\n let mut index: Option<u32> = Option::none();\n for i in 0..N {\n let j = N - i - 1;\n if find(array[j]) {\n index = Option::some(j);\n break;\n }\n }\n index\n}\n\n//**********************************************************************************\n// FREE ARRAY FUNCTIONS (to deprecate or make into methods of array wrappers)\n//**********************************************************************************\n\n/// Deprecated.\n///\n/// Helper function to count the number of non-empty elements in a validated array.\n/// Important: Only use it for validated arrays where validate_array(array) returns true,\n/// which ensures that:\n/// 1. All elements before the first empty element are non-empty\n/// 2. All elements after and including the first empty element are empty\n/// 3. The array forms a contiguous sequence of non-empty elements followed by empty elements\npub fn array_length<T, let N: u32>(array: [T; N]) -> u32\nwhere\n T: Empty,\n{\n // We get the length by checking the index of the first empty element.\n\n // Safety: This is safe because we have validated the array (see function doc above) and the emptiness\n // of the element and non-emptiness of the previous element is checked below.\n let maybe_length = unsafe { find_index_hint(array, |elem: T| elem.is_empty()) };\n\n let mut length = N;\n\n if maybe_length.is_some() {\n length = maybe_length.unwrap_unchecked();\n\n array[length].assert_empty(\"Expected array empty\");\n }\n\n if length != 0 {\n assert(!array[length - 1].is_empty());\n }\n\n length\n}\n\n// Returns an array length defined by fully trimming _all_ \"empty\" items\n// from the RHS.\npub unconstrained fn trimmed_array_length_hint<T, let N: u32>(array: [T; N]) -> u32\nwhere\n T: Empty,\n{\n let maybe_index_of_last_nonempty =\n find_index_hint_in_reverse(array, |elem: T| !elem.is_empty());\n let length: u32 = if maybe_index_of_last_nonempty.is_some() {\n 1 + maybe_index_of_last_nonempty.unwrap_unchecked()\n } else {\n 0\n };\n length\n}\n\n/// This function assumes that `array1` and `array2` contain no more than N non-empty elements between them,\n/// if this is not the case then elements from the end of `array2` will be dropped.\npub fn array_merge<T, let N: u32>(array1: [T; N], array2: [T; N]) -> [T; N]\nwhere\n T: Empty,\n{\n // Safety: we constrain this array below\n let result = unsafe { array_merge_helper(array1, array2) };\n // We assume arrays have been validated. The only use cases so far are with previously validated arrays.\n let array1_len = array_length(array1);\n let mut add_from_left = true;\n for i in 0..N {\n add_from_left &= i != array1_len;\n if add_from_left {\n assert_eq(result[i], array1[i]);\n } else {\n assert_eq(result[i], array2[i - array1_len]);\n }\n }\n result\n}\n\nunconstrained fn array_merge_helper<T, let N: u32>(array1: [T; N], array2: [T; N]) -> [T; N]\nwhere\n T: Empty,\n{\n let mut result: [T; N] = [T::empty(); N];\n let mut i = 0;\n for elem in array1 {\n if !elem.is_empty() {\n result[i] = elem;\n i += 1;\n }\n }\n for elem in array2 {\n if !elem.is_empty() {\n result[i] = elem;\n i += 1;\n }\n }\n result\n}\n\n// Returns the number of consecutive elements at the start of the array for which the predicate returns false.\n// This function ensures that any element after the first matching element (predicate returns true) also matches the predicate.\npub fn array_length_until<T, let N: u32, Env>(array: [T; N], predicate: fn[Env](T) -> bool) -> u32 {\n let mut length = 0;\n let mut stop = false;\n for i in 0..N {\n if predicate(array[i]) {\n stop = true;\n } else {\n assert(\n stop == false,\n \"matching element found after already encountering a non-matching element\",\n );\n length += 1;\n }\n }\n length\n}\n\npub fn check_permutation<T, let N: u32>(\n original_array: [T; N],\n permuted_array: [T; N],\n original_indexes: [u32; N],\n)\nwhere\n T: Eq,\n{\n let mut seen_value = [false; N];\n for i in 0..N {\n let index = original_indexes[i];\n let original_value = original_array[index];\n assert(permuted_array[i].eq(original_value), \"Invalid index\");\n assert(!seen_value[index], \"Duplicated index\");\n seen_value[index] = true;\n }\n}\n\n// Helper function to check if an array is padded with a given value from a given index.\n// Different to padded_array_length in that it allows the elements before the given index to be the same as the padded value.\npub fn array_padded_with<T, let N: u32>(array: [T; N], from_index: u32, padded_with: T) -> bool\nwhere\n T: Eq,\n{\n let mut is_valid = true;\n let mut should_check = false;\n for i in 0..N {\n should_check |= i == from_index;\n is_valid &= !should_check | (array[i] == padded_with);\n }\n is_valid\n}\n\n//**********************************************************************************\n// ARRAY WRAPPERS\n//**********************************************************************************\n\n/*\n *\n *\n * \n * |-----------------------------------------|------------------------------| \n * | LHS | RHS |\n * |-----------------------------------------|------------------------------|\n * ClaimedLengthArray | Interspersed 0s possible. | Unvalidated. |\n * | Possibly not fully trimmed. | Nonempty elements possible. |\n * |-----------------------------------------|------------------------------|\n * EmptyRHSArray | Interspersed 0s possible. | All 0s (validated). |\n * | Possibly not fully trimmed. | |\n * |-----------------------------------------|------------------------------|\n * TrimmedArray | Interspersed 0s possible. | All 0s (validated) |\n * | Last lhs element validated as nonempty. | |\n * | (I.e. fully trimmed) | |\n * |-----------------------------------------|------------------------------|\n * DenseTrimmedArray | Dense (validated). | All 0s (validated) |\n * |-----------------------------------------|------------------------------|\n *\n *\n * | What guarantees do we have? |\n * |--------|--------|--------------------------------| \n * | Dense? | RHS | Length vs Fully Trimmed Length |\n * |--------|--------|--------------------------------|\n * ClaimedLengthArray | ? | ? | ? |\n * | | | |\n * |--------|--------|--------------------------------|\n * EmptyRHSArray | ? | All 0s | Length >= Fully Trimmed Length |\n * | | | |\n * |--------|--------|--------------------------------|\n * TrimmedArray | ? | All 0s | Length == Fully Trimmed Length |\n * | | | |\n * | | | |\n * |--------|--------|--------------------------------|\n * DenseTrimmedArray | Yes | All 0s | Length == Fully Trimmed Length |\n * |--------|--------|--------------------------------|\n *\n *\n * An ClaimedLengthArray is distinct from a regular array [T; N], because it carries a length.\n * \n */\n\n/// ClaimedLengthArray - An array interpreted by Kernel circuits.\n/// Its `length` is merely a claim that must eventually be validated.\n/// Validation must include:\n/// - Asserting all items to the LHS of the length are nonempty (dense).\n/// - Asserting all items to the RHS of the length are empty.\n#[derive(Deserialize, Serialize)]\npub struct ClaimedLengthArray<T, let N: u32> {\n pub array: [T; N],\n pub length: u32,\n}\n\nimpl<T, let N: u32> ClaimedLengthArray<T, N>\nwhere\n T: Empty,\n{\n // No constructor. Append to an empty one.\n\n // For constrained append functions, see the dedicated file: assert_array_appended.nr\n\n pub fn assert_dense_trimmed(self) {\n for_i_in_0_(\n self.length,\n self.array.len(),\n |i| {\n assert(!self.array[i].is_empty(), \"LHS of input array is not dense\")\n // Requires Noir #9002:\n // self.array[i].assert_not_empty(\"LHS of input array is not dense\"); // LHS of input array is not dense.\n },\n |i| self.array[i].assert_empty(\"RHS of input array is not empty\"),\n false,\n );\n }\n\n pub fn assert_empty<let S: u32>(self, msg: str<S>) {\n for i in 0..N {\n self.array[i].assert_empty(msg);\n }\n assert_eq(self.length, 0);\n }\n\n pub fn push(&mut self, item: T) {\n assert(self.length != N, \"Array full\");\n\n let next_index = self.length;\n self.array[next_index] = item;\n self.length += 1;\n }\n\n pub fn pop(&mut self) -> T {\n assert(self.length != 0, \"Array empty\");\n\n let mut top_index = self.length - 1;\n let popped_item = self.array[top_index];\n self.array[top_index] = T::empty();\n self.length -= 1;\n popped_item\n }\n\n pub fn for_each<Env>(self, f: fn[Env](T) -> ()) {\n // We pass `false`, because by construction, we should know that self.length <= self.array.len().\n for_i_only_in_0_(self.length, self.array.len(), |i| f(self.array[i]), false);\n }\n\n // E.g.\n // dest.for_each_i(|source_item, i| { assert_eq(dest.array[i], source_item, \"bad copy\"); })\n pub fn for_each_i<Env>(self, f: fn[Env](T, u32) -> ()) {\n // We pass `false`, because by construction, we should know that self.length <= self.array.len().\n for_i_only_in_0_(\n self.length,\n self.array.len(),\n |i| f(self.array[i], i),\n false,\n );\n }\n\n pub fn from_bounded_vec(vec: BoundedVec<T, N>) -> Self {\n Self { array: vec.storage(), length: vec.len() }\n }\n}\n\n// TODO: compiler bug. No idea why this is needed, if we have #[derive(Eq)] above the struct definition.\nimpl<T, let N: u32> Eq for ClaimedLengthArray<T, N>\nwhere\n T: Eq,\n{\n fn eq(self, other: Self) -> bool {\n (self.array == other.array) & (self.length == other.length)\n }\n}\n\nimpl<T, let N: u32> Empty for ClaimedLengthArray<T, N>\nwhere\n T: Empty,\n{\n fn empty() -> Self {\n Self { array: [T::empty(); N], length: 0 }\n }\n}\n\n#[test]\nfn test_empty_array_length() {\n assert_eq(array_length([0]), 0);\n assert_eq(array_length([0, 0, 0]), 0);\n}\n\n#[test]\nfn test_array_length() {\n assert_eq(array_length([123]), 1);\n assert_eq(array_length([123, 0, 0]), 1);\n assert_eq(array_length([123, 456]), 2);\n assert_eq(array_length([123, 456, 0]), 2);\n}\n\n#[test]\nfn test_array_length_invalid_arrays() {\n // Result can be misleading (but correct) for invalid arrays.\n assert_eq(array_length([0, 0, 123]), 0);\n assert_eq(array_length([0, 123, 0]), 0);\n assert_eq(array_length([0, 123, 456]), 0);\n assert_eq(array_length([123, 0, 456]), 1);\n}\n\n#[test]\nfn test_array_length_until() {\n let array = [11, 22, 33, 44, 55];\n assert_eq(array_length_until(array, |x| x == 55), 4);\n assert_eq(array_length_until(array, |x| x == 56), 5);\n assert_eq(array_length_until(array, |x| x > 40), 3);\n assert_eq(array_length_until(array, |x| x > 10), 0);\n}\n\n#[test(should_fail_with = \"matching element found after already encountering a non-matching element\")]\nfn test_array_length_until_non_consecutive_fails() {\n let array = [1, 1, 0, 1, 0];\n let _ = array_length_until(array, |x| x == 0);\n}\n\n#[test(should_fail_with = \"matching element found after already encountering a non-matching element\")]\nfn test_array_length_until_first_non_matching_fails() {\n let array = [1, 0, 0, 0, 0];\n let _ = array_length_until(array, |x| x == 1);\n}\n\n#[test]\nunconstrained fn find_index_greater_than_min() {\n let values = [10, 20, 30, 40];\n let min = 22;\n let maybe_index = find_index_hint(values, |v: Field| min.lt(v));\n assert_eq(maybe_index.unwrap_unchecked(), 2);\n}\n\n#[test]\nunconstrained fn find_index_not_found() {\n let values = [10, 20, 30, 40];\n let min = 100;\n let maybe_index = find_index_hint(values, |v: Field| min.lt(v));\n assert_eq(maybe_index.is_none(), true);\n}\n\n#[test]\nfn check_permutation_basic_test() {\n let original_array = [1, 2, 3];\n let permuted_array = [3, 1, 2];\n let indexes = [2, 0, 1];\n check_permutation(original_array, permuted_array, indexes);\n}\n\n#[test(should_fail_with = \"Duplicated index\")]\nfn check_permutation_duplicated_index() {\n let original_array = [0, 1, 0];\n let permuted_array = [1, 0, 0];\n let indexes = [1, 0, 0];\n check_permutation(original_array, permuted_array, indexes);\n}\n\n#[test(should_fail_with = \"Invalid index\")]\nfn check_permutation_invalid_index() {\n let original_array = [0, 1, 2];\n let permuted_array = [1, 0, 0];\n let indexes = [1, 0, 2];\n check_permutation(original_array, permuted_array, indexes);\n}\n\n#[test]\nfn test_array_padded_with() {\n let array = [11, 22, 33, 44, 44];\n assert_eq(array_padded_with(array, 0, 44), false);\n assert_eq(array_padded_with(array, 1, 44), false);\n assert_eq(array_padded_with(array, 2, 44), false);\n assert_eq(array_padded_with(array, 3, 44), true);\n assert_eq(array_padded_with(array, 4, 44), true);\n assert_eq(array_padded_with(array, 4, 33), false);\n assert_eq(array_padded_with(array, 5, 44), true); // Index out of bounds.\n assert_eq(array_padded_with(array, 0, 11), false);\n}\n"
|
|
8148
|
+
"source": "pub(crate) mod assert_trailing_zeros;\npub(crate) mod find_index;\npub(crate) mod get_sorted_tuples;\n\n// Re-exports.\npub use assert_trailing_zeros::assert_trailing_zeros;\npub use find_index::{find_first_index, find_last_index};\npub use get_sorted_tuples::{get_sorted_tuples, SortedTuple};\n\nuse crate::traits::{Deserialize, Empty, Serialize};\nuse super::for_loop::{for_i_in_0_, for_i_only_in_0_};\n\n//**********************************************************************************\n// ARRAY\n//**********************************************************************************\n\n// TODO: Consider making this a part of the noir stdlib.\n/// Helper fn to create a subarray from a given array.\npub fn subarray<T, let N: u32, let M: u32>(array: [T; N], offset: u32) -> [T; M]\nwhere\n T: Empty,\n{\n let mut result: [T; M] = [T::empty(); M];\n for i in 0..M {\n result[i] = array[offset + i];\n }\n result\n}\n\n// Helper function to find the index of the first element in an array that satisfies a given predicate.\n// If the element is not found, the function returns Option::none.\n// TODO: Consider making this a part of the noir stdlib.\npub unconstrained fn find_index_hint<T, let N: u32, Env>(\n array: [T; N],\n find: fn[Env](T) -> bool,\n) -> Option<u32> {\n let mut index: Option<u32> = Option::none();\n for i in 0..N {\n if find(array[i]) {\n index = Option::some(i);\n break;\n }\n }\n index\n}\n\n// Helper function to find the index of the first element (starting from the back) of an array that satisfies a given predicate.\n// If the element is not found, the function returns Option::none.\n// TODO: Consider making this a part of the noir stdlib.\npub unconstrained fn find_index_hint_in_reverse<T, let N: u32, Env>(\n array: [T; N],\n find: fn[Env](T) -> bool,\n) -> Option<u32> {\n let mut index: Option<u32> = Option::none();\n for i in 0..N {\n let j = N - i - 1;\n if find(array[j]) {\n index = Option::some(j);\n break;\n }\n }\n index\n}\n\n//**********************************************************************************\n// FREE ARRAY FUNCTIONS (to deprecate or make into methods of array wrappers)\n//**********************************************************************************\n\n/// Deprecated.\n///\n/// Helper function to count the number of non-empty elements in a validated array.\n/// Important: Only use it for validated arrays where validate_array(array) returns true,\n/// which ensures that:\n/// 1. All elements before the first empty element are non-empty\n/// 2. All elements after and including the first empty element are empty\n/// 3. The array forms a contiguous sequence of non-empty elements followed by empty elements\npub fn array_length<T, let N: u32>(array: [T; N]) -> u32\nwhere\n T: Empty,\n{\n // We get the length by checking the index of the first empty element.\n\n // Safety: This is safe because we have validated the array (see function doc above) and the emptiness\n // of the element and non-emptiness of the previous element is checked below.\n let maybe_length = unsafe { find_index_hint(array, |elem: T| elem.is_empty()) };\n\n let mut length = N;\n\n if maybe_length.is_some() {\n length = maybe_length.unwrap_unchecked();\n\n array[length].assert_empty(\"Expected array empty\");\n }\n\n if length != 0 {\n assert(!array[length - 1].is_empty());\n }\n\n length\n}\n\n// Returns an array length defined by fully trimming _all_ \"empty\" items\n// from the RHS.\npub unconstrained fn trimmed_array_length_hint<T, let N: u32>(array: [T; N]) -> u32\nwhere\n T: Empty,\n{\n let maybe_index_of_last_nonempty =\n find_index_hint_in_reverse(array, |elem: T| !elem.is_empty());\n let length: u32 = if maybe_index_of_last_nonempty.is_some() {\n 1 + maybe_index_of_last_nonempty.unwrap_unchecked()\n } else {\n 0\n };\n length\n}\n\n/// This function assumes that `array1` and `array2` contain no more than N non-empty elements between them,\n/// if this is not the case then elements from the end of `array2` will be dropped.\npub fn array_merge<T, let N: u32>(array1: [T; N], array2: [T; N]) -> [T; N]\nwhere\n T: Empty,\n{\n // Safety: we constrain this array below\n let result = unsafe { array_merge_helper(array1, array2) };\n // We assume arrays have been validated. The only use cases so far are with previously validated arrays.\n let array1_len = array_length(array1);\n let mut add_from_left = true;\n for i in 0..N {\n add_from_left &= i != array1_len;\n if add_from_left {\n assert_eq(result[i], array1[i]);\n } else {\n assert_eq(result[i], array2[i - array1_len]);\n }\n }\n result\n}\n\nunconstrained fn array_merge_helper<T, let N: u32>(array1: [T; N], array2: [T; N]) -> [T; N]\nwhere\n T: Empty,\n{\n let mut result: [T; N] = [T::empty(); N];\n let mut i = 0;\n for elem in array1 {\n if !elem.is_empty() {\n result[i] = elem;\n i += 1;\n }\n }\n for elem in array2 {\n if !elem.is_empty() {\n result[i] = elem;\n i += 1;\n }\n }\n result\n}\n\n// Returns the number of consecutive elements at the start of the array for which the predicate returns false.\n// This function ensures that any element after the first matching element (predicate returns true) also matches the predicate.\npub fn array_length_until<T, let N: u32, Env>(array: [T; N], predicate: fn[Env](T) -> bool) -> u32 {\n let mut length = 0;\n let mut stop = false;\n for i in 0..N {\n if predicate(array[i]) {\n stop = true;\n } else {\n assert(\n stop == false,\n \"matching element found after already encountering a non-matching element\",\n );\n length += 1;\n }\n }\n length\n}\n\npub fn check_permutation<T, let N: u32>(\n original_array: [T; N],\n permuted_array: [T; N],\n original_indexes: [u32; N],\n)\nwhere\n T: Eq,\n{\n let mut seen_value = [false; N];\n for i in 0..N {\n let index = original_indexes[i];\n let original_value = original_array[index];\n assert(permuted_array[i].eq(original_value), \"Invalid index\");\n assert(!seen_value[index], \"Duplicated index\");\n seen_value[index] = true;\n }\n}\n\n// Helper function to check if an array is padded with a given value from a given index.\n// Different to padded_array_length in that it allows the elements before the given index to be the same as the padded value.\npub fn array_padded_with<T, let N: u32>(array: [T; N], from_index: u32, padded_with: T) -> bool\nwhere\n T: Eq,\n{\n let mut is_valid = true;\n let mut should_check = false;\n for i in 0..N {\n should_check |= i == from_index;\n is_valid &= !should_check | (array[i] == padded_with);\n }\n is_valid\n}\n\n//**********************************************************************************\n// ARRAY WRAPPERS\n//**********************************************************************************\n\n/*\n *\n *\n * \n * |-----------------------------------------|------------------------------| \n * | LHS | RHS |\n * |-----------------------------------------|------------------------------|\n * ClaimedLengthArray | Interspersed 0s possible. | Unvalidated. |\n * | Possibly not fully trimmed. | Nonempty elements possible. |\n * |-----------------------------------------|------------------------------|\n * EmptyRHSArray | Interspersed 0s possible. | All 0s (validated). |\n * | Possibly not fully trimmed. | |\n * |-----------------------------------------|------------------------------|\n * TrimmedArray | Interspersed 0s possible. | All 0s (validated) |\n * | Last lhs element validated as nonempty. | |\n * | (I.e. fully trimmed) | |\n * |-----------------------------------------|------------------------------|\n * DenseTrimmedArray | Dense (validated). | All 0s (validated) |\n * |-----------------------------------------|------------------------------|\n *\n *\n * | What guarantees do we have? |\n * |--------|--------|--------------------------------| \n * | Dense? | RHS | Length vs Fully Trimmed Length |\n * |--------|--------|--------------------------------|\n * ClaimedLengthArray | ? | ? | ? |\n * | | | |\n * |--------|--------|--------------------------------|\n * EmptyRHSArray | ? | All 0s | Length >= Fully Trimmed Length |\n * | | | |\n * |--------|--------|--------------------------------|\n * TrimmedArray | ? | All 0s | Length == Fully Trimmed Length |\n * | | | |\n * | | | |\n * |--------|--------|--------------------------------|\n * DenseTrimmedArray | Yes | All 0s | Length == Fully Trimmed Length |\n * |--------|--------|--------------------------------|\n *\n *\n * An ClaimedLengthArray is distinct from a regular array [T; N], because it carries a length.\n * \n */\n\n/// ClaimedLengthArray - An array interpreted by Kernel circuits.\n/// Its `length` is merely a claim that must eventually be validated.\n/// Validation must include:\n/// - Asserting all items to the LHS of the length are nonempty (dense).\n/// - Asserting all items to the RHS of the length are empty.\n#[derive(Deserialize, Serialize)]\npub struct ClaimedLengthArray<T, let N: u32> {\n pub array: [T; N],\n pub length: u32,\n}\n\nimpl<T, let N: u32> ClaimedLengthArray<T, N>\nwhere\n T: Empty,\n{\n // No constructor. Append to an empty one.\n\n // For constrained append functions, see the dedicated file: assert_array_appended.nr\n\n pub fn assert_dense_trimmed(self) {\n for_i_in_0_(\n self.length,\n self.array.len(),\n |i| {\n assert(!self.array[i].is_empty(), \"LHS of input array is not dense\")\n // Requires Noir #9002:\n // self.array[i].assert_not_empty(\"LHS of input array is not dense\"); // LHS of input array is not dense.\n },\n |i| self.array[i].assert_empty(\"RHS of input array is not empty\"),\n false,\n );\n }\n\n pub fn assert_empty<let S: u32>(self, msg: str<S>) {\n for i in 0..N {\n self.array[i].assert_empty(msg);\n }\n assert_eq(self.length, 0);\n }\n\n pub fn assert_length_within_bounds<let S: u32>(self, msg: str<S>) {\n assert(self.length <= N, msg);\n }\n\n pub fn push(&mut self, item: T) {\n assert(self.length != N, \"Array full\");\n\n let next_index = self.length;\n self.array[next_index] = item;\n self.length += 1;\n }\n\n pub fn pop(&mut self) -> T {\n assert(self.length != 0, \"Array empty\");\n\n let mut top_index = self.length - 1;\n let popped_item = self.array[top_index];\n self.array[top_index] = T::empty();\n self.length -= 1;\n popped_item\n }\n\n pub fn for_each<Env>(self, f: fn[Env](T) -> ()) {\n // We pass `false`, because by construction, we should know that self.length <= self.array.len().\n for_i_only_in_0_(self.length, self.array.len(), |i| f(self.array[i]), false);\n }\n\n // E.g.\n // dest.for_each_i(|source_item, i| { assert_eq(dest.array[i], source_item, \"bad copy\"); })\n pub fn for_each_i<Env>(self, f: fn[Env](T, u32) -> ()) {\n // We pass `false`, because by construction, we should know that self.length <= self.array.len().\n for_i_only_in_0_(\n self.length,\n self.array.len(),\n |i| f(self.array[i], i),\n false,\n );\n }\n\n pub fn from_bounded_vec(vec: BoundedVec<T, N>) -> Self {\n Self { array: vec.storage(), length: vec.len() }\n }\n}\n\n// TODO: compiler bug. No idea why this is needed, if we have #[derive(Eq)] above the struct definition.\nimpl<T, let N: u32> Eq for ClaimedLengthArray<T, N>\nwhere\n T: Eq,\n{\n fn eq(self, other: Self) -> bool {\n (self.array == other.array) & (self.length == other.length)\n }\n}\n\nimpl<T, let N: u32> Empty for ClaimedLengthArray<T, N>\nwhere\n T: Empty,\n{\n fn empty() -> Self {\n Self { array: [T::empty(); N], length: 0 }\n }\n}\n\n#[test]\nfn test_empty_array_length() {\n assert_eq(array_length([0]), 0);\n assert_eq(array_length([0, 0, 0]), 0);\n}\n\n#[test]\nfn test_array_length() {\n assert_eq(array_length([123]), 1);\n assert_eq(array_length([123, 0, 0]), 1);\n assert_eq(array_length([123, 456]), 2);\n assert_eq(array_length([123, 456, 0]), 2);\n}\n\n#[test]\nfn test_array_length_invalid_arrays() {\n // Result can be misleading (but correct) for invalid arrays.\n assert_eq(array_length([0, 0, 123]), 0);\n assert_eq(array_length([0, 123, 0]), 0);\n assert_eq(array_length([0, 123, 456]), 0);\n assert_eq(array_length([123, 0, 456]), 1);\n}\n\n#[test]\nfn test_array_length_until() {\n let array = [11, 22, 33, 44, 55];\n assert_eq(array_length_until(array, |x| x == 55), 4);\n assert_eq(array_length_until(array, |x| x == 56), 5);\n assert_eq(array_length_until(array, |x| x > 40), 3);\n assert_eq(array_length_until(array, |x| x > 10), 0);\n}\n\n#[test(should_fail_with = \"matching element found after already encountering a non-matching element\")]\nfn test_array_length_until_non_consecutive_fails() {\n let array = [1, 1, 0, 1, 0];\n let _ = array_length_until(array, |x| x == 0);\n}\n\n#[test(should_fail_with = \"matching element found after already encountering a non-matching element\")]\nfn test_array_length_until_first_non_matching_fails() {\n let array = [1, 0, 0, 0, 0];\n let _ = array_length_until(array, |x| x == 1);\n}\n\n#[test]\nunconstrained fn find_index_greater_than_min() {\n let values = [10, 20, 30, 40];\n let min = 22;\n let maybe_index = find_index_hint(values, |v: Field| min.lt(v));\n assert_eq(maybe_index.unwrap_unchecked(), 2);\n}\n\n#[test]\nunconstrained fn find_index_not_found() {\n let values = [10, 20, 30, 40];\n let min = 100;\n let maybe_index = find_index_hint(values, |v: Field| min.lt(v));\n assert_eq(maybe_index.is_none(), true);\n}\n\n#[test]\nfn check_permutation_basic_test() {\n let original_array = [1, 2, 3];\n let permuted_array = [3, 1, 2];\n let indexes = [2, 0, 1];\n check_permutation(original_array, permuted_array, indexes);\n}\n\n#[test(should_fail_with = \"Duplicated index\")]\nfn check_permutation_duplicated_index() {\n let original_array = [0, 1, 0];\n let permuted_array = [1, 0, 0];\n let indexes = [1, 0, 0];\n check_permutation(original_array, permuted_array, indexes);\n}\n\n#[test(should_fail_with = \"Invalid index\")]\nfn check_permutation_invalid_index() {\n let original_array = [0, 1, 2];\n let permuted_array = [1, 0, 0];\n let indexes = [1, 0, 2];\n check_permutation(original_array, permuted_array, indexes);\n}\n\n#[test]\nfn test_array_padded_with() {\n let array = [11, 22, 33, 44, 44];\n assert_eq(array_padded_with(array, 0, 44), false);\n assert_eq(array_padded_with(array, 1, 44), false);\n assert_eq(array_padded_with(array, 2, 44), false);\n assert_eq(array_padded_with(array, 3, 44), true);\n assert_eq(array_padded_with(array, 4, 44), true);\n assert_eq(array_padded_with(array, 4, 33), false);\n assert_eq(array_padded_with(array, 5, 44), true); // Index out of bounds.\n assert_eq(array_padded_with(array, 0, 11), false);\n}\n"
|
|
8149
8149
|
},
|
|
8150
8150
|
"377": {
|
|
8151
8151
|
"path": "/home/aztec-dev/aztec-packages/noir-projects/noir-protocol-circuits/crates/types/src/utils/field.nr",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"transpiled": true,
|
|
3
|
-
"noir_version": "1.0.0-beta.15+
|
|
3
|
+
"noir_version": "1.0.0-beta.15+b34828f17e2b52b3137fca8f7881abaf91b74ad4-aztec",
|
|
4
4
|
"name": "ContractInstanceRegistry",
|
|
5
5
|
"functions": [
|
|
6
6
|
{
|
|
@@ -3506,7 +3506,7 @@
|
|
|
3506
3506
|
},
|
|
3507
3507
|
"372": {
|
|
3508
3508
|
"path": "/home/aztec-dev/aztec-packages/noir-projects/noir-protocol-circuits/crates/types/src/utils/arrays.nr",
|
|
3509
|
-
"source": "pub(crate) mod assert_trailing_zeros;\npub(crate) mod find_index;\npub(crate) mod get_sorted_tuples;\n\n// Re-exports.\npub use assert_trailing_zeros::assert_trailing_zeros;\npub use find_index::{find_first_index, find_last_index};\npub use get_sorted_tuples::{get_sorted_tuples, SortedTuple};\n\nuse crate::traits::{Deserialize, Empty, Serialize};\nuse super::for_loop::{for_i_in_0_, for_i_only_in_0_};\n\n//**********************************************************************************\n// ARRAY\n//**********************************************************************************\n\n// TODO: Consider making this a part of the noir stdlib.\n/// Helper fn to create a subarray from a given array.\npub fn subarray<T, let N: u32, let M: u32>(array: [T; N], offset: u32) -> [T; M]\nwhere\n T: Empty,\n{\n let mut result: [T; M] = [T::empty(); M];\n for i in 0..M {\n result[i] = array[offset + i];\n }\n result\n}\n\n// Helper function to find the index of the first element in an array that satisfies a given predicate.\n// If the element is not found, the function returns Option::none.\n// TODO: Consider making this a part of the noir stdlib.\npub unconstrained fn find_index_hint<T, let N: u32, Env>(\n array: [T; N],\n find: fn[Env](T) -> bool,\n) -> Option<u32> {\n let mut index: Option<u32> = Option::none();\n for i in 0..N {\n if find(array[i]) {\n index = Option::some(i);\n break;\n }\n }\n index\n}\n\n// Helper function to find the index of the first element (starting from the back) of an array that satisfies a given predicate.\n// If the element is not found, the function returns Option::none.\n// TODO: Consider making this a part of the noir stdlib.\npub unconstrained fn find_index_hint_in_reverse<T, let N: u32, Env>(\n array: [T; N],\n find: fn[Env](T) -> bool,\n) -> Option<u32> {\n let mut index: Option<u32> = Option::none();\n for i in 0..N {\n let j = N - i - 1;\n if find(array[j]) {\n index = Option::some(j);\n break;\n }\n }\n index\n}\n\n//**********************************************************************************\n// FREE ARRAY FUNCTIONS (to deprecate or make into methods of array wrappers)\n//**********************************************************************************\n\n/// Deprecated.\n///\n/// Helper function to count the number of non-empty elements in a validated array.\n/// Important: Only use it for validated arrays where validate_array(array) returns true,\n/// which ensures that:\n/// 1. All elements before the first empty element are non-empty\n/// 2. All elements after and including the first empty element are empty\n/// 3. The array forms a contiguous sequence of non-empty elements followed by empty elements\npub fn array_length<T, let N: u32>(array: [T; N]) -> u32\nwhere\n T: Empty,\n{\n // We get the length by checking the index of the first empty element.\n\n // Safety: This is safe because we have validated the array (see function doc above) and the emptiness\n // of the element and non-emptiness of the previous element is checked below.\n let maybe_length = unsafe { find_index_hint(array, |elem: T| elem.is_empty()) };\n\n let mut length = N;\n\n if maybe_length.is_some() {\n length = maybe_length.unwrap_unchecked();\n\n array[length].assert_empty(\"Expected array empty\");\n }\n\n if length != 0 {\n assert(!array[length - 1].is_empty());\n }\n\n length\n}\n\n// Returns an array length defined by fully trimming _all_ \"empty\" items\n// from the RHS.\npub unconstrained fn trimmed_array_length_hint<T, let N: u32>(array: [T; N]) -> u32\nwhere\n T: Empty,\n{\n let maybe_index_of_last_nonempty =\n find_index_hint_in_reverse(array, |elem: T| !elem.is_empty());\n let length: u32 = if maybe_index_of_last_nonempty.is_some() {\n 1 + maybe_index_of_last_nonempty.unwrap_unchecked()\n } else {\n 0\n };\n length\n}\n\n/// This function assumes that `array1` and `array2` contain no more than N non-empty elements between them,\n/// if this is not the case then elements from the end of `array2` will be dropped.\npub fn array_merge<T, let N: u32>(array1: [T; N], array2: [T; N]) -> [T; N]\nwhere\n T: Empty,\n{\n // Safety: we constrain this array below\n let result = unsafe { array_merge_helper(array1, array2) };\n // We assume arrays have been validated. The only use cases so far are with previously validated arrays.\n let array1_len = array_length(array1);\n let mut add_from_left = true;\n for i in 0..N {\n add_from_left &= i != array1_len;\n if add_from_left {\n assert_eq(result[i], array1[i]);\n } else {\n assert_eq(result[i], array2[i - array1_len]);\n }\n }\n result\n}\n\nunconstrained fn array_merge_helper<T, let N: u32>(array1: [T; N], array2: [T; N]) -> [T; N]\nwhere\n T: Empty,\n{\n let mut result: [T; N] = [T::empty(); N];\n let mut i = 0;\n for elem in array1 {\n if !elem.is_empty() {\n result[i] = elem;\n i += 1;\n }\n }\n for elem in array2 {\n if !elem.is_empty() {\n result[i] = elem;\n i += 1;\n }\n }\n result\n}\n\n// Returns the number of consecutive elements at the start of the array for which the predicate returns false.\n// This function ensures that any element after the first matching element (predicate returns true) also matches the predicate.\npub fn array_length_until<T, let N: u32, Env>(array: [T; N], predicate: fn[Env](T) -> bool) -> u32 {\n let mut length = 0;\n let mut stop = false;\n for i in 0..N {\n if predicate(array[i]) {\n stop = true;\n } else {\n assert(\n stop == false,\n \"matching element found after already encountering a non-matching element\",\n );\n length += 1;\n }\n }\n length\n}\n\npub fn check_permutation<T, let N: u32>(\n original_array: [T; N],\n permuted_array: [T; N],\n original_indexes: [u32; N],\n)\nwhere\n T: Eq,\n{\n let mut seen_value = [false; N];\n for i in 0..N {\n let index = original_indexes[i];\n let original_value = original_array[index];\n assert(permuted_array[i].eq(original_value), \"Invalid index\");\n assert(!seen_value[index], \"Duplicated index\");\n seen_value[index] = true;\n }\n}\n\n// Helper function to check if an array is padded with a given value from a given index.\n// Different to padded_array_length in that it allows the elements before the given index to be the same as the padded value.\npub fn array_padded_with<T, let N: u32>(array: [T; N], from_index: u32, padded_with: T) -> bool\nwhere\n T: Eq,\n{\n let mut is_valid = true;\n let mut should_check = false;\n for i in 0..N {\n should_check |= i == from_index;\n is_valid &= !should_check | (array[i] == padded_with);\n }\n is_valid\n}\n\n//**********************************************************************************\n// ARRAY WRAPPERS\n//**********************************************************************************\n\n/*\n *\n *\n * \n * |-----------------------------------------|------------------------------| \n * | LHS | RHS |\n * |-----------------------------------------|------------------------------|\n * ClaimedLengthArray | Interspersed 0s possible. | Unvalidated. |\n * | Possibly not fully trimmed. | Nonempty elements possible. |\n * |-----------------------------------------|------------------------------|\n * EmptyRHSArray | Interspersed 0s possible. | All 0s (validated). |\n * | Possibly not fully trimmed. | |\n * |-----------------------------------------|------------------------------|\n * TrimmedArray | Interspersed 0s possible. | All 0s (validated) |\n * | Last lhs element validated as nonempty. | |\n * | (I.e. fully trimmed) | |\n * |-----------------------------------------|------------------------------|\n * DenseTrimmedArray | Dense (validated). | All 0s (validated) |\n * |-----------------------------------------|------------------------------|\n *\n *\n * | What guarantees do we have? |\n * |--------|--------|--------------------------------| \n * | Dense? | RHS | Length vs Fully Trimmed Length |\n * |--------|--------|--------------------------------|\n * ClaimedLengthArray | ? | ? | ? |\n * | | | |\n * |--------|--------|--------------------------------|\n * EmptyRHSArray | ? | All 0s | Length >= Fully Trimmed Length |\n * | | | |\n * |--------|--------|--------------------------------|\n * TrimmedArray | ? | All 0s | Length == Fully Trimmed Length |\n * | | | |\n * | | | |\n * |--------|--------|--------------------------------|\n * DenseTrimmedArray | Yes | All 0s | Length == Fully Trimmed Length |\n * |--------|--------|--------------------------------|\n *\n *\n * An ClaimedLengthArray is distinct from a regular array [T; N], because it carries a length.\n * \n */\n\n/// ClaimedLengthArray - An array interpreted by Kernel circuits.\n/// Its `length` is merely a claim that must eventually be validated.\n/// Validation must include:\n/// - Asserting all items to the LHS of the length are nonempty (dense).\n/// - Asserting all items to the RHS of the length are empty.\n#[derive(Deserialize, Serialize)]\npub struct ClaimedLengthArray<T, let N: u32> {\n pub array: [T; N],\n pub length: u32,\n}\n\nimpl<T, let N: u32> ClaimedLengthArray<T, N>\nwhere\n T: Empty,\n{\n // No constructor. Append to an empty one.\n\n // For constrained append functions, see the dedicated file: assert_array_appended.nr\n\n pub fn assert_dense_trimmed(self) {\n for_i_in_0_(\n self.length,\n self.array.len(),\n |i| {\n assert(!self.array[i].is_empty(), \"LHS of input array is not dense\")\n // Requires Noir #9002:\n // self.array[i].assert_not_empty(\"LHS of input array is not dense\"); // LHS of input array is not dense.\n },\n |i| self.array[i].assert_empty(\"RHS of input array is not empty\"),\n false,\n );\n }\n\n pub fn assert_empty<let S: u32>(self, msg: str<S>) {\n for i in 0..N {\n self.array[i].assert_empty(msg);\n }\n assert_eq(self.length, 0);\n }\n\n pub fn push(&mut self, item: T) {\n assert(self.length != N, \"Array full\");\n\n let next_index = self.length;\n self.array[next_index] = item;\n self.length += 1;\n }\n\n pub fn pop(&mut self) -> T {\n assert(self.length != 0, \"Array empty\");\n\n let mut top_index = self.length - 1;\n let popped_item = self.array[top_index];\n self.array[top_index] = T::empty();\n self.length -= 1;\n popped_item\n }\n\n pub fn for_each<Env>(self, f: fn[Env](T) -> ()) {\n // We pass `false`, because by construction, we should know that self.length <= self.array.len().\n for_i_only_in_0_(self.length, self.array.len(), |i| f(self.array[i]), false);\n }\n\n // E.g.\n // dest.for_each_i(|source_item, i| { assert_eq(dest.array[i], source_item, \"bad copy\"); })\n pub fn for_each_i<Env>(self, f: fn[Env](T, u32) -> ()) {\n // We pass `false`, because by construction, we should know that self.length <= self.array.len().\n for_i_only_in_0_(\n self.length,\n self.array.len(),\n |i| f(self.array[i], i),\n false,\n );\n }\n\n pub fn from_bounded_vec(vec: BoundedVec<T, N>) -> Self {\n Self { array: vec.storage(), length: vec.len() }\n }\n}\n\n// TODO: compiler bug. No idea why this is needed, if we have #[derive(Eq)] above the struct definition.\nimpl<T, let N: u32> Eq for ClaimedLengthArray<T, N>\nwhere\n T: Eq,\n{\n fn eq(self, other: Self) -> bool {\n (self.array == other.array) & (self.length == other.length)\n }\n}\n\nimpl<T, let N: u32> Empty for ClaimedLengthArray<T, N>\nwhere\n T: Empty,\n{\n fn empty() -> Self {\n Self { array: [T::empty(); N], length: 0 }\n }\n}\n\n#[test]\nfn test_empty_array_length() {\n assert_eq(array_length([0]), 0);\n assert_eq(array_length([0, 0, 0]), 0);\n}\n\n#[test]\nfn test_array_length() {\n assert_eq(array_length([123]), 1);\n assert_eq(array_length([123, 0, 0]), 1);\n assert_eq(array_length([123, 456]), 2);\n assert_eq(array_length([123, 456, 0]), 2);\n}\n\n#[test]\nfn test_array_length_invalid_arrays() {\n // Result can be misleading (but correct) for invalid arrays.\n assert_eq(array_length([0, 0, 123]), 0);\n assert_eq(array_length([0, 123, 0]), 0);\n assert_eq(array_length([0, 123, 456]), 0);\n assert_eq(array_length([123, 0, 456]), 1);\n}\n\n#[test]\nfn test_array_length_until() {\n let array = [11, 22, 33, 44, 55];\n assert_eq(array_length_until(array, |x| x == 55), 4);\n assert_eq(array_length_until(array, |x| x == 56), 5);\n assert_eq(array_length_until(array, |x| x > 40), 3);\n assert_eq(array_length_until(array, |x| x > 10), 0);\n}\n\n#[test(should_fail_with = \"matching element found after already encountering a non-matching element\")]\nfn test_array_length_until_non_consecutive_fails() {\n let array = [1, 1, 0, 1, 0];\n let _ = array_length_until(array, |x| x == 0);\n}\n\n#[test(should_fail_with = \"matching element found after already encountering a non-matching element\")]\nfn test_array_length_until_first_non_matching_fails() {\n let array = [1, 0, 0, 0, 0];\n let _ = array_length_until(array, |x| x == 1);\n}\n\n#[test]\nunconstrained fn find_index_greater_than_min() {\n let values = [10, 20, 30, 40];\n let min = 22;\n let maybe_index = find_index_hint(values, |v: Field| min.lt(v));\n assert_eq(maybe_index.unwrap_unchecked(), 2);\n}\n\n#[test]\nunconstrained fn find_index_not_found() {\n let values = [10, 20, 30, 40];\n let min = 100;\n let maybe_index = find_index_hint(values, |v: Field| min.lt(v));\n assert_eq(maybe_index.is_none(), true);\n}\n\n#[test]\nfn check_permutation_basic_test() {\n let original_array = [1, 2, 3];\n let permuted_array = [3, 1, 2];\n let indexes = [2, 0, 1];\n check_permutation(original_array, permuted_array, indexes);\n}\n\n#[test(should_fail_with = \"Duplicated index\")]\nfn check_permutation_duplicated_index() {\n let original_array = [0, 1, 0];\n let permuted_array = [1, 0, 0];\n let indexes = [1, 0, 0];\n check_permutation(original_array, permuted_array, indexes);\n}\n\n#[test(should_fail_with = \"Invalid index\")]\nfn check_permutation_invalid_index() {\n let original_array = [0, 1, 2];\n let permuted_array = [1, 0, 0];\n let indexes = [1, 0, 2];\n check_permutation(original_array, permuted_array, indexes);\n}\n\n#[test]\nfn test_array_padded_with() {\n let array = [11, 22, 33, 44, 44];\n assert_eq(array_padded_with(array, 0, 44), false);\n assert_eq(array_padded_with(array, 1, 44), false);\n assert_eq(array_padded_with(array, 2, 44), false);\n assert_eq(array_padded_with(array, 3, 44), true);\n assert_eq(array_padded_with(array, 4, 44), true);\n assert_eq(array_padded_with(array, 4, 33), false);\n assert_eq(array_padded_with(array, 5, 44), true); // Index out of bounds.\n assert_eq(array_padded_with(array, 0, 11), false);\n}\n"
|
|
3509
|
+
"source": "pub(crate) mod assert_trailing_zeros;\npub(crate) mod find_index;\npub(crate) mod get_sorted_tuples;\n\n// Re-exports.\npub use assert_trailing_zeros::assert_trailing_zeros;\npub use find_index::{find_first_index, find_last_index};\npub use get_sorted_tuples::{get_sorted_tuples, SortedTuple};\n\nuse crate::traits::{Deserialize, Empty, Serialize};\nuse super::for_loop::{for_i_in_0_, for_i_only_in_0_};\n\n//**********************************************************************************\n// ARRAY\n//**********************************************************************************\n\n// TODO: Consider making this a part of the noir stdlib.\n/// Helper fn to create a subarray from a given array.\npub fn subarray<T, let N: u32, let M: u32>(array: [T; N], offset: u32) -> [T; M]\nwhere\n T: Empty,\n{\n let mut result: [T; M] = [T::empty(); M];\n for i in 0..M {\n result[i] = array[offset + i];\n }\n result\n}\n\n// Helper function to find the index of the first element in an array that satisfies a given predicate.\n// If the element is not found, the function returns Option::none.\n// TODO: Consider making this a part of the noir stdlib.\npub unconstrained fn find_index_hint<T, let N: u32, Env>(\n array: [T; N],\n find: fn[Env](T) -> bool,\n) -> Option<u32> {\n let mut index: Option<u32> = Option::none();\n for i in 0..N {\n if find(array[i]) {\n index = Option::some(i);\n break;\n }\n }\n index\n}\n\n// Helper function to find the index of the first element (starting from the back) of an array that satisfies a given predicate.\n// If the element is not found, the function returns Option::none.\n// TODO: Consider making this a part of the noir stdlib.\npub unconstrained fn find_index_hint_in_reverse<T, let N: u32, Env>(\n array: [T; N],\n find: fn[Env](T) -> bool,\n) -> Option<u32> {\n let mut index: Option<u32> = Option::none();\n for i in 0..N {\n let j = N - i - 1;\n if find(array[j]) {\n index = Option::some(j);\n break;\n }\n }\n index\n}\n\n//**********************************************************************************\n// FREE ARRAY FUNCTIONS (to deprecate or make into methods of array wrappers)\n//**********************************************************************************\n\n/// Deprecated.\n///\n/// Helper function to count the number of non-empty elements in a validated array.\n/// Important: Only use it for validated arrays where validate_array(array) returns true,\n/// which ensures that:\n/// 1. All elements before the first empty element are non-empty\n/// 2. All elements after and including the first empty element are empty\n/// 3. The array forms a contiguous sequence of non-empty elements followed by empty elements\npub fn array_length<T, let N: u32>(array: [T; N]) -> u32\nwhere\n T: Empty,\n{\n // We get the length by checking the index of the first empty element.\n\n // Safety: This is safe because we have validated the array (see function doc above) and the emptiness\n // of the element and non-emptiness of the previous element is checked below.\n let maybe_length = unsafe { find_index_hint(array, |elem: T| elem.is_empty()) };\n\n let mut length = N;\n\n if maybe_length.is_some() {\n length = maybe_length.unwrap_unchecked();\n\n array[length].assert_empty(\"Expected array empty\");\n }\n\n if length != 0 {\n assert(!array[length - 1].is_empty());\n }\n\n length\n}\n\n// Returns an array length defined by fully trimming _all_ \"empty\" items\n// from the RHS.\npub unconstrained fn trimmed_array_length_hint<T, let N: u32>(array: [T; N]) -> u32\nwhere\n T: Empty,\n{\n let maybe_index_of_last_nonempty =\n find_index_hint_in_reverse(array, |elem: T| !elem.is_empty());\n let length: u32 = if maybe_index_of_last_nonempty.is_some() {\n 1 + maybe_index_of_last_nonempty.unwrap_unchecked()\n } else {\n 0\n };\n length\n}\n\n/// This function assumes that `array1` and `array2` contain no more than N non-empty elements between them,\n/// if this is not the case then elements from the end of `array2` will be dropped.\npub fn array_merge<T, let N: u32>(array1: [T; N], array2: [T; N]) -> [T; N]\nwhere\n T: Empty,\n{\n // Safety: we constrain this array below\n let result = unsafe { array_merge_helper(array1, array2) };\n // We assume arrays have been validated. The only use cases so far are with previously validated arrays.\n let array1_len = array_length(array1);\n let mut add_from_left = true;\n for i in 0..N {\n add_from_left &= i != array1_len;\n if add_from_left {\n assert_eq(result[i], array1[i]);\n } else {\n assert_eq(result[i], array2[i - array1_len]);\n }\n }\n result\n}\n\nunconstrained fn array_merge_helper<T, let N: u32>(array1: [T; N], array2: [T; N]) -> [T; N]\nwhere\n T: Empty,\n{\n let mut result: [T; N] = [T::empty(); N];\n let mut i = 0;\n for elem in array1 {\n if !elem.is_empty() {\n result[i] = elem;\n i += 1;\n }\n }\n for elem in array2 {\n if !elem.is_empty() {\n result[i] = elem;\n i += 1;\n }\n }\n result\n}\n\n// Returns the number of consecutive elements at the start of the array for which the predicate returns false.\n// This function ensures that any element after the first matching element (predicate returns true) also matches the predicate.\npub fn array_length_until<T, let N: u32, Env>(array: [T; N], predicate: fn[Env](T) -> bool) -> u32 {\n let mut length = 0;\n let mut stop = false;\n for i in 0..N {\n if predicate(array[i]) {\n stop = true;\n } else {\n assert(\n stop == false,\n \"matching element found after already encountering a non-matching element\",\n );\n length += 1;\n }\n }\n length\n}\n\npub fn check_permutation<T, let N: u32>(\n original_array: [T; N],\n permuted_array: [T; N],\n original_indexes: [u32; N],\n)\nwhere\n T: Eq,\n{\n let mut seen_value = [false; N];\n for i in 0..N {\n let index = original_indexes[i];\n let original_value = original_array[index];\n assert(permuted_array[i].eq(original_value), \"Invalid index\");\n assert(!seen_value[index], \"Duplicated index\");\n seen_value[index] = true;\n }\n}\n\n// Helper function to check if an array is padded with a given value from a given index.\n// Different to padded_array_length in that it allows the elements before the given index to be the same as the padded value.\npub fn array_padded_with<T, let N: u32>(array: [T; N], from_index: u32, padded_with: T) -> bool\nwhere\n T: Eq,\n{\n let mut is_valid = true;\n let mut should_check = false;\n for i in 0..N {\n should_check |= i == from_index;\n is_valid &= !should_check | (array[i] == padded_with);\n }\n is_valid\n}\n\n//**********************************************************************************\n// ARRAY WRAPPERS\n//**********************************************************************************\n\n/*\n *\n *\n * \n * |-----------------------------------------|------------------------------| \n * | LHS | RHS |\n * |-----------------------------------------|------------------------------|\n * ClaimedLengthArray | Interspersed 0s possible. | Unvalidated. |\n * | Possibly not fully trimmed. | Nonempty elements possible. |\n * |-----------------------------------------|------------------------------|\n * EmptyRHSArray | Interspersed 0s possible. | All 0s (validated). |\n * | Possibly not fully trimmed. | |\n * |-----------------------------------------|------------------------------|\n * TrimmedArray | Interspersed 0s possible. | All 0s (validated) |\n * | Last lhs element validated as nonempty. | |\n * | (I.e. fully trimmed) | |\n * |-----------------------------------------|------------------------------|\n * DenseTrimmedArray | Dense (validated). | All 0s (validated) |\n * |-----------------------------------------|------------------------------|\n *\n *\n * | What guarantees do we have? |\n * |--------|--------|--------------------------------| \n * | Dense? | RHS | Length vs Fully Trimmed Length |\n * |--------|--------|--------------------------------|\n * ClaimedLengthArray | ? | ? | ? |\n * | | | |\n * |--------|--------|--------------------------------|\n * EmptyRHSArray | ? | All 0s | Length >= Fully Trimmed Length |\n * | | | |\n * |--------|--------|--------------------------------|\n * TrimmedArray | ? | All 0s | Length == Fully Trimmed Length |\n * | | | |\n * | | | |\n * |--------|--------|--------------------------------|\n * DenseTrimmedArray | Yes | All 0s | Length == Fully Trimmed Length |\n * |--------|--------|--------------------------------|\n *\n *\n * An ClaimedLengthArray is distinct from a regular array [T; N], because it carries a length.\n * \n */\n\n/// ClaimedLengthArray - An array interpreted by Kernel circuits.\n/// Its `length` is merely a claim that must eventually be validated.\n/// Validation must include:\n/// - Asserting all items to the LHS of the length are nonempty (dense).\n/// - Asserting all items to the RHS of the length are empty.\n#[derive(Deserialize, Serialize)]\npub struct ClaimedLengthArray<T, let N: u32> {\n pub array: [T; N],\n pub length: u32,\n}\n\nimpl<T, let N: u32> ClaimedLengthArray<T, N>\nwhere\n T: Empty,\n{\n // No constructor. Append to an empty one.\n\n // For constrained append functions, see the dedicated file: assert_array_appended.nr\n\n pub fn assert_dense_trimmed(self) {\n for_i_in_0_(\n self.length,\n self.array.len(),\n |i| {\n assert(!self.array[i].is_empty(), \"LHS of input array is not dense\")\n // Requires Noir #9002:\n // self.array[i].assert_not_empty(\"LHS of input array is not dense\"); // LHS of input array is not dense.\n },\n |i| self.array[i].assert_empty(\"RHS of input array is not empty\"),\n false,\n );\n }\n\n pub fn assert_empty<let S: u32>(self, msg: str<S>) {\n for i in 0..N {\n self.array[i].assert_empty(msg);\n }\n assert_eq(self.length, 0);\n }\n\n pub fn assert_length_within_bounds<let S: u32>(self, msg: str<S>) {\n assert(self.length <= N, msg);\n }\n\n pub fn push(&mut self, item: T) {\n assert(self.length != N, \"Array full\");\n\n let next_index = self.length;\n self.array[next_index] = item;\n self.length += 1;\n }\n\n pub fn pop(&mut self) -> T {\n assert(self.length != 0, \"Array empty\");\n\n let mut top_index = self.length - 1;\n let popped_item = self.array[top_index];\n self.array[top_index] = T::empty();\n self.length -= 1;\n popped_item\n }\n\n pub fn for_each<Env>(self, f: fn[Env](T) -> ()) {\n // We pass `false`, because by construction, we should know that self.length <= self.array.len().\n for_i_only_in_0_(self.length, self.array.len(), |i| f(self.array[i]), false);\n }\n\n // E.g.\n // dest.for_each_i(|source_item, i| { assert_eq(dest.array[i], source_item, \"bad copy\"); })\n pub fn for_each_i<Env>(self, f: fn[Env](T, u32) -> ()) {\n // We pass `false`, because by construction, we should know that self.length <= self.array.len().\n for_i_only_in_0_(\n self.length,\n self.array.len(),\n |i| f(self.array[i], i),\n false,\n );\n }\n\n pub fn from_bounded_vec(vec: BoundedVec<T, N>) -> Self {\n Self { array: vec.storage(), length: vec.len() }\n }\n}\n\n// TODO: compiler bug. No idea why this is needed, if we have #[derive(Eq)] above the struct definition.\nimpl<T, let N: u32> Eq for ClaimedLengthArray<T, N>\nwhere\n T: Eq,\n{\n fn eq(self, other: Self) -> bool {\n (self.array == other.array) & (self.length == other.length)\n }\n}\n\nimpl<T, let N: u32> Empty for ClaimedLengthArray<T, N>\nwhere\n T: Empty,\n{\n fn empty() -> Self {\n Self { array: [T::empty(); N], length: 0 }\n }\n}\n\n#[test]\nfn test_empty_array_length() {\n assert_eq(array_length([0]), 0);\n assert_eq(array_length([0, 0, 0]), 0);\n}\n\n#[test]\nfn test_array_length() {\n assert_eq(array_length([123]), 1);\n assert_eq(array_length([123, 0, 0]), 1);\n assert_eq(array_length([123, 456]), 2);\n assert_eq(array_length([123, 456, 0]), 2);\n}\n\n#[test]\nfn test_array_length_invalid_arrays() {\n // Result can be misleading (but correct) for invalid arrays.\n assert_eq(array_length([0, 0, 123]), 0);\n assert_eq(array_length([0, 123, 0]), 0);\n assert_eq(array_length([0, 123, 456]), 0);\n assert_eq(array_length([123, 0, 456]), 1);\n}\n\n#[test]\nfn test_array_length_until() {\n let array = [11, 22, 33, 44, 55];\n assert_eq(array_length_until(array, |x| x == 55), 4);\n assert_eq(array_length_until(array, |x| x == 56), 5);\n assert_eq(array_length_until(array, |x| x > 40), 3);\n assert_eq(array_length_until(array, |x| x > 10), 0);\n}\n\n#[test(should_fail_with = \"matching element found after already encountering a non-matching element\")]\nfn test_array_length_until_non_consecutive_fails() {\n let array = [1, 1, 0, 1, 0];\n let _ = array_length_until(array, |x| x == 0);\n}\n\n#[test(should_fail_with = \"matching element found after already encountering a non-matching element\")]\nfn test_array_length_until_first_non_matching_fails() {\n let array = [1, 0, 0, 0, 0];\n let _ = array_length_until(array, |x| x == 1);\n}\n\n#[test]\nunconstrained fn find_index_greater_than_min() {\n let values = [10, 20, 30, 40];\n let min = 22;\n let maybe_index = find_index_hint(values, |v: Field| min.lt(v));\n assert_eq(maybe_index.unwrap_unchecked(), 2);\n}\n\n#[test]\nunconstrained fn find_index_not_found() {\n let values = [10, 20, 30, 40];\n let min = 100;\n let maybe_index = find_index_hint(values, |v: Field| min.lt(v));\n assert_eq(maybe_index.is_none(), true);\n}\n\n#[test]\nfn check_permutation_basic_test() {\n let original_array = [1, 2, 3];\n let permuted_array = [3, 1, 2];\n let indexes = [2, 0, 1];\n check_permutation(original_array, permuted_array, indexes);\n}\n\n#[test(should_fail_with = \"Duplicated index\")]\nfn check_permutation_duplicated_index() {\n let original_array = [0, 1, 0];\n let permuted_array = [1, 0, 0];\n let indexes = [1, 0, 0];\n check_permutation(original_array, permuted_array, indexes);\n}\n\n#[test(should_fail_with = \"Invalid index\")]\nfn check_permutation_invalid_index() {\n let original_array = [0, 1, 2];\n let permuted_array = [1, 0, 0];\n let indexes = [1, 0, 2];\n check_permutation(original_array, permuted_array, indexes);\n}\n\n#[test]\nfn test_array_padded_with() {\n let array = [11, 22, 33, 44, 44];\n assert_eq(array_padded_with(array, 0, 44), false);\n assert_eq(array_padded_with(array, 1, 44), false);\n assert_eq(array_padded_with(array, 2, 44), false);\n assert_eq(array_padded_with(array, 3, 44), true);\n assert_eq(array_padded_with(array, 4, 44), true);\n assert_eq(array_padded_with(array, 4, 33), false);\n assert_eq(array_padded_with(array, 5, 44), true); // Index out of bounds.\n assert_eq(array_padded_with(array, 0, 11), false);\n}\n"
|
|
3510
3510
|
},
|
|
3511
3511
|
"373": {
|
|
3512
3512
|
"path": "/home/aztec-dev/aztec-packages/noir-projects/noir-protocol-circuits/crates/types/src/utils/field.nr",
|
package/artifacts/FeeJuice.json
CHANGED
package/artifacts/Router.json
CHANGED
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@aztec/protocol-contracts",
|
|
3
3
|
"homepage": "https://github.com/AztecProtocol/aztec-packages/tree/master/yarn-project/protocol-contracts",
|
|
4
4
|
"description": "Canonical Noir contracts for the Aztec Network",
|
|
5
|
-
"version": "3.0.0-nightly.
|
|
5
|
+
"version": "3.0.0-nightly.20251114",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": "./dest/index.js",
|
|
@@ -73,9 +73,9 @@
|
|
|
73
73
|
]
|
|
74
74
|
},
|
|
75
75
|
"dependencies": {
|
|
76
|
-
"@aztec/constants": "3.0.0-nightly.
|
|
77
|
-
"@aztec/foundation": "3.0.0-nightly.
|
|
78
|
-
"@aztec/stdlib": "3.0.0-nightly.
|
|
76
|
+
"@aztec/constants": "3.0.0-nightly.20251114",
|
|
77
|
+
"@aztec/foundation": "3.0.0-nightly.20251114",
|
|
78
|
+
"@aztec/stdlib": "3.0.0-nightly.20251114",
|
|
79
79
|
"lodash.chunk": "^4.2.0",
|
|
80
80
|
"lodash.omit": "^4.5.0",
|
|
81
81
|
"tslib": "^2.4.0"
|