put behind config flag
This commit is contained in:
parent
db4fcd04d8
commit
ad4717e67f
6 changed files with 68 additions and 24 deletions
|
@ -56,6 +56,10 @@ pub fn subcommand() -> App<'static, 'static> {
|
||||||
.long("allow-unconstrained-variables")
|
.long("allow-unconstrained-variables")
|
||||||
.help("Allow unconstrained variables by inserting dummy constraints")
|
.help("Allow unconstrained variables by inserting dummy constraints")
|
||||||
.required(false)
|
.required(false)
|
||||||
|
).arg(Arg::with_name("isolate-branches")
|
||||||
|
.long("isolate-branches")
|
||||||
|
.help("Isolate the execution of branches: a panic in a branch only makes the program panic if this branch is being logically executed")
|
||||||
|
.required(false)
|
||||||
).arg(Arg::with_name("ztf")
|
).arg(Arg::with_name("ztf")
|
||||||
.long("ztf")
|
.long("ztf")
|
||||||
.help("Write human readable output (ztf)")
|
.help("Write human readable output (ztf)")
|
||||||
|
@ -124,6 +128,7 @@ fn cli_compile<T: Field>(sub_matches: &ArgMatches) -> Result<(), String> {
|
||||||
|
|
||||||
let config = CompileConfig {
|
let config = CompileConfig {
|
||||||
allow_unconstrained_variables: sub_matches.is_present("allow-unconstrained-variables"),
|
allow_unconstrained_variables: sub_matches.is_present("allow-unconstrained-variables"),
|
||||||
|
isolate_branches: sub_matches.is_present("isolate-branches"),
|
||||||
};
|
};
|
||||||
|
|
||||||
let resolver = FileSystemResolver::with_stdlib_root(stdlib_path);
|
let resolver = FileSystemResolver::with_stdlib_root(stdlib_path);
|
||||||
|
|
|
@ -156,9 +156,10 @@ impl fmt::Display for CompileErrorInner {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, Serialize, Deserialize)]
|
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
|
||||||
pub struct CompileConfig {
|
pub struct CompileConfig {
|
||||||
pub allow_unconstrained_variables: bool,
|
pub allow_unconstrained_variables: bool,
|
||||||
|
pub isolate_branches: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
type FilePath = PathBuf;
|
type FilePath = PathBuf;
|
||||||
|
|
|
@ -2177,25 +2177,34 @@ impl<'ast, T: Field> Flattener<'ast, T> {
|
||||||
ZirStatement::IfElse(condition, consequence, alternative) => {
|
ZirStatement::IfElse(condition, consequence, alternative) => {
|
||||||
let condition = self.flatten_boolean_expression(statements_flattened, condition);
|
let condition = self.flatten_boolean_expression(statements_flattened, condition);
|
||||||
|
|
||||||
let mut consequence_statements = vec![];
|
if self.config.isolate_branches {
|
||||||
let mut alternative_statements = vec![];
|
let mut consequence_statements = vec![];
|
||||||
|
let mut alternative_statements = vec![];
|
||||||
|
|
||||||
consequence
|
consequence
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.for_each(|s| self.flatten_statement(&mut consequence_statements, s));
|
.for_each(|s| self.flatten_statement(&mut consequence_statements, s));
|
||||||
alternative
|
alternative
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.for_each(|s| self.flatten_statement(&mut alternative_statements, s));
|
.for_each(|s| self.flatten_statement(&mut alternative_statements, s));
|
||||||
|
|
||||||
let consequence_statements =
|
let consequence_statements =
|
||||||
self.make_conditional(consequence_statements, condition.clone());
|
self.make_conditional(consequence_statements, condition.clone());
|
||||||
let alternative_statements = self.make_conditional(
|
let alternative_statements = self.make_conditional(
|
||||||
alternative_statements,
|
alternative_statements,
|
||||||
FlatExpression::Sub(box FlatExpression::Number(T::one()), box condition),
|
FlatExpression::Sub(box FlatExpression::Number(T::one()), box condition),
|
||||||
);
|
);
|
||||||
|
|
||||||
statements_flattened.extend(consequence_statements);
|
statements_flattened.extend(consequence_statements);
|
||||||
statements_flattened.extend(alternative_statements);
|
statements_flattened.extend(alternative_statements);
|
||||||
|
} else {
|
||||||
|
consequence
|
||||||
|
.into_iter()
|
||||||
|
.for_each(|s| self.flatten_statement(statements_flattened, s));
|
||||||
|
alternative
|
||||||
|
.into_iter()
|
||||||
|
.for_each(|s| self.flatten_statement(statements_flattened, s));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ZirStatement::Definition(assignee, expr) => {
|
ZirStatement::Definition(assignee, expr) => {
|
||||||
// define n variables with n the number of primitive types for v_type
|
// define n variables with n the number of primitive types for v_type
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
{
|
{
|
||||||
"entry_point": "./tests/tests/panics/panic_isolation.zok",
|
"entry_point": "./tests/tests/panics/panic_isolation.zok",
|
||||||
|
"config": {
|
||||||
|
"allow_unconstrained_variables": false,
|
||||||
|
"isolate_branches": true
|
||||||
|
},
|
||||||
"curves": ["Bn128"],
|
"curves": ["Bn128"],
|
||||||
"tests": [
|
"tests": [
|
||||||
{
|
{
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
{
|
||||||
|
"entry_point": "./tests/tests/panics/panic_isolation.zok",
|
||||||
|
"config": {
|
||||||
|
"allow_unconstrained_variables": false,
|
||||||
|
"isolate_branches": false
|
||||||
|
},
|
||||||
|
"curves": ["Bn128"],
|
||||||
|
"tests": [
|
||||||
|
{
|
||||||
|
"input": {
|
||||||
|
"values": [
|
||||||
|
"1",
|
||||||
|
"1",
|
||||||
|
"1",
|
||||||
|
"1"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"output": {
|
||||||
|
"Err": {
|
||||||
|
"UnsatisfiedConstraint": {
|
||||||
|
"left": "1",
|
||||||
|
"right": "0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -23,6 +23,7 @@ struct Tests {
|
||||||
pub entry_point: Option<PathBuf>,
|
pub entry_point: Option<PathBuf>,
|
||||||
pub curves: Option<Vec<Curve>>,
|
pub curves: Option<Vec<Curve>>,
|
||||||
pub max_constraint_count: Option<usize>,
|
pub max_constraint_count: Option<usize>,
|
||||||
|
pub config: Option<CompileConfig>,
|
||||||
pub tests: Vec<Test>,
|
pub tests: Vec<Test>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,18 +120,14 @@ pub fn test_inner(test_path: &str) {
|
||||||
fn compile_and_run<T: Field>(t: Tests) {
|
fn compile_and_run<T: Field>(t: Tests) {
|
||||||
let entry_point = t.entry_point.unwrap();
|
let entry_point = t.entry_point.unwrap();
|
||||||
|
|
||||||
|
let config = t.config.unwrap_or_default();
|
||||||
|
|
||||||
let code = std::fs::read_to_string(&entry_point).unwrap();
|
let code = std::fs::read_to_string(&entry_point).unwrap();
|
||||||
|
|
||||||
let stdlib = std::fs::canonicalize("../zokrates_stdlib/stdlib").unwrap();
|
let stdlib = std::fs::canonicalize("../zokrates_stdlib/stdlib").unwrap();
|
||||||
let resolver = FileSystemResolver::with_stdlib_root(stdlib.to_str().unwrap());
|
let resolver = FileSystemResolver::with_stdlib_root(stdlib.to_str().unwrap());
|
||||||
|
|
||||||
let artifacts = compile::<T, _>(
|
let artifacts = compile::<T, _>(code, entry_point.clone(), Some(&resolver), &config).unwrap();
|
||||||
code,
|
|
||||||
entry_point.clone(),
|
|
||||||
Some(&resolver),
|
|
||||||
&CompileConfig::default(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let bin = artifacts.prog();
|
let bin = artifacts.prog();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue