From 7af5c69940d9db021bfa44b27ceed3e92cc79841 Mon Sep 17 00:00:00 2001 From: Edward Chen Date: Tue, 28 Sep 2021 20:27:50 -0400 Subject: [PATCH] ite... doesn't work yet --- Makefile | 9 ++++-- .../unit_tests/ite_tests/2pc_ite_ret_bool.c | 9 ++++++ .../unit_tests/ite_tests/2pc_ite_ret_int.c | 7 +++++ .../__pycache__/utils.cpython-38.pyc | Bin 3746 -> 3786 bytes scripts/aby_tests/c_test_aby.py | 3 +- scripts/aby_tests/utils.py | 1 + scripts/build_mpc_c_test.zsh | 4 +++ scripts/build_mpc_zokrates_test.zsh | 8 ++--- src/front/c/mod.rs | 17 +++++++--- src/front/c/term.rs | 29 ++++++++++++++++++ 10 files changed, 75 insertions(+), 12 deletions(-) create mode 100644 examples/C/mpc/unit_tests/ite_tests/2pc_ite_ret_bool.c create mode 100644 examples/C/mpc/unit_tests/ite_tests/2pc_ite_ret_int.c 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 f685fd98ce8e3ed939a86b7890565994b90e40c8..35f5c3d258bb53704872779347177851567a8727 100644 GIT binary patch delta 377 zcmX9)y-EW?5T4ol-@DZ=Cc!|oOOqE61i{)OA)OY&CP4xh%#DrA67YHvueC_5ZN$nq z5DVYK7qE8jvdw-!1M_|J+WYRAi7^`FolM5pr|%}z>n;KiSY#*_!W^8k%6ZmvJ|QW_ z64~!Ui3sG?>>RG%E|m*hC~jDy^%klak#BttZP0-OnSH<=b`b@F!j5PzrciLyfqIim zDB?u;T8XKgrnsST!%~TnQNmucuI&tG8%-%k9JA)M^tTe{_JuOeN-VxuZ8S%<#sT{+ zu)#vDhGa^RsUhvvzl2p1ws!ucZqgJI?RY0XVbPKf+NPU*R$JV)u-O;}=3|hy@$kMo vy36yMeBP&NK}CPIT=i$GWlq!FR%h9xpU7FOpX{PKGSBg_K60Y5`ZT})O?O#i delta 334 zcmX|7y-EW?5T2de+q=En)l`}Y>Cy;xzCsEc8-tw|!a|BvCR5H5aC;FC`^4Ht?0o { + 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>, }