Booleanize u64 objects.

This commit is contained in:
Sean Bowe 2018-02-19 19:27:03 -07:00
parent 1df7fbeeff
commit ec7323159c
No known key found for this signature in database
GPG Key ID: 95684257D8F8B031
1 changed files with 53 additions and 1 deletions

View File

@ -234,6 +234,36 @@ impl AllocatedBit {
}
}
pub fn u64_into_allocated_bits_be<E: Engine, CS: ConstraintSystem<E>>(
mut cs: CS,
value: Option<u64>
) -> Result<Vec<AllocatedBit>, SynthesisError>
{
let values = match value {
Some(ref value) => {
let mut tmp = Vec::with_capacity(64);
for i in (0..64).rev() {
tmp.push(Some(*value >> i & 1 == 1));
}
tmp
},
None => {
vec![None; 64]
}
};
let bits = values.into_iter().enumerate().map(|(i, b)| {
AllocatedBit::alloc(
cs.namespace(|| format!("bit {}", i)),
b
)
}).collect::<Result<Vec<_>, SynthesisError>>()?;
Ok(bits)
}
pub fn field_into_allocated_bits_be<E: Engine, CS: ConstraintSystem<E>, F: PrimeField>(
mut cs: CS,
value: Option<F>
@ -553,7 +583,8 @@ mod test {
use super::{
AllocatedBit,
Boolean,
field_into_allocated_bits_be
field_into_allocated_bits_be,
u64_into_allocated_bits_be
};
#[test]
@ -1175,6 +1206,27 @@ mod test {
}
}
#[test]
fn test_u64_into_allocated_bits_be() {
let mut cs = TestConstraintSystem::<Bls12>::new();
let bits = u64_into_allocated_bits_be(&mut cs, Some(17234652694787248421)).unwrap();
assert!(cs.is_satisfied());
assert_eq!(bits.len(), 64);
assert_eq!(bits[0].value.unwrap(), true);
assert_eq!(bits[1].value.unwrap(), true);
assert_eq!(bits[2].value.unwrap(), true);
assert_eq!(bits[3].value.unwrap(), false);
assert_eq!(bits[4].value.unwrap(), true);
assert_eq!(bits[5].value.unwrap(), true);
assert_eq!(bits[20].value.unwrap(), true);
assert_eq!(bits[21].value.unwrap(), false);
assert_eq!(bits[22].value.unwrap(), false);
}
#[test]
fn test_field_into_allocated_bits_be() {
let mut cs = TestConstraintSystem::<Bls12>::new();