Skip to content

Commit

Permalink
chore: update pilcom from powdr
Browse files Browse the repository at this point in the history
  • Loading branch information
eigmax committed Dec 1, 2023
1 parent 701032d commit c0d518c
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 108 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = [
"zkit",
"zkvm",
"plonky",
"starky",
"algebraic",
Expand Down
8 changes: 4 additions & 4 deletions starky/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ plonky = { package="plonky", path = "../plonky", version = "0.0.2" }
algebraic = { path = "../algebraic" }

#powdr pil compile tool. branch = "main"
pil_analyzer = {git = "https://github.com/powdr-labs/powdr.git", rev = "eb593dee15e194bd066085022f8a6ea52e2dab9b", package = "pil_analyzer"}
number = {git = "https://github.com/powdr-labs/powdr.git", rev = "eb593dee15e194bd066085022f8a6ea52e2dab9b", package = "number"}
ast = {git = "https://github.com/powdr-labs/powdr.git", rev = "eb593dee15e194bd066085022f8a6ea52e2dab9b",package = "ast"}
pil_analyzer = {git = "https://github.com/powdr-labs/powdr.git", branch = "main", package = "pil_analyzer"}
number = {git = "https://github.com/powdr-labs/powdr.git", branch = "main", package = "number"}
ast = {git = "https://github.com/powdr-labs/powdr.git", branch = "main",package = "ast"}


[dev-dependencies]
Expand All @@ -59,4 +59,4 @@ harness = false

[[bench]]
name = "poseidon"
harness = false
harness = false
145 changes: 62 additions & 83 deletions starky/src/pilcom/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ use crate::types::{
PolIdentity, Public, Reference, PIL,
};
use ast::analyzed::{
Analyzed, BinaryOperator, Expression, FunctionValueDefinition, IdentityKind, PolyID,
PolynomialReference, PolynomialType, Reference::*, StatementIdentifier, SymbolKind,
UnaryOperator,
AlgebraicBinaryOperator, AlgebraicExpression as Expression, AlgebraicUnaryOperator, Analyzed,
IdentityKind, PolyID, PolynomialType, StatementIdentifier, SymbolKind,
};

use super::expression_counter::compute_intermediate_expression_ids;
Expand Down Expand Up @@ -46,23 +45,26 @@ pub fn export<T: FieldElement>(analyzed: &Analyzed<T>) -> PIL {
for item in &analyzed.source_order {
match item {
StatementIdentifier::Definition(name) => {
if let (poly, Some(value)) = &analyzed.definitions[name] {
if poly.kind == SymbolKind::Poly(PolynomialType::Intermediate) {
if let FunctionValueDefinition::Expression(value) = value {
let expression_id = exporter.extract_expression(value, 1);
assert_eq!(
expression_id,
exporter.intermediate_poly_expression_ids[&poly.id] as usize
);
} else {
panic!("Expected single value");
}
}
if let Some((poly, value)) = analyzed.intermediate_columns.get(name) {
assert_eq!(poly.kind, SymbolKind::Poly(PolynomialType::Intermediate));
let expression_id = exporter.extract_expression(value, 1);
assert_eq!(
expression_id,
exporter.intermediate_poly_expression_ids[&poly.id] as usize
);
}
}
StatementIdentifier::PublicDeclaration(name) => {
let pub_def = &analyzed.public_declarations[name];
let (_, expr) = exporter.polynomial_reference_to_json(&pub_def.polynomial);
let pub_ref = &pub_def.polynomial;
let poly_id = pub_ref.poly_id.unwrap();
let (_, expr) = exporter.polynomial_reference_to_json(
PolyID {
id: poly_id.id + pub_def.array_index.unwrap_or_default() as u64,
..poly_id
},
false,
);
let id = publics.len();
publics.push(Public {
polType: polynomial_reference_type_to_type(&expr.op).to_string(),
Expand Down Expand Up @@ -145,6 +147,7 @@ fn symbol_kind_to_json_string(k: SymbolKind) -> &'static str {
match k {
SymbolKind::Poly(poly_type) => polynomial_type_to_json_string(poly_type),
SymbolKind::Other() => panic!("Cannot translate \"other\" symbol to json."),
SymbolKind::Constant() => unreachable!(),
}
}

Expand Down Expand Up @@ -183,23 +186,46 @@ impl<'a, T: FieldElement> Exporter<'a, T> {
self.analyzed
.definitions
.iter()
.map(|(name, (symbol, _value))| {
let id = if symbol.kind == SymbolKind::Poly(PolynomialType::Intermediate) {
self.intermediate_poly_expression_ids[&symbol.id]
} else {
symbol.id
};
.filter_map(|(name, (symbol, _value))| {
let id = match symbol.kind {
SymbolKind::Poly(PolynomialType::Intermediate) => {
panic!("Should be in intermediates")
}
SymbolKind::Poly(_) => Some(symbol.id),
SymbolKind::Other() | SymbolKind::Constant() => None,
}?;

let out = Reference {
polType: None,
type_: symbol_kind_to_json_string(symbol.kind).to_string(),
id: id as usize,
polDeg: symbol.degree as usize,
polDeg: self.analyzed.degree() as usize,
isArray: symbol.is_array(),
elementType: None,
len: symbol.length.map(|l| l as usize),
};
(name.clone(), out)
Some((name.clone(), out))
})
.chain(
self.analyzed
.intermediate_columns
.iter()
.map(|(name, (symbol, _))| {
assert_eq!(symbol.kind, SymbolKind::Poly(PolynomialType::Intermediate));
let id = self.intermediate_poly_expression_ids[&symbol.id];

let out = Reference {
polType: None,
type_: symbol_kind_to_json_string(symbol.kind).to_string(),
id: id as usize,
polDeg: self.analyzed.degree() as usize,
isArray: symbol.is_array(),
elementType: None,
len: symbol.length.map(|l| l as usize),
};
(name.clone(), out)
}),
)
.collect::<HashMap<String, Reference>>()
}

Expand Down Expand Up @@ -235,18 +261,8 @@ impl<'a, T: FieldElement> Exporter<'a, T> {
/// returns the degree and the JSON value (intermediate polynomial IDs)
fn expression_to_json(&self, expr: &Expression<T>) -> (u32, StarkyExpr) {
match expr {
Expression::Constant(name) => (
0,
StarkyExpr {
op: "number".to_string(),
deg: 0,
value: Some(format!("{}", self.analyzed.constants[name])),
..DEFAULT_EXPR
},
),
Expression::Reference(Poly(reference)) => self.polynomial_reference_to_json(reference),
Expression::Reference(LocalVar(_, _)) => {
panic!("No local variable references allowed here.")
Expression::Reference(reference) => {
self.polynomial_reference_to_json(reference.poly_id, reference.next)
}
Expression::PublicReference(name) => (
0,
Expand All @@ -270,34 +286,17 @@ impl<'a, T: FieldElement> Exporter<'a, T> {
let (deg_left, left) = self.expression_to_json(left);
let (deg_right, right) = self.expression_to_json(right);
let (op, degree) = match op {
BinaryOperator::Add => ("add", cmp::max(deg_left, deg_right)),
BinaryOperator::Sub => ("sub", cmp::max(deg_left, deg_right)),
BinaryOperator::Mul => ("mul", deg_left + deg_right),
BinaryOperator::Div => panic!("Div is not really allowed"),
BinaryOperator::Pow => {
AlgebraicBinaryOperator::Add => ("add", cmp::max(deg_left, deg_right)),
AlgebraicBinaryOperator::Sub => ("sub", cmp::max(deg_left, deg_right)),
AlgebraicBinaryOperator::Mul => ("mul", deg_left + deg_right),
AlgebraicBinaryOperator::Pow => {
assert_eq!(
deg_left + deg_right,
0,
"Exponentiation can only be used on constants."
);
("pow", deg_left + deg_right)
}
BinaryOperator::Mod
| BinaryOperator::BinaryAnd
| BinaryOperator::BinaryOr
| BinaryOperator::BinaryXor
| BinaryOperator::ShiftLeft
| BinaryOperator::ShiftRight
| BinaryOperator::LogicalOr
| BinaryOperator::LogicalAnd
| BinaryOperator::Less
| BinaryOperator::LessEqual
| BinaryOperator::Equal
| BinaryOperator::NotEqual
| BinaryOperator::GreaterEqual
| BinaryOperator::Greater => {
panic!("Operator {op:?} not supported on polynomials.")
}
};
(
degree,
Expand All @@ -312,8 +311,8 @@ impl<'a, T: FieldElement> Exporter<'a, T> {
Expression::UnaryOperation(op, value) => {
let (deg, value) = self.expression_to_json(value);
match op {
UnaryOperator::Plus => (deg, value),
UnaryOperator::Minus => (
AlgebraicUnaryOperator::Plus => (deg, value),
AlgebraicUnaryOperator::Minus => (
deg,
StarkyExpr {
op: "neg".to_string(),
Expand All @@ -322,46 +321,26 @@ impl<'a, T: FieldElement> Exporter<'a, T> {
..DEFAULT_EXPR
},
),
UnaryOperator::LogicalNot => panic!("Operator {op} not allowed here."),
}
}
Expression::FunctionCall(_) => panic!("No function calls allowed here."),
Expression::String(_) => panic!("Strings not allowed here."),
Expression::Tuple(_) => panic!("Tuples not allowed here"),
Expression::ArrayLiteral(_) => panic!("Array literals not allowed here"),
Expression::MatchExpression(_, _) => {
panic!("No match expressions allowed here.")
}
Expression::LambdaExpression(_) => {
panic!("No lambda expressions allowed here.")
}
Expression::FreeInput(_) => {
panic!("No free input expressions allowed here.")
}
}
}

fn polynomial_reference_to_json(
&self,
PolynomialReference {
name: _,
index,
poly_id,
next,
}: &PolynomialReference,
PolyID { id, ptype }: PolyID,
next: bool,
) -> (u32, StarkyExpr) {
let PolyID { id, ptype } = poly_id.unwrap();
let id = if ptype == PolynomialType::Intermediate {
assert!(index.is_none());
self.intermediate_poly_expression_ids[&id]
} else {
id + index.unwrap_or_default()
id
};
let poly = StarkyExpr {
id: Some(id as usize),
op: polynomial_reference_type_to_json_string(ptype).to_string(),
deg: 1,
next: Some(*next),
next: Some(next),
..DEFAULT_EXPR
};
(1, poly)
Expand Down
38 changes: 17 additions & 21 deletions starky/src/pilcom/expression_counter.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
//! porting it from powdr
use std::collections::HashMap;

use ast::analyzed::{
Analyzed, Expression, Identity, PolynomialType, PublicDeclaration, SelectedExpressions,
StatementIdentifier, Symbol, SymbolKind,
use ast::{
analyzed::{
Analyzed, Identity, PolynomialType, PublicDeclaration, StatementIdentifier, Symbol,
SymbolKind,
},
parsed::SelectedExpressions,
};

/// Computes expression IDs for each intermediate polynomial.
Expand All @@ -13,11 +16,16 @@ pub fn compute_intermediate_expression_ids<T>(analyzed: &Analyzed<T>) -> HashMap
for item in &analyzed.source_order {
expression_counter += match item {
StatementIdentifier::Definition(name) => {
let poly = &analyzed.definitions[name].0;
if poly.kind == SymbolKind::Poly(PolynomialType::Intermediate) {
if let Some((poly, _)) = analyzed.definitions.get(name) {
assert!(poly.kind != SymbolKind::Poly(PolynomialType::Intermediate));
poly.expression_count()
} else if let Some((poly, _)) = analyzed.intermediate_columns.get(name) {
assert!(poly.kind == SymbolKind::Poly(PolynomialType::Intermediate));
ids.insert(poly.id, expression_counter as u64);
poly.expression_count()
} else {
unreachable!()
}
poly.expression_count()
}
StatementIdentifier::PublicDeclaration(name) => {
analyzed.public_declarations[name].expression_count()
Expand All @@ -33,7 +41,7 @@ trait ExpressionCounter {
fn expression_count(&self) -> usize;
}

impl<T> ExpressionCounter for Identity<T> {
impl<Expr> ExpressionCounter for Identity<Expr> {
fn expression_count(&self) -> usize {
self.left.expression_count() + self.right.expression_count()
}
Expand All @@ -51,20 +59,8 @@ impl ExpressionCounter for PublicDeclaration {
}
}

impl<T> ExpressionCounter for SelectedExpressions<T> {
impl<Expr> ExpressionCounter for SelectedExpressions<Expr> {
fn expression_count(&self) -> usize {
self.selector.expression_count() + self.expressions.expression_count()
}
}

impl<T> ExpressionCounter for Vec<Expression<T>> {
fn expression_count(&self) -> usize {
self.len()
}
}

impl<T> ExpressionCounter for Option<Expression<T>> {
fn expression_count(&self) -> usize {
(self.is_some()).into()
self.selector.is_some() as usize + self.expressions.len()
}
}
8 changes: 8 additions & 0 deletions zkvm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "zkvm"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
1 change: 1 addition & 0 deletions zkvm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//! stay tuned

0 comments on commit c0d518c

Please sign in to comment.