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

fix conflict

This commit is contained in:
schaeff 2019-03-15 16:41:27 +01:00
commit ec1fdbb6e7
6 changed files with 68 additions and 228 deletions

View file

@ -98,7 +98,7 @@ mod integration {
"--light",
];
if program_name.contains("libsnark") {
if program_name.contains("sha") {
// we don't want to test libsnark integrations if libsnark is not available
#[cfg(not(feature = "libsnark"))]
return;

View file

@ -36,7 +36,7 @@ impl fmt::Debug for FlatParameter {
}
impl FlatParameter {
pub fn apply_direct_substitution(
pub fn apply_substitution(
self,
substitution: &HashMap<FlatVariable, FlatVariable>,
) -> FlatParameter {

View file

@ -50,15 +50,8 @@ impl fmt::Debug for FlatVariable {
}
impl FlatVariable {
pub fn apply_substitution(
self,
substitution: &HashMap<FlatVariable, FlatVariable>,
should_fallback: bool,
) -> Self {
match should_fallback {
true => substitution.get(&self).unwrap_or(&self).clone(),
false => substitution.get(&self).unwrap().clone(),
}
pub fn apply_substitution(self, substitution: &HashMap<FlatVariable, FlatVariable>) -> &Self {
substitution.get(&self).unwrap()
}
pub fn is_output(&self) -> bool {

View file

@ -213,47 +213,30 @@ impl<T: Field> fmt::Debug for FlatStatement<T> {
}
impl<T: Field> FlatStatement<T> {
pub fn apply_recursive_substitution(
pub fn apply_substitution(
self,
substitution: &HashMap<FlatVariable, FlatVariable>,
) -> FlatStatement<T> {
self.apply_substitution(substitution, true)
}
pub fn apply_direct_substitution(
self,
substitution: &HashMap<FlatVariable, FlatVariable>,
) -> FlatStatement<T> {
self.apply_substitution(substitution, false)
}
fn apply_substitution(
self,
substitution: &HashMap<FlatVariable, FlatVariable>,
should_fallback: bool,
) -> FlatStatement<T> {
match self {
FlatStatement::Definition(id, x) => FlatStatement::Definition(
id.apply_substitution(substitution, should_fallback),
x.apply_substitution(substitution, should_fallback),
*id.apply_substitution(substitution),
x.apply_substitution(substitution),
),
FlatStatement::Return(x) => {
FlatStatement::Return(x.apply_substitution(substitution, should_fallback))
}
FlatStatement::Return(x) => FlatStatement::Return(x.apply_substitution(substitution)),
FlatStatement::Condition(x, y) => FlatStatement::Condition(
x.apply_substitution(substitution, should_fallback),
y.apply_substitution(substitution, should_fallback),
x.apply_substitution(substitution),
y.apply_substitution(substitution),
),
FlatStatement::Directive(d) => {
let outputs = d
.outputs
.into_iter()
.map(|o| o.apply_substitution(substitution, should_fallback))
.map(|o| *o.apply_substitution(substitution))
.collect();
let inputs = d
.inputs
.into_iter()
.map(|i| i.apply_substitution(substitution, should_fallback))
.map(|i| i.apply_substitution(substitution))
.collect();
FlatStatement::Directive(DirectiveStatement {
@ -276,41 +259,26 @@ pub enum FlatExpression<T: Field> {
}
impl<T: Field> FlatExpression<T> {
pub fn apply_recursive_substitution(
pub fn apply_substitution(
self,
substitution: &HashMap<FlatVariable, FlatVariable>,
) -> FlatExpression<T> {
self.apply_substitution(substitution, true)
}
pub fn apply_direct_substitution(
self,
substitution: &HashMap<FlatVariable, FlatVariable>,
) -> FlatExpression<T> {
self.apply_substitution(substitution, false)
}
fn apply_substitution(
self,
substitution: &HashMap<FlatVariable, FlatVariable>,
should_fallback: bool,
) -> FlatExpression<T> {
match self {
e @ FlatExpression::Number(_) => e,
FlatExpression::Identifier(id) => {
FlatExpression::Identifier(id.apply_substitution(substitution, should_fallback))
FlatExpression::Identifier(*id.apply_substitution(substitution))
}
FlatExpression::Add(e1, e2) => FlatExpression::Add(
box e1.apply_substitution(substitution, should_fallback),
box e2.apply_substitution(substitution, should_fallback),
box e1.apply_substitution(substitution),
box e2.apply_substitution(substitution),
),
FlatExpression::Sub(e1, e2) => FlatExpression::Sub(
box e1.apply_substitution(substitution, should_fallback),
box e2.apply_substitution(substitution, should_fallback),
box e1.apply_substitution(substitution),
box e2.apply_substitution(substitution),
),
FlatExpression::Mult(e1, e2) => FlatExpression::Mult(
box e1.apply_substitution(substitution, should_fallback),
box e2.apply_substitution(substitution, should_fallback),
box e1.apply_substitution(substitution),
box e2.apply_substitution(substitution),
),
}
}
@ -392,33 +360,18 @@ impl<T: Field> fmt::Display for FlatExpressionList<T> {
}
impl<T: Field> FlatExpressionList<T> {
fn apply_substitution(
pub fn apply_substitution(
self,
substitution: &HashMap<FlatVariable, FlatVariable>,
should_fallback: bool,
) -> FlatExpressionList<T> {
FlatExpressionList {
expressions: self
.expressions
.into_iter()
.map(|e| e.apply_substitution(substitution, should_fallback))
.map(|e| e.apply_substitution(substitution))
.collect(),
}
}
pub fn apply_recursive_substitution(
self,
substitution: &HashMap<FlatVariable, FlatVariable>,
) -> FlatExpressionList<T> {
self.apply_substitution(substitution, true)
}
pub fn apply_direct_substitution(
self,
substitution: &HashMap<FlatVariable, FlatVariable>,
) -> FlatExpressionList<T> {
self.apply_substitution(substitution, false)
}
}
impl<T: Field> fmt::Debug for FlatExpressionList<T> {

View file

@ -8,7 +8,7 @@
use bimap::BiMap;
use flat_absy::*;
use helpers::{DirectiveStatement, Helper, RustHelper};
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use typed_absy::*;
use types::conversions::cast;
use types::Signature;
@ -18,10 +18,6 @@ use zokrates_field::field::Field;
/// Flattener, computes flattened program.
#[derive(Debug)]
pub struct Flattener {
/// Vector containing all used variables while processing the program.
variables: HashSet<FlatVariable>,
/// Map of renamings for reassigned variables while processing the program.
substitution: HashMap<FlatVariable, FlatVariable>,
/// Index of the next introduced variable while processing the program.
next_var_idx: usize,
///
@ -35,8 +31,6 @@ impl Flattener {
/// * `bits` - Number of bits needed to represent the maximum value.
pub fn new() -> Flattener {
Flattener {
variables: HashSet::new(),
substitution: HashMap::new(),
next_var_idx: 0,
bijection: BiMap::new(),
}
@ -473,11 +467,6 @@ impl Flattener {
.flat_map(|x| x)
.collect::<Vec<_>>();
let params_flattened = params_flattened
.into_iter()
.map(|e| e.apply_recursive_substitution(&self.substitution))
.collect::<Vec<_>>();
for (index, r) in params_flattened.into_iter().enumerate() {
let new_var = self.use_sym();
statements_flattened.push(FlatStatement::Definition(new_var, r));
@ -494,19 +483,19 @@ impl Flattener {
expressions: list
.expressions
.into_iter()
.map(|x| x.apply_direct_substitution(&replacement_map))
.map(|x| x.apply_substitution(&replacement_map))
.collect(),
};
}
FlatStatement::Definition(var, rhs) => {
let new_var = self.issue_new_variable();
replacement_map.insert(var, new_var);
let new_rhs = rhs.apply_direct_substitution(&replacement_map);
let new_rhs = rhs.apply_substitution(&replacement_map);
statements_flattened.push(FlatStatement::Definition(new_var, new_rhs));
}
FlatStatement::Condition(lhs, rhs) => {
let new_lhs = lhs.apply_direct_substitution(&replacement_map);
let new_rhs = rhs.apply_direct_substitution(&replacement_map);
let new_lhs = lhs.apply_substitution(&replacement_map);
let new_rhs = rhs.apply_substitution(&replacement_map);
statements_flattened.push(FlatStatement::Condition(new_lhs, new_rhs));
}
FlatStatement::Directive(d) => {
@ -522,7 +511,7 @@ impl Flattener {
let new_inputs = d
.inputs
.into_iter()
.map(|i| i.apply_direct_substitution(&replacement_map))
.map(|i| i.apply_substitution(&replacement_map))
.collect();
statements_flattened.push(FlatStatement::Directive(
DirectiveStatement {
@ -732,14 +721,12 @@ impl Flattener {
match exponent {
FieldElementExpression::Number(ref e) => {
// flatten the base expression
let base_flattened = self
.flatten_field_expression(
functions_flattened,
arguments_flattened,
statements_flattened,
base.clone(),
)
.apply_recursive_substitution(&self.substitution);
let base_flattened = self.flatten_field_expression(
functions_flattened,
arguments_flattened,
statements_flattened,
base.clone(),
);
// we require from the base to be linear
// TODO change that
@ -758,19 +745,15 @@ impl Flattener {
// flatten(base ** n) = flatten(base) * flatten(base ** (n-1))
e => {
// flatten(base ** (n-1))
let tmp_expression = self
.flatten_field_expression(
functions_flattened,
arguments_flattened,
statements_flattened,
FieldElementExpression::Pow(
box base,
box FieldElementExpression::Number(
e.clone() - T::one(),
),
),
)
.apply_recursive_substitution(&self.substitution);
let tmp_expression = self.flatten_field_expression(
functions_flattened,
arguments_flattened,
statements_flattened,
FieldElementExpression::Pow(
box base,
box FieldElementExpression::Number(e.clone() - T::one()),
),
);
let id = self.use_sym();
@ -828,7 +811,6 @@ impl Flattener {
statements_flattened,
expressions[n.to_dec_string().parse::<usize>().unwrap()].clone(),
)
.apply_recursive_substitution(&self.substitution)
}
FieldElementArrayExpression::FunctionCall(..) => {
unimplemented!("please use intermediate variables for now")
@ -855,7 +837,6 @@ impl Flattener {
),
),
)
.apply_recursive_substitution(&self.substitution)
}
},
e => {
@ -943,7 +924,6 @@ impl Flattener {
statements_flattened,
lookup,
)
.apply_recursive_substitution(&self.substitution)
}
}
}
@ -1047,11 +1027,6 @@ impl Flattener {
.flat_map(|x| x)
.collect::<Vec<_>>();
let flat_expressions = flat_expressions
.into_iter()
.map(|e| e.apply_recursive_substitution(&self.substitution))
.collect();
statements_flattened.push(FlatStatement::Return(FlatExpressionList {
expressions: flat_expressions,
}));
@ -1071,11 +1046,6 @@ impl Flattener {
expr.clone(),
);
let rhs = rhs
.into_iter()
.map(|e| e.apply_recursive_substitution(&self.substitution))
.collect::<Vec<_>>();
match expr.get_type() {
Type::FieldElement | Type::Boolean => {
match assignee {
@ -1083,14 +1053,6 @@ impl Flattener {
let debug_name = v.clone().id;
let var = self.use_variable(&debug_name);
// handle return of function call
let var_to_replace = self.get_latest_var_substitution(&debug_name);
if !(var == var_to_replace)
&& self.variables.contains(&var_to_replace)
&& !self.substitution.contains_key(&var_to_replace)
{
self.substitution
.insert(var_to_replace.clone(), var.clone());
}
statements_flattened
.push(FlatStatement::Definition(var, rhs[0].clone()));
}
@ -1100,32 +1062,17 @@ impl Flattener {
_ => panic!("not a field element as rhs of array element update, should have been caught at semantic")
};
match index {
box FieldElementExpression::Number(n) => {
match array {
box TypedAssignee::Identifier(id) => {
let debug_name = format!("{}_c{}", id.id, n);
let var = self.use_variable(&debug_name);
// handle return of function call
let var_to_replace =
self.get_latest_var_substitution(&debug_name);
if !(var == var_to_replace)
&& self.variables.contains(&var_to_replace)
&& !self
.substitution
.contains_key(&var_to_replace)
{
self.substitution.insert(
var_to_replace.clone(),
var.clone(),
);
}
statements_flattened.push(
FlatStatement::Definition(var, rhs[0].clone()),
);
}
_ => panic!("no multidimension array for now"),
box FieldElementExpression::Number(n) => match array {
box TypedAssignee::Identifier(id) => {
let debug_name = format!("{}_c{}", id.id, n);
let var = self.use_variable(&debug_name);
statements_flattened.push(FlatStatement::Definition(
var,
rhs[0].clone(),
));
}
}
_ => panic!("no multidimension array for now"),
},
box e => {
// we have array[e] with e an arbitrary expression
// first we check that e is in 0..array.len(), so we check that sum(if e == i then 1 else 0) == 1
@ -1205,15 +1152,6 @@ impl Flattener {
_ => unimplemented!(),
};
let var = self.use_variable(&debug_name);
// handle return of function call
let var_to_replace = self.get_latest_var_substitution(&debug_name);
if !(var == var_to_replace)
&& self.variables.contains(&var_to_replace)
&& !self.substitution.contains_key(&var_to_replace)
{
self.substitution
.insert(var_to_replace.clone(), var.clone());
}
statements_flattened.push(FlatStatement::Definition(var, r));
}
}
@ -1231,15 +1169,13 @@ impl Flattener {
arguments_flattened,
statements_flattened,
e1,
)
.apply_recursive_substitution(&self.substitution),
),
self.flatten_field_expression(
functions_flattened,
arguments_flattened,
statements_flattened,
e2,
)
.apply_recursive_substitution(&self.substitution),
),
);
if lhs.is_linear() {
@ -1258,15 +1194,13 @@ impl Flattener {
arguments_flattened,
statements_flattened,
e1,
)
.apply_recursive_substitution(&self.substitution),
),
self.flatten_boolean_expression(
functions_flattened,
arguments_flattened,
statements_flattened,
e2,
)
.apply_recursive_substitution(&self.substitution),
),
);
if lhs.is_linear() {
@ -1297,13 +1231,6 @@ impl Flattener {
),
);
let (lhs, rhs) = (
lhs.into_iter()
.map(|e| e.apply_recursive_substitution(&self.substitution)),
rhs.into_iter()
.map(|e| e.apply_recursive_substitution(&self.substitution)),
);
assert_eq!(lhs.len(), rhs.len());
for (l, r) in lhs.into_iter().zip(rhs.into_iter()) {
@ -1348,16 +1275,14 @@ impl Flattener {
match rhs {
TypedExpressionList::FunctionCall(fun_id, exprs, _) => {
let rhs_flattened = self
.flatten_function_call(
functions_flattened,
arguments_flattened,
statements_flattened,
&fun_id,
var_types,
&exprs,
)
.apply_recursive_substitution(&self.substitution);
let rhs_flattened = self.flatten_function_call(
functions_flattened,
arguments_flattened,
statements_flattened,
&fun_id,
var_types,
&exprs,
);
let mut iterator = rhs_flattened.expressions.into_iter();
@ -1369,16 +1294,6 @@ impl Flattener {
for index in 0..size {
let debug_name = format!("{}_c{}", v.id, index);
let var = self.use_variable(&debug_name);
// handle return of function call
let var_to_replace =
self.get_latest_var_substitution(&debug_name);
if !(var == var_to_replace)
&& self.variables.contains(&var_to_replace)
&& !self.substitution.contains_key(&var_to_replace)
{
self.substitution
.insert(var_to_replace.clone(), var.clone());
}
statements_flattened.push(FlatStatement::Definition(
var,
iterator.next().unwrap(),
@ -1388,16 +1303,6 @@ impl Flattener {
Type::Boolean | Type::FieldElement => {
let debug_name = v.id;
let var = self.use_variable(&debug_name);
// handle return of function call
let var_to_replace =
self.get_latest_var_substitution(&debug_name);
if !(var == var_to_replace)
&& self.variables.contains(&var_to_replace)
&& !self.substitution.contains_key(&var_to_replace)
{
self.substitution
.insert(var_to_replace.clone(), var.clone());
}
statements_flattened.push(FlatStatement::Definition(
var,
iterator.next().unwrap(),
@ -1425,9 +1330,6 @@ impl Flattener {
functions_flattened: &mut Vec<FlatFunction<T>>,
funct: TypedFunction<T>,
) -> FlatFunction<T> {
self.variables = HashSet::new();
self.substitution = HashMap::new();
self.bijection = BiMap::new();
self.next_var_idx = 0;
@ -1532,15 +1434,7 @@ impl Flattener {
fn get_latest_var_substitution(&mut self, name: &String) -> FlatVariable {
// start with the variable name
let latest_var = self.bijection.get_by_left(name).unwrap().clone();
// loop {
// // walk the substitutions
// match self.substitution.get(&latest_var) {
// Some(x) => latest_var = x,
// None => break,
// }
// }
latest_var
self.bijection.get_by_left(name).unwrap().clone()
}
}

View file

@ -103,7 +103,7 @@ impl Optimizer {
// filter out synonyms definitions
FlatStatement::Definition(_, FlatExpression::Identifier(_)) => None,
// substitute all other statements
_ => Some(statement.apply_direct_substitution(&self.substitution)),
_ => Some(statement.apply_substitution(&self.substitution)),
}
})
.collect();
@ -112,7 +112,7 @@ impl Optimizer {
let optimized_arguments = funct
.arguments
.into_iter()
.map(|arg| arg.apply_direct_substitution(&self.substitution))
.map(|arg| arg.apply_substitution(&self.substitution))
.collect();
FlatFunction {