openvm_native_compiler/ir/
collections.rs

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
use alloc::rc::Rc;
use std::cell::RefCell;

use itertools::Itertools;
use openvm_stark_backend::p3_field::AbstractField;
use serde::{Deserialize, Serialize};

use super::{
    Builder, Config, FromConstant, MemIndex, MemVariable, Ptr, RVar, Ref, SymbolicVar, Usize, Var,
    Variable,
};

/// A logical array.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Array<C: Config, T> {
    /// Array of some local variables or constants, which can only be manipulated statically. It
    /// only exists in the DSL syntax and isn't backed by memory.
    Fixed(Rc<RefCell<Vec<Option<T>>>>),
    /// Array on heap. Index access can use variables. Length could be determined on runtime but
    /// cannot change after initialization.
    Dyn(Ptr<C::N>, Usize<C::N>),
}

impl<C: Config, V: MemVariable<C>> Array<C, V> {
    /// Gets a right value of the array.
    pub fn vec(&self) -> Vec<V> {
        match self {
            Self::Fixed(vec) => vec.borrow().iter().map(|x| x.clone().unwrap()).collect(),
            _ => panic!("array is dynamic, not fixed"),
        }
    }

    pub fn ptr(&self) -> Ptr<C::N> {
        match *self {
            Array::Dyn(ptr, _) => ptr,
            Array::Fixed(_) => panic!("cannot retrieve pointer for a compile-time array"),
        }
    }

    /// Gets the length of the array as a variable inside the DSL.
    pub fn len(&self) -> Usize<C::N> {
        match self {
            Self::Fixed(vec) => Usize::from(vec.borrow().len()),
            Self::Dyn(_, len) => len.clone(),
        }
    }

    /// Asserts that an array has a certain length. Change its length to constant if it is a variable.
    pub fn assert_len(&self, builder: &mut Builder<C>, len: usize) {
        match self {
            Self::Fixed(vec) => {
                assert_eq!(vec.borrow().len(), len);
            }
            Self::Dyn(_, c_len) => match c_len {
                Usize::Const(_) => {
                    assert_eq!(c_len.value(), len);
                }
                Usize::Var(c_len) => {
                    builder.assert_eq::<Var<_>>(*c_len, C::N::from_canonical_usize(len));
                }
            },
        }
    }

    /// Shifts the array by `shift` elements.
    /// !Attention!: the behavior of `Fixed` and `Dyn` is different. For Dyn, the shift is a view
    /// and shares memory with the original. For `Fixed`, `set`/`set_value` on slices won't impact
    /// the original array.
    pub fn shift(&self, builder: &mut Builder<C>, shift: impl Into<RVar<C::N>>) -> Array<C, V> {
        match self {
            Self::Fixed(v) => {
                let shift = shift.into();
                if let RVar::Const(_) = shift {
                    let shift = shift.value();
                    Array::Fixed(Rc::new(RefCell::new(v.borrow()[shift..].to_vec())))
                } else {
                    panic!("Cannot shift a fixed array with a variable shift");
                }
            }
            Self::Dyn(ptr, len) => {
                assert_eq!(V::size_of(), 1, "only support variables of size 1");
                let len = RVar::from(len.clone());
                let shift = shift.into();
                let new_ptr = builder.eval(*ptr + shift);
                let new_length = builder.eval(len - shift);
                Array::Dyn(new_ptr, Usize::Var(new_length))
            }
        }
    }

    /// Truncates the array to `len` elements.
    pub fn truncate(&self, builder: &mut Builder<C>, len: Usize<C::N>) {
        match self {
            Self::Fixed(v) => {
                let len = len.value();
                v.borrow_mut().truncate(len);
            }
            Self::Dyn(_, old_len) => {
                builder.assign(old_len, len);
            }
        };
    }

    /// Slices the array from `start` to `end`.
    /// !Attention!: the behavior of `Fixed` and `Dyn` is different. For Dyn, the shift is a view
    /// and shares memory with the original. For `Fixed`, `set`/`set_value` on slices won't impact
    /// the original array.
    pub fn slice(
        &self,
        builder: &mut Builder<C>,
        start: impl Into<RVar<C::N>>,
        end: impl Into<RVar<C::N>>,
    ) -> Array<C, V> {
        let start = start.into();
        let end = end.into();
        match self {
            Self::Fixed(v) => {
                if let (RVar::Const(_), RVar::Const(_)) = (&start, &end) {
                    Array::Fixed(Rc::new(RefCell::new(
                        v.borrow()[start.value()..end.value()].to_vec(),
                    )))
                } else {
                    panic!("Cannot slice a fixed array with a variable start or end");
                }
            }
            Self::Dyn(ptr, len) => {
                if builder.flags.debug {
                    let valid = builder.lt(start, end);
                    builder.assert_var_eq(valid, C::N::ONE);

                    let len_plus_1_v = SymbolicVar::from(len.clone()) + C::N::ONE;
                    let valid = builder.lt(end, len_plus_1_v);
                    builder.assert_var_eq(valid, C::N::ONE);
                }

                let slice_len = builder.eval(end - start);
                let address = builder.eval(ptr.address + start);
                let ptr = Ptr { address };
                Array::Dyn(ptr, Usize::Var(slice_len))
            }
        }
    }
}

impl<C: Config> Builder<C> {
    /// Initialize an array of fixed length `len`. The entries will be uninitialized.
    pub fn array<V: MemVariable<C>>(&mut self, len: impl Into<RVar<C::N>>) -> Array<C, V> {
        let len = len.into();
        if self.flags.static_only {
            self.uninit_fixed_array(len.value())
        } else {
            self.dyn_array(len)
        }
    }

    /// Creates an array from a vector.
    pub fn vec<V: MemVariable<C>>(&mut self, v: Vec<V>) -> Array<C, V> {
        Array::Fixed(Rc::new(RefCell::new(
            v.into_iter().map(|x| Some(x)).collect(),
        )))
    }

    /// Create an uninitialized Array::Fixed.
    pub fn uninit_fixed_array<V: Variable<C>>(&mut self, len: usize) -> Array<C, V> {
        Array::Fixed(Rc::new(RefCell::new(vec![None::<V>; len])))
    }

    /// Creates a dynamic array for a length.
    pub fn dyn_array<V: MemVariable<C>>(&mut self, len: impl Into<RVar<C::N>>) -> Array<C, V> {
        let len: Var<_> = self.eval(len.into());
        let ptr = self.alloc(len, V::size_of());
        Array::Dyn(ptr, Usize::Var(len))
    }

    pub fn get<V: MemVariable<C>, I: Into<RVar<C::N>>>(
        &mut self,
        slice: &Array<C, V>,
        index: I,
    ) -> V {
        let index = index.into();

        match slice {
            Array::Fixed(slice) => {
                if let RVar::Const(_) = index {
                    let idx = index.value();
                    if let Some(ele) = &slice.borrow()[idx] {
                        ele.clone()
                    } else {
                        panic!("Cannot get an uninitialized element in a fixed slice");
                    }
                } else {
                    panic!("Cannot index into a fixed slice with a variable size")
                }
            }
            Array::Dyn(ptr, len) => {
                if self.flags.debug {
                    let valid = self.lt(index, len.clone());
                    self.assert_var_eq(valid, C::N::ONE);
                }
                let index = MemIndex {
                    index,
                    offset: 0,
                    size: V::size_of(),
                };
                let var: V = self.uninit();
                self.load(var.clone(), *ptr, index);
                var
            }
        }
    }

    /// Returns a pointer to the array at the specified `index` within the given `slice`.
    pub fn get_ptr<V: MemVariable<C>, I: Into<RVar<C::N>>>(
        &mut self,
        slice: &Array<C, Array<C, V>>,
        index: I,
    ) -> Ptr<C::N> {
        let index = index.into();

        match slice {
            Array::Fixed(_) => {
                todo!()
            }
            Array::Dyn(ptr, len) => {
                if self.flags.debug {
                    let valid = self.lt(index, len.clone());
                    self.assert_var_eq(valid, C::N::ONE);
                }
                let index = MemIndex {
                    index,
                    offset: 0,
                    size: <Array<C, V> as MemVariable<C>>::size_of(),
                };
                let var: Ptr<C::N> = self.uninit();
                self.load(var, *ptr, index);
                var
            }
        }
    }

    fn ptr_at<V: MemVariable<C>, I: Into<RVar<C::N>>>(
        &mut self,
        slice: &Array<C, V>,
        index: I,
    ) -> Ptr<C::N> {
        let index = index.into();

        match slice {
            Array::Fixed(_) => {
                panic!();
            }
            Array::Dyn(ptr, len) => {
                if self.flags.debug {
                    let valid = self.lt(index, len.clone());
                    self.assert_var_eq(valid, C::N::ONE);
                }
                Ptr {
                    address: self.eval(
                        ptr.address
                            + index * RVar::from_field(C::N::from_canonical_usize(V::size_of())),
                    ),
                }
            }
        }
    }

    pub fn get_ref<V: MemVariable<C>, I: Into<RVar<C::N>>>(
        &mut self,
        slice: &Array<C, V>,
        index: I,
    ) -> Ref<C, V> {
        let index = index.into();
        let ptr = self.ptr_at(slice, index);
        Ref::from_ptr(ptr)
    }

    pub fn set<V: MemVariable<C>, I: Into<RVar<C::N>>, Expr: Into<V::Expression>>(
        &mut self,
        slice: &Array<C, V>,
        index: I,
        value: Expr,
    ) {
        let index = index.into();

        match slice {
            Array::Fixed(v) => {
                if let RVar::Const(_) = index {
                    let idx = index.value();
                    let value = self.eval(value);
                    v.borrow_mut()[idx] = Some(value);
                } else {
                    panic!("Cannot index into a fixed slice with a variable index")
                }
            }
            Array::Dyn(ptr, len) => {
                if self.flags.debug {
                    let valid = self.lt(index, len.clone());
                    self.assert_var_eq(valid, C::N::ONE);
                }
                let index = MemIndex {
                    index,
                    offset: 0,
                    size: V::size_of(),
                };
                let value: V = self.eval(value);
                self.store(*ptr, index, value);
            }
        }
    }

    pub fn set_value<V: MemVariable<C>, I: Into<RVar<C::N>>>(
        &mut self,
        slice: &Array<C, V>,
        index: I,
        value: V,
    ) {
        let index = index.into();

        match slice {
            Array::Fixed(v) => {
                if let RVar::Const(_) = index {
                    let idx = index.value();
                    v.borrow_mut()[idx] = Some(value);
                } else {
                    panic!("Cannot index into a fixed slice with a variable size")
                }
            }
            Array::Dyn(ptr, _) => {
                let index = MemIndex {
                    index,
                    offset: 0,
                    size: V::size_of(),
                };
                self.store(*ptr, index, value);
            }
        }
    }
}

impl<C: Config, T: MemVariable<C>> Variable<C> for Array<C, T> {
    type Expression = Self;

    fn uninit(builder: &mut Builder<C>) -> Self {
        Array::Dyn(builder.uninit(), builder.uninit())
    }

    fn assign(&self, src: Self::Expression, builder: &mut Builder<C>) {
        match (self, src.clone()) {
            (Array::Dyn(lhs_ptr, lhs_len), Array::Dyn(rhs_ptr, rhs_len)) => {
                builder.assign(lhs_ptr, rhs_ptr);
                builder.assign(lhs_len, rhs_len);
            }
            _ => unreachable!(),
        }
    }

    fn assert_eq(
        lhs: impl Into<Self::Expression>,
        rhs: impl Into<Self::Expression>,
        builder: &mut Builder<C>,
    ) {
        let lhs = lhs.into();
        let rhs = rhs.into();

        match (lhs.clone(), rhs.clone()) {
            (Array::Fixed(lhs), Array::Fixed(rhs)) => {
                // No need to compare if they are the same reference. The same reference will
                // also cause borrow errors in the following loop.
                if Rc::ptr_eq(&lhs, &rhs) {
                    return;
                }
                for (l, r) in lhs.borrow().iter().zip_eq(rhs.borrow().iter()) {
                    assert!(l.is_some(), "lhs array is not fully initialized");
                    assert!(r.is_some(), "rhs array is not fully initialized");
                    T::assert_eq(
                        T::Expression::from(l.as_ref().unwrap().clone()),
                        T::Expression::from(r.as_ref().unwrap().clone()),
                        builder,
                    );
                }
            }
            (Array::Dyn(_, lhs_len), Array::Dyn(_, rhs_len)) => {
                builder.assert_eq::<Usize<_>>(lhs_len.clone(), rhs_len);

                builder.range(0, lhs_len).for_each(|i, builder| {
                    let a = builder.get(&lhs, i);
                    let b = builder.get(&rhs, i);
                    builder.assert_eq::<T>(a, b);
                });
            }
            _ => panic!("cannot compare arrays of different types"),
        }
    }

    fn assert_ne(
        lhs: impl Into<Self::Expression>,
        rhs: impl Into<Self::Expression>,
        builder: &mut Builder<C>,
    ) {
        let lhs = lhs.into();
        let rhs = rhs.into();

        match (lhs.clone(), rhs.clone()) {
            (Array::Fixed(lhs), Array::Fixed(rhs)) => {
                // No need to compare if they are the same reference. The same reference will
                // also cause borrow errors.
                if Rc::ptr_eq(&lhs, &rhs) {
                    panic!("assert not equal on the same array");
                }
                for (l, r) in lhs.borrow().iter().zip_eq(rhs.borrow().iter()) {
                    assert!(l.is_some(), "lhs array is not fully initialized");
                    assert!(r.is_some(), "rhs array is not fully initialized");
                    T::assert_ne(
                        T::Expression::from(l.as_ref().unwrap().clone()),
                        T::Expression::from(r.as_ref().unwrap().clone()),
                        builder,
                    );
                }
            }
            (Array::Dyn(_, lhs_len), Array::Dyn(_, rhs_len)) => {
                builder.assert_eq::<Usize<_>>(lhs_len.clone(), rhs_len);

                builder.range(0, lhs_len).for_each(|i, builder| {
                    let a = builder.get(&lhs, i);
                    let b = builder.get(&rhs, i);
                    builder.assert_ne::<T>(a, b);
                });
            }
            _ => panic!("cannot compare arrays of different types"),
        }
    }

    // The default version calls `uninit`. If `expr` is `Fixed`, it will be converted into `Dyn`.
    fn eval(_builder: &mut Builder<C>, expr: impl Into<Self::Expression>) -> Self {
        expr.into()
    }
}

impl<C: Config, T: MemVariable<C>> MemVariable<C> for Array<C, T> {
    fn size_of() -> usize {
        2
    }

    fn load(&self, src: Ptr<C::N>, index: MemIndex<C::N>, builder: &mut Builder<C>) {
        match self {
            Array::Dyn(dst, Usize::Var(len)) => {
                let mut index = index;
                dst.load(src, index, builder);
                index.offset += <Ptr<C::N> as MemVariable<C>>::size_of();
                len.load(src, index, builder);
            }
            _ => unreachable!(),
        }
    }

    fn store(&self, dst: Ptr<<C as Config>::N>, index: MemIndex<C::N>, builder: &mut Builder<C>) {
        match self {
            Array::Dyn(src, Usize::Var(len)) => {
                let mut index = index;
                src.store(dst, index, builder);
                index.offset += <Ptr<C::N> as MemVariable<C>>::size_of();
                len.store(dst, index, builder);
            }
            _ => unreachable!(),
        }
    }
}

impl<C: Config, V: FromConstant<C> + MemVariable<C>> FromConstant<C> for Array<C, V> {
    type Constant = Vec<V::Constant>;

    fn constant(value: Self::Constant, builder: &mut Builder<C>) -> Self {
        let array = builder.dyn_array(value.len());
        for (i, val) in value.into_iter().enumerate() {
            let val = V::constant(val, builder);
            builder.set(&array, i, val);
        }
        array
    }
}

/// Unsafe transmute from array of one type to another.
///
/// SAFETY: only use this if the memory layout of types `S` and `T` align.
/// Only usable for `Array::Dyn`, will panic otherwise.
pub fn unsafe_array_transmute<C: Config, S, T>(arr: Array<C, S>) -> Array<C, T> {
    if let Array::Dyn(ptr, len) = arr {
        Array::Dyn(ptr, len)
    } else {
        unreachable!()
    }
}