arnaucube
arnaucube/ark-r1cs-stdPublic Self-hosted repository
Return the internally recorded value in `Allocated{Bool, Fp}::value()` for performance
winderica winderica@gmail.com
Tue, 30 Apr 2024 02:29:16 +0800
commit

1737456304122678c1ef6902772e18b0d3701a2c

parent

4020fbc22625621baa8125ede87abaeac3c1ca26

2 files changed, 24 insertions(+), 27 deletions(-)

jump to
M src/boolean/allocated.rs → src/boolean/allocated.rs

@@ -6,7 +6,6 @@

use crate::{ alloc::{AllocVar, AllocationMode}, select::CondSelectGadget, - Assignment, }; use super::Boolean;

@@ -22,23 +21,13 @@ #[must_use]

pub struct AllocatedBool<F: Field> { pub(super) variable: Variable, pub(super) cs: ConstraintSystemRef<F>, -} - -pub(crate) fn bool_to_field<F: Field>(val: impl Borrow<bool>) -> F { - F::from(*val.borrow()) + pub(super) value: Option<bool>, } impl<F: Field> AllocatedBool<F> { /// Get the assigned value for `self`. pub fn value(&self) -> Result<bool, SynthesisError> { - let value = self.cs.assigned_value(self.variable).get()?; - if value.is_zero() { - Ok(false) - } else if value.is_one() { - Ok(true) - } else { - unreachable!("Incorrect value assigned: {:?}", value); - } + self.value.ok_or(SynthesisError::AssignmentMissing) } /// Get the R1CS variable for `self`.

@@ -52,8 +41,12 @@ pub fn new_witness_without_booleanity_check<T: Borrow<bool>>(

cs: ConstraintSystemRef<F>, f: impl FnOnce() -> Result<T, SynthesisError>, ) -> Result<Self, SynthesisError> { - let variable = cs.new_witness_variable(|| f().map(bool_to_field))?; - Ok(Self { variable, cs }) + let value = f().map(|b| *b.borrow()); + Ok(Self { + variable: cs.new_witness_variable(|| value.map(F::from))?, + cs, + value: value.ok(), + }) } /// Performs an XOR operation over the two operands, returning

@@ -64,6 +57,7 @@ let variable = self.cs.new_lc(lc!() + Variable::One - self.variable)?;

Ok(Self { variable, cs: self.cs.clone(), + value: self.value.map(|v| !v), }) }

@@ -189,25 +183,28 @@ ) -> Result<Self, SynthesisError> {

let ns = cs.into(); let cs = ns.cs(); if mode == AllocationMode::Constant { - let variable = if *f()?.borrow() { - Variable::One - } else { - Variable::Zero - }; - Ok(Self { variable, cs }) + let value = *f()?.borrow(); + Ok(Self { + variable: if value { Variable::One } else { Variable::Zero }, + cs, + value: Some(value), + }) } else { + let value = f().map(|b| *b.borrow()); let variable = if mode == AllocationMode::Input { - cs.new_input_variable(|| f().map(bool_to_field))? + cs.new_input_variable(|| value.map(F::from))? } else { - cs.new_witness_variable(|| f().map(bool_to_field))? + cs.new_witness_variable(|| value.map(F::from))? }; // Constrain: (1 - a) * a = 0 // This constrains a to be either 0 or 1. - cs.enforce_constraint(lc!() + Variable::One - variable, lc!() + variable, lc!())?; - - Ok(Self { variable, cs }) + Ok(Self { + variable, + cs, + value: value.ok(), + }) } } }
M src/fields/fp/mod.rs → src/fields/fp/mod.rs

@@ -136,7 +136,7 @@

/// Returns the value assigned to `self` in the underlying constraint system /// (if a value was assigned). pub fn value(&self) -> Result<F, SynthesisError> { - self.cs.assigned_value(self.variable).get() + self.value.ok_or(SynthesisError::AssignmentMissing) } /// Outputs `self + other`.