From 36183bd01afae25ef627c3ee2c743cff1caec032 Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Sat, 27 Nov 2021 09:41:27 -0500 Subject: [PATCH] Return AssignedCell from circuit::Region::assign_* APIs. --- src/circuit.rs | 86 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 58 insertions(+), 28 deletions(-) diff --git a/src/circuit.rs b/src/circuit.rs index a4984a4e..08282b0f 100644 --- a/src/circuit.rs +++ b/src/circuit.rs @@ -93,7 +93,7 @@ pub struct Cell { } /// An assigned cell. -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct AssignedCell>, F: Field> { value: Option, cell: Cell, @@ -132,16 +132,12 @@ impl>, F: Field> AssignedCell { AR: Into, { 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) })?; - region.constrain_equal(cell, self.cell())?; + region.constrain_equal(assigned_cell.cell(), self.cell())?; - Ok(AssignedCell { - value, - cell, - _marker: PhantomData, - }) + Ok(assigned_cell) } } @@ -192,17 +188,28 @@ impl<'r, F: Field> Region<'r, F> { column: Column, offset: usize, mut to: V, - ) -> Result + ) -> Result, Error> where V: FnMut() -> Result + 'v, - VR: Into>, + VR: Clone + Into>, A: Fn() -> AR, AR: Into, { - self.region - .assign_advice(&|| annotation().into(), column, offset, &mut || { - to().map(|v| v.into()) - }) + let mut value = None; + let cell = + 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. @@ -217,18 +224,24 @@ impl<'r, F: Field> Region<'r, F> { column: Column, offset: usize, constant: VR, - ) -> Result + ) -> Result, Error> where - VR: Into>, + VR: Clone + Into>, A: Fn() -> AR, AR: Into, { - self.region.assign_advice_from_constant( + let cell = self.region.assign_advice_from_constant( &|| annotation().into(), column, 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 @@ -242,18 +255,24 @@ impl<'r, F: Field> Region<'r, F> { row: usize, advice: Column, offset: usize, - ) -> Result<(Cell, Option), Error> + ) -> Result, Error> where A: Fn() -> AR, AR: Into, { - self.region.assign_advice_from_instance( + let (cell, value) = self.region.assign_advice_from_instance( &|| annotation().into(), instance, row, advice, offset, - ) + )?; + + Ok(AssignedCell { + value, + cell, + _marker: PhantomData, + }) } /// Assign a fixed value. @@ -265,17 +284,28 @@ impl<'r, F: Field> Region<'r, F> { column: Column, offset: usize, mut to: V, - ) -> Result + ) -> Result, Error> where V: FnMut() -> Result + 'v, - VR: Into>, + VR: Clone + Into>, A: Fn() -> AR, AR: Into, { - self.region - .assign_fixed(&|| annotation().into(), column, offset, &mut || { - to().map(|v| v.into()) - }) + let mut value = None; + let cell = + 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.