diff --git a/Makefile b/Makefile index 6cf5f415..9d729be1 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,20 @@ all: build test build: init - cargo build --release --example circ && ./scripts/build_mpc_zokrates_test.zsh && ./scripts/build_aby.zsh + cargo build --release --example circ && ./scripts/build_mpc_zokrates_test.zsh && ./scripts/build_mpc_c_test.zsh && ./scripts/build_aby.zsh test: - cargo test && ./scripts/zokrates_test.zsh && python3 ./scripts/aby_tests/zokrates_test_aby.py && ./scripts/test_zok_to_ilp.zsh + cargo test && ./scripts/zokrates_test.zsh && python3 ./scripts/aby_tests/zokrates_test_aby.py && python3 ./scripts/aby_tests/c_test_aby.py && ./scripts/test_zok_to_ilp.zsh init: git submodule update --init -aby: +c_aby: ./scripts/build_mpc_c_test.zsh && ./scripts/build_aby.zsh && python3 ./scripts/aby_tests/c_test_aby.py +z_aby: + ./scripts/build_mpc_zokrates_test.zsh && ./scripts/build_aby.zsh && python3 ./scripts/aby_tests/zokrates_test_aby.py + clean: # remove all generated files touch ./third_party/ABY/build && rm -r -- ./third_party/ABY/build diff --git a/examples/C/mpc/unit_tests/ite_tests/2pc_ite_ret_bool.c b/examples/C/mpc/unit_tests/ite_tests/2pc_ite_ret_bool.c new file mode 100644 index 00000000..39816a81 --- /dev/null +++ b/examples/C/mpc/unit_tests/ite_tests/2pc_ite_ret_bool.c @@ -0,0 +1,9 @@ +#include + +bool main(__attribute__((private(0))) bool a, __attribute__((private(1))) bool b, __attribute__((public)) bool sel) { + if (sel) { + return a; + } else { + return b; + } +} diff --git a/examples/C/mpc/unit_tests/ite_tests/2pc_ite_ret_int.c b/examples/C/mpc/unit_tests/ite_tests/2pc_ite_ret_int.c new file mode 100644 index 00000000..86b307dc --- /dev/null +++ b/examples/C/mpc/unit_tests/ite_tests/2pc_ite_ret_int.c @@ -0,0 +1,7 @@ +int main(__attribute__((private(0))) int a, __attribute__((private(1))) int b, __attribute__((public)) bool sel) { + if (sel) { + return a; + } else { + return b; + } +} diff --git a/scripts/aby_tests/__pycache__/utils.cpython-38.pyc b/scripts/aby_tests/__pycache__/utils.cpython-38.pyc index f685fd98..35f5c3d2 100644 Binary files a/scripts/aby_tests/__pycache__/utils.cpython-38.pyc and b/scripts/aby_tests/__pycache__/utils.cpython-38.pyc differ diff --git a/scripts/aby_tests/c_test_aby.py b/scripts/aby_tests/c_test_aby.py index bba465a4..c969a8cd 100755 --- a/scripts/aby_tests/c_test_aby.py +++ b/scripts/aby_tests/c_test_aby.py @@ -9,7 +9,8 @@ if __name__ == "__main__": boolean_tests + \ nary_boolean_tests + \ const_arith_tests + \ - const_bool_tests # + \ + const_bool_tests + \ + ite_tests # loop_tests + \ # ite_tests + \ # function_tests + \ diff --git a/scripts/aby_tests/utils.py b/scripts/aby_tests/utils.py index 71707c5c..133c9ab6 100644 --- a/scripts/aby_tests/utils.py +++ b/scripts/aby_tests/utils.py @@ -72,6 +72,7 @@ def run_tests(lang: str, tests: List[dict]): 4. server arguments: dict[name] = value 5. client arguments: dict[name] = value ''' + print("Running tests for frontend", lang) failed_test_descs = [] num_retries = 3 progress_inc = 5 diff --git a/scripts/build_mpc_c_test.zsh b/scripts/build_mpc_c_test.zsh index cec3742e..a9b4be29 100755 --- a/scripts/build_mpc_c_test.zsh +++ b/scripts/build_mpc_c_test.zsh @@ -55,3 +55,7 @@ mpc_test 2 ./examples/C/mpc/unit_tests/nary_boolean_tests/2pc_nary_boolean_and.c mpc_test 2 ./examples/C/mpc/unit_tests/const_tests/2pc_const_arith.c mpc_test 2 ./examples/C/mpc/unit_tests/const_tests/2pc_const_bool.c +# build ite tests +mpc_test 2 ./examples/C/mpc/unit_tests/ite_tests/2pc_ite_ret_bool.c +# mpc_test 2 ./examples/C/mpc/unit_tests/ite_tests/2pc_ite_ret_int.c + diff --git a/scripts/build_mpc_zokrates_test.zsh b/scripts/build_mpc_zokrates_test.zsh index 6939c335..1bda72d8 100755 --- a/scripts/build_mpc_zokrates_test.zsh +++ b/scripts/build_mpc_zokrates_test.zsh @@ -51,14 +51,14 @@ mpc_test 2 ./examples/ZoKrates/mpc/unit_tests/boolean_tests/2pc_boolean_equals.z # build mpc nary boolean tests mpc_test 2 ./examples/ZoKrates/mpc/unit_tests/nary_boolean_tests/2pc_nary_boolean_and.zok -# build ite tests -mpc_test 2 ./examples/ZoKrates/mpc/unit_tests/ite_tests/2pc_ite_ret_bool.zok -mpc_test 2 ./examples/ZoKrates/mpc/unit_tests/ite_tests/2pc_ite_ret_int.zok - # build mpc const tests mpc_test 2 ./examples/ZoKrates/mpc/unit_tests/const_tests/2pc_const_arith.zok mpc_test 2 ./examples/ZoKrates/mpc/unit_tests/const_tests/2pc_const_bool.zok +# build ite tests +mpc_test 2 ./examples/ZoKrates/mpc/unit_tests/ite_tests/2pc_ite_ret_bool.zok +mpc_test 2 ./examples/ZoKrates/mpc/unit_tests/ite_tests/2pc_ite_ret_int.zok + # build mpc array tests # mpc_test 2 ./examples/ZoKrates/mpc/unit_tests/array_tests/2pc_array_sum.zok # mpc_test 2 ./examples/ZoKrates/mpc/unit_tests/array_tests/2pc_array_ret.zok diff --git a/src/front/c/mod.rs b/src/front/c/mod.rs index 304b1833..7547580c 100644 --- a/src/front/c/mod.rs +++ b/src/front/c/mod.rs @@ -209,11 +209,8 @@ impl CGen { } } } - println!("EXPR: {}", expr.clone().unwrap()); let res = self.circ.declare_init(name.clone(), ty.unwrap(), Val::Term(expr.unwrap())); self.unwrap(res); - - println!("VALUE: {:#?}", self.circ.get_value(Loc::local(name.clone()))); } fn gen_stmt(&mut self, stmt: Statement) { @@ -236,6 +233,14 @@ impl CGen { } } } + Statement::If(node) => { + println!("{:#?}", node.node); + let cond = self.gen_expr(node.node.condition.node); + self.gen_stmt(node.node.then_statement.node); + if let Some(f_cond) = node.node.else_statement { + self.gen_stmt(f_cond.node); + } + } Statement::Return(ret) => { match ret { Some(expr) => { @@ -243,9 +248,13 @@ impl CGen { let ret_res = self.circ.return_(Some(ret)); self.unwrap(ret_res); } - None => {} + None => { + let ret_res = self.circ.return_(None); + self.unwrap(ret_res); + } }; } + Statement::Expression(_expr) => {} _ => unimplemented!("Expression {:#?} hasn't been implemented", stmt), } diff --git a/src/front/c/term.rs b/src/front/c/term.rs index 1f04912e..65e71566 100644 --- a/src/front/c/term.rs +++ b/src/front/c/term.rs @@ -316,6 +316,35 @@ pub fn const_int(a: CTerm) -> Result { s.ok_or_else(|| format!("{} is not a constant integer", a)) } +pub fn bool(a: CTerm) -> Result { + match a.term { + CTermData::CBool(b) => Ok(b), + a => Err(format!("{} is not a boolean", a)), + } +} + +fn ite(c: Term, a: CTerm, b: CTerm) -> Result { + match (a.term, b.term) { + (CTermData::CInt(na, a), CTermData::CInt(nb, b)) if na == nb => Ok( + CTerm { + term: CTermData::CInt(na, term![Op::Ite; c, a, b]), + udef: false, + } + ), + (CTermData::CBool(a), CTermData::CBool(b)) => Ok( + CTerm { + term: CTermData::CBool(term![Op::Ite; c, a, b]), + udef: false, + } + ), + (x, y) => Err(format!("Cannot perform ITE on {} and {}", x, y)), + } +} + +pub fn cond(c: CTerm, a: CTerm, b: CTerm) -> Result { + ite(bool(c)?, a, b) +} + pub struct Ct { values: Option>, }