Skip to content

Commit

Permalink
use batch inversion for assigments
Browse files Browse the repository at this point in the history
  • Loading branch information
Mason Liang committed Nov 7, 2023
1 parent bbda728 commit 7d09897
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/assignment_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::constraint_builder::{
use halo2_proofs::{
arithmetic::FieldExt,
circuit::{Region, Value},
plonk::Error,
plonk::{Assigned, Error},
};
use itertools::Itertools;
use rayon::prelude::*;
Expand All @@ -13,11 +13,11 @@ use std::collections::BTreeMap;
pub struct Assignment<F: FieldExt> {
pub offset: usize,
pub column: Column,
pub value: Value<F>,
pub value: Value<Assigned<F>>,
}

#[derive(Clone, Default)]
pub struct AssignmentMap<F: FieldExt>(BTreeMap<usize, Vec<(Column, Value<F>)>>);
pub struct AssignmentMap<F: FieldExt>(BTreeMap<usize, Vec<(Column, Value<Assigned<F>>)>>);

impl<F: FieldExt> AssignmentMap<F> {
pub fn new(stream: impl ParallelIterator<Item = Assignment<F>>) -> Self {
Expand Down
4 changes: 2 additions & 2 deletions src/constraint_builder/binary_column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use halo2_proofs::{
arithmetic::FieldExt,
circuit::{Region, Value},
plonk::ConstraintSystem,
plonk::{Advice, Column},
plonk::{Advice, Assigned, Column},
};

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -49,7 +49,7 @@ impl BinaryColumn {
Assignment {
offset,
column: ColumnEnum::from(AdviceColumn(self.0)),
value: Value::known(if value { F::one() } else { F::zero() }),
value: Value::known(Assigned::Trivial(if value { F::one() } else { F::zero() })),
}
}
}
20 changes: 16 additions & 4 deletions src/constraint_builder/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl SelectorColumn {
Assignment {
offset,
column: ColumnEnum::from(*self),
value: Value::known(if enable { F::one() } else { F::zero() }),
value: Value::known(Assigned::Trivial(if enable { F::one() } else { F::zero() })),
}
}
}
Expand Down Expand Up @@ -79,7 +79,7 @@ impl FixedColumn {
Assignment {
offset,
column: ColumnEnum::from(*self),
value: Value::known(value.try_into().unwrap()),
value: Value::known(value.try_into().unwrap()).into(),
}
}
}
Expand Down Expand Up @@ -137,7 +137,7 @@ impl AdviceColumn {
Assignment {
offset,
column: ColumnEnum::from(*self),
value: Value::known(value.try_into().unwrap()),
value: Value::known(Assigned::Trivial(value.try_into().unwrap())),
}
}

Expand All @@ -156,6 +156,18 @@ impl AdviceColumn {
)
.expect("failed assign_inverse_or_zero");
}

pub fn inverse_or_zero_assignment<F: FieldExt>(
&self,
offset: usize,
value: F,
) -> Assignment<F> {
Assignment {
offset,
column: ColumnEnum::from(*self),
value: Value::known(Assigned::Trivial(value).invert()),
}
}
}

#[derive(Clone, Copy, Hash, Eq, PartialEq, PartialOrd, Ord)]
Expand Down Expand Up @@ -184,7 +196,7 @@ impl SecondPhaseAdviceColumn {
Assignment {
offset,
column: ColumnEnum::from(*self),
value,
value: value.into(),
}
}
}
7 changes: 3 additions & 4 deletions src/gadgets/is_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ impl IsZeroGadget {
where
<T as TryInto<F>>::Error: Debug,
{
let value = value.try_into().unwrap();
[
self.value.assignment(offset, value),
self.inverse_or_zero.assignment(
offset,
(value.try_into().unwrap()).invert().unwrap_or_else(F::zero),
),
self.inverse_or_zero
.inverse_or_zero_assignment(offset, value),
]
}

Expand Down

0 comments on commit 7d09897

Please sign in to comment.