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
pub(crate) use air_script_core::{
    Constant, ConstantType, Expression, Identifier, IndexedTraceAccess, Iterable,
    ListComprehension, ListFoldingType, MatrixAccess, NamedTraceAccess, Range, Variable,
    VariableType, VectorAccess,
};

pub mod pub_inputs;
pub use pub_inputs::PublicInput;

pub mod periodic_columns;
pub use periodic_columns::PeriodicColumn;

pub mod boundary_constraints;
pub use boundary_constraints::*;

pub mod integrity_constraints;
pub use integrity_constraints::*;

pub mod random_values;
pub use random_values::*;

// AST
// ================================================================================================

/// [Source] is the root node of the AST representing the AIR constraints file.
#[derive(Debug, Eq, PartialEq)]
pub struct Source(pub Vec<SourceSection>);

/// Source is divided into SourceSections.
/// There are 6 types of Source Sections:
/// - AirDef: Name of the air constraints module.
/// - TraceCols: Trace Columns representing columns of the execution trace.
/// - PublicInputs: Public inputs are each represented by a fixed-size array. At least one public
///   input is required, but there is no limit to the number of public inputs that can be specified.
/// - PeriodicColumns: Periodic columns are each represented by a fixed-size array with all of its
///   elements specified. The array length is expected to be a power of 2, but this is not checked
///   during parsing.
/// - RandomValues: Random Values represent the randomness sent by the Verifier.
/// - BoundaryConstraints: Boundary Constraints to be enforced on the boundaries of columns defined
///   in the TraceCols section. Currently there are two types of boundaries, First and Last
///   representing the first and last rows of the column.
/// - IntegrityConstraints: Integrity Constraints to be enforced on the trace columns defined
///   in the TraceCols section.
#[derive(Debug, Eq, PartialEq)]
pub enum SourceSection {
    AirDef(Identifier),
    Constant(Constant),
    Trace(Trace),
    PublicInputs(Vec<PublicInput>),
    PeriodicColumns(Vec<PeriodicColumn>),
    RandomValues(RandomValues),
    BoundaryConstraints(Vec<BoundaryStmt>),
    IntegrityConstraints(Vec<IntegrityStmt>),
}

// TRACE
// ================================================================================================

/// [Trace] contains the main and auxiliary trace segments of the execution trace.
#[derive(Debug, Eq, PartialEq)]
pub struct Trace {
    pub main_cols: Vec<TraceCols>,
    pub aux_cols: Vec<TraceCols>,
}

/// [TraceCols] is used to represent a single or a group of columns in the execution trace. For
/// single columns, the size is 1. For groups, the size is the number of columns in the group.
#[derive(Debug, Eq, PartialEq)]
pub struct TraceCols {
    name: Identifier,
    size: u64,
}

impl TraceCols {
    pub fn new(name: Identifier, size: u64) -> Self {
        Self { name, size }
    }

    pub fn name(&self) -> &str {
        self.name.name()
    }

    pub fn size(&self) -> u64 {
        self.size
    }
}