pub fn select_zero_or<F: PrimeField, CS: ConstraintSystem<F>>(
  mut cs: CS,
  a: &AllocatedNum<F>,
  condition: &AllocatedNum<F>,
) -> Result<AllocatedNum<F>, SynthesisError> {
  let c = AllocatedNum::alloc(cs.namespace(|| "conditional select result"), || {
    if *condition.get_value().get()? == F::one() {
      Ok(F::zero())
    } else {
      Ok(*a.get_value().get()?)
    }
  })?;

  // a * (1 - condition) = c
  cs.enforce(
    || "conditional select constraint",
    |lc| lc + a.get_variable(),
    |lc| lc + CS::one() - condition.get_variable(),
    |lc| lc + c.get_variable(),
  );

  Ok(c)
}
/// If condition set to 0 otherwise a
If condition set to 0 otherwise a
If condition set to 0 otherwise a
pub fn select_zero_or<F: PrimeField, CS: ConstraintSystem<F>>(
  mut cs: CS,
  a: &AllocatedNum<F>,
  condition: &AllocatedNum<F>,
) -> Result<AllocatedNum<F>, SynthesisError> {
  let c = AllocatedNum::alloc(cs.namespace(|| "conditional select result"), || {
    if *condition.get_value().get()? == F::one() {
      Ok(F::zero())
    } else {
      Ok(*a.get_value().get()?)
    }
  })?;
  // a * (1 - condition) = c
  cs.enforce(
    || "conditional select constraint",
    |lc| lc + a.get_variable(),
    |lc| lc + CS::one() - condition.get_variable(),
    |lc| lc + c.get_variable(),
  );
  Ok(c)
}
pub fn select_zero_or < F : PrimeField , CS : ConstraintSystem < F > > ( mut cs : CS , a : & AllocatedNum < F > , condition : & AllocatedNum < F > , ) -> Result < AllocatedNum < F > , SynthesisError > { let c = AllocatedNum :: alloc ( cs . namespace ( | | " " ) , | | { if * condition . get_value ( ) . get ( ) ? == F :: one ( ) { Ok ( F :: zero ( ) ) } else { Ok ( * a . get_value ( ) . get ( ) ? ) } } ) ? ; // a * (1 - condition) = c cs . enforce ( | | " " , | lc | lc + a . get_variable ( ) , | lc | lc + CS :: one ( ) - condition . get_variable ( ) , | lc | lc + c . get_variable ( ) , ) ; Ok ( c ) }
If condition set to 0 otherwise a
If condition set to 0 otherwise a
cs
CS
a
&AllocatedNum<F>
condition
&AllocatedNum<F>
cs
CS
a
&AllocatedNum<F>
condition
&AllocatedNum<F>