sp1-core-machine 6.0.0-beta.1

SP1 is a performant, 100% open-source, contributor-friendly zkVM.
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
use std::iter::once;

use itertools::Itertools;
use slop_air::AirBuilder;
use slop_algebra::{AbstractField, Field};
use sp1_core_executor::ByteOpcode;
use sp1_hypercube::{
    air::{AirInteraction, BaseAirBuilder, ByteAirBuilder, InteractionScope},
    InteractionKind, Word,
};

use crate::memory::{
    MemoryAccessCols, MemoryAccessTimestamp, PageProtAccessCols, RegisterAccessCols,
    RegisterAccessTimestamp,
};

pub trait MemoryAirBuilder: BaseAirBuilder {
    /// Constrain a memory read, by using the read value as the write value.
    /// The constraints enforce that the new timestamp is greater than the previous one.
    fn eval_memory_access_read<E: Into<Self::Expr> + Clone>(
        &mut self,
        clk_high: impl Into<Self::Expr>,
        clk_low: impl Into<Self::Expr>,
        addr: &[Self::Expr; 3],
        mem_access: MemoryAccessCols<E>,
        do_check: impl Into<Self::Expr>,
    ) {
        let do_check: Self::Expr = do_check.into();
        let clk_high: Self::Expr = clk_high.into();
        let clk_low: Self::Expr = clk_low.into();

        self.assert_bool(do_check.clone());
        // Verify that the current memory access time is greater than the previous's.
        self.eval_memory_access_timestamp(
            &mem_access.access_timestamp,
            do_check.clone(),
            clk_high.clone(),
            clk_low.clone(),
        );

        // Add to the memory argument.
        let prev_high = mem_access.access_timestamp.prev_high.clone().into();
        let prev_low = mem_access.access_timestamp.prev_low.clone().into();
        let prev_values = once(prev_high)
            .chain(once(prev_low))
            .chain(addr.clone())
            .chain(mem_access.prev_value.clone().map(Into::into))
            .collect();
        let current_values = once(clk_high)
            .chain(once(clk_low))
            .chain(addr.clone())
            .chain(mem_access.prev_value.clone().map(Into::into))
            .collect();

        // The previous values get sent with multiplicity = 1, for "read".
        self.send(
            AirInteraction::new(prev_values, do_check.clone(), InteractionKind::Memory),
            InteractionScope::Local,
        );

        // The current values get "received", i.e. multiplicity = -1
        self.receive(
            AirInteraction::new(current_values, do_check.clone(), InteractionKind::Memory),
            InteractionScope::Local,
        );
    }

    /// Constrain a memory write, given the write value.
    /// The constraints enforce that the new timestamp is greater than the previous one.
    fn eval_memory_access_write<E: Into<Self::Expr> + Clone>(
        &mut self,
        clk_high: impl Into<Self::Expr>,
        clk_low: impl Into<Self::Expr>,
        addr: &[Self::Expr; 3],
        mem_access: MemoryAccessCols<E>,
        write_value: Word<impl Into<Self::Expr>>,
        do_check: impl Into<Self::Expr>,
    ) {
        let do_check: Self::Expr = do_check.into();
        let clk_high: Self::Expr = clk_high.into();
        let clk_low: Self::Expr = clk_low.into();

        self.assert_bool(do_check.clone());
        // Verify that the current memory access time is greater than the previous's.
        self.eval_memory_access_timestamp(
            &mem_access.access_timestamp,
            do_check.clone(),
            clk_high.clone(),
            clk_low.clone(),
        );

        // Add to the memory argument.
        let prev_high = mem_access.access_timestamp.prev_high.clone().into();
        let prev_low = mem_access.access_timestamp.prev_low.clone().into();
        let prev_values = once(prev_high)
            .chain(once(prev_low.clone()))
            .chain(addr.clone())
            .chain(mem_access.prev_value.clone().map(Into::into))
            .collect();
        let current_values = once(clk_high.clone())
            .chain(once(clk_low.clone()))
            .chain(addr.clone())
            .chain(write_value.map(Into::into))
            .collect();

        // The previous values get sent with multiplicity = 1, for "read".
        self.send(
            AirInteraction::new(prev_values, do_check.clone(), InteractionKind::Memory),
            InteractionScope::Local,
        );

        // The current values get "received", i.e. multiplicity = -1
        self.receive(
            AirInteraction::new(current_values, do_check.clone(), InteractionKind::Memory),
            InteractionScope::Local,
        );
    }

    /// Constrain a register read, by using the read value as the write value.
    /// The constraints enforce that the new timestamp is greater than the previous one.
    /// For register reads, the top limb of the timestamp is equal to the previous one.
    fn eval_register_access_read<E: Into<Self::Expr> + Clone>(
        &mut self,
        clk_high: impl Into<Self::Expr>,
        clk_low: impl Into<Self::Expr>,
        addr: [Self::Expr; 3],
        reg_access: RegisterAccessCols<E>,
        do_check: impl Into<Self::Expr>,
    ) {
        let do_check: Self::Expr = do_check.into();
        let clk_high: Self::Expr = clk_high.into();
        let clk_low: Self::Expr = clk_low.into();

        self.assert_bool(do_check.clone());
        // Verify that the current memory access time is greater than the previous's.
        self.eval_register_access_timestamp(
            &reg_access.access_timestamp,
            do_check.clone(),
            clk_low.clone(),
        );

        // Add to the memory argument.
        let prev_low = reg_access.access_timestamp.prev_low.clone().into();
        let prev_values = once(clk_high.clone())
            .chain(once(prev_low))
            .chain(addr.clone())
            .chain(reg_access.prev_value.clone().map(Into::into))
            .collect();
        let current_values = once(clk_high)
            .chain(once(clk_low))
            .chain(addr.clone())
            .chain(reg_access.prev_value.clone().map(Into::into))
            .collect();

        // The previous values get sent with multiplicity = 1, for "read".
        self.send(
            AirInteraction::new(prev_values, do_check.clone(), InteractionKind::Memory),
            InteractionScope::Local,
        );

        // The current values get "received", i.e. multiplicity = -1
        self.receive(
            AirInteraction::new(current_values, do_check.clone(), InteractionKind::Memory),
            InteractionScope::Local,
        );
    }

    /// Constrain a register write, given the write value.
    /// The constraints enforce that the new timestamp is greater than the previous one.
    /// For register reads, the top limb of the timestamp is equal to the previous one.
    fn eval_register_access_write<E: Into<Self::Expr> + Clone>(
        &mut self,
        clk_high: impl Into<Self::Expr>,
        clk_low: impl Into<Self::Expr>,
        addr: [Self::Expr; 3],
        reg_access: RegisterAccessCols<E>,
        write_value: Word<impl Into<Self::Expr>>,
        do_check: impl Into<Self::Expr>,
    ) {
        let do_check: Self::Expr = do_check.into();
        let clk_high: Self::Expr = clk_high.into();
        let clk_low: Self::Expr = clk_low.into();

        self.assert_bool(do_check.clone());
        // Verify that the current memory access time is greater than the previous's.
        self.eval_register_access_timestamp(
            &reg_access.access_timestamp,
            do_check.clone(),
            clk_low.clone(),
        );

        // Add to the memory argument.
        let prev_low = reg_access.access_timestamp.prev_low.clone().into();
        let prev_values = once(clk_high.clone())
            .chain(once(prev_low.clone()))
            .chain(addr.clone())
            .chain(reg_access.prev_value.clone().map(Into::into))
            .collect();
        let current_values = once(clk_high.clone())
            .chain(once(clk_low.clone()))
            .chain(addr.clone())
            .chain(write_value.map(Into::into))
            .collect();

        // The previous values get sent with multiplicity = 1, for "read".
        self.send(
            AirInteraction::new(prev_values, do_check.clone(), InteractionKind::Memory),
            InteractionScope::Local,
        );

        // The current values get "received", i.e. multiplicity = -1
        self.receive(
            AirInteraction::new(current_values, do_check.clone(), InteractionKind::Memory),
            InteractionScope::Local,
        );
    }

    /// Constraints a memory read to a slice of `MemoryAccessCols`.
    fn eval_memory_access_slice_read<E: Into<Self::Expr> + Copy>(
        &mut self,
        clk_high: impl Into<Self::Expr> + Clone,
        clk_low: impl Into<Self::Expr> + Clone,
        addr_slice: &[[Self::Expr; 3]],
        memory_access_slice: &[MemoryAccessCols<E>],
        verify_memory_access: impl Into<Self::Expr> + Copy,
    ) {
        for (access_slice, addr) in memory_access_slice.iter().zip(addr_slice) {
            self.eval_memory_access_read(
                clk_high.clone(),
                clk_low.clone(),
                addr,
                *access_slice,
                verify_memory_access,
            );
        }
    }

    /// Constraints a memory write to a slice of `MemoryAccessCols`.
    fn eval_memory_access_slice_write<E: Into<Self::Expr> + Copy>(
        &mut self,
        clk_high: impl Into<Self::Expr> + Clone,
        clk_low: impl Into<Self::Expr> + Clone,
        addr_slice: &[[Self::Expr; 3]],
        memory_access_slice: &[MemoryAccessCols<E>],
        write_values: Vec<Word<impl Into<Self::Expr>>>,
        verify_memory_access: impl Into<Self::Expr> + Copy,
    ) {
        for ((access_slice, addr), write_value) in
            memory_access_slice.iter().zip_eq(addr_slice).zip_eq(write_values)
        {
            self.eval_memory_access_write(
                clk_high.clone(),
                clk_low.clone(),
                addr,
                *access_slice,
                write_value,
                verify_memory_access,
            );
        }
    }

    /// Verifies the memory access timestamp.
    ///
    /// This method verifies that the current memory access happened after the previous one's.
    fn eval_memory_access_timestamp(
        &mut self,
        mem_access: &MemoryAccessTimestamp<impl Into<Self::Expr> + Clone>,
        do_check: impl Into<Self::Expr>,
        clk_high: impl Into<Self::Expr> + Clone,
        clk_low: impl Into<Self::Expr>,
    ) {
        let do_check: Self::Expr = do_check.into();
        let compare_low: Self::Expr = mem_access.compare_low.clone().into();
        let clk_high: Self::Expr = clk_high.clone().into();
        let prev_high: Self::Expr = mem_access.prev_high.clone().into();

        // First verify that compare_clk's value is correct.
        self.when(do_check.clone()).assert_bool(compare_low.clone());
        self.when(do_check.clone())
            .when(compare_low.clone())
            .assert_eq(clk_high.clone(), prev_high);

        // Get the comparison timestamp values for the current and previous memory access.
        let prev_comp_value = self.if_else(
            compare_low.clone(),
            mem_access.prev_low.clone(),
            mem_access.prev_high.clone(),
        );

        let current_comp_val = self.if_else(compare_low.clone(), clk_low.into(), clk_high.clone());

        // Assert `current_comp_val > prev_comp_val`. We check this by asserting that
        // `0 <= current_comp_val-prev_comp_val-1 < 2^24`.
        //
        // The equivalence of these statements comes from the fact that if
        // `current_comp_val <= prev_comp_val`, then `current_comp_val-prev_comp_val-1 < 0` and will
        // underflow in the prime field, resulting in a value that is `>= 2^24` as long as both
        // `current_comp_val, prev_comp_val` are range-checked to be `< 2^24` and as long as we're
        // working in a field larger than `2 * 2^24` (which is true of the SP1Field).
        let diff_minus_one = current_comp_val - prev_comp_value - Self::Expr::one();

        // Verify that value = limb_low + limb_high * 2^16.
        self.when(do_check.clone()).assert_eq(
            diff_minus_one,
            mem_access.diff_low_limb.clone().into()
                + mem_access.diff_high_limb.clone().into()
                    * Self::Expr::from_canonical_u32(1 << 16),
        );

        // Send the range checks for the limbs.
        self.send_byte(
            Self::Expr::from_canonical_u8(ByteOpcode::Range as u8),
            mem_access.diff_low_limb.clone(),
            Self::Expr::from_canonical_u32(16),
            Self::Expr::zero(),
            do_check.clone(),
        );

        self.send_byte(
            Self::Expr::from_canonical_u8(ByteOpcode::U8Range as u8),
            Self::Expr::zero(),
            mem_access.diff_high_limb.clone(),
            Self::Expr::zero(),
            do_check,
        )
    }

    /// Verifies the register access timestamp, where the high limb of the timestamp is equal.
    ///
    /// This method verifies that the current memory access happened after the previous one's.
    /// Specifically it will ensure that the current's clk val is greater than the previous's.
    fn eval_register_access_timestamp(
        &mut self,
        reg_access: &RegisterAccessTimestamp<impl Into<Self::Expr> + Clone>,
        do_check: impl Into<Self::Expr>,
        clk: impl Into<Self::Expr>,
    ) {
        let do_check: Self::Expr = do_check.into();

        let diff_minus_one = clk.into() - reg_access.prev_low.clone().into() - Self::Expr::one();
        let diff_high_limb = (diff_minus_one.clone() - reg_access.diff_low_limb.clone().into())
            * Self::F::from_canonical_u32(1 << 16).inverse();

        // Send the range checks for the limbs.
        self.send_byte(
            Self::Expr::from_canonical_u8(ByteOpcode::Range as u8),
            reg_access.diff_low_limb.clone(),
            Self::Expr::from_canonical_u32(16),
            Self::Expr::zero(),
            do_check.clone(),
        );

        self.send_byte(
            Self::Expr::from_canonical_u8(ByteOpcode::U8Range as u8),
            Self::Expr::zero(),
            diff_high_limb,
            Self::Expr::zero(),
            do_check,
        )
    }

    /// This function is used to send to the a request to check an address's permissions.
    #[allow(clippy::too_many_arguments)]
    fn send_page_prot(
        &mut self,
        clk_high: impl Into<Self::Expr>,
        clk_low: impl Into<Self::Expr>,
        addr: &[Self::Expr; 3],
        permissions: impl Into<Self::Expr>,
        multiplicity: impl Into<Self::Expr>,
    ) {
        let values = once(clk_high.into())
            .chain(once(clk_low.into()))
            .chain(addr.clone())
            .chain(once(permissions.into()))
            .collect();

        self.send(
            AirInteraction::new(values, multiplicity.into(), InteractionKind::PageProt),
            InteractionScope::Local,
        );
    }

    /// This function is used to receive a response to a request to check an address's permissions.
    #[allow(clippy::too_many_arguments)]
    fn receive_page_prot(
        &mut self,
        clk_high: impl Into<Self::Expr>,
        clk_low: impl Into<Self::Expr>,
        addr: &[Self::Expr; 3],
        permissions: impl Into<Self::Expr>,
        multiplicity: impl Into<Self::Expr>,
    ) {
        let values = once(clk_high.into())
            .chain(once(clk_low.into()))
            .chain(addr.clone())
            .chain(once(permissions.into()))
            .collect();

        self.receive(
            AirInteraction::new(values, multiplicity.into(), InteractionKind::PageProt),
            InteractionScope::Local,
        );
    }

    /// Constrain a page prot read, by using the read value as the write value.
    /// The constraints enforce that the new clk is greater than the previous one.
    fn eval_page_prot_access_read<E: Into<Self::Expr> + Clone>(
        &mut self,
        clk_high: impl Into<Self::Expr>,
        clk_low: impl Into<Self::Expr>,
        prot_page_idx: &[impl Into<Self::Expr> + Clone; 3],
        page_prot_access: PageProtAccessCols<E>,
        do_check: impl Into<Self::Expr>,
    ) {
        let do_check: Self::Expr = do_check.into();
        let clk_high: Self::Expr = clk_high.into();
        let clk_low: Self::Expr = clk_low.into();

        self.assert_bool(do_check.clone());
        // Verify that the current memory access time is greater than the previous's.
        self.eval_memory_access_timestamp(
            &page_prot_access.access_timestamp,
            do_check.clone(),
            clk_high.clone(),
            clk_low.clone(),
        );

        // Add to the memory argument.
        let prev_high = page_prot_access.access_timestamp.prev_high.clone().into();
        let prev_low = page_prot_access.access_timestamp.prev_low.clone().into();
        let prev_values = once(prev_high)
            .chain(once(prev_low))
            .chain(prot_page_idx.clone().map(Into::into))
            .chain(once(page_prot_access.prev_prot_bitmap.clone().into()))
            .collect();
        let current_values = once(clk_high)
            .chain(once(clk_low))
            .chain(prot_page_idx.clone().map(Into::into))
            .chain(once(page_prot_access.prev_prot_bitmap.into()))
            .collect();

        // The previous values get sent with multiplicity = 1, for "read".
        self.send(
            AirInteraction::new(prev_values, do_check.clone(), InteractionKind::PageProtAccess),
            InteractionScope::Local,
        );

        // The current values get "received", i.e. multiplicity = -1
        self.receive(
            AirInteraction::new(current_values, do_check.clone(), InteractionKind::PageProtAccess),
            InteractionScope::Local,
        );
    }

    /// Constrain a page prot write, updating the protection bitmap.
    /// The constraints enforce that the new clk is greater than the previous one.
    fn eval_page_prot_access_write<E: Into<Self::Expr> + Clone>(
        &mut self,
        clk_high: impl Into<Self::Expr>,
        clk_low: impl Into<Self::Expr>,
        prot_page_idx: &[impl Into<Self::Expr> + Clone; 3],
        page_prot_access: PageProtAccessCols<E>,
        new_prot_bitmap: impl Into<Self::Expr>,
        do_check: impl Into<Self::Expr>,
    ) {
        let do_check: Self::Expr = do_check.into();
        let clk_high: Self::Expr = clk_high.into();
        let clk_low: Self::Expr = clk_low.into();
        let new_prot_bitmap: Self::Expr = new_prot_bitmap.into();

        self.assert_bool(do_check.clone());

        // Verify that the current memory access time is greater than the previous's.
        self.eval_memory_access_timestamp(
            &page_prot_access.access_timestamp,
            do_check.clone(),
            clk_high.clone(),
            clk_low.clone(),
        );

        // Read the previous state
        let prev_high = page_prot_access.access_timestamp.prev_high.clone().into();
        let prev_low = page_prot_access.access_timestamp.prev_low.clone().into();
        let prev_values = once(prev_high)
            .chain(once(prev_low))
            .chain(prot_page_idx.clone().map(Into::into))
            .chain(once(page_prot_access.prev_prot_bitmap.clone().into()))
            .collect();

        // Write the new state with updated protection bitmap
        let current_values = once(clk_high)
            .chain(once(clk_low))
            .chain(prot_page_idx.clone().map(Into::into))
            .chain(once(new_prot_bitmap))
            .collect();

        // The previous values get sent with multiplicity = 1, for "read".
        self.send(
            AirInteraction::new(prev_values, do_check.clone(), InteractionKind::PageProtAccess),
            InteractionScope::Local,
        );

        // The current values get "received", i.e. multiplicity = -1
        self.receive(
            AirInteraction::new(current_values, do_check.clone(), InteractionKind::PageProtAccess),
            InteractionScope::Local,
        );
    }
}