winter_air/air/transition/
frame.rs

1// Copyright (c) Facebook, Inc. and its affiliates.
2//
3// This source code is licensed under the MIT license found in the
4// LICENSE file in the root directory of this source tree.
5
6use alloc::vec::Vec;
7
8use super::FieldElement;
9
10// EVALUATION FRAME
11// ================================================================================================
12
13/// A set of execution trace rows required for evaluation of transition constraints.
14///
15/// In the current implementation, an evaluation frame always contains two consecutive rows of the
16/// execution trace. It is passed in as one of the parameters into
17/// [Air::evaluate_transition()](crate::Air::evaluate_transition) function.
18#[derive(Debug, Clone)]
19pub struct EvaluationFrame<E: FieldElement> {
20    current: Vec<E>,
21    next: Vec<E>,
22}
23
24impl<E: FieldElement> EvaluationFrame<E> {
25    // CONSTRUCTORS
26    // --------------------------------------------------------------------------------------------
27
28    /// Returns a new evaluation frame instantiated with the specified number of columns.
29    ///
30    /// # Panics
31    /// Panics if `num_columns` is zero.
32    pub fn new(num_columns: usize) -> Self {
33        assert!(num_columns > 0, "number of columns must be greater than zero");
34        EvaluationFrame {
35            current: vec![E::ZERO; num_columns],
36            next: vec![E::ZERO; num_columns],
37        }
38    }
39
40    /// Returns a new evaluation frame instantiated from the provided rows.
41    ///
42    /// # Panics
43    /// Panics if:
44    /// * Lengths of the provided rows are zero.
45    /// * Lengths of the provided rows are not the same.
46    pub fn from_rows(current: Vec<E>, next: Vec<E>) -> Self {
47        assert!(!current.is_empty(), "a row must contain at least one value");
48        assert_eq!(current.len(), next.len(), "number of values in the rows must be the same");
49        Self { current, next }
50    }
51
52    // ROW ACCESSORS
53    // --------------------------------------------------------------------------------------------
54
55    /// Returns a reference to the current row.
56    #[inline(always)]
57    pub fn current(&self) -> &[E] {
58        &self.current
59    }
60
61    /// Returns a reference to the next row.
62    #[inline(always)]
63    pub fn next(&self) -> &[E] {
64        &self.next
65    }
66
67    // DATA MUTATORS
68    // --------------------------------------------------------------------------------------------
69
70    /// Returns a mutable reference to the current row.
71    #[inline(always)]
72    pub fn current_mut(&mut self) -> &mut [E] {
73        &mut self.current
74    }
75
76    /// Returns a mutable reference to the next row.
77    #[inline(always)]
78    pub fn next_mut(&mut self) -> &mut [E] {
79        &mut self.next
80    }
81}