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
use crate::{
errors::{pest_error_to_miette_error, InterpreterError, PolentaError},
utils::PolentaUtilExt,
};
use lambdaworks_math::{
field::{element::FieldElement, traits::IsPrimeField},
polynomial::Polynomial,
};
use std::collections::HashMap;
use crate::parser::{BinaryOp, Expr, PolentaParser, Stmt, UnaryOp};
/// Polenta interpreter.
pub struct Polenta<F: IsPrimeField> {
/// Symbol table as a map from identifiers to polynomials.
/// Constant values are stored as constant polynomials.
pub symbols: HashMap<String, Polynomial<FieldElement<F>>>,
}
impl<F: IsPrimeField> Default for Polenta<F> {
fn default() -> Self {
Self::new()
}
}
impl<F: IsPrimeField> Polenta<F> {
pub fn new() -> Self {
Self {
symbols: HashMap::new(),
}
}
/// Interprets the given input string and returns the resulting polynomials.
///
/// The input is expected to be composed of several statements, each interpreted in the given
/// order and resulting in a polynomial.
///
/// May throw out a `PolentaError` if an error occurs during interpretation, either within the
/// parsing step or the interpretation step.
///
/// ## Example
///
/// ```rs
/// let input = r#"
/// let P(x) = 3 * x + 1;
/// let Q(x) = x / 2;
/// let z = Q@P@(5);
/// assert z == 8;
/// "#;
///
/// Polenta::<F>::new().interpret(input)?;
/// ```
pub fn interpret(
&mut self,
input: &str,
) -> Result<Vec<Polynomial<FieldElement<F>>>, PolentaError> {
let stmts = PolentaParser::parse_input(input.trim()).map_err(pest_error_to_miette_error)?;
stmts
.into_iter()
.map(|stmt| self.process_statement(stmt).map_err(|e| e.into()))
.collect()
}
fn process_expr(
&mut self,
expr: Expr,
term: Option<&String>,
) -> Result<Polynomial<FieldElement<F>>, InterpreterError> {
match expr {
Expr::Identifier(identifier) => {
// if this identifier is a term, treat it as P(x) = x
if term
.and_then(|t| if t == &identifier { Some(t) } else { None })
.is_some()
{
Ok(Polynomial::new_monomial(FieldElement::one(), 1))
} else {
// otherwise, look up the identifier in the symbol table
let value = self.symbols.get(&identifier).cloned();
match value {
Some(value) => Ok(value),
None => Err(InterpreterError::UnknownIdentifier(identifier).into()),
}
}
}
Expr::Integer(value) => Ok(Polynomial::new_monomial(FieldElement::from(value), 0)),
Expr::UnaryOp { op, rhs } => match op {
UnaryOp::Minus => Ok(-self.process_expr(*rhs, term)?),
},
Expr::BinaryOp { lhs, op, rhs } => {
let lhs = self.process_expr(*lhs, term)?;
let rhs = self.process_expr(*rhs, term)?;
match op {
// arithmetic operations
BinaryOp::Add => Ok(lhs + rhs),
BinaryOp::Sub => Ok(lhs - rhs),
BinaryOp::Mul => Ok(lhs * rhs),
BinaryOp::Div => {
if rhs.coeff_len() == 0 {
Err(InterpreterError::DivisionByZero)
} else {
Ok(lhs / rhs)
}
}
BinaryOp::Mod => Ok(lhs.long_division_with_remainder(&rhs).1),
BinaryOp::Pow => Ok(Self::poly_pow(&lhs, Self::poly_as_felt(&rhs)?)),
// comparison operations
BinaryOp::Eq => Ok(Self::poly_from_bool(lhs == rhs)),
BinaryOp::Ne => Ok(Self::poly_from_bool(lhs != rhs)),
// evaluation
BinaryOp::Evl => {
Ok(Self::felt_as_poly(lhs.evaluate(&Self::poly_as_felt(&rhs)?)))
}
}
}
}
}
/// The value of last evaluated "expression statement" is stored at `!!` symbol for internal testing.
fn process_statement(
&mut self,
stmt: Stmt,
) -> Result<Polynomial<FieldElement<F>>, InterpreterError> {
match stmt {
Stmt::Let(identifier, expr) => {
let poly = self.process_expr(expr, None)?;
self.symbols.insert(identifier, poly.clone());
Ok(poly)
}
Stmt::LetPoly(identifier, term, expr) => {
let poly = self.process_expr(expr, Some(&term))?;
self.symbols.insert(identifier, poly.clone());
Ok(poly)
}
Stmt::Expr(expr) => {
let poly = self.process_expr(expr, None)?;
self.symbols.insert("!!".to_string(), poly.clone());
Ok(poly)
}
Stmt::Assert(expr) => {
let result = self.process_expr(expr, None)?;
// fail if the result is zero, which means the assertion is false
// otherwise, return the result as is
if Self::poly_is_zero(&result) {
Err(InterpreterError::AssertionFailed)
} else {
Ok(result)
}
}
}
}
}