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

feat: bb logup permutations #32

Merged
merged 10 commits into from
Dec 14, 2023
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
22 changes: 18 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions asm_to_pil/src/vm_to_constrained.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ impl<T: FieldElement> ASMPILConverter<T> {
),
PilStatement::PolynomialIdentity(
0,
None,
lhs - (Expression::from(T::one())
- next_reference("first_step"))
* direct_reference(pc_update_name),
Expand All @@ -154,10 +155,14 @@ impl<T: FieldElement> ASMPILConverter<T> {
ReadOnly => {
let not_reset: Expression<T> =
Expression::from(T::one()) - direct_reference("instr__reset");
vec![PilStatement::PolynomialIdentity(0, not_reset * (lhs - rhs))]
vec![PilStatement::PolynomialIdentity(
0,
None,
not_reset * (lhs - rhs),
)]
}
_ => {
vec![PilStatement::PolynomialIdentity(0, lhs - rhs)]
vec![PilStatement::PolynomialIdentity(0, None, lhs - rhs)]
}
}
})
Expand Down Expand Up @@ -389,7 +394,7 @@ impl<T: FieldElement> ASMPILConverter<T> {
});

for mut statement in body {
if let PilStatement::PolynomialIdentity(_start, expr) = statement {
if let PilStatement::PolynomialIdentity(_start, _attr, expr) = statement {
match extract_update(expr) {
(Some(var), expr) => {
let reference = direct_reference(&instruction_flag);
Expand All @@ -406,12 +411,13 @@ impl<T: FieldElement> ASMPILConverter<T> {
}
(None, expr) => self.pil.push(PilStatement::PolynomialIdentity(
0,
None,
direct_reference(&instruction_flag) * expr.clone(),
)),
}
} else {
match &mut statement {
PilStatement::PermutationIdentity(_, left, _)
PilStatement::PermutationIdentity(_, _, left, _)
| PilStatement::PlookupIdentity(_, left, _) => {
assert!(
left.selector.is_none(),
Expand Down Expand Up @@ -715,6 +721,7 @@ impl<T: FieldElement> ASMPILConverter<T> {
.sum();
self.pil.push(PilStatement::PolynomialIdentity(
0,
None,
direct_reference(register) - assign_constraint,
));
}
Expand Down
8 changes: 7 additions & 1 deletion ast/src/analyzed/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,13 @@ impl<T: Display> Display for Identity<Expression<T>> {
}
}
IdentityKind::Plookup => write!(f, "{} in {};", self.left, self.right),
IdentityKind::Permutation => write!(f, "{} is {};", self.left, self.right),
IdentityKind::Permutation => write!(
f,
"#[{}] {} is {};",
self.attribute.clone().unwrap_or_default(),
self.left,
self.right
),
IdentityKind::Connect => write!(f, "{} connect {};", self.left, self.right),
}
}
Expand Down
2 changes: 2 additions & 0 deletions ast/src/analyzed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ impl<T> Analyzed<T> {
self.identities.push(Identity {
id,
kind: IdentityKind::Polynomial,
attribute: None, // TODO(md): None for the meantime as we do not have tagged identities, will be updated in following pr
source,
left: SelectedExpressions {
selector: Some(identity),
Expand Down Expand Up @@ -504,6 +505,7 @@ pub struct Identity<Expr> {
/// The ID is specific to the identity kind.
pub id: u64,
pub kind: IdentityKind,
pub attribute: Option<String>,
pub source: SourceRef,
/// For a simple polynomial identity, the selector contains
/// the actual expression (see expression_for_poly_id).
Expand Down
9 changes: 7 additions & 2 deletions ast/src/parsed/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,15 +383,20 @@ impl<T: Display> Display for PilStatement<T> {
value.as_ref().map(|v| format!("{v}")).unwrap_or_default()
)
}
PilStatement::PolynomialIdentity(_, expression) => {
PilStatement::PolynomialIdentity(_, _attr, expression) => {
if let Expression::BinaryOperation(left, BinaryOperator::Sub, right) = expression {
write!(f, "{left} = {right};")
} else {
write!(f, "{expression} = 0;")
}
}
PilStatement::PlookupIdentity(_, left, right) => write!(f, "{left} in {right};"),
PilStatement::PermutationIdentity(_, left, right) => write!(f, "{left} is {right};"),
PilStatement::PermutationIdentity(
_, //
_, //
left,
right,
) => write!(f, "{left} is {right};"), //
PilStatement::ConnectIdentity(_, left, right) => write!(
f,
"{{ {} }} connect {{ {} }};",
Expand Down
8 changes: 7 additions & 1 deletion ast/src/parsed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ pub enum PilStatement<T> {
PolynomialConstantDeclaration(usize, Vec<PolynomialName<T>>),
PolynomialConstantDefinition(usize, String, FunctionDefinition<T>),
PolynomialCommitDeclaration(usize, Vec<PolynomialName<T>>, Option<FunctionDefinition<T>>),
PolynomialIdentity(usize, Expression<T>),
PolynomialIdentity(usize, Option<String>, Expression<T>),
PlookupIdentity(
usize,
SelectedExpressions<Expression<T>>,
SelectedExpressions<Expression<T>>,
),
PermutationIdentity(
usize,
Option<String>,
SelectedExpressions<Expression<T>>,
SelectedExpressions<Expression<T>>,
),
Expand All @@ -50,6 +51,11 @@ pub enum PilStatement<T> {
Expression(usize, Expression<T>),
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct Attribute {
pub name: Option<String>,
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct SelectedExpressions<Expr> {
pub selector: Option<Expr>,
Expand Down
18 changes: 14 additions & 4 deletions ast/src/parsed/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,12 @@ impl<T> ExpressionVisitable<Expression<T, NamespacedPolynomialReference>> for Pi
match self {
PilStatement::Expression(_, e) => e.visit_expressions_mut(f, o),
PilStatement::PlookupIdentity(_, left, right)
| PilStatement::PermutationIdentity(_, left, right) => [left, right]
| PilStatement::PermutationIdentity(
_, //
_, //
left,
right,
) => [left, right] //
.into_iter()
.try_for_each(|e| e.visit_expressions_mut(f, o)),
PilStatement::ConnectIdentity(_start, left, right) => left
Expand All @@ -203,7 +208,7 @@ impl<T> ExpressionVisitable<Expression<T, NamespacedPolynomialReference>> for Pi

PilStatement::Namespace(_, _, e)
| PilStatement::PolynomialDefinition(_, _, e)
| PilStatement::PolynomialIdentity(_, e)
| PilStatement::PolynomialIdentity(_, _, e)
| PilStatement::PublicDeclaration(_, _, _, None, e)
| PilStatement::ConstantDefinition(_, _, e)
| PilStatement::LetStatement(_, _, Some(e)) => e.visit_expressions_mut(f, o),
Expand All @@ -230,7 +235,12 @@ impl<T> ExpressionVisitable<Expression<T, NamespacedPolynomialReference>> for Pi
match self {
PilStatement::Expression(_, e) => e.visit_expressions(f, o),
PilStatement::PlookupIdentity(_, left, right)
| PilStatement::PermutationIdentity(_, left, right) => [left, right]
| PilStatement::PermutationIdentity(
_, //
_, //
left,
right,
) => [left, right] //
.into_iter()
.try_for_each(|e| e.visit_expressions(f, o)),
PilStatement::ConnectIdentity(_start, left, right) => left
Expand All @@ -240,7 +250,7 @@ impl<T> ExpressionVisitable<Expression<T, NamespacedPolynomialReference>> for Pi

PilStatement::Namespace(_, _, e)
| PilStatement::PolynomialDefinition(_, _, e)
| PilStatement::PolynomialIdentity(_, e)
| PilStatement::PolynomialIdentity(_, _, e)
| PilStatement::PublicDeclaration(_, _, _, None, e)
| PilStatement::ConstantDefinition(_, _, e)
| PilStatement::LetStatement(_, _, Some(e)) => e.visit_expressions(f, o),
Expand Down
Loading
Loading