Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Feature/optimise step selector builder #154

Merged
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b40dab4
implemented basic version of binary step builder with selector_expres…
rutefig Oct 13, 2023
21e758c
added assignment to selector_expr_not and selector_assignment
rutefig Oct 14, 2023
840dc53
Table formatting in the Fibonacci Tutorial (#151)
nullbitx8 Oct 14, 2023
f9b3f84
feat: add pretty debug information for common items (#111)
dyxushuai Oct 14, 2023
5e0c616
Use plaf automatic instance (#144)
jae-cuz Oct 14, 2023
b4d247e
Fixed LHS auto typing conversion for Expr arithmetic (#146)
qwang98 Oct 16, 2023
991deeb
LogN step builder now builds the selector with the same structure as …
rutefig Oct 17, 2023
9f0b33a
fixed polynomial expressions for the optimised step builder
rutefig Oct 18, 2023
5bfe8d2
fixed logic on build function and started to implement tests to check…
rutefig Oct 20, 2023
19c7c8f
implemented another test for 10 step types
rutefig Oct 20, 2023
1588a45
Merge branch 'main' into feature/optimise-step-selector-builder
rutefig Oct 20, 2023
60df017
removed print
rutefig Oct 20, 2023
85c8d56
fixed calculation for number of cols - we need to add 1 extra since w…
rutefig Oct 23, 2023
b518553
Merge branch 'main' into feature/optimise-step-selector-builder
rutefig Oct 23, 2023
2c82777
refactored tests
rutefig Oct 25, 2023
e40458b
bug fixes, code refactor and added more assertions to tests
rutefig Oct 26, 2023
c16b8a6
removed useless comments
rutefig Oct 26, 2023
894deb8
Merge branch 'main' into feature/optimise-step-selector-builder
leolara Oct 26, 2023
ae1a796
Fixed LHS auto typing conversion for Expr arithmetic (#146)
qwang98 Oct 16, 2023
2960668
fixed formatting
rutefig Oct 26, 2023
9b0746a
Merge branch 'main' into feature/optimise-step-selector-builder
rutefig Oct 26, 2023
af665b6
assert string representation for the selector expressions for 3 step …
rutefig Oct 28, 2023
5642dc6
Merge branch 'main' into feature/optimise-step-selector-builder
rutefig Oct 28, 2023
de44409
updated expression not to be calculated based on the expression
rutefig Oct 30, 2023
b0a42ea
fix clippy check
rutefig Oct 30, 2023
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
196 changes: 196 additions & 0 deletions src/plonkish/compiler/step_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,68 @@ impl StepSelectorBuilder for TwoStepsSelectorBuilder {
}
}

#[derive(Debug, Default, Clone)]
pub struct LogNSelectorBuilder {}

impl StepSelectorBuilder for LogNSelectorBuilder {
fn build<F: Field>(&self, unit: &mut CompilationUnit<F>) {
let mut selector: StepSelector<F> = StepSelector {
selector_expr: HashMap::new(),
selector_expr_not: HashMap::new(),
selector_assignment: HashMap::new(),
columns: Vec::new(),
};

let n_step_types = unit.step_types.len() as u64;
let n_cols = (n_step_types as f64 + 1.0).log2().ceil() as u64;

let mut annotation;
for index in 0..n_cols {
annotation = format!("'binary selector column {}'", index);

let column = Column::advice(annotation.clone(), 0);
selector.columns.push(column.clone());
}

let mut step_value = 1;
for step in unit.step_types.values() {
let mut combined_expr = PolyExpr::Const(F::ZERO);
let mut combined_expr_not = PolyExpr::Const(F::ZERO);
let mut assignments = Vec::new();

for i in 0..n_cols {
let bit = (step_value >> i) & 1; // Extract the i-th bit of step_value
let column = &selector.columns[i as usize];

if bit == 1 {
combined_expr = combined_expr * column.query(0, format!("Column {}", i));
combined_expr_not = combined_expr_not
* (PolyExpr::Const(F::ONE) - column.query(0, format!("Column {}", i)));
assignments.push((column.query(0, format!("Column {}", i)), F::ONE));
} else {
combined_expr = combined_expr
* (PolyExpr::Const(F::ONE) - column.query(0, format!("Column {}", i)));
combined_expr_not =
combined_expr_not * column.query(0, format!("Column {}", i));
}
}
rutefig marked this conversation as resolved.
Show resolved Hide resolved

selector
.selector_expr
.insert(step.uuid(), combined_expr.clone());
selector
.selector_expr_not
.insert(step.uuid(), combined_expr_not.clone());
selector
.selector_assignment
.insert(step.uuid(), assignments);
step_value += 1;
}
unit.columns.extend_from_slice(&selector.columns);
unit.selector = selector;
}
}

fn other_step_type<F>(unit: &CompilationUnit<F>, uuid: UUID) -> Option<Rc<StepType<F>>> {
for step_type in unit.step_types.values() {
if step_type.uuid() != uuid {
Expand All @@ -197,3 +259,137 @@ fn other_step_type<F>(unit: &CompilationUnit<F>, uuid: UUID) -> Option<Rc<StepTy

None
}

#[cfg(test)]
mod tests {
use halo2curves::bn256::Fr;
use uuid::Uuid;

use super::*;

fn mock_compilation_unit<F>() -> CompilationUnit<F> {
CompilationUnit::default()
}

#[test]
fn test_log_n_selector_builder_3_step_types() {
// Setup
let builder = LogNSelectorBuilder {};
let mut unit = mock_compilation_unit::<Fr>();

// Assuming you have 3 step types for this test
unit.step_types.insert(
Uuid::now_v1(&[1, 2, 3, 4, 5, 6]).as_u128(),
Rc::new(StepType::new(
Uuid::now_v1(&[1, 2, 3, 4, 5, 6]).as_u128(),
"StepType1".to_owned(),
)),
);
unit.step_types.insert(
Uuid::now_v1(&[1, 2, 3, 4, 5, 6]).as_u128(),
Rc::new(StepType::new(
Uuid::now_v1(&[1, 2, 3, 4, 5, 6]).as_u128(),
"StepType2".to_owned(),
)),
);
unit.step_types.insert(
Uuid::now_v1(&[1, 2, 3, 4, 5, 6]).as_u128(),
Rc::new(StepType::new(
Uuid::now_v1(&[1, 2, 3, 4, 5, 6]).as_u128(),
"StepType3".to_owned(),
)),
);

builder.build(&mut unit);

// Assert number of columns
assert_eq!(unit.columns.len(), 2);
assert_eq!(unit.selector.columns.len(), 2);

// TODO: Assert expressions

// TODO: Assert assignments

}

#[test]
fn test_log_n_selector_builder_4_step_types() {
// Setup
let builder = LogNSelectorBuilder {};
let mut unit = mock_compilation_unit::<Fr>();

// Assuming you have 4 step types for this test
// Assuming you have 3 step types for this test
unit.step_types.insert(
Uuid::now_v1(&[1, 2, 3, 4, 5, 6]).as_u128(),
Rc::new(StepType::new(
Uuid::now_v1(&[1, 2, 3, 4, 5, 6]).as_u128(),
"StepType1".to_owned(),
)),
);
unit.step_types.insert(
Uuid::now_v1(&[1, 2, 3, 4, 5, 6]).as_u128(),
Rc::new(StepType::new(
Uuid::now_v1(&[1, 2, 3, 4, 5, 6]).as_u128(),
"StepType2".to_owned(),
)),
);
unit.step_types.insert(
Uuid::now_v1(&[1, 2, 3, 4, 5, 6]).as_u128(),
Rc::new(StepType::new(
Uuid::now_v1(&[1, 2, 3, 4, 5, 6]).as_u128(),
"StepType3".to_owned(),
)),
);
unit.step_types.insert(
Uuid::now_v1(&[1, 2, 3, 4, 5, 6]).as_u128(),
Rc::new(StepType::new(
Uuid::now_v1(&[1, 2, 3, 4, 5, 6]).as_u128(),
"StepType43".to_owned(),
)),
);

builder.build(&mut unit);

// Assert number of columns
assert_eq!(unit.columns.len(), 3);
assert_eq!(unit.selector.columns.len(), 3);

// TODO: Assert expressions
rutefig marked this conversation as resolved.
Show resolved Hide resolved

// TODO: Assert assignments

}

#[test]
fn test_log_n_selector_builder_10_step_types() {
// Setup
let builder = LogNSelectorBuilder {};
let mut unit = mock_compilation_unit::<Fr>();

// Assuming you have 10 step types for this test
let n_step_types = 10;

for i in 0..n_step_types {
unit.step_types.insert(
Uuid::now_v1(&[1, 2, 3, 4, 5, 6]).as_u128(),
Rc::new(StepType::new(
Uuid::now_v1(&[1, 2, 3, 4, 5, 6]).as_u128(),
format!("StepType{}", i),
)),
);
}

builder.build(&mut unit);

// Assert number of columns
let n_cols = (n_step_types as f64).log2().ceil() as usize;
rutefig marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(unit.columns.len(), n_cols);
assert_eq!(unit.selector.columns.len(), n_cols);

// TODO: Assert expressions

// TODO: Assert assignments

}
}
Loading