Return AssignedCell from circuit::Region::assign_* APIs.

This commit is contained in:
therealyingtong 2021-11-27 09:41:27 -05:00
parent 4aabf7c1ca
commit 36183bd01a
1 changed files with 58 additions and 28 deletions

View File

@ -93,7 +93,7 @@ pub struct Cell {
} }
/// An assigned cell. /// An assigned cell.
#[derive(Debug)] #[derive(Clone, Debug)]
pub struct AssignedCell<V: Clone + Into<Assigned<F>>, F: Field> { pub struct AssignedCell<V: Clone + Into<Assigned<F>>, F: Field> {
value: Option<V>, value: Option<V>,
cell: Cell, cell: Cell,
@ -132,16 +132,12 @@ impl<V: Clone + Into<Assigned<F>>, F: Field> AssignedCell<V, F> {
AR: Into<String>, AR: Into<String>,
{ {
let value = self.value(); let value = self.value();
let cell = region.assign_advice(annotation, column, offset, || { let assigned_cell = region.assign_advice(annotation, column, offset, || {
value.clone().ok_or(Error::Synthesis) value.clone().ok_or(Error::Synthesis)
})?; })?;
region.constrain_equal(cell, self.cell())?; region.constrain_equal(assigned_cell.cell(), self.cell())?;
Ok(AssignedCell { Ok(assigned_cell)
value,
cell,
_marker: PhantomData,
})
} }
} }
@ -192,17 +188,28 @@ impl<'r, F: Field> Region<'r, F> {
column: Column<Advice>, column: Column<Advice>,
offset: usize, offset: usize,
mut to: V, mut to: V,
) -> Result<Cell, Error> ) -> Result<AssignedCell<VR, F>, Error>
where where
V: FnMut() -> Result<VR, Error> + 'v, V: FnMut() -> Result<VR, Error> + 'v,
VR: Into<Assigned<F>>, VR: Clone + Into<Assigned<F>>,
A: Fn() -> AR, A: Fn() -> AR,
AR: Into<String>, AR: Into<String>,
{ {
self.region let mut value = None;
.assign_advice(&|| annotation().into(), column, offset, &mut || { let cell =
to().map(|v| v.into()) self.region
}) .assign_advice(&|| annotation().into(), column, offset, &mut || {
let v = to()?;
let value_f = v.clone().into();
value = Some(v);
Ok(value_f)
})?;
Ok(AssignedCell {
value,
cell,
_marker: PhantomData,
})
} }
/// Assigns a constant value to the column `advice` at `offset` within this region. /// Assigns a constant value to the column `advice` at `offset` within this region.
@ -217,18 +224,24 @@ impl<'r, F: Field> Region<'r, F> {
column: Column<Advice>, column: Column<Advice>,
offset: usize, offset: usize,
constant: VR, constant: VR,
) -> Result<Cell, Error> ) -> Result<AssignedCell<VR, F>, Error>
where where
VR: Into<Assigned<F>>, VR: Clone + Into<Assigned<F>>,
A: Fn() -> AR, A: Fn() -> AR,
AR: Into<String>, AR: Into<String>,
{ {
self.region.assign_advice_from_constant( let cell = self.region.assign_advice_from_constant(
&|| annotation().into(), &|| annotation().into(),
column, column,
offset, offset,
constant.into(), constant.clone().into(),
) )?;
Ok(AssignedCell {
value: Some(constant),
cell,
_marker: PhantomData,
})
} }
/// Assign the value of the instance column's cell at absolute location /// Assign the value of the instance column's cell at absolute location
@ -242,18 +255,24 @@ impl<'r, F: Field> Region<'r, F> {
row: usize, row: usize,
advice: Column<Advice>, advice: Column<Advice>,
offset: usize, offset: usize,
) -> Result<(Cell, Option<F>), Error> ) -> Result<AssignedCell<F, F>, Error>
where where
A: Fn() -> AR, A: Fn() -> AR,
AR: Into<String>, AR: Into<String>,
{ {
self.region.assign_advice_from_instance( let (cell, value) = self.region.assign_advice_from_instance(
&|| annotation().into(), &|| annotation().into(),
instance, instance,
row, row,
advice, advice,
offset, offset,
) )?;
Ok(AssignedCell {
value,
cell,
_marker: PhantomData,
})
} }
/// Assign a fixed value. /// Assign a fixed value.
@ -265,17 +284,28 @@ impl<'r, F: Field> Region<'r, F> {
column: Column<Fixed>, column: Column<Fixed>,
offset: usize, offset: usize,
mut to: V, mut to: V,
) -> Result<Cell, Error> ) -> Result<AssignedCell<VR, F>, Error>
where where
V: FnMut() -> Result<VR, Error> + 'v, V: FnMut() -> Result<VR, Error> + 'v,
VR: Into<Assigned<F>>, VR: Clone + Into<Assigned<F>>,
A: Fn() -> AR, A: Fn() -> AR,
AR: Into<String>, AR: Into<String>,
{ {
self.region let mut value = None;
.assign_fixed(&|| annotation().into(), column, offset, &mut || { let cell =
to().map(|v| v.into()) self.region
}) .assign_fixed(&|| annotation().into(), column, offset, &mut || {
let v = to()?;
let value_f = v.clone().into();
value = Some(v);
Ok(value_f)
})?;
Ok(AssignedCell {
value,
cell,
_marker: PhantomData,
})
} }
/// Constrains a cell to have a constant value. /// Constrains a cell to have a constant value.