This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Don't add column for SSB with only one step type #134
Closed
000wan
wants to merge
6
commits into
privacy-scaling-explorations:main
from
000wan:feature/single-step-type
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0c41eeb
Don't add column for single step type
000wan 2eb6925
Write a unit test for single step ssb
000wan c783e42
Use `matches!` macro
000wan 8c82e69
Apply early return
000wan 2e13e1b
Set value of `selector_assignment` to be an Option
000wan bc48063
Merge branch 'main' into feature/single-step-type
leolara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ pub type SelectorAssignment<F> = (PolyExpr<F>, F); | |
pub struct StepSelector<F> { | ||
pub selector_expr: HashMap<StepTypeUUID, PolyExpr<F>>, | ||
pub selector_expr_not: HashMap<StepTypeUUID, PolyExpr<F>>, | ||
pub selector_assignment: HashMap<StepTypeUUID, Vec<SelectorAssignment<F>>>, | ||
pub selector_assignment: HashMap<StepTypeUUID, Option<Vec<SelectorAssignment<F>>>>, | ||
pub columns: Vec<Column>, | ||
} | ||
|
||
|
@@ -50,7 +50,10 @@ impl<F: Clone> StepSelector<F> { | |
.clone() | ||
} | ||
|
||
pub fn get_selector_assignment(&self, step_uuid: StepTypeUUID) -> Vec<SelectorAssignment<F>> { | ||
pub fn get_selector_assignment( | ||
&self, | ||
step_uuid: StepTypeUUID, | ||
) -> Option<Vec<SelectorAssignment<F>>> { | ||
self.selector_assignment | ||
.get(&step_uuid) | ||
.expect("selector assignment for step not found") | ||
|
@@ -74,6 +77,25 @@ impl StepSelectorBuilder for SimpleStepSelectorBuilder { | |
columns: Vec::new(), | ||
}; | ||
|
||
// don't add a column for a single step type | ||
if unit.step_types.len() == 1 { | ||
let step = unit.step_types.values().next().expect("step not found"); | ||
|
||
// use F::ONE for selector constantly on, F::ZERO for off | ||
selector | ||
.selector_expr | ||
.insert(step.uuid(), PolyExpr::Const(F::ONE)); | ||
|
||
selector | ||
.selector_expr_not | ||
.insert(step.uuid(), PolyExpr::Const(F::ZERO)); | ||
|
||
selector.selector_assignment.insert(step.uuid(), None); | ||
|
||
unit.selector = selector; | ||
return; | ||
} | ||
|
||
Comment on lines
+80
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've made StepSelector to have a constant ONE for selector expression so that it selects always the same step. And it seems it's somehow working... Do you think this is an acceptable solution? @leolara |
||
for step in unit.step_types.values() { | ||
let annotation = if let Some(annotation) = unit.annotations.get(&step.uuid()) { | ||
format!("'step selector for {}'", annotation) | ||
|
@@ -96,7 +118,7 @@ impl StepSelectorBuilder for SimpleStepSelectorBuilder { | |
|
||
selector.selector_assignment.insert( | ||
step.uuid(), | ||
vec![(column.query(0, annotation.clone()), F::ONE)], | ||
Some(vec![(column.query(0, annotation.clone()), F::ONE)]), | ||
); | ||
} | ||
|
||
|
@@ -168,7 +190,7 @@ impl StepSelectorBuilder for TwoStepsSelectorBuilder { | |
|
||
unit.selector.selector_assignment.insert( | ||
step_zero.uuid(), | ||
vec![(column.query(0, "selector step zero"), F::ZERO)], | ||
Some(vec![(column.query(0, "selector step zero"), F::ZERO)]), | ||
); | ||
|
||
// One | ||
|
@@ -183,7 +205,7 @@ impl StepSelectorBuilder for TwoStepsSelectorBuilder { | |
|
||
unit.selector.selector_assignment.insert( | ||
step_one.uuid(), | ||
vec![(column.query(0, "selector step one"), F::ONE)], | ||
Some(vec![(column.query(0, "selector step one"), F::ONE)]), | ||
); | ||
} | ||
} | ||
|
@@ -197,3 +219,56 @@ fn other_step_type<F>(unit: &CompilationUnit<F>, uuid: UUID) -> Option<Rc<StepTy | |
|
||
None | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::{ | ||
ast::Constraint, | ||
frontend::dsl::cb::eq, | ||
plonkish::compiler::{transform_expr, CompilationUnit}, | ||
poly::ToExpr, | ||
}; | ||
use halo2_proofs::halo2curves::bn256::Fr; | ||
|
||
#[test] | ||
fn test_ssb_single_step() { | ||
// unit test for SSB with only one step type | ||
let mut unit = CompilationUnit::<Fr>::default(); | ||
let step = StepType::<Fr>::new(UUID::default(), "single step".to_string()); | ||
let uuid = step.uuid(); | ||
unit.annotations.insert(uuid, step.name.clone()); | ||
unit.step_types.insert(uuid, Rc::new(step)); | ||
|
||
let ssb = SimpleStepSelectorBuilder {}; | ||
ssb.build(&mut unit); | ||
|
||
let constraint = Constraint::<Fr> { | ||
annotation: "test constraint".to_string(), | ||
expr: eq(1.expr() - 1.expr(), 0.expr()).into(), | ||
}; | ||
let constraint = transform_expr( | ||
&unit, | ||
unit.step_types.get(&uuid).expect("step not found {}"), | ||
&constraint.expr, | ||
); | ||
|
||
// selector.select should return constraint switched on | ||
assert_eq!( | ||
format!("{:?}", unit.selector.select(uuid, &constraint)), | ||
format!( | ||
"{:?}", | ||
PolyExpr::Mul(vec![PolyExpr::Const(Fr::ONE), constraint]) | ||
) | ||
); | ||
assert_eq!( | ||
format!("{:?}", unit.selector.unselect(uuid)), | ||
format!("{:?}", PolyExpr::Const(Fr::ZERO)) | ||
); | ||
assert_eq!( | ||
format!("{:?}", unit.selector.next_expr(uuid, 1)), | ||
format!("{:?}", PolyExpr::Const(Fr::ONE)) | ||
); | ||
assert!(unit.selector.get_selector_assignment(uuid).is_none()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made this Option in order to distinguish the cases where the uuid is invalid, or the uuid is valid but has no assignments.