pub fn conditionally_select_bignat<F: PrimeField, CS: ConstraintSystem<F>>(
  mut cs: CS,
  a: &BigNat<F>,
  b: &BigNat<F>,
  condition: &Boolean,
) -> Result<BigNat<F>, SynthesisError> {
  assert!(a.limbs.len() == b.limbs.len());
  let c = BigNat::alloc_from_nat(
    cs.namespace(|| "conditional select result"),
    || {
      if *condition.get_value().get()? {
        Ok(a.value.get()?.clone())
      } else {
        Ok(b.value.get()?.clone())
      }
    },
    a.params.limb_width,
    a.params.n_limbs,
  )?;

  // a * condition + b*(1-condition) = c ->
  // a * condition - b*condition = c - b
  for i in 0..c.limbs.len() {
    cs.enforce(
      || format!("conditional select constraint {}", i),
      |lc| lc + &a.limbs[i] - &b.limbs[i],
      |_| condition.lc(CS::one(), F::one()),
      |lc| lc + &c.limbs[i] - &b.limbs[i],
    );
  }
  Ok(c)
}
/// If condition return a otherwise b where a and b are BigNats
If condition return a otherwise b where a and b are BigNats
If condition return a otherwise b where a and b are BigNats
pub fn conditionally_select_bignat<F: PrimeField, CS: ConstraintSystem<F>>(
  mut cs: CS,
  a: &BigNat<F>,
  b: &BigNat<F>,
  condition: &Boolean,
) -> Result<BigNat<F>, SynthesisError> {
  assert!(a.limbs.len() == b.limbs.len());
  let c = BigNat::alloc_from_nat(
    cs.namespace(|| "conditional select result"),
    || {
      if *condition.get_value().get()? {
        Ok(a.value.get()?.clone())
      } else {
        Ok(b.value.get()?.clone())
      }
    },
    a.params.limb_width,
    a.params.n_limbs,
  )?;
  // a * condition + b*(1-condition) = c ->
  // a * condition - b*condition = c - b
  for i in 0..c.limbs.len() {
    cs.enforce(
      || format!("conditional select constraint {}", i),
      |lc| lc + &a.limbs[i] - &b.limbs[i],
      |_| condition.lc(CS::one(), F::one()),
      |lc| lc + &c.limbs[i] - &b.limbs[i],
    );
  }
  Ok(c)
}
pub fn conditionally_select_bignat < F : PrimeField , CS : ConstraintSystem < F > > ( mut cs : CS , a : & BigNat < F > , b : & BigNat < F > , condition : & Boolean , ) -> Result < BigNat < F > , SynthesisError > { assert ! ( a limbs len ( ) b limbs len ( ) ) ; let c = BigNat :: alloc_from_nat ( cs . namespace ( | | " " ) , | | { if * condition . get_value ( ) . get ( ) ? { Ok ( a . value . get ( ) ? . clone ( ) ) } else { Ok ( b . value . get ( ) ? . clone ( ) ) } } , a . params . limb_width , a . params . n_limbs , ) ? ; // a * condition + b*(1-condition) = c -> // a * condition - b*condition = c - b for i in 0 .. c . limbs . len ( ) { cs . enforce ( | | format ! ( " " i ) , | lc | lc + & a . limbs [ i ] - & b . limbs [ i ] , | _ | condition . lc ( CS :: one ( ) , F :: one ( ) ) , | lc | lc + & c . limbs [ i ] - & b . limbs [ i ] , ) ; } Ok ( c ) }
If condition return a otherwise b where a and b are BigNats
If condition return a otherwise b where a and b are BigNats
cs
CS
a
&BigNat<F>
b
&BigNat<F>
condition
&Boolean
cs
CS
a
&BigNat<F>
b
&BigNat<F>
condition
&Boolean