pub fn le_bits_to_num<Scalar, CS>(
  mut cs: CS,
  bits: Vec<AllocatedBit>,
) -> Result<AllocatedNum<Scalar>, SynthesisError>
where
  Scalar: PrimeField + PrimeFieldBits,
  CS: ConstraintSystem<Scalar>,
{
  // We loop over the input bits and construct the constraint
  // and the field element that corresponds to the result
  let mut lc = LinearCombination::zero();
  let mut coeff = Scalar::one();
  let mut fe = Some(Scalar::zero());
  for bit in bits.iter() {
    lc = lc + (coeff, bit.get_variable());
    fe = bit.get_value().map(|val| {
      if val {
        fe.unwrap() + coeff
      } else {
        fe.unwrap()
      }
    });
    coeff = coeff.double();
  }
  let num = AllocatedNum::alloc(cs.namespace(|| "Field element"), || {
    fe.ok_or(SynthesisError::AssignmentMissing)
  })?;
  lc = lc - num.get_variable();
  cs.enforce(|| "compute number from bits", |lc| lc, |lc| lc, |_| lc);
  Ok(num)
}
/// Gets as input the little indian representation of a number and spits out the number
Gets as input the little indian representation of a number and spits out the number
Gets as input the little indian representation of a number and spits out the number
pub fn le_bits_to_num<Scalar, CS>(
  mut cs: CS,
  bits: Vec<AllocatedBit>,
) -> Result<AllocatedNum<Scalar>, SynthesisError>
where
  Scalar: PrimeField + PrimeFieldBits,
  CS: ConstraintSystem<Scalar>,
{
  // We loop over the input bits and construct the constraint
  // and the field element that corresponds to the result
  let mut lc = LinearCombination::zero();
  let mut coeff = Scalar::one();
  let mut fe = Some(Scalar::zero());
  for bit in bits.iter() {
    lc = lc + (coeff, bit.get_variable());
    fe = bit.get_value().map(|val| {
      if val {
        fe.unwrap() + coeff
      } else {
        fe.unwrap()
      }
    });
    coeff = coeff.double();
  }
  let num = AllocatedNum::alloc(cs.namespace(|| "Field element"), || {
    fe.ok_or(SynthesisError::AssignmentMissing)
  })?;
  lc = lc - num.get_variable();
  cs.enforce(|| "compute number from bits", |lc| lc, |lc| lc, |_| lc);
  Ok(num)
}
pub fn le_bits_to_num < Scalar , CS > ( mut cs : CS , bits : Vec < AllocatedBit > , ) -> Result < AllocatedNum < Scalar > , SynthesisError > where Scalar : PrimeField + PrimeFieldBits , CS : ConstraintSystem < Scalar > , { // We loop over the input bits and construct the constraint // and the field element that corresponds to the result let mut lc = LinearCombination :: zero ( ) ; let mut coeff = Scalar :: one ( ) ; let mut fe = Some ( Scalar :: zero ( ) ) ; for bit in bits . iter ( ) { lc = lc + ( coeff , bit . get_variable ( ) ) ; fe = bit . get_value ( ) . map ( | val | { if val { fe . unwrap ( ) + coeff } else { fe . unwrap ( ) } } ) ; coeff = coeff . double ( ) ; } let num = AllocatedNum :: alloc ( cs . namespace ( | | " " ) , | | { fe . ok_or ( SynthesisError :: AssignmentMissing ) } ) ? ; lc = lc - num . get_variable ( ) ; cs . enforce ( | | " " , | lc | lc , | lc | lc , | _ | lc ) ; Ok ( num ) }
Gets as input the little indian representation of a number and spits out the number
Gets as input the little indian representation of a number and spits out the number
cs
CS
bits
Vec<AllocatedBit>
cs
CS
bits
Vec<AllocatedBit>