Skip to content

Commit

Permalink
feat: setting column size factor manually (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavePearce authored Oct 15, 2024
1 parent 61228f3 commit 334b913
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 67 deletions.
17 changes: 14 additions & 3 deletions src/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ impl<'a> Iterator for ValueBackingIter<'a> {
pub struct Register {
pub handle: Option<Handle>,
pub magma: Magma,
pub length_multiplier: usize,
#[serde(skip_serializing, skip_deserializing, default)]
backing: Option<ValueBacking>,
width: usize,
Expand Down Expand Up @@ -1163,10 +1164,16 @@ impl ColumnSet {
self._cols.iter()
}

pub(crate) fn new_register(&mut self, handle: Handle, magma: Magma) -> RegisterID {
pub(crate) fn new_register(
&mut self,
handle: Handle,
magma: Magma,
length_multiplier: usize,
) -> RegisterID {
self.registers.push(Register {
handle: Some(handle),
magma,
length_multiplier,
backing: None,
width: crate::constants::col_count_magma(magma),
});
Expand Down Expand Up @@ -1219,15 +1226,19 @@ impl ColumnSet {
}

pub fn insert_column_and_register(&mut self, mut column: Column) -> Result<ColumnRef> {
column.register = Some(self.new_register(column.handle.clone(), column.t));
let length_multiplier = column.intrinsic_size_factor.unwrap_or(1);
column.register =
Some(self.new_register(column.handle.clone(), column.t, length_multiplier));
self.insert_column(column)
}

pub fn maybe_insert_column_and_register(&mut self, mut column: Column) -> Option<ColumnRef> {
if self.cols.contains_key(&column.handle) {
None
} else {
column.register = Some(self.new_register(column.handle.clone(), column.t));
let length_multiplier = column.intrinsic_size_factor.unwrap_or(1);
column.register =
Some(self.new_register(column.handle.clone(), column.t, length_multiplier));
self.maybe_insert_column(column)
}
}
Expand Down
28 changes: 21 additions & 7 deletions src/compiler/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,11 @@ impl ConstraintSet {

// module-global columns all have their own register
for c in pool.root {
let col = self.columns.column(&c).unwrap();
let reg = self.columns.new_register(
self.handle(&c).to_owned(),
self.columns.column(&c).unwrap().t,
col.t,
col.intrinsic_size_factor.unwrap_or(1),
);
self.columns.assign_register(&c, reg).unwrap();
}
Expand All @@ -459,9 +461,9 @@ impl ConstraintSet {
.filter_map(|v| v.get(i))
.map(|r| self.handle(r).name.to_owned())
.join("_xor_");
let reg = self
.columns
.new_register(Handle::new(module, names), *magma);
let reg =
self.columns
.new_register(Handle::new(module, names), *magma, *size);
for cols in sets.values() {
if let Some(col_id) = cols.get(i) {
self.columns.assign_register(col_id, reg).unwrap();
Expand Down Expand Up @@ -494,7 +496,11 @@ impl ConstraintSet {
| Computation::CyclicFrom { target, .. }
| Computation::Composite { target, .. } => {
let col = self.columns.column(&target).unwrap();
let reg = self.columns.new_register(col.handle.clone(), col.t);
let reg = self.columns.new_register(
col.handle.clone(),
col.t,
col.intrinsic_size_factor.unwrap_or(1),
);
self.columns.assign_register(&target, reg).unwrap();
}
Computation::Sorted { froms, tos, .. } => {
Expand All @@ -504,7 +510,11 @@ impl ConstraintSet {
.entry(self.columns.column(f).unwrap().register.unwrap())
.or_insert_with(|| {
let col = self.columns.column(t).unwrap();
self.columns.new_register(col.handle.clone(), col.t)
self.columns.new_register(
col.handle.clone(),
col.t,
col.intrinsic_size_factor.unwrap_or(1),
)
});
self.columns.assign_register(t, *reg).unwrap();
}
Expand All @@ -522,7 +532,11 @@ impl ConstraintSet {
.chain(delta_bytes.iter())
{
let col = self.columns.column(r).unwrap();
let reg = self.columns.new_register(col.handle.clone(), col.t);
let reg = self.columns.new_register(
col.handle.clone(),
col.t,
col.intrinsic_size_factor.unwrap_or(1),
);
self.columns.assign_register(r, reg).unwrap();
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ pub fn make<S1: AsRef<str>, S2: AsRef<str>>(
padding_value,
base,
must_prove,
length_multiplier,
..
} => {
let column = Column::builder()
.handle(handle.as_handle().clone())
.and_padding_value(padding_value.to_owned())
.and_intrinsic_size_factor(length_multiplier.to_owned())
.kind(k.to_nil())
.t(symbol.t().m())
.must_prove(*must_prove)
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ pub enum Expression {
kind: Kind<Box<Node>>,
must_prove: bool,
padding_value: Option<i64>,
length_multiplier: Option<usize>,
base: Base,
},
ArrayColumn {
Expand Down Expand Up @@ -304,6 +305,7 @@ impl Node {
base: Option<Base>,
kind: Option<Kind<Box<Node>>>,
padding_value: Option<i64>,
length_multiplier: Option<usize>,
must_prove: Option<bool>,
t: Option<Magma>,
) -> Node {
Expand All @@ -327,6 +329,7 @@ impl Node {
kind: kind.unwrap_or(Kind::Computed),
must_prove: must_prove.unwrap_or(false),
padding_value,
length_multiplier,
base: base.unwrap_or_else(|| t.unwrap_or(Magma::native()).into()),
},
_t: Some(Type::Column(t.unwrap_or(Magma::native()))),
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/parser/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ fn reduce(e: &AstNode, ctx: &mut Scope, settings: &CompileSettings) -> Result<()
t,
kind,
padding_value,
length_multiplier,
must_prove,
base,
} => {
Expand All @@ -63,6 +64,7 @@ fn reduce(e: &AstNode, ctx: &mut Scope, settings: &CompileSettings) -> Result<()
Kind::Expression(_) => Kind::Computed,
})
.and_padding_value(*padding_value)
.and_length_multiplier(*length_multiplier)
.t(t.m())
.must_prove(*must_prove)
.base(*base)
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ pub enum Token {
kind: Kind<Box<AstNode>>,
/// the value to pad the column with; defaults to 0 if None
padding_value: Option<i64>,
/// the length multiplier for this column; defaults to 1 if None
length_multiplier: Option<usize>,
/// if set, generate constraint to prove the column type
must_prove: bool,
/// which numeric base should be used to display column values; this is a purely aesthetic setting
Expand Down
19 changes: 19 additions & 0 deletions src/compiler/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ struct ColumnAttributes {
must_prove: bool,
range: OnceCell<Box<Domain<AstNode>>>,
padding_value: OnceCell<i64>,
length_multiplier: OnceCell<usize>,
base: OnceCell<Base>,
computation: Option<AstNode>,
}
Expand Down Expand Up @@ -220,6 +221,7 @@ fn parse_column_attributes(source: AstNode) -> Result<ColumnAttributes> {
Array,
Computation,
PaddingValue,
LengthMultiplier,
Base,
}
let re_type = regex_lite::Regex::new(
Expand Down Expand Up @@ -257,6 +259,8 @@ fn parse_column_attributes(source: AstNode) -> Result<ColumnAttributes> {
":padding" => ColumnParser::PaddingValue,
// how to display the column values in debug
":display" => ColumnParser::Base,
// a specific length multiplier
":length" => ColumnParser::LengthMultiplier,
_ => {
if let Some(caps) = re_type.captures(kw) {
let raw_magma = if let Some(integer) = caps.name("Integer") {
Expand Down Expand Up @@ -339,6 +343,19 @@ fn parse_column_attributes(source: AstNode) -> Result<ColumnAttributes> {
})?;
ColumnParser::Begin
}
ColumnParser::LengthMultiplier => {
attributes
.length_multiplier
.set(x.as_u64()? as usize)
.map_err(|_| {
anyhow!(
"invalid length multiplier for column {} ({})",
attributes.name,
x.as_i64().unwrap()
)
})?;
ColumnParser::Begin
}
ColumnParser::Base => {
let base = if let Token::Keyword(ref kw) = x.class {
kw.as_str().try_into()?
Expand All @@ -363,6 +380,7 @@ fn parse_column_attributes(source: AstNode) -> Result<ColumnAttributes> {
ColumnParser::Array => bail!("incomplete :array definition"),
ColumnParser::Computation => bail!("incomplate :comp definition"),
ColumnParser::PaddingValue => bail!("incomplete :padding definition"),
ColumnParser::LengthMultiplier => bail!("incomplete :length definition"),
ColumnParser::Base => bail!("incomplete :display definition"),
}
Ok(attributes)
Expand Down Expand Up @@ -411,6 +429,7 @@ fn parse_defcolumns<I: Iterator<Item = Result<AstNode>>>(
.map(|c| Kind::Expression(Box::new(c)))
.unwrap_or(Kind::Commitment),
padding_value: column_attributes.padding_value.get().cloned(),
length_multiplier: column_attributes.length_multiplier.get().cloned(),
must_prove: column_attributes.must_prove,
base,
}
Expand Down
Loading

0 comments on commit 334b913

Please sign in to comment.