miden-processor 1.0.0-rc.0

Miden VM processor
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#[cfg(test)]
use alloc::vec::Vec;

use miden_air::{
    MIDEN_AIR_COUNT, PcsParams, memory,
    trace::{MIN_TRACE_LEN, eidos_compression::EIDOS_COMPRESSION_CYCLE_LEN},
};

use super::chiplets::Chiplets;
use crate::{Felt, ONE};
#[cfg(test)]
use crate::{operation::Operation, utils::ToElements};

// ROW-MAJOR TRACE WRITER
// ================================================================================================

/// Row-major flat buffer writer (`write_row` is a single `copy_from_slice`).
///
/// `payload` is the number of leading columns written per row; `stride` is the physical row
/// width of the backing buffer. When `stride > payload`, the trailing `stride - payload`
/// columns of each row are left untouched (callers rely on them staying zero-initialized).
#[derive(Debug)]
pub struct RowMajorTraceWriter<'a, E> {
    data: &'a mut [E],
    payload: usize,
    stride: usize,
}

impl<'a, E: Copy> RowMajorTraceWriter<'a, E> {
    /// Creates a writer whose physical row width equals the per-row payload.
    #[cfg(test)]
    pub(super) fn new(data: &'a mut [E], width: usize) -> Self {
        Self::with_stride(data, width, width)
    }

    /// Creates a writer that writes `payload` columns per row into a buffer with physical row
    /// width `stride` (`stride >= payload`).
    pub fn with_stride(data: &'a mut [E], payload: usize, stride: usize) -> Self {
        debug_assert!(stride >= payload, "stride must be >= payload");
        debug_assert_eq!(data.len() % stride, 0, "buffer length must be a multiple of stride");
        Self { data, payload, stride }
    }

    /// Writes one row's payload; `values.len()` must equal `payload`.
    #[inline(always)]
    pub fn write_row(&mut self, row: usize, values: &[E]) {
        debug_assert_eq!(values.len(), self.payload);
        let start = row * self.stride;
        self.data[start..start + self.payload].copy_from_slice(values);
    }
}

// TRACE FRAGMENT
// ================================================================================================

/// A writable, row-major view over one chiplet's region of the chiplets trace.
///
/// A chiplet occupies a contiguous band of rows and a contiguous band of columns
/// `[col_start, col_start + num_cols)`. [`Self::copy_rows_from`] also writes the per-row
/// `prefix_one_cols` selectors and (when there's room) the trailing `chip_clk` column.
pub struct ChipletTraceFragment<'a> {
    /// Contiguous `num_rows * stride` row-major slice (this chiplet's rows).
    band: &'a mut [Felt],
    stride: usize,
    col_start: usize,
    num_rows: usize,
    num_cols: usize,
    /// Global row offset of `band[0]` in the chiplets trace; used to compute `chip_clk`.
    row_offset: usize,
    /// Columns `< col_start` to set to ONE on every row in this band.
    prefix_one_cols: &'static [usize],
}

impl<'a> ChipletTraceFragment<'a> {
    /// Bare fragment with no prefix selectors or `chip_clk`. For chiplet-level unit tests.
    pub fn row_major(
        band: &'a mut [Felt],
        stride: usize,
        col_start: usize,
        num_cols: usize,
    ) -> Self {
        Self::with_overheads(band, stride, col_start, num_cols, 0, &[])
    }

    /// Adds the chiplets-trace overheads: per-row ONEs at `prefix_one_cols` and `chip_clk` at
    /// column `stride - 1` (when there's room), using `row_offset` as `band[0]`'s global row.
    pub fn with_overheads(
        band: &'a mut [Felt],
        stride: usize,
        col_start: usize,
        num_cols: usize,
        row_offset: usize,
        prefix_one_cols: &'static [usize],
    ) -> Self {
        debug_assert_eq!(band.len() % stride, 0, "band length must be a multiple of stride");
        debug_assert!(col_start + num_cols <= stride, "column band overruns the row stride");
        debug_assert!(
            prefix_one_cols.iter().all(|&c| c < col_start),
            "prefix_one_cols must lie before col_start",
        );
        let num_rows = band.len() / stride;
        Self {
            band,
            stride,
            col_start,
            num_rows,
            num_cols,
            row_offset,
            prefix_one_cols,
        }
    }

    // PUBLIC ACCESSORS
    // --------------------------------------------------------------------------------------------

    /// Returns the number of columns in this execution trace fragment.
    pub fn width(&self) -> usize {
        self.num_cols
    }

    /// Returns the number of rows in this execution trace fragment.
    pub fn len(&self) -> usize {
        self.num_rows
    }

    /// Mutable access to columns `[0..col_start)` of `row`, for chiplets whose prefix
    /// selectors vary per row (e.g. the hasher's `s_ctrl`).
    pub fn prefix_mut(&mut self, row: usize) -> &mut [Felt] {
        let row_start = row * self.stride;
        &mut self.band[row_start..row_start + self.col_start]
    }

    // DATA MUTATORS
    // --------------------------------------------------------------------------------------------

    /// Copies a chiplet's row-major buffer (`num_cols` cells per row) into this fragment's
    /// band, fusing the per-row prefix-selector ONEs and trailing `chip_clk` when configured.
    pub fn copy_rows_from(&mut self, src: &[Felt]) {
        debug_assert_eq!(src.len(), self.num_rows * self.num_cols, "source buffer size mismatch");
        self.copy_rows_into(0, src);
    }

    /// Copies `src.len() / num_cols` rows starting at `row_offset` into this fragment's band,
    /// fusing the per-row prefix-selector ONEs and trailing `chip_clk` when configured.
    pub fn copy_rows_into(&mut self, row_offset: usize, src: &[Felt]) {
        debug_assert_eq!(src.len() % self.num_cols, 0, "source buffer size not row-aligned");
        let chunk_rows = src.len() / self.num_cols;
        debug_assert!(
            row_offset + chunk_rows <= self.num_rows,
            "chunk overruns fragment row range",
        );
        let write_chip_clk = self.stride > self.col_start + self.num_cols;
        let clk_col = self.stride - 1;
        for r in 0..chunk_rows {
            let dst_row = row_offset + r;
            let row_start = dst_row * self.stride;
            let row = &mut self.band[row_start..row_start + self.stride];
            for &col in self.prefix_one_cols {
                row[col] = ONE;
            }
            row[self.col_start..self.col_start + self.num_cols]
                .copy_from_slice(&src[r * self.num_cols..(r + 1) * self.num_cols]);
            if write_chip_clk {
                row[clk_col] = Felt::from_u32((self.row_offset + dst_row + 1) as u32);
            }
        }
    }
}

// TRACE LENGTH SUMMARY
// ================================================================================================

/// Row counts for the AIR segments produced by trace generation.
///
/// Dynamic AIRs store their unpadded row counts. Use the `*_height()` accessors for their padded
/// power-of-two AIR matrix heights. The byte-pair lookup stores its fixed physical row count.
#[derive(Debug, Default, Eq, PartialEq, Clone, Copy)]
pub struct TraceLenSummary {
    core_rows: usize,
    chiplets: ChipletsLengths,
    eidos_compression_rows: usize,
    byte_pair_lookup_rows: usize,
    /// Set by the trace builder when known, in [`miden_air::AIRS`] order. `None` falls back to
    /// deriving the four padded heights from the unpadded component row counts.
    padded_heights: Option<[usize; MIDEN_AIR_COUNT]>,
}

impl TraceLenSummary {
    pub fn new(
        core_rows: usize,
        chiplets: ChipletsLengths,
        eidos_compression_rows: usize,
        byte_pair_lookup_rows: usize,
    ) -> Self {
        TraceLenSummary {
            core_rows,
            chiplets,
            eidos_compression_rows,
            byte_pair_lookup_rows,
            padded_heights: None,
        }
    }

    /// Builds a summary after the trace builder has computed the padded per-AIR heights, in
    /// [`miden_air::AIRS`] order: Core, Chiplets, Eidos compression, then And8 lookup.
    pub fn new_with_padded(
        core_rows: usize,
        chiplets: ChipletsLengths,
        eidos_compression_rows: usize,
        byte_pair_lookup_rows: usize,
        padded_heights: [usize; MIDEN_AIR_COUNT],
    ) -> Self {
        TraceLenSummary {
            core_rows,
            chiplets,
            eidos_compression_rows,
            byte_pair_lookup_rows,
            padded_heights: Some(padded_heights),
        }
    }

    /// Returns the unpadded core AIR rows (system + decoder + stack).
    pub fn core_rows(&self) -> usize {
        self.core_rows
    }

    /// Returns the chiplet row-count breakdown.
    pub fn chiplets(&self) -> ChipletsLengths {
        self.chiplets
    }

    /// Returns the unpadded chiplets AIR rows.
    pub fn chiplets_rows(&self) -> usize {
        self.chiplets.trace_len()
    }

    /// Returns the unpadded Eidos compression AIR rows.
    pub fn eidos_compression_rows(&self) -> usize {
        self.eidos_compression_rows
    }

    /// Returns the number of Eidos compression blocks represented by the standalone compression
    /// AIR rows.
    pub fn eidos_compression_count(&self) -> usize {
        debug_assert_eq!(self.eidos_compression_rows % EIDOS_COMPRESSION_CYCLE_LEN, 0);
        self.eidos_compression_rows / EIDOS_COMPRESSION_CYCLE_LEN
    }

    /// Returns the fixed byte-pair lookup AIR rows.
    ///
    /// This table has one row per byte pair, so its row count is already its physical AIR height.
    pub fn byte_pair_lookup_rows(&self) -> usize {
        self.byte_pair_lookup_rows
    }

    /// Returns the maximum unpadded row count among the four AIRs.
    pub fn trace_len(&self) -> usize {
        self.core_rows
            .max(self.chiplets_rows())
            .max(self.eidos_compression_rows)
            .max(self.byte_pair_lookup_rows)
    }

    /// Returns the padded height of the core AIR.
    pub fn core_height(&self) -> usize {
        padded_height(self.core_rows)
    }

    /// Returns the padded height of the chiplets AIR.
    pub fn chiplets_height(&self) -> usize {
        padded_height(self.chiplets_rows())
    }

    /// Returns the padded height of the Eidos compression AIR.
    pub fn eidos_compression_height(&self) -> usize {
        padded_height(self.eidos_compression_rows)
    }

    /// Returns the greatest padded height among the four AIRs.
    pub fn padded_trace_len(&self) -> usize {
        self.padded_heights
            .map(|heights| heights.into_iter().max().expect("heights is non-empty"))
            .unwrap_or_else(|| {
                self.core_height()
                    .max(self.chiplets_height())
                    .max(self.eidos_compression_height())
                    .max(self.byte_pair_lookup_rows)
            })
    }

    /// Returns the padded per-AIR heights, in [`miden_air::AIRS`] order, if known.
    pub fn padded_heights(&self) -> Option<&[usize; MIDEN_AIR_COUNT]> {
        self.padded_heights.as_ref()
    }

    /// Returns the modelled peak prover memory, in bytes, for the padded per-AIR heights, if
    /// known. See [`miden_air::memory::prover_peak_bytes`] for what this does and does not cover.
    pub fn prover_memory_bytes(&self, params: &PcsParams) -> Option<u64> {
        memory::prover_peak_bytes(self.padded_heights()?, params)
    }

    /// Returns the percent (0 - 100) of rows added by padding.
    pub fn padding_percentage(&self) -> usize {
        (self.padded_trace_len() - self.trace_len()) * 100 / self.padded_trace_len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn trace_len_summary_reports_eidos_compression_count() {
        let summary =
            TraceLenSummary::new(0, ChipletsLengths::default(), 3 * EIDOS_COMPRESSION_CYCLE_LEN, 0);

        assert_eq!(summary.eidos_compression_rows(), 3 * EIDOS_COMPRESSION_CYCLE_LEN);
        assert_eq!(summary.eidos_compression_count(), 3);
    }

    #[test]
    fn trace_len_summary_records_four_air_heights_in_instance_order() {
        let heights = [MIN_TRACE_LEN, 2 * MIN_TRACE_LEN, 4 * MIN_TRACE_LEN, 1 << 16];
        let summary = TraceLenSummary::new_with_padded(
            17,
            ChipletsLengths::from_parts(19, 0, 0, 0, 0),
            3 * EIDOS_COMPRESSION_CYCLE_LEN,
            1 << 16,
            heights,
        );

        assert_eq!(summary.core_rows(), 17);
        // ChipletsLengths adds the mandatory connector-padding row.
        assert_eq!(summary.chiplets_rows(), 20);
        assert_eq!(summary.eidos_compression_rows(), 3 * EIDOS_COMPRESSION_CYCLE_LEN);
        assert_eq!(summary.byte_pair_lookup_rows(), 1 << 16);
        assert_eq!(summary.padded_heights(), Some(&heights));
        assert_eq!(summary.padded_trace_len(), 1 << 16);

        let params = miden_air::config::pcs_params();
        assert_eq!(
            summary.prover_memory_bytes(&params),
            memory::prover_peak_bytes(&heights, &params)
        );
    }
}

fn padded_height(rows: usize) -> usize {
    rows.next_power_of_two().max(MIN_TRACE_LEN)
}

// CHIPLET LENGTHS
// ================================================================================================

/// Contains trace lengths of all chiplets: hash, bitwise, memory, ACE, and kernel ROM.
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq)]
pub struct ChipletsLengths {
    hash_chiplet_len: usize,
    bitwise_chiplet_len: usize,
    memory_chiplet_len: usize,
    ace_chiplet_len: usize,
    kernel_rom_len: usize,
}

impl ChipletsLengths {
    pub fn new(chiplets: &Chiplets) -> Self {
        ChipletsLengths {
            hash_chiplet_len: chiplets.bitwise_start().into(),
            bitwise_chiplet_len: chiplets.memory_start() - chiplets.bitwise_start(),
            memory_chiplet_len: chiplets.ace_start() - chiplets.memory_start(),
            ace_chiplet_len: chiplets.kernel_rom_start() - chiplets.ace_start(),
            kernel_rom_len: chiplets.padding_start() - chiplets.kernel_rom_start(),
        }
    }

    pub fn from_parts(
        hash_len: usize,
        bitwise_len: usize,
        memory_len: usize,
        ace_len: usize,
        kernel_len: usize,
    ) -> Self {
        ChipletsLengths {
            hash_chiplet_len: hash_len,
            bitwise_chiplet_len: bitwise_len,
            memory_chiplet_len: memory_len,
            ace_chiplet_len: ace_len,
            kernel_rom_len: kernel_len,
        }
    }

    /// Returns the length of the hash chiplet trace.
    pub fn hash_chiplet_len(&self) -> usize {
        self.hash_chiplet_len
    }

    /// Returns the length of the bitwise trace.
    pub fn bitwise_chiplet_len(&self) -> usize {
        self.bitwise_chiplet_len
    }

    /// Returns the length of the memory trace.
    pub fn memory_chiplet_len(&self) -> usize {
        self.memory_chiplet_len
    }

    /// Returns the length of the ACE chiplet trace.
    pub fn ace_chiplet_len(&self) -> usize {
        self.ace_chiplet_len
    }

    /// Returns the length of the kernel ROM trace.
    pub fn kernel_rom_len(&self) -> usize {
        self.kernel_rom_len
    }

    /// Returns the length of the trace required to accommodate chiplet components and 1
    /// mandatory padding row required for ensuring sufficient trace length for auxiliary connector
    /// columns that rely on the memory chiplet.
    pub fn trace_len(&self) -> usize {
        self.hash_chiplet_len()
            + self.bitwise_chiplet_len()
            + self.memory_chiplet_len()
            + self.ace_chiplet_len()
            + self.kernel_rom_len()
            + 1
    }
}

// U32 HELPERS
// ================================================================================================

/// Splits an element into two 16 bit integer limbs. It assumes that the field element contains a
/// valid 32-bit integer value.
pub(crate) fn split_element_u32_into_u16(value: Felt) -> (Felt, Felt) {
    let (hi, lo) = split_u32_into_u16(value.as_canonical_u64());
    (Felt::new_unchecked(hi as u64), Felt::new_unchecked(lo as u64))
}

/// Splits a u64 integer assumed to contain a 32-bit value into two u16 integers.
///
/// # Errors
/// Fails in debug mode if the provided value is not a 32-bit value.
pub(crate) fn split_u32_into_u16(value: u64) -> (u16, u16) {
    const U32MAX: u64 = u32::MAX as u64;
    debug_assert!(value <= U32MAX, "not a 32-bit value");

    let lo = value as u16;
    let hi = (value >> 16) as u16;

    (hi, lo)
}

// TEST HELPERS
// ================================================================================================

/// Builds a 17-op basic block payload that straddles a RESPAN batch boundary, plus the initial
/// values its `Push` ops emit. Consumed by decoder / hasher tests that exercise multi-batch
/// SPAN execution.
#[cfg(test)]
pub(super) fn build_span_with_respan_ops() -> (Vec<Operation>, Vec<Felt>) {
    let iv = [1, 3, 5, 7, 9, 11, 13, 15, 17].to_elements();
    let ops = alloc::vec![
        Operation::Push(iv[0]),
        Operation::Push(iv[1]),
        Operation::Push(iv[2]),
        Operation::Push(iv[3]),
        Operation::Push(iv[4]),
        Operation::Push(iv[5]),
        Operation::Push(iv[6]),
        // next batch
        Operation::Push(iv[7]),
        Operation::Push(iv[8]),
        Operation::Add,
        // drops to make sure stack overflow is empty on exit
        Operation::Drop,
        Operation::Drop,
        Operation::Drop,
        Operation::Drop,
        Operation::Drop,
        Operation::Drop,
        Operation::Drop,
        Operation::Drop,
    ];
    (ops, iv)
}