miden-core-lib 0.24.2

Miden VM core library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use miden::core::word

# CONSTANTS
# =================================================================================================
const LOWERBOUND_ARRAY_EVENT = event("miden::core::collections::sorted_array::lowerbound_array")
const LOWERBOUND_KEY_VALUE_EVENT = event("miden::core::collections::sorted_array::lowerbound_key_value")

# ===== ARRAY UTILITIES ============================================================================

#! Finds a value in a sorted array of words.
#!
#! **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.
#!
#! Input:  [VALUE, start_ptr, end_ptr]
#! Output: [is_value_found, value_ptr, start_ptr, end_ptr]
#!
#! # Panics
#!
#! Panics if:
#! - start_ptr, end_ptr are not word-aligned
#! - `start_ptr > end_ptr`
#!
#! Cycles:
#!   Value exists: 46 cycles
#!   Value doesn't exist and the array is empty: 25 cycles
#!   Value doesn't exist and is smaller than all elements: 151 cycles
#!   Value doesn't exist and is larger than all elements: 149 cycles
#!   Value doesn't exist: 286 cycles
pub proc find_word
    # Call the non-deterministic lowerbound advisor (5 cycles)
    emit.LOWERBOUND_ARRAY_EVENT
    adv_push adv_push
    #=> [was_value_found, maybe_value_ptr, VALUE, start_ptr, end_ptr]

    if.true
        # assert `start_ptr <= maybe_value_ptr < end_ptr` (15 cycles)
        dup dup.6 u32assert2.err="maybe_value_ptr is not u32"
        u32gte assert.err="lowerbound_array invariant: maybe_value_ptr must be >= start_ptr"
        dup dup.7 u32assert2.err="maybe_value_ptr is not u32"
        u32lt assert.err="lowerbound_array invariant: maybe_value_ptr must be < end_ptr"
        #=> [maybe_value_ptr, VALUE, start_ptr, end_ptr]

        # dereference maybe_value_ptr (8 cycles)
        dup movdn.5 padw movup.4 mem_loadw_le
        #=> [MAYBE_VALUE, VALUE, maybe_value_ptr, start_ptr, end_ptr]

        # check if it matches the requested VALUE (15 cycles)
        exec.word::eq
        dup assert.err="lowerbound_array invariant: value_ptr must point to VALUE"
        #=> [is_value_found, maybe_value_ptr, start_ptr, end_ptr]
    else
        # the value was not found

        # pre-compute is_start_ptr, is_end_ptr to reduce the depth of the generated MAST
        # (6 cycles)
        dup.6 dup.1 eq
        #=> [is_end_ptr, maybe_value_ptr, VALUE, start_ptr, end_ptr]

        dup.6 dup.2 eq
        #=> [is_start_ptr, is_end_ptr, maybe_value_ptr, VALUE, start_ptr, end_ptr]

        if.true
            # the value was not found, and `maybe_value_ptr = start_ptr`

            if.true
                # the list is empty and `start_ptr = end_ptr` (both are equal to `maybe_value_ptr`)
                #=> [maybe_value_ptr = start_ptr = end_ptr, VALUE, start_ptr, end_ptr]

                # Prepare output (6 cycles)
                movdn.4 dropw push.0
                #=> [is_value_found = 0, value_ptr = start_ptr, start_ptr, end_ptr]
            else
                # the value was not found, `maybe_value_ptr = start_ptr`, and the list is not empty
                # assert `the value at start_ptr > VALUE`
                #=> [maybe_value_ptr = start_ptr, VALUE, start_ptr, end_ptr]

                movdn.4
                #=> [VALUE, maybe_value_ptr, start_ptr, end_ptr]

                # dereference maybe_value_ptr (7 cycles)
                dup.4 padw movup.4 mem_loadw_le
                #=> [FIRST_WORD, VALUE, maybe_value_ptr = start_ptr, start_ptr, end_ptr]

                # there was no match, the first array element must be larger than VALUE
                # (123 cycles)
                exec.word::lt
                assert.err="lowerbound_array invariant: start_ptr must point to a word greater than VALUE"

                # No value was found (1 cycle)
                push.0
                #=> [is_value_found = 0, maybe_value_ptr = start_ptr, start_ptr, end_ptr]
            end
        else
            if.true
                # the value was not found, `maybe_value_ptr = end_ptr`
                # the list is not empty, otherwise `maybe_value_ptr = start_ptr` and handled above
                # assert `the value before end_ptr < VALUE`

                #=> [maybe_value_ptr = end_ptr, VALUE, start_ptr, end_ptr]

                # fetch the last word in the array (10 cycles)
                dup movdn.5 sub.4 padw movup.4 mem_loadw_le
                #=> [LAST_WORD, VALUE, maybe_value_ptr = end_ptr, start_ptr, end_ptr]

                # there was no match, the last array element must be smaller than VALUE
                # (119 cycles)
                exec.word::gt
                assert.err="lowerbound_array invariant: last word must be smaller than VALUE"

                # No value was found (1 cycle)
                push.0
                #=> [is_value_found = 0, maybe_value_ptr = end_ptr, start_ptr, end_ptr]
            else
                # The value was not found, and the maybe_value_ptr is neither the start nor the end
                # of the list. Make sure that:
                # - `start_ptr <= maybe_value_ptr < end_ptr`, and
                # - `the value at maybe_value_ptr-1 < VALUE`, and
                # - `the value at maybe_value_ptr > VALUE`.
                # `maybe_value_ptr` must already be word aligned to allow reading values.
                #=> [maybe_value_ptr, VALUE, start_ptr, end_ptr]

                # `maybe_value_ptr` bounds check
                dup dup.6 u32assert2.err="maybe_value_ptr is not u32"
                u32gte assert.err="lowerbound_array invariant: maybe_value_ptr must be >= start_ptr"

                dup dup.7 u32assert2.err="maybe_value_ptr is not u32"
                u32lt assert.err="lowerbound_array invariant: maybe_value_ptr must be < end_ptr"

                # Duplicate VALUE word (5 cycles)
                movdn.4 dupw
                #=> [VALUE, VALUE, maybe_value_ptr, start_ptr, end_ptr]

                # dereference maybe_value_ptr (8 cycles)
                padw dup.12 mem_loadw_le
                #=> [MAYBE_VALUE, VALUE, VALUE, maybe_value_ptr, start_ptr, end_ptr]

                # there was no match, MAYBE_VALUE must be larger than VALUE (123 cycles)
                exec.word::lt
                assert.err="lowerbound_array invariant: *maybe_value_ptr must be greater than VALUE"

                # dereference `maybe_value_ptr-4` (11 cycles)
                padw dup.8 sub.4 mem_loadw_le
                #=> [MAYBE_VALUE_PREV, VALUE, maybe_value_ptr, start_ptr, end_ptr]

                # there was no match, MAYBE_VALUE_PREV must be smaller than VALUE (119 cycles)
                exec.word::gt
                assert.err="lowerbound_array invariant: *(maybe_value_ptr-4) must be smaller than VALUE"

                # No value was found (1 cycle)
                push.0
                #=> [is_value_found = 0, maybe_value_ptr, start_ptr, end_ptr]
            end
        end
    end
end

# ===== KEY-VALUE UTILITIES ============================================================================

#! Finds a key in a sorted array of (key, value) word tuples.
#!
#! **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.
#!
#! Inputs:  [KEY, start_ptr, end_ptr]
#! Outputs: [is_key_found, key_ptr, start_ptr, end_ptr]
#!
#! # Panics
#!
#! Panics if:
#! - start_ptr is not word-aligned
#! - end_ptr is not double-word-aligned with the start_ptr:
#!     - `(end_ptr - start_ptr)` must be divisible by 8
#! - `start_ptr > end_ptr`
pub proc find_key_value
    push.1
    # => [use_full_key = 1, KEY, start_ptr, end_ptr]

    movdn.6
    # => [KEY, start_ptr, end_ptr, use_full_key]

    exec.find_partial_key_value
    # => [is_key_found, key_ptr, start_ptr, end_ptr]
end

#! Finds a half-key in a sorted array of (key, value) word tuples.
#!
#! Half-key means that, out of the keys in the array, only half of the key - the most significant
#! element (prefix) and the second most significant element (suffix) - need to match.
#!
#! **The half-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 half-keys are not sorted.
#!
#! Inputs: [key_suffix, key_prefix, start_ptr, end_ptr]
#! Output: [is_key_found, key_ptr, start_ptr, end_ptr]
#!
#! # Panics
#!
#! Panics if:
#! - start_ptr is not word-aligned
#! - end_ptr is not double-word-aligned with the start_ptr:
#!     - `(end_ptr - start_ptr)` must be divisible by 8
#! - `start_ptr > end_ptr`
pub proc find_half_key_value
    # Build KEY with key_prefix at word[3] (most significant in BE) and key_suffix at word[2]
    # word[0] and word[1] are 0 (will be zeroed in half-key comparison anyway)
    push.0 push.0
    # => [0, 0, key_suffix, key_prefix, start_ptr, end_ptr]
    # KEY = [0, 0, key_suffix, key_prefix] with w0=0 on top, w3=key_prefix

    push.0
    # => [use_full_key = 0, KEY, start_ptr, end_ptr]

    movdn.6
    # => [KEY, start_ptr, end_ptr, use_full_key]

    exec.find_partial_key_value
    # => [is_key_found, key_ptr, start_ptr, end_ptr]
end

#! Finds a key in a sorted array of (key, value) word tuples.
#!
#! - If use_full_key = 1, then a match on the entire KEY is required.
#! - If use_full_key = 0, then the KEY's two least significant elements *must* be zeroes and
#!   upon loading the key for comparison, its two least significant elements are zeroized.
#!   This effectively means that only a match on the two most significant elements is required.
#!
#! **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.
#!
#! Input:  [KEY, start_ptr, end_ptr, use_full_key]
#! Output: [is_key_found, key_ptr, start_ptr, end_ptr]
#!
#! # Panics
#!
#! Panics if:
#! - start_ptr is not word-aligned
#! - end_ptr is not double-word-aligned with the start_ptr:
#!     - `(end_ptr - start_ptr)` must be divisible by 8
#! - `start_ptr > end_ptr`
#!
#! Cycles:
#!   Key exists: 70 cycles
#!   Key doesn't exist and the array is empty: 25 cycles
#!   Key doesn't exist and is smaller than all stored keys: 166 cycles
#!   Key doesn't exist and is larger than all stored keys: 162 cycles
#!   Key doesn't exist: 322 cycles
@locals(1)
proc find_partial_key_value
    # Call the non-deterministic lowerbound advisor (5 cycles)
    emit.LOWERBOUND_KEY_VALUE_EVENT
    # => [KEY, start_ptr, end_ptr, use_full_key]

    # store use_full_key for later
    movup.6 loc_store.0
    # => [KEY, start_ptr, end_ptr]

    adv_push adv_push
    #=> [was_key_found, maybe_key_ptr, KEY, start_ptr, end_ptr]

    if.true
        # assert `start_ptr <= maybe_key_ptr < end_ptr` (15 cycles)
        dup dup.6 u32assert2.err="maybe_key_ptr is not u32"
        u32gte assert.err="lowerbound_key_value invariant: maybe_key_ptr must be >= start_ptr"
        dup dup.7 u32assert2.err="maybe_key_ptr is not u32"
        u32lt assert.err="lowerbound_key_value invariant: maybe_key_ptr must be < end_ptr"
        #=> [maybe_key_ptr, KEY, start_ptr, end_ptr]

        # make sure maybe_key_ptr is pointing to a key, not value (10 cycles)
        dup dup.6 sub u32mod.8 assertz.err="lowerbound_key_value invariant: key_ptr must be double-word aligned with start_ptr"
        #=> [maybe_key_ptr, KEY, start_ptr, end_ptr]

        # dereference maybe_key_ptr (22 cycles)
        dup movdn.5
        #=> [maybe_key_ptr, KEY, maybe_key_ptr, start_ptr, end_ptr]

        loc_load.0 exec.load_key
        #=> [MAYBE_KEY, KEY, maybe_key_ptr, start_ptr, end_ptr]

        # check if it matches the requested KEY (15 cycles)
        exec.word::eq
        dup assert.err="lowerbound_key_value invariant: key_ptr must point to KEY"
        #=> [is_key_found, maybe_key_ptr, start_ptr, end_ptr]
    else
        # pre-compute is_start_ptr, is_end_ptr to reduce the depth of the
        # generated MAST (6 cycles)
        dup.6 dup.1 eq
        #=> [is_end_ptr, maybe_key_ptr, KEY, start_ptr, end_ptr]

        dup.6 dup.2 eq
        #=> [is_start_ptr, is_end_ptr, maybe_key_ptr, KEY, start_ptr, end_ptr]

        if.true
            # the key was not found, `maybe_key_ptr = start_ptr`

            if.true
                # the list is empty and `start_ptr = end_ptr` (both are equal to `maybe_key_ptr`)
                #=> [maybe_key_ptr = start_ptr = end_ptr, KEY, start_ptr, end_ptr]

                # Prepare output (6 cycles)
                movdn.4 dropw push.0
                #=> [is_key_found = 0, key_ptr = start_ptr, start_ptr, end_ptr]
            else
                # the key was not found, `maybe_key_ptr = start_ptr` and the list is not empty
                # assert `the key at start_ptr > KEY`
                #=> [maybe_key_ptr = start_ptr, KEY, start_ptr, end_ptr]

                # load key (22 cycles)
                dup movdn.5
                #=> [maybe_key_ptr, KEY, maybe_key_ptr, start_ptr, end_ptr]

                loc_load.0 exec.load_key
                #=> [FIRST_KEY, KEY, maybe_key_ptr = start_ptr, start_ptr, end_ptr]

                # there was no match, the first map element must be larger than KEY
                # (123 cycles)
                exec.word::lt
                assert.err="lowerbound_key_value invariant: start_ptr must point to a key greater than KEY"

                # No value was found (1 cycle)
                push.0
                #=> [is_key_found = 0, maybe_key_ptr = start_ptr, start_ptr, end_ptr]
            end
        else
            if.true
                # the key was not found, `maybe_key_ptr = end_ptr`
                # the list is not empty, otherwise `maybe_key_ptr = start_ptr` and handled above
                # assert `the key before end_ptr < KEY`

                #=> [maybe_key_ptr = end_ptr, KEY, start_ptr, end_ptr]

                # fetch the last key in the array (23 cycles)
                dup movdn.5 sub.8
                #=> [maybe_key_ptr-8, KEY, maybe_key_ptr, start_ptr, end_ptr]

                loc_load.0 exec.load_key
                #=> [LAST_KEY, KEY, maybe_key_ptr = end_ptr, start_ptr, end_ptr]

                # there was no match, the last map element must be smaller than KEY
                # (119 cycles)
                exec.word::gt
                assert.err="lowerbound_key_value invariant: last key-value pair must be smaller than KEY"

                # No value was found (1 cycle)
                push.0
                #=> [is_key_found = 0, maybe_key_ptr = end_ptr, start_ptr, end_ptr]
            else
                # The key was not found, and the maybe_key_ptr is neither the start nor the end
                # of the list. Make sure that:
                # - `start_ptr <= maybe_key_ptr < end_ptr`, and
                # - `maybe_key_ptr` is word aligned with `start_ptr`, and
                # - `the key at maybe_key_ptr-1 < KEY`, and
                # - `the key at maybe_key_ptr > KEY`
                #=> [maybe_key_ptr, KEY, start_ptr, end_ptr]

                # `maybe_key_ptr` bounds check
                dup dup.6 u32assert2.err="maybe_key_ptr is not u32"
                u32gte assert.err="lowerbound_key_value invariant: maybe_key_ptr must be >= start_ptr"

                dup dup.7 u32assert2.err="maybe_key_ptr is not u32"
                u32lt assert.err="lowerbound_key_value invariant: maybe_key_ptr must be < end_ptr"

                # make sure maybe_key_ptr is pointing to a key, not value (10 cycles)
                dup dup.6 sub u32assert2.err="maybe_key_ptr is not u32"
                u32mod.8 assertz.err="lowerbound_key_value invariant: key_ptr must be double-word aligned with start_ptr"

                # Duplicate KEY word (5 cycles)
                movdn.4 dupw
                #=> [KEY, KEY, maybe_key_ptr, start_ptr, end_ptr]

                # load key (21 cycles)
                dup.8 loc_load.0 exec.load_key
                #=> [MAYBE_KEY, KEY, KEY, maybe_key_ptr, start_ptr, end_ptr]

                # there was no match, `*maybe_key_ptr` must be larger than KEY (123 cycles)
                exec.word::lt
                assert.err="lowerbound_key_value invariant: MAYBE_KEY must be greater than KEY"
                #=> [KEY, maybe_key_ptr, start_ptr, end_ptr]

                # dereference `maybe_key_ptr-8` (previous key) (22 cycles)
                dup.4 sub.8
                #=> [maybe_key_ptr-8, KEY, maybe_key_ptr, start_ptr, end_ptr]

                loc_load.0 exec.load_key
                #=> [MAYBE_KEY_PREV, KEY, maybe_key_ptr, start_ptr, end_ptr]

                # there was no match, `*(maybe_key_ptr-8)` must be smaller than KEY (119 cycles)
                exec.word::gt
                assert.err="lowerbound_key_value invariant: MAYBE_KEY_PREV must be smaller than KEY"

                # No value was found (1 cycle)
                push.0
                #=> [is_key_found = 0, maybe_key_ptr, start_ptr, end_ptr]
            end
        end
    end
end

#! Loads a key from the pointer.
#!
#! If use_full_key is 0, the key is loaded with the two least significant elements (word[0] and
#! word[1]) set to zero, keeping only word[2] and word[3] for half-key comparison.
#! If use_full_key is 1, the full key is loaded unchanged.
#!
#! This procedure avoids branches as an optimization.
#!
#! Inputs:  [use_full_key, key_ptr]
#! Outputs: [KEY]
#!
#! Cycles: 16
proc load_key
    padw movup.5 mem_loadw_le
    # => [w0, w1, w2, w3, use_full_key]

    # For half-key comparison (use_full_key=0), zero w0 and w1 (the BE least significant elements)
    # keeping w2 and w3 for comparison
    dup.4 mul
    # => [w0', w1, w2, w3, use_full_key] where w0' = w0 * use_full_key

    swap dup.4 mul swap
    # => [w0', w1', w2, w3, use_full_key] where w1' = w1 * use_full_key

    movup.4 drop
    # => [w0', w1', w2, w3]
end