mirror of
https://github.com/Qortal/pirate-librustzcash.git
synced 2025-02-14 18:55:47 +00:00
Refactor "booleanization" of objects.
This commit is contained in:
parent
8d633db82b
commit
1df7fbeeff
@ -234,6 +234,47 @@ impl AllocatedBit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn field_into_allocated_bits_be<E: Engine, CS: ConstraintSystem<E>, F: PrimeField>(
|
||||||
|
mut cs: CS,
|
||||||
|
value: Option<F>
|
||||||
|
) -> Result<Vec<AllocatedBit>, SynthesisError>
|
||||||
|
{
|
||||||
|
let values = match value {
|
||||||
|
Some(ref value) => {
|
||||||
|
let mut field_char = BitIterator::new(F::char());
|
||||||
|
|
||||||
|
let mut tmp = Vec::with_capacity(F::NUM_BITS as usize);
|
||||||
|
|
||||||
|
let mut found_one = false;
|
||||||
|
for b in BitIterator::new(value.into_repr()) {
|
||||||
|
// Skip leading bits
|
||||||
|
found_one |= field_char.next().unwrap();
|
||||||
|
if !found_one {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp.push(Some(b));
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!(tmp.len(), F::NUM_BITS as usize);
|
||||||
|
|
||||||
|
tmp
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
vec![None; F::NUM_BITS as usize]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let bits = values.into_iter().enumerate().map(|(i, b)| {
|
||||||
|
AllocatedBit::alloc(
|
||||||
|
cs.namespace(|| format!("bit {}", i)),
|
||||||
|
b
|
||||||
|
)
|
||||||
|
}).collect::<Result<Vec<_>, SynthesisError>>()?;
|
||||||
|
|
||||||
|
Ok(bits)
|
||||||
|
}
|
||||||
|
|
||||||
/// This is a boolean value which may be either a constant or
|
/// This is a boolean value which may be either a constant or
|
||||||
/// an interpretation of an `AllocatedBit`.
|
/// an interpretation of an `AllocatedBit`.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@ -509,7 +550,11 @@ mod test {
|
|||||||
use pairing::bls12_381::{Bls12, Fr};
|
use pairing::bls12_381::{Bls12, Fr};
|
||||||
use pairing::{Field, PrimeField, PrimeFieldRepr, BitIterator};
|
use pairing::{Field, PrimeField, PrimeFieldRepr, BitIterator};
|
||||||
use ::circuit::test::*;
|
use ::circuit::test::*;
|
||||||
use super::{AllocatedBit, Boolean};
|
use super::{
|
||||||
|
AllocatedBit,
|
||||||
|
Boolean,
|
||||||
|
field_into_allocated_bits_be
|
||||||
|
};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_allocated_bit() {
|
fn test_allocated_bit() {
|
||||||
@ -1129,4 +1174,26 @@ mod test {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_field_into_allocated_bits_be() {
|
||||||
|
let mut cs = TestConstraintSystem::<Bls12>::new();
|
||||||
|
|
||||||
|
let r = Fr::from_str("9147677615426976802526883532204139322118074541891858454835346926874644257775").unwrap();
|
||||||
|
|
||||||
|
let bits = field_into_allocated_bits_be(&mut cs, Some(r)).unwrap();
|
||||||
|
|
||||||
|
assert!(cs.is_satisfied());
|
||||||
|
|
||||||
|
assert_eq!(bits.len(), 255);
|
||||||
|
|
||||||
|
assert_eq!(bits[0].value.unwrap(), false);
|
||||||
|
assert_eq!(bits[1].value.unwrap(), false);
|
||||||
|
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(), false);
|
||||||
|
assert_eq!(bits[20].value.unwrap(), true);
|
||||||
|
assert_eq!(bits[23].value.unwrap(), true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ use super::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use super::boolean::{
|
use super::boolean::{
|
||||||
|
self,
|
||||||
Boolean,
|
Boolean,
|
||||||
AllocatedBit
|
AllocatedBit
|
||||||
};
|
};
|
||||||
@ -76,39 +77,10 @@ impl<E: Engine> AllocatedNum<E> {
|
|||||||
) -> Result<Vec<Boolean>, SynthesisError>
|
) -> Result<Vec<Boolean>, SynthesisError>
|
||||||
where CS: ConstraintSystem<E>
|
where CS: ConstraintSystem<E>
|
||||||
{
|
{
|
||||||
let bit_values = match self.value {
|
let bits = boolean::field_into_allocated_bits_be(
|
||||||
Some(value) => {
|
&mut cs,
|
||||||
let mut field_char = BitIterator::new(E::Fr::char());
|
self.value
|
||||||
|
)?;
|
||||||
let mut tmp = Vec::with_capacity(E::Fr::NUM_BITS as usize);
|
|
||||||
|
|
||||||
let mut found_one = false;
|
|
||||||
for b in BitIterator::new(value.into_repr()) {
|
|
||||||
// Skip leading bits
|
|
||||||
found_one |= field_char.next().unwrap();
|
|
||||||
if !found_one {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp.push(Some(b));
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(tmp.len(), E::Fr::NUM_BITS as usize);
|
|
||||||
|
|
||||||
tmp
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
vec![None; E::Fr::NUM_BITS as usize]
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut bits = vec![];
|
|
||||||
for (i, b) in bit_values.into_iter().enumerate() {
|
|
||||||
bits.push(AllocatedBit::alloc(
|
|
||||||
cs.namespace(|| format!("bit {}", i)),
|
|
||||||
b
|
|
||||||
)?);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut lc = LinearCombination::zero();
|
let mut lc = LinearCombination::zero();
|
||||||
let mut coeff = E::Fr::one();
|
let mut coeff = E::Fr::one();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user