Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for parsing of constants #63

Merged
merged 5 commits into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion codegen/winterfell/src/air/boundary_constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ impl Codegen for BoundaryExpr {
format!("Felt::new({})", value)
}
}
Self::PubInput(name, index) => format!("self.{}[{}]", name, index),
Self::VectorAccess(vector_access) => {
format!("self.{}[{}]", vector_access.name(), vector_access.idx())
}
Self::Rand(index) => {
format!("aux_rand_elements.get_segment_elements(0)[{}]", index)
}
Expand Down Expand Up @@ -131,6 +133,7 @@ impl Codegen for BoundaryExpr {
Self::Exp(lhs, rhs) => {
format!("({}).exp({})", lhs.to_string(is_aux_constraint), rhs)
}
BoundaryExpr::Elem(_) | BoundaryExpr::MatrixAccess(_) => todo!(),
}
}
}
6 changes: 3 additions & 3 deletions ir/src/boundary_constraints.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{BTreeMap, BoundaryExpr, Identifier, IdentifierType, SemanticError, SymbolTable};
use super::{BTreeMap, BoundaryExpr, IdentifierType, SemanticError, SymbolTable};
use parser::ast;

// BOUNDARY CONSTRAINTS
Expand Down Expand Up @@ -121,8 +121,8 @@ fn validate_expression(
expr: &ast::BoundaryExpr,
) -> Result<(), SemanticError> {
match expr {
BoundaryExpr::PubInput(Identifier(name), index) => {
symbol_table.validate_public_input(name, *index)
BoundaryExpr::VectorAccess(vector_access) => {
symbol_table.validate_public_input(vector_access.name(), vector_access.idx())
}
BoundaryExpr::Add(lhs, rhs) | BoundaryExpr::Sub(lhs, rhs) => {
validate_expression(symbol_table, lhs)?;
Expand Down
3 changes: 2 additions & 1 deletion ir/src/transition_constraints/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl AlgebraicGraph {
let node_index = self.insert_op(Operation::Const(value));
Ok((constraint_type, node_index))
}
TransitionExpr::Var(Identifier(ident)) => self.insert_variable(symbol_table, &ident),
TransitionExpr::Elem(Identifier(ident)) => self.insert_variable(symbol_table, &ident),
TransitionExpr::Next(Identifier(ident)) => self.insert_next(symbol_table, &ident),
TransitionExpr::Rand(index) => {
let constraint_type = ConstraintType::Auxiliary;
Expand Down Expand Up @@ -139,6 +139,7 @@ impl AlgebraicGraph {
let node_index = self.insert_op(Operation::Exp(lhs, rhs as usize));
Ok((constraint_type, node_index))
}
TransitionExpr::VectorAccess(_) | TransitionExpr::MatrixAccess(_) => todo!(),
}
}

Expand Down
13 changes: 9 additions & 4 deletions parser/src/ast/boundary_constraints.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::Identifier;
use super::{Identifier, MatrixAccess, VectorAccess};
use std::fmt::Display;

// BOUNDARY CONSTRAINTS
Expand Down Expand Up @@ -61,9 +61,14 @@ impl Display for Boundary {
#[derive(Debug, PartialEq, Clone)]
pub enum BoundaryExpr {
Const(u64),
/// Reference to a public input element, identified by the name of a public input array and the
/// index of the cell.
PubInput(Identifier, usize),
/// Represents any named constant or variable.
Elem(Identifier),
/// Represents an element inside a constant or variable vector. [VectorAccess] contains the
/// name of the vector and the index of the element to access.
VectorAccess(VectorAccess),
/// Represents an element inside a constant or variable matrix. [MatrixAccess] contains the
/// name of the matrix and indices of the element to access.
MatrixAccess(MatrixAccess),
/// Represents a random value provided by the verifier. The inner value is the index of this
/// random value in the array of all random values.
Rand(usize),
Expand Down
42 changes: 42 additions & 0 deletions parser/src/ast/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// CONSTANTS
// ================================================================================================

use super::Identifier;

/// Stores a constant's name and value. There are three types of constants:
/// - Scalar: 123
/// - Vector: \[1, 2, 3\]
/// - Matrix: \[\[1, 2, 3\], \[4, 5, 6\]\]
grjte marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug, PartialEq, Eq)]
pub struct Constant {
name: Identifier,
value: ConstantType,
}

impl Constant {
/// Returns a new instance of a [Constant]
pub fn new(name: Identifier, value: ConstantType) -> Self {
Self { name, value }
}

/// Returns the name of the [Constant]
pub fn name(&self) -> &Identifier {
&self.name
}

/// Returns the value of the [Constant]
pub fn value(&self) -> &ConstantType {
&self.value
}
}

/// Type of constant. Constants can be of 3 types:
/// - Scalar: 123
/// - Vector: \[1, 2, 3\]
/// - Matrix: \[\[1, 2, 3\], \[4, 5, 6\]\]
#[derive(Debug, PartialEq, Eq)]
pub enum ConstantType {
Scalar(u64),
Vector(Vec<u64>),
Matrix(Vec<Vec<u64>>),
}
70 changes: 70 additions & 0 deletions parser/src/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::fmt;

pub mod constants;
use constants::Constant;

pub mod pub_inputs;
pub use pub_inputs::PublicInput;

Expand Down Expand Up @@ -36,6 +39,7 @@ pub struct Source(pub Vec<SourceSection>);
#[derive(Debug, PartialEq)]
pub enum SourceSection {
AirDef(Identifier),
Constants(Vec<Constant>),
TraceCols(TraceCols),
PublicInputs(Vec<PublicInput>),
PeriodicColumns(Vec<PeriodicColumn>),
Expand All @@ -60,8 +64,74 @@ pub struct TraceCols {
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Clone)]
pub struct Identifier(pub String);

impl Identifier {
/// Returns the name of the identifier.
pub fn name(&self) -> &str {
&self.0
}
}

impl fmt::Display for Identifier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", &self.0)
}
}

/// [VectorAccess] is used to represent an element inside vector at the specified index.
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Clone)]
pub struct VectorAccess {
name: Identifier,
idx: usize,
}

impl VectorAccess {
/// Creates a new [VectorAccess] instance with the specified identifier name and index.
pub fn new(name: Identifier, idx: usize) -> Self {
Self { name, idx }
}

/// Returns the name of the vector.
pub fn name(&self) -> &str {
self.name.name()
}

/// Returns the index of the vector access.
pub fn idx(&self) -> usize {
self.idx
}
}

/// [MatrixAccess] is used to represent an element inside a matrix at the specified row and column
/// indices.
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Clone)]
pub struct MatrixAccess {
name: Identifier,
row_idx: usize,
col_idx: usize,
}

impl MatrixAccess {
/// Creates a new [MatrixAccess] instance with the specified identifier name and indices.
pub fn new(name: Identifier, col_idx: usize, row_idx: usize) -> Self {
Self {
name,
row_idx,
col_idx,
}
}

/// Returns the name of the matrix.
pub fn name(&self) -> &str {
self.name.name()
}

/// Returns the row index of the matrix access.
pub fn row_idx(&self) -> usize {
self.row_idx
}

/// Returns the column index of the matrix access.
pub fn col_idx(&self) -> usize {
self.col_idx
}
}
11 changes: 9 additions & 2 deletions parser/src/ast/transition_constraints.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::Identifier;
use super::{Identifier, MatrixAccess, VectorAccess};

// TRANSITION CONSTRAINTS
// ================================================================================================
Expand Down Expand Up @@ -32,7 +32,14 @@ impl TransitionConstraint {
#[derive(Debug, PartialEq, Clone)]
pub enum TransitionExpr {
Const(u64),
Var(Identifier),
/// Represents any named constant or variable.
Elem(Identifier),
/// Represents an element inside a constant or variable vector. [VectorAccess] contains the
/// name of the vector and the index of the element to access.
VectorAccess(VectorAccess),
/// Represents an element inside a constant or variable matrix. [MatrixAccess] contains the
/// name of the matrix and indices of the element to access.
MatrixAccess(MatrixAccess),
Next(Identifier),
/// Represents a random value provided by the verifier. The inner value is the index of this
/// random value in the array of all random values.
Expand Down
1 change: 1 addition & 0 deletions parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ pub enum ParseError {
InvalidInt(String),
InvalidTraceCols(String),
MissingMainTraceCols(String),
InvalidConst(String),
}
4 changes: 4 additions & 0 deletions parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ pub enum Token {
#[token("def")]
Def,

/// Used to declare constants in the AIR constraints module.
#[token("constants")]
Constants,

/// Used to declare trace columns section in the AIR constraints module.
#[token("trace_columns")]
TraceColumnns,
Expand Down
Loading