pub struct Expression<F> {
pub mul_terms: Vec<(F, Witness, Witness)>,
pub linear_combinations: Vec<(F, Witness)>,
pub q_c: F,
}
Expand description
An expression representing a quadratic polynomial.
This struct is primarily used to express arithmetic relations between variables. It includes multiplication terms, linear combinations, and a constant term.
§Addition polynomial
- Unlike standard plonk constraints with fixed wire assignments (wL, wR, wO), we allow arbitrary fan-in and fan-out. This means we need a more flexible representation and we need more than wL, wR, and wO.
- When looking at the quotient polynomial for the assert-zero opcode in standard plonk,
you can interpret the structure in two ways:
- Fan-in 2 and fan-out 1
- Fan-in 1 and fan-out 2
§Multiplication polynomial
- If we were allow the degree of the quotient polynomial to be arbitrary, then we will need a vector of wire values.
Fields§
§mul_terms: Vec<(F, Witness, Witness)>
Collection of multiplication terms.
To avoid having to create intermediate variables pre-optimization We collect all of the multiplication terms in the assert-zero opcode A multiplication term is of the form q_M * wL * wR Hence this vector represents the following sum: q_M1 * wL1 * wR1 + q_M2 * wL2 * wR2 + .. +
linear_combinations: Vec<(F, Witness)>
Collection of linear terms in the expression.
Each term follows the form: q_L * w
, where q_L
is a coefficient
and w
is a witness.
q_c: F
A constant term in the expression
Implementations§
Source§impl<F: Ord> Expression<F>
impl<F: Ord> Expression<F>
fn get_max_idx(&self) -> WitnessIdx
Sourcefn get_max_term(&self, idx: &mut WitnessIdx) -> Option<Witness>
fn get_max_term(&self, idx: &mut WitnessIdx) -> Option<Witness>
Returns the maximum witness at the provided position, and decrement the position.
This function assumes the gate is sorted
fn cmp_max(m1: Option<Witness>, m2: Option<Witness>) -> Ordering
Source§impl<F> Expression<F>
impl<F> Expression<F>
Sourcepub fn num_mul_terms(&self) -> usize
pub fn num_mul_terms(&self) -> usize
Returns the number of multiplication terms
Sourcepub fn push_addition_term(&mut self, coefficient: F, variable: Witness)
pub fn push_addition_term(&mut self, coefficient: F, variable: Witness)
Adds a new linear term to the Expression
.
Sourcepub fn push_multiplication_term(
&mut self,
coefficient: F,
lhs: Witness,
rhs: Witness,
)
pub fn push_multiplication_term( &mut self, coefficient: F, lhs: Witness, rhs: Witness, )
Adds a new quadratic term to the Expression
.
Sourcepub fn is_const(&self) -> bool
pub fn is_const(&self) -> bool
Returns true
if the expression represents a constant polynomial.
Examples:
- f(x,y) = x + y would return false
- f(x,y) = xy would return false, the degree here is 2
- f(x,y) = 5 would return true, the degree is 0
Sourcepub fn to_const(&self) -> Option<&F>
pub fn to_const(&self) -> Option<&F>
Returns a FieldElement
if the expression represents a constant polynomial.
Otherwise returns None
.
Examples:
- f(x,y) = x would return
None
- f(x,y) = x + 6 would return
None
- f(x,y) = 2*y + 6 would return
None
- f(x,y) = x + y would return
None
- f(x,y) = 5 would return
FieldElement(5)
Sourcepub fn is_linear(&self) -> bool
pub fn is_linear(&self) -> bool
Returns true
if highest degree term in the expression is one or less.
mul_term
in an expression contains degree-2 termslinear_combinations
contains degree-1 terms
Hence, it is sufficient to check that there are no mul_terms
Examples:
- f(x,y) = x + y would return true
- f(x,y) = xy would return false, the degree here is 2
- f(x,y) = 0 would return true, the degree is 0
Sourcepub fn is_degree_one_univariate(&self) -> bool
pub fn is_degree_one_univariate(&self) -> bool
Returns true
if the expression can be seen as a degree-1 univariate polynomial
mul_terms
in an expression can be univariate, however unless the coefficient is zero, it is always degree-2.linear_combinations
contains the sum of degree-1 terms, these terms do not need to contain the same variable and so it can be multivariate. However, we have thus far only checked iflinear_combinations
contains one term, so this method will return false, if theExpression
has not been simplified.
Hence, we check in the simplest case if an expression is a degree-1 univariate,
by checking if it contains no mul_terms
and it contains one linear_combination
term.
Examples:
- f(x,y) = x would return true
- f(x,y) = x + 6 would return true
- f(x,y) = 2*y + 6 would return true
- f(x,y) = x + y would return false
- f(x,y) = x + x should return true, but we return false *** (we do not simplify)
- f(x,y) = 5 would return false
Source§impl<F: AcirField> Expression<F>
impl<F: AcirField> Expression<F>
pub fn from_field(q_c: F) -> Self
pub fn zero() -> Self
pub fn is_zero(&self) -> bool
pub fn one() -> Self
Sourcepub fn to_witness(&self) -> Option<Witness>
pub fn to_witness(&self) -> Option<Witness>
Returns a Witness
if the Expression
can be represented as a degree-1
univariate polynomial. Otherwise returns None
.
Note that Witness
is only capable of expressing polynomials of the form
f(x) = x and not polynomials of the form f(x) = mx+c , so this method has
extra checks to ensure that m=1 and c=0
Trait Implementations§
Source§impl<F: AcirField> Add<&Expression<F>> for &Expression<F>
impl<F: AcirField> Add<&Expression<F>> for &Expression<F>
Source§type Output = Expression<F>
type Output = Expression<F>
+
operator.Source§impl<F: AcirField> Add<&Expression<F>> for Witness
impl<F: AcirField> Add<&Expression<F>> for Witness
Source§type Output = Expression<F>
type Output = Expression<F>
+
operator.Source§impl<F: AcirField> Add<F> for Expression<F>
impl<F: AcirField> Add<F> for Expression<F>
Source§impl<F: Clone> Clone for Expression<F>
impl<F: Clone> Clone for Expression<F>
Source§fn clone(&self) -> Expression<F>
fn clone(&self) -> Expression<F>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<F: Debug> Debug for Expression<F>
impl<F: Debug> Debug for Expression<F>
Source§impl<F: AcirField> Default for Expression<F>
impl<F: AcirField> Default for Expression<F>
Source§impl<'de, F> Deserialize<'de> for Expression<F>where
F: Deserialize<'de>,
impl<'de, F> Deserialize<'de> for Expression<F>where
F: Deserialize<'de>,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<F: Display> Display for Expression<F>
impl<F: Display> Display for Expression<F>
Source§impl<F: AcirField> From<F> for Expression<F>
impl<F: AcirField> From<F> for Expression<F>
Source§impl<F: Hash> Hash for Expression<F>
impl<F: Hash> Hash for Expression<F>
Source§impl<F: AcirField> Mul<&Expression<F>> for &Expression<F>
impl<F: AcirField> Mul<&Expression<F>> for &Expression<F>
Source§impl<F: AcirField> Mul<F> for &Expression<F>
impl<F: AcirField> Mul<F> for &Expression<F>
Source§impl<F: AcirField> Neg for &Expression<F>
impl<F: AcirField> Neg for &Expression<F>
Source§impl<F: Ord> Ord for Expression<F>
impl<F: Ord> Ord for Expression<F>
Source§impl<F: PartialEq> PartialEq for Expression<F>
impl<F: PartialEq> PartialEq for Expression<F>
Source§impl<F: Ord> PartialOrd for Expression<F>
impl<F: Ord> PartialOrd for Expression<F>
Source§impl<F> ProtoCodec<Expression<F>, Expression> for ProtoSchema<F>where
F: AcirField,
impl<F> ProtoCodec<Expression<F>, Expression> for ProtoSchema<F>where
F: AcirField,
Source§fn encode(value: &Expression<F>) -> Expression
fn encode(value: &Expression<F>) -> Expression
T
to protobuf representation R
.Source§fn decode(value: &Expression) -> Result<Expression<F>>
fn decode(value: &Expression) -> Result<Expression<F>>
R
to domain type T
.Source§fn encode_some(value: &T) -> Option<R>
fn encode_some(value: &T) -> Option<R>
Some
.Source§fn encode_vec<'a, I>(values: I) -> Vec<R>where
I: IntoIterator<Item = &'a T>,
T: 'a,
fn encode_vec<'a, I>(values: I) -> Vec<R>where
I: IntoIterator<Item = &'a T>,
T: 'a,
Source§fn decode_wrap(value: &R, msg: &'static str) -> Result<T, Report>
fn decode_wrap(value: &R, msg: &'static str) -> Result<T, Report>
Source§fn decode_vec_wrap(values: &[R], msg: &'static str) -> Result<Vec<T>, Report>
fn decode_vec_wrap(values: &[R], msg: &'static str) -> Result<Vec<T>, Report>
Source§fn decode_arr<const N: usize>(values: &[R]) -> Result<[T; N], Report>
fn decode_arr<const N: usize>(values: &[R]) -> Result<[T; N], Report>
Source§fn decode_arr_wrap<const N: usize>(
values: &[R],
msg: &'static str,
) -> Result<[T; N], Report>
fn decode_arr_wrap<const N: usize>( values: &[R], msg: &'static str, ) -> Result<[T; N], Report>
Source§fn decode_box_arr<const N: usize>(values: &[R]) -> Result<Box<[T; N]>, Report>
fn decode_box_arr<const N: usize>(values: &[R]) -> Result<Box<[T; N]>, Report>
Source§fn decode_box_arr_wrap<const N: usize>(
values: &[R],
msg: &'static str,
) -> Result<Box<[T; N]>, Report>
fn decode_box_arr_wrap<const N: usize>( values: &[R], msg: &'static str, ) -> Result<Box<[T; N]>, Report>
Source§fn decode_some(value: &Option<R>) -> Result<T, Report>
fn decode_some(value: &Option<R>) -> Result<T, Report>
None
.Source§fn decode_some_wrap(value: &Option<R>, msg: &'static str) -> Result<T, Report>
fn decode_some_wrap(value: &Option<R>, msg: &'static str) -> Result<T, Report>
Source§fn decode_opt_wrap(
value: &Option<R>,
msg: &'static str,
) -> Result<Option<T>, Report>
fn decode_opt_wrap( value: &Option<R>, msg: &'static str, ) -> Result<Option<T>, Report>
None
if the field is missing.Source§impl<F> Serialize for Expression<F>where
F: Serialize,
impl<F> Serialize for Expression<F>where
F: Serialize,
Source§impl<F: AcirField> Sub<&Expression<F>> for &Expression<F>
impl<F: AcirField> Sub<&Expression<F>> for &Expression<F>
Source§type Output = Expression<F>
type Output = Expression<F>
-
operator.Source§impl<F: AcirField> Sub<&Expression<F>> for Witness
impl<F: AcirField> Sub<&Expression<F>> for Witness
Source§type Output = Expression<F>
type Output = Expression<F>
-
operator.Source§impl<F: AcirField> Sub<F> for Expression<F>
impl<F: AcirField> Sub<F> for Expression<F>
impl<F: Eq> Eq for Expression<F>
impl<F> StructuralPartialEq for Expression<F>
Auto Trait Implementations§
impl<F> Freeze for Expression<F>where
F: Freeze,
impl<F> RefUnwindSafe for Expression<F>where
F: RefUnwindSafe,
impl<F> Send for Expression<F>where
F: Send,
impl<F> Sync for Expression<F>where
F: Sync,
impl<F> Unpin for Expression<F>where
F: Unpin,
impl<F> UnwindSafe for Expression<F>where
F: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<D> OwoColorize for D
impl<D> OwoColorize for D
§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg
] or
a color-specific method, such as [OwoColorize::green
], Read more§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg
] or
a color-specific method, such as [OwoColorize::on_yellow
], Read more