Allocate the note value directly in little-endian bit order.

This commit is contained in:
Sean Bowe
2018-03-05 09:37:13 -07:00
parent e52befb58e
commit 3e15751fd1
2 changed files with 23 additions and 31 deletions
+17 -17
View File
@@ -271,16 +271,16 @@ impl AllocatedBit {
}
}
pub fn u64_into_allocated_bits_be<E: Engine, CS: ConstraintSystem<E>>(
pub fn u64_into_boolean_vec_le<E: Engine, CS: ConstraintSystem<E>>(
mut cs: CS,
value: Option<u64>
) -> Result<Vec<AllocatedBit>, SynthesisError>
) -> Result<Vec<Boolean>, SynthesisError>
{
let values = match value {
Some(ref value) => {
let mut tmp = Vec::with_capacity(64);
for i in (0..64).rev() {
for i in 0..64 {
tmp.push(Some(*value >> i & 1 == 1));
}
@@ -292,10 +292,10 @@ pub fn u64_into_allocated_bits_be<E: Engine, CS: ConstraintSystem<E>>(
};
let bits = values.into_iter().enumerate().map(|(i, b)| {
AllocatedBit::alloc(
Ok(Boolean::from(AllocatedBit::alloc(
cs.namespace(|| format!("bit {}", i)),
b
)
)?))
}).collect::<Result<Vec<_>, SynthesisError>>()?;
Ok(bits)
@@ -513,7 +513,7 @@ mod test {
AllocatedBit,
Boolean,
field_into_allocated_bits_be,
u64_into_allocated_bits_be
u64_into_boolean_vec_le
};
#[test]
@@ -982,24 +982,24 @@ mod test {
}
#[test]
fn test_u64_into_allocated_bits_be() {
fn test_u64_into_boolean_vec_le() {
let mut cs = TestConstraintSystem::<Bls12>::new();
let bits = u64_into_allocated_bits_be(&mut cs, Some(17234652694787248421)).unwrap();
let bits = u64_into_boolean_vec_le(&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);
assert_eq!(bits[63 - 0].get_value().unwrap(), true);
assert_eq!(bits[63 - 1].get_value().unwrap(), true);
assert_eq!(bits[63 - 2].get_value().unwrap(), true);
assert_eq!(bits[63 - 3].get_value().unwrap(), false);
assert_eq!(bits[63 - 4].get_value().unwrap(), true);
assert_eq!(bits[63 - 5].get_value().unwrap(), true);
assert_eq!(bits[63 - 20].get_value().unwrap(), true);
assert_eq!(bits[63 - 21].get_value().unwrap(), false);
assert_eq!(bits[63 - 22].get_value().unwrap(), false);
}
#[test]