Skip to content

Commit 38e2c13

Browse files
committed
docs(corelib): document sortedness precondition for sorted array operations
1 parent 28c4c7a commit 38e2c13

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Reverted `InvokeKind::ProcRef` back to `InvokeKind::Exec` in `visit_mut_procref` and added an explanatory comment (#2893).
1111
#### Changes
1212

13+
- Documented sortedness precondition more prominently for sorted array operations (#2832).
1314
- Documented that enum variants are module-level constants and must be unique within a module (#2932).
1415
- [BREAKING] Sync execution and proving APIs now require `SyncHost`; async `Host`, `execute`, and `prove` remain available ([#2865](https://github.com/0xMiden/miden-vm/pull/2865)).
1516
- [BREAKING] `miden_processor::execute()` and `execute_sync()` now return `ExecutionOutput`; trace building remains explicit via `execute_trace_inputs*()` and `trace::build_trace()` ([#2865](https://github.com/0xMiden/miden-vm/pull/2865)).

crates/lib/core/asm/collections/sorted_array.masm

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ const LOWERBOUND_KEY_VALUE_EVENT = event("miden::core::collections::sorted_array
99

1010
#! Finds a value in a sorted array of words.
1111
#!
12-
#! This function will crash if the following conditions aren't met:
13-
#! - words must be sorted in non-decreasing order,
14-
#! - start_ptr, end_ptr are word-aligned
15-
#! - `start_ptr <= end_ptr`
12+
#! **The input array must be sorted in non-decreasing lexicographic order.** The host event handler validates this and will return an error if the array is not sorted.
1613
#!
1714
#! Input: [VALUE, start_ptr, end_ptr]
1815
#! Output: [is_value_found, value_ptr, start_ptr, end_ptr]
1916
#!
17+
#! # Panics
18+
#!
19+
#! Panics if:
20+
#! - start_ptr, end_ptr are not word-aligned
21+
#! - `start_ptr > end_ptr`
22+
#!
2023
#! Cycles:
2124
#! Value exists: 46 cycles
2225
#! Value doesn't exist and the array is empty: 25 cycles
@@ -146,20 +149,18 @@ end
146149

147150
#! Finds a key in a sorted array of (key, value) word tuples.
148151
#!
152+
#! **The keys in the array must be sorted in non-decreasing lexicographic order.** The host event handler validates this and will return an error if the keys are not sorted.
153+
#!
149154
#! Inputs: [KEY, start_ptr, end_ptr]
150155
#! Outputs: [is_key_found, key_ptr, start_ptr, end_ptr]
151156
#!
152157
#! # Panics
153158
#!
154159
#! Panics if:
155-
#! - keys are not sorted in non-decreasing order,
156160
#! - start_ptr is not word-aligned
157161
#! - end_ptr is not double-word-aligned with the start_ptr:
158162
#! - `(end_ptr - start_ptr)` must be divisible by 8
159163
#! - `start_ptr > end_ptr`
160-
#!
161-
#! Inputs: [KEY, start_ptr, end_ptr]
162-
#! Output: [is_key_found, key_ptr, start_ptr, end_ptr]
163164
pub proc find_key_value
164165
push.1
165166
# => [use_full_key = 1, KEY, start_ptr, end_ptr]
@@ -176,13 +177,14 @@ end
176177
#! Half-key means that, out of the keys in the array, only half of the key - the most significant
177178
#! element (prefix) and the second most significant element (suffix) - need to match.
178179
#!
180+
#! **The keys in the array must be sorted in non-decreasing lexicographic order.** The host event handler validates this and will return an error if the keys are not sorted.
181+
#!
179182
#! Inputs: [key_suffix, key_prefix, start_ptr, end_ptr]
180183
#! Output: [is_key_found, key_ptr, start_ptr, end_ptr]
181184
#!
182185
#! # Panics
183186
#!
184187
#! Panics if:
185-
#! - keys are not sorted in non-decreasing order,
186188
#! - start_ptr is not word-aligned
187189
#! - end_ptr is not double-word-aligned with the start_ptr:
188190
#! - `(end_ptr - start_ptr)` must be divisible by 8
@@ -211,13 +213,14 @@ end
211213
#! upon loading the key for comparison, its two least significant elements are zeroized.
212214
#! This effectively means that only a match on the two most significant elements is required.
213215
#!
216+
#! **The keys in the array must be sorted in non-decreasing lexicographic order.** The host event handler validates this and will return an error if the keys are not sorted.
217+
#!
214218
#! Input: [KEY, start_ptr, end_ptr, use_full_key]
215219
#! Output: [is_key_found, key_ptr, start_ptr, end_ptr]
216220
#!
217221
#! # Panics
218222
#!
219223
#! Panics if:
220-
#! - keys are not sorted in non-decreasing order,
221224
#! - start_ptr is not word-aligned
222225
#! - end_ptr is not double-word-aligned with the start_ptr:
223226
#! - `(end_ptr - start_ptr)` must be divisible by 8

crates/lib/core/docs/collections/sorted_array.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
## miden::core::collections::sorted_array
33
| Procedure | Description |
44
| ----------- | ------------- |
5-
| find_word | Finds a value in a sorted array of words.<br /><br />This function will crash if the following conditions aren't met:<br />- words must be sorted in non-decreasing order,<br />- start_ptr, end_ptr are word-aligned<br />- `start_ptr <= end_ptr`<br /><br />Input: [VALUE, start_ptr, end_ptr]<br />Output: [is_value_found, value_ptr, start_ptr, end_ptr]<br /><br />Cycles:<br />Value exists: 46 cycles<br />Value doesn't exist and the array is empty: 25 cycles<br />Value doesn't exist and is smaller than all elements: 151 cycles<br />Value doesn't exist and is larger than all elements: 149 cycles<br />Value doesn't exist: 286 cycles<br /> |
6-
| find_key_value | Finds a key in a sorted array of (key, value) word tuples.<br /><br />Inputs: [KEY, start_ptr, end_ptr]<br />Outputs: [is_key_found, key_ptr, start_ptr, end_ptr]<br /><br /># Panics<br /><br />Panics if:<br />- keys are not sorted in non-decreasing order,<br />- start_ptr is not word-aligned<br />- end_ptr is not double-word-aligned with the start_ptr:<br />- `(end_ptr - start_ptr)` must be divisible by 8<br />- `start_ptr > end_ptr`<br /><br />Inputs: [KEY, start_ptr, end_ptr]<br />Output: [is_key_found, key_ptr, start_ptr, end_ptr]<br /> |
7-
| find_half_key_value | Finds a half-key in a sorted array of (key, value) word tuples.<br /><br />Half-key means that, out of the keys in the array, only half of the key - the most significant<br />element (prefix) and the second most significant element (suffix) - need to match.<br /><br />Inputs: [key_suffix, key_prefix, start_ptr, end_ptr]<br />Output: [is_key_found, key_ptr, start_ptr, end_ptr]<br /><br /># Panics<br /><br />Panics if:<br />- keys are not sorted in non-decreasing order,<br />- start_ptr is not word-aligned<br />- end_ptr is not double-word-aligned with the start_ptr:<br />- `(end_ptr - start_ptr)` must be divisible by 8<br />- `start_ptr > end_ptr`<br /> |
5+
| find_word | Finds a value in a sorted array of words.<br /><br />**The input array must be sorted in non-decreasing lexicographic order.** The host event handler validates this and will return an error if the array is not sorted.<br /><br />Input: [VALUE, start_ptr, end_ptr]<br />Output: [is_value_found, value_ptr, start_ptr, end_ptr]<br /><br /># Panics<br /><br />Panics if:<br />- start_ptr, end_ptr are not word-aligned<br />- `start_ptr > end_ptr`<br /><br />Cycles:<br />Value exists: 46 cycles<br />Value doesn't exist and the array is empty: 25 cycles<br />Value doesn't exist and is smaller than all elements: 151 cycles<br />Value doesn't exist and is larger than all elements: 149 cycles<br />Value doesn't exist: 286 cycles<br /> |
6+
| find_key_value | Finds a key in a sorted array of (key, value) word tuples.<br /><br />**The keys in the array must be sorted in non-decreasing lexicographic order.** The host event handler validates this and will return an error if the keys are not sorted.<br /><br />Inputs: [KEY, start_ptr, end_ptr]<br />Outputs: [is_key_found, key_ptr, start_ptr, end_ptr]<br /><br /># Panics<br /><br />Panics if:<br />- start_ptr is not word-aligned<br />- end_ptr is not double-word-aligned with the start_ptr:<br />- `(end_ptr - start_ptr)` must be divisible by 8<br />- `start_ptr > end_ptr`<br /> |
7+
| find_half_key_value | Finds a half-key in a sorted array of (key, value) word tuples.<br /><br />Half-key means that, out of the keys in the array, only half of the key - the most significant<br />element (prefix) and the second most significant element (suffix) - need to match.<br /><br />**The keys in the array must be sorted in non-decreasing lexicographic order.** The host event handler validates this and will return an error if the keys are not sorted.<br /><br />Inputs: [key_suffix, key_prefix, start_ptr, end_ptr]<br />Output: [is_key_found, key_ptr, start_ptr, end_ptr]<br /><br /># Panics<br /><br />Panics if:<br />- start_ptr is not word-aligned<br />- end_ptr is not double-word-aligned with the start_ptr:<br />- `(end_ptr - start_ptr)` must be divisible by 8<br />- `start_ptr > end_ptr`<br /> |

0 commit comments

Comments
 (0)