Skip to main content

haloumi_core/
expressions.rs

1//! Traits defining the behavior of expressions.
2
3use crate::{
4    info_traits::{ChallengeInfo, CreateQuery, QueryInfo, SelectorInfo},
5    query::{Advice, Fixed, Instance},
6};
7
8/// Trait with types associated to expressions.
9pub trait ExpressionTypes: Sized {
10    /// Type used for Expression::Selector.
11    type Selector: SelectorInfo + Copy + std::fmt::Debug;
12    /// Type used for Expression::Fixed.
13    type FixedQuery: QueryInfo<Kind = Fixed> + CreateQuery<Self> + Copy + std::fmt::Debug;
14    /// Type used for Expression::Advice.
15    type AdviceQuery: QueryInfo<Kind = Advice> + CreateQuery<Self> + Copy + std::fmt::Debug;
16    /// Type used for Expression::Instance.
17    type InstanceQuery: QueryInfo<Kind = Instance> + CreateQuery<Self> + Copy + std::fmt::Debug;
18    /// Type used for Expression::Challenge.
19    type Challenge: ChallengeInfo + Copy + std::fmt::Debug;
20}
21
22/// Trait for querying information about expressions.
23pub trait ExpressionInfo: ExpressionTypes {
24    /// If the expression is a negation returns a reference to the inner expression. Otherwise
25    /// should return `None`.
26    fn as_negation(&self) -> Option<&Self>;
27
28    /// If the expression is a query to a fixed cells returns a reference to the query. Otherwise
29    /// should return `None`.
30    fn as_fixed_query(&self) -> Option<&Self::FixedQuery>;
31}
32
33/// Factory trait for creating expressions.
34pub trait ExprBuilder<F>: ExpressionTypes {
35    /// Create the Expression::Constant case.
36    fn constant(f: F) -> Self;
37
38    /// Create the Expression::Selector case.
39    fn selector(selector: Self::Selector) -> Self;
40
41    /// Create the Expression::Fixed case.
42    fn fixed(fixed_query: Self::FixedQuery) -> Self;
43
44    /// Create the Expression::Advice case.
45    fn advice(advice_query: Self::AdviceQuery) -> Self;
46
47    /// Create the Expression::Instance case.
48    fn instance(instance_query: Self::InstanceQuery) -> Self;
49
50    /// Create the Expression::Challenge case.
51    fn challenge(challenge: Self::Challenge) -> Self;
52
53    /// Create the Expression::Negated case.
54    fn negated(expr: Self) -> Self;
55
56    /// Create the Expression::Sum case.
57    fn sum(lhs: Self, rhs: Self) -> Self;
58
59    /// Create the Expression::Product case.
60    fn product(lhs: Self, rhs: Self) -> Self;
61
62    /// Create the Expression::Scaled case.
63    fn scaled(lhs: Self, rhs: F) -> Self;
64}
65
66/// Allows evaluating the type with an [`EvalExpression`] evaluator.
67pub trait EvaluableExpr<F>: ExpressionTypes {
68    /// Evaluates the expression.
69    fn evaluate<E: EvalExpression<F, Self>>(&self, evaluator: &E) -> E::Output;
70}
71
72/// Evaluates an [`EvaluableExpr`].
73pub trait EvalExpression<F, E>
74where
75    E: ExpressionTypes,
76{
77    /// Output of the evaluation.
78    type Output;
79
80    /// Evaluate the Expression::Constant case.
81    fn constant(&self, f: &F) -> Self::Output;
82
83    /// Evaluate the Expression::Selector case.
84    fn selector(&self, selector: &E::Selector) -> Self::Output;
85
86    /// Evaluate the Expression::Fixed case.
87    fn fixed(&self, fixed_query: &E::FixedQuery) -> Self::Output;
88
89    /// Evaluate the Expression::Advice case.
90    fn advice(&self, advice_query: &E::AdviceQuery) -> Self::Output;
91
92    /// Evaluate the Expression::Instance case.
93    fn instance(&self, instance_query: &E::InstanceQuery) -> Self::Output;
94
95    /// Evaluate the Expression::Challenge case.
96    fn challenge(&self, challenge: &E::Challenge) -> Self::Output;
97
98    /// Evaluate the Expression::Negated case.
99    fn negated(&self, expr: Self::Output) -> Self::Output;
100
101    /// Evaluate the Expression::Sum case.
102    fn sum(&self, lhs: Self::Output, rhs: Self::Output) -> Self::Output;
103
104    /// Evaluate the Expression::Product case.
105    fn product(&self, lhs: Self::Output, rhs: Self::Output) -> Self::Output;
106
107    /// Evaluate the Expression::Scaled case.
108    fn scaled(&self, lhs: Self::Output, rhs: &F) -> Self::Output;
109}