Apply suggestions from code review

Co-authored-by: Pratyush Mishra <pratyushmishra@berkeley.edu>
This commit is contained in:
Nirvan Tyagi
2020-11-20 12:54:13 -05:00
committed by Pratyush Mishra
parent 0abb3a7dfe
commit 957ac48da4
2 changed files with 24 additions and 22 deletions

View File

@@ -330,23 +330,25 @@ macro_rules! make_uint {
} }
impl<ConstraintF: Field> CondSelectGadget<ConstraintF> for $name<ConstraintF> { impl<ConstraintF: Field> CondSelectGadget<ConstraintF> for $name<ConstraintF> {
#[tracing::instrument(target = "r1cs")] #[tracing::instrument(target = "r1cs", skip(cond, true_value, false_value))]
fn conditionally_select( fn conditionally_select(
cond: &Boolean<ConstraintF>, cond: &Boolean<ConstraintF>,
true_value: &Self, true_value: &Self,
false_value: &Self, false_value: &Self,
) -> Result<Self, SynthesisError> { ) -> Result<Self, SynthesisError> {
let selected_bits = true_value.bits.iter().zip(&false_value.bits) let selected_bits = true_value
.map(|(true_bit, false_bit)| { .bits
cond.select(true_bit, false_bit) .iter()
}).collect::<Result<Vec<Boolean<ConstraintF>>, SynthesisError>>()?; .zip(&false_value.bits)
let selected_value = match (cond.value(), true_value.value(), false_value.value()) { .map(|(t, f)| cond.select(t, f))
(Ok(true), Err(_), _) => None, .collect::<Result<Vec<_>, SynthesisError>>()?;
(Ok(true), Ok(v), _) => Some(v), let selected_value = cond.value().ok().and_then(|cond| {
(Ok(false), _, Err(_)) => None, if cond {
(Ok(false), _, Ok(v)) => Some(v), true_value.value().ok()
(Err(_), _, _) => None, } else {
}; false_value.value().ok()
}
});
Ok(Self { Ok(Self {
bits: selected_bits, bits: selected_bits,
value: selected_value, value: selected_value,

View File

@@ -285,7 +285,7 @@ impl<ConstraintF: Field> EqGadget<ConstraintF> for UInt8<ConstraintF> {
} }
impl<ConstraintF: Field> CondSelectGadget<ConstraintF> for UInt8<ConstraintF> { impl<ConstraintF: Field> CondSelectGadget<ConstraintF> for UInt8<ConstraintF> {
#[tracing::instrument(target = "r1cs")] #[tracing::instrument(target = "r1cs", skip(cond, true_value, false_value))]
fn conditionally_select( fn conditionally_select(
cond: &Boolean<ConstraintF>, cond: &Boolean<ConstraintF>,
true_value: &Self, true_value: &Self,
@@ -295,15 +295,15 @@ impl<ConstraintF: Field> CondSelectGadget<ConstraintF> for UInt8<ConstraintF> {
.bits .bits
.iter() .iter()
.zip(&false_value.bits) .zip(&false_value.bits)
.map(|(true_bit, false_bit)| cond.select(true_bit, false_bit)) .map(|(t, f)| cond.select(t, f))
.collect::<Result<Vec<Boolean<ConstraintF>>, SynthesisError>>()?; .collect::<Result<Vec<_>, SynthesisError>>()?;
let selected_value = match (cond.value(), true_value.value(), false_value.value()) { let selected_value = cond.value().ok().and_then(|cond| {
(Ok(true), Err(_), _) => None, if cond {
(Ok(true), Ok(v), _) => Some(v), true_value.value().ok()
(Ok(false), _, Err(_)) => None, } else {
(Ok(false), _, Ok(v)) => Some(v), false_value.value().ok()
(Err(_), _, _) => None, }
}; });
Ok(Self { Ok(Self {
bits: selected_bits, bits: selected_bits,
value: selected_value, value: selected_value,