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

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

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