1
0
Fork 0
mirror of synced 2025-09-24 04:40:05 +00:00

add comments, use iterators

This commit is contained in:
schaeff 2021-08-05 13:36:48 +02:00
parent 6e19e7754c
commit 33c8fba1e1
2 changed files with 19 additions and 16 deletions

View file

@ -1161,13 +1161,16 @@ impl<'ast, T: Field> Flattener<'ast, T> {
vec![self.flatten_bits_to_u(statements_flattened, param_expressions, 8.into())] vec![self.flatten_bits_to_u(statements_flattened, param_expressions, 8.into())]
} }
crate::embed::FlatEmbed::BitArrayLe => { crate::embed::FlatEmbed::BitArrayLe => {
// get the length of the bit arrays
let len = generics[0]; let len = generics[0];
// split the arguments into the two bit arrays of size `len`
let (expressions, constants) = ( let (expressions, constants) = (
param_expressions[..len as usize].to_vec(), param_expressions[..len as usize].to_vec(),
param_expressions[len as usize..].to_vec(), param_expressions[len as usize..].to_vec(),
); );
// define variables for the variable bits
let variables: Vec<_> = expressions let variables: Vec<_> = expressions
.into_iter() .into_iter()
.map(|e| { .map(|e| {
@ -1178,6 +1181,7 @@ impl<'ast, T: Field> Flattener<'ast, T> {
}) })
.collect(); .collect();
// get constants for the constant bits
let constants: Vec<_> = constants let constants: Vec<_> = constants
.into_iter() .into_iter()
.map(|e| { .map(|e| {
@ -1185,11 +1189,13 @@ impl<'ast, T: Field> Flattener<'ast, T> {
.get_field_unchecked() .get_field_unchecked()
}) })
.map(|e| match e { .map(|e| match e {
FlatExpression::Number(n) => n == T::one(), FlatExpression::Number(n) if n == T::one() => true,
FlatExpression::Number(n) if n == T::zero() => false,
_ => unreachable!(), _ => unreachable!(),
}) })
.collect(); .collect();
// get the list of conditions which must hold iff the `<=` relation holds
let conditions = let conditions =
self.constant_le_check(statements_flattened, &variables, &constants); self.constant_le_check(statements_flattened, &variables, &constants);

View file

@ -160,22 +160,19 @@ impl Interpreter {
let bit_width = bit_width - padding; let bit_width = bit_width - padding;
let mut num = inputs[0].clone(); let num = inputs[0].clone();
let mut res = vec![];
for _ in 0..padding { (0..padding)
res.push(T::zero()); .map(|_| T::zero())
} .chain((0..bit_width).rev().scan(num, |state, i| {
if T::from(2).pow(i) <= *state {
for i in (0..bit_width).rev() { *state = (*state).clone() - T::from(2).pow(i);
if T::from(2).pow(i) <= num { Some(T::one())
num = num - T::from(2).pow(i); } else {
res.push(T::one()); Some(T::zero())
} else { }
res.push(T::zero()); }))
} .collect()
}
res
} }
Solver::Xor => { Solver::Xor => {
let x = inputs[0].clone(); let x = inputs[0].clone();