|
|
@ -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, |
|
|
|
mut cs: CS, |
|
|
|
value: Option<u64> |
|
|
|
value: Option<u64> |
|
|
|
) -> Result<Vec<AllocatedBit>, SynthesisError> |
|
|
|
) -> Result<Vec<Boolean>, SynthesisError> |
|
|
|
{ |
|
|
|
{ |
|
|
|
let values = match value { |
|
|
|
let values = match value { |
|
|
|
Some(ref value) => { |
|
|
|
Some(ref value) => { |
|
|
|
let mut tmp = Vec::with_capacity(64); |
|
|
|
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)); |
|
|
|
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)| { |
|
|
|
let bits = values.into_iter().enumerate().map(|(i, b)| { |
|
|
|
AllocatedBit::alloc( |
|
|
|
Ok(Boolean::from(AllocatedBit::alloc( |
|
|
|
cs.namespace(|| format!("bit {}", i)), |
|
|
|
cs.namespace(|| format!("bit {}", i)), |
|
|
|
b |
|
|
|
b |
|
|
|
) |
|
|
|
)?)) |
|
|
|
}).collect::<Result<Vec<_>, SynthesisError>>()?; |
|
|
|
}).collect::<Result<Vec<_>, SynthesisError>>()?; |
|
|
|
|
|
|
|
|
|
|
|
Ok(bits) |
|
|
|
Ok(bits) |
|
|
@ -513,7 +513,7 @@ mod test { |
|
|
|
AllocatedBit, |
|
|
|
AllocatedBit, |
|
|
|
Boolean, |
|
|
|
Boolean, |
|
|
|
field_into_allocated_bits_be, |
|
|
|
field_into_allocated_bits_be, |
|
|
|
u64_into_allocated_bits_be |
|
|
|
u64_into_boolean_vec_le |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
@ -982,24 +982,24 @@ mod test { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_u64_into_allocated_bits_be() { |
|
|
|
fn test_u64_into_boolean_vec_le() { |
|
|
|
let mut cs = TestConstraintSystem::<Bls12>::new(); |
|
|
|
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!(cs.is_satisfied()); |
|
|
|
|
|
|
|
|
|
|
|
assert_eq!(bits.len(), 64); |
|
|
|
assert_eq!(bits.len(), 64); |
|
|
|
|
|
|
|
|
|
|
|
assert_eq!(bits[0].value.unwrap(), true); |
|
|
|
assert_eq!(bits[63 - 0].get_value().unwrap(), true); |
|
|
|
assert_eq!(bits[1].value.unwrap(), true); |
|
|
|
assert_eq!(bits[63 - 1].get_value().unwrap(), true); |
|
|
|
assert_eq!(bits[2].value.unwrap(), true); |
|
|
|
assert_eq!(bits[63 - 2].get_value().unwrap(), true); |
|
|
|
assert_eq!(bits[3].value.unwrap(), false); |
|
|
|
assert_eq!(bits[63 - 3].get_value().unwrap(), false); |
|
|
|
assert_eq!(bits[4].value.unwrap(), true); |
|
|
|
assert_eq!(bits[63 - 4].get_value().unwrap(), true); |
|
|
|
assert_eq!(bits[5].value.unwrap(), true); |
|
|
|
assert_eq!(bits[63 - 5].get_value().unwrap(), true); |
|
|
|
assert_eq!(bits[20].value.unwrap(), true); |
|
|
|
assert_eq!(bits[63 - 20].get_value().unwrap(), true); |
|
|
|
assert_eq!(bits[21].value.unwrap(), false); |
|
|
|
assert_eq!(bits[63 - 21].get_value().unwrap(), false); |
|
|
|
assert_eq!(bits[22].value.unwrap(), false); |
|
|
|
assert_eq!(bits[63 - 22].get_value().unwrap(), false); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|