diff --git a/examples/C/mpc/playground.c b/examples/C/mpc/playground.c index 2eba6acf..7c3c408e 100644 --- a/examples/C/mpc/playground.c +++ b/examples/C/mpc/playground.c @@ -1,13 +1,11 @@ -void bar(int *x){ - x[0] += 1; - x[1] += 1; +int foo(int* x){ + int ret = 0; + for(int i = 0; i < 4; i++){ + ret += x[i]; + } + return ret; } -void foo(int* x){ - bar(x); -} - -int main(__attribute__((private(0))) int a[2], __attribute__((private(1))) int b[2]) { - foo(a); - return a[0] + b[0]; +int main(__attribute__((private(0))) int a[4], __attribute__((private(1))) int b) { + return foo(a) + b; } \ No newline at end of file diff --git a/examples/circ.rs b/examples/circ.rs index fc663a6f..96bd1efc 100644 --- a/examples/circ.rs +++ b/examples/circ.rs @@ -266,6 +266,7 @@ fn main() { Opt::Tuple, Opt::ConstantFold(Box::new(ignore.clone())), Opt::Ite, + Opt::ConstantIndex, // Inline Function Calls // Opt::Link, Opt::Tuple, @@ -303,14 +304,12 @@ fn main() { println!("LOG: Optimizations: {:#?}", now.elapsed()); println!("Done with IR optimization"); - // for (name, c) in &cs.computations { - // println!("name: {}", name); - // for t in c.terms_postorder() { - // println!("t: {}", t); - // } - // } - - // todo!("hello"); + for (name, c) in &cs.computations { + println!("name: {}", name); + for t in c.terms_postorder() { + println!("t: {}", t); + } + } now = Instant::now(); match options.backend { diff --git a/scripts/aby_tests/c_test_aby.py b/scripts/aby_tests/c_test_aby.py index bb2d6973..ffb32194 100755 --- a/scripts/aby_tests/c_test_aby.py +++ b/scripts/aby_tests/c_test_aby.py @@ -30,11 +30,10 @@ if __name__ == "__main__": mnist_tests + \ cryptonets_tests + \ histogram_tests + \ - gcd_tests + \ - psi_tests + gcd_tests + # tests = ts - # tests = biomatch_tests # TODO: add support unsigned + int promotion diff --git a/scripts/build_mpc_c_test.zsh b/scripts/build_mpc_c_test.zsh index 39a1380b..d1d851cf 100755 --- a/scripts/build_mpc_c_test.zsh +++ b/scripts/build_mpc_c_test.zsh @@ -91,105 +91,105 @@ function mpc_test_9 { RUST_BACKTRACE=1 measure_time $BIN --parties $parties $cpath mpc --cost-model "hycc" --selection-scheme "smart_lp" --num-parts 96 --mut-level 2 --mut-step-size 1 --graph-type 0 } -# mpc_test 2 ./examples/C/mpc/playground.c +mpc_test_css 2 ./examples/C/mpc/playground.c -# build mpc arithmetic tests -mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_add.c -mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_sub.c -mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_mult.c -mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_mult_add_pub.c -mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_mod.c -# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_add_unsigned.c +# # build mpc arithmetic tests +# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_add.c +# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_sub.c +# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_mult.c +# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_mult_add_pub.c +# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_mod.c +# # mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_add_unsigned.c -mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_int_equals.c -mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_int_greater_than.c -mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_int_greater_equals.c -mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_int_less_than.c -mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_int_less_equals.c +# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_int_equals.c +# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_int_greater_than.c +# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_int_greater_equals.c +# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_int_less_than.c +# mpc_test 2 ./examples/C/mpc/unit_tests/arithmetic_tests/2pc_int_less_equals.c -# build nary arithmetic tests -mpc_test 2 ./examples/C/mpc/unit_tests/nary_arithmetic_tests/2pc_nary_arithmetic_add.c +# # build nary arithmetic tests +# mpc_test 2 ./examples/C/mpc/unit_tests/nary_arithmetic_tests/2pc_nary_arithmetic_add.c -# build bitwise tests -mpc_test 2 ./examples/C/mpc/unit_tests/bitwise_tests/2pc_bitwise_and.c -mpc_test 2 ./examples/C/mpc/unit_tests/bitwise_tests/2pc_bitwise_or.c -mpc_test 2 ./examples/C/mpc/unit_tests/bitwise_tests/2pc_bitwise_xor.c +# # build bitwise tests +# mpc_test 2 ./examples/C/mpc/unit_tests/bitwise_tests/2pc_bitwise_and.c +# mpc_test 2 ./examples/C/mpc/unit_tests/bitwise_tests/2pc_bitwise_or.c +# mpc_test 2 ./examples/C/mpc/unit_tests/bitwise_tests/2pc_bitwise_xor.c -# build boolean tests -mpc_test 2 ./examples/C/mpc/unit_tests/boolean_tests/2pc_boolean_and.c -mpc_test 2 ./examples/C/mpc/unit_tests/boolean_tests/2pc_boolean_or.c -mpc_test 2 ./examples/C/mpc/unit_tests/boolean_tests/2pc_boolean_equals.c +# # build boolean tests +# mpc_test 2 ./examples/C/mpc/unit_tests/boolean_tests/2pc_boolean_and.c +# mpc_test 2 ./examples/C/mpc/unit_tests/boolean_tests/2pc_boolean_or.c +# mpc_test 2 ./examples/C/mpc/unit_tests/boolean_tests/2pc_boolean_equals.c -# build nary boolean tests -mpc_test 2 ./examples/C/mpc/unit_tests/nary_boolean_tests/2pc_nary_boolean_and.c +# # build nary boolean tests +# mpc_test 2 ./examples/C/mpc/unit_tests/nary_boolean_tests/2pc_nary_boolean_and.c -# build const tests -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 const tests +# 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 if statement 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 -mpc_test 2 ./examples/C/mpc/unit_tests/ite_tests/2pc_ite_only_if.c +# # build if statement 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 +# mpc_test 2 ./examples/C/mpc/unit_tests/ite_tests/2pc_ite_only_if.c -# build shift tests -mpc_test 2 ./examples/C/mpc/unit_tests/shift_tests/2pc_lhs.c -mpc_test 2 ./examples/C/mpc/unit_tests/shift_tests/2pc_rhs.c +# # build shift tests +# mpc_test 2 ./examples/C/mpc/unit_tests/shift_tests/2pc_lhs.c +# mpc_test 2 ./examples/C/mpc/unit_tests/shift_tests/2pc_rhs.c -# build div tests -mpc_test 2 ./examples/C/mpc/unit_tests/div_tests/2pc_div.c +# # build div tests +# mpc_test 2 ./examples/C/mpc/unit_tests/div_tests/2pc_div.c -# build array tests -mpc_test 2 ./examples/C/mpc/unit_tests/array_tests/2pc_array_sum.c -mpc_test 2 ./examples/C/mpc/unit_tests/array_tests/2pc_array_index.c -mpc_test 2 ./examples/C/mpc/unit_tests/array_tests/2pc_array_index_2.c -mpc_test 2 ./examples/C/mpc/unit_tests/array_tests/2pc_array_index_3.c +# # build array tests +# mpc_test 2 ./examples/C/mpc/unit_tests/array_tests/2pc_array_sum.c +# mpc_test 2 ./examples/C/mpc/unit_tests/array_tests/2pc_array_index.c +# mpc_test 2 ./examples/C/mpc/unit_tests/array_tests/2pc_array_index_2.c +# mpc_test 2 ./examples/C/mpc/unit_tests/array_tests/2pc_array_index_3.c -# build circ/compiler array tests -mpc_test 2 ./examples/C/mpc/unit_tests/c_array_tests/2pc_array.c -mpc_test 2 ./examples/C/mpc/unit_tests/c_array_tests/2pc_array_1.c -mpc_test 2 ./examples/C/mpc/unit_tests/c_array_tests/2pc_array_2.c -mpc_test 2 ./examples/C/mpc/unit_tests/c_array_tests/2pc_array_3.c -mpc_test 2 ./examples/C/mpc/unit_tests/c_array_tests/2pc_array_sum_c.c +# # build circ/compiler array tests +# mpc_test 2 ./examples/C/mpc/unit_tests/c_array_tests/2pc_array.c +# mpc_test 2 ./examples/C/mpc/unit_tests/c_array_tests/2pc_array_1.c +# mpc_test 2 ./examples/C/mpc/unit_tests/c_array_tests/2pc_array_2.c +# mpc_test 2 ./examples/C/mpc/unit_tests/c_array_tests/2pc_array_3.c +# mpc_test 2 ./examples/C/mpc/unit_tests/c_array_tests/2pc_array_sum_c.c -# build function tests -mpc_test 2 ./examples/C/mpc/unit_tests/function_tests/2pc_function_add.c +# # build function tests +# mpc_test 2 ./examples/C/mpc/unit_tests/function_tests/2pc_function_add.c -# build struct tests -mpc_test 2 ./examples/C/mpc/unit_tests/struct_tests/2pc_struct_add.c -mpc_test 2 ./examples/C/mpc/unit_tests/struct_tests/2pc_struct_array_add.c -mpc_test 2 ./examples/C/mpc/unit_tests/struct_tests/ret_struct.c +# # build struct tests +# mpc_test 2 ./examples/C/mpc/unit_tests/struct_tests/2pc_struct_add.c +# mpc_test 2 ./examples/C/mpc/unit_tests/struct_tests/2pc_struct_array_add.c +# mpc_test 2 ./examples/C/mpc/unit_tests/struct_tests/ret_struct.c -# build matrix tests -mpc_test 2 ./examples/C/mpc/unit_tests/matrix_tests/2pc_matrix_add.c -mpc_test 2 ./examples/C/mpc/unit_tests/matrix_tests/2pc_matrix_assign_add.c -mpc_test 2 ./examples/C/mpc/unit_tests/matrix_tests/2pc_matrix_ptr_add.c +# # build matrix tests +# mpc_test 2 ./examples/C/mpc/unit_tests/matrix_tests/2pc_matrix_add.c +# mpc_test 2 ./examples/C/mpc/unit_tests/matrix_tests/2pc_matrix_assign_add.c +# mpc_test 2 ./examples/C/mpc/unit_tests/matrix_tests/2pc_matrix_ptr_add.c -# build ptr tests -mpc_test 2 ./examples/C/mpc/unit_tests/ptr_tests/2pc_ptr_add.c -mpc_test 2 ./examples/C/mpc/unit_tests/ptr_tests/2pc_ptr_arith.c +# # build ptr tests +# mpc_test 2 ./examples/C/mpc/unit_tests/ptr_tests/2pc_ptr_add.c +# mpc_test 2 ./examples/C/mpc/unit_tests/ptr_tests/2pc_ptr_arith.c -# build misc tests -mpc_test 2 ./examples/C/mpc/unit_tests/misc_tests/2pc_millionaires.c -mpc_test 2 ./examples/C/mpc/unit_tests/misc_tests/2pc_multi_var.c +# # build misc tests +# mpc_test 2 ./examples/C/mpc/unit_tests/misc_tests/2pc_millionaires.c +# mpc_test 2 ./examples/C/mpc/unit_tests/misc_tests/2pc_multi_var.c -# build hycc benchmarks -mpc_test_2 2 ./examples/C/mpc/benchmarks/biomatch/2pc_biomatch.c -mpc_test_2 2 ./examples/C/mpc/benchmarks/biomatch/biomatch.c -mpc_test_2 2 ./examples/C/mpc/benchmarks/kmeans/2pc_kmeans_.c -mpc_test_2 2 ./examples/C/mpc/benchmarks/gauss/2pc_gauss_inline.c -mpc_test_2 2 ./examples/C/mpc/benchmarks/db/db_join.c -mpc_test_2 2 ./examples/C/mpc/benchmarks/db/db_join2.c -mpc_test_2 2 ./examples/C/mpc/benchmarks/db/db_merge.c -mpc_test_2 2 ./examples/C/mpc/benchmarks/mnist/mnist_16.c -mpc_test_2 2 ./examples/C/mpc/benchmarks/mnist/mnist_decomp_main_16.c -mpc_test_2 2 ./examples/C/mpc/benchmarks/mnist/mnist_decomp_convolution.c -mpc_test_2 2 ./examples/C/mpc/benchmarks/cryptonets/cryptonets_16.c +# # build hycc benchmarks +# mpc_test_2 2 ./examples/C/mpc/benchmarks/biomatch/2pc_biomatch.c +# mpc_test_2 2 ./examples/C/mpc/benchmarks/biomatch/biomatch.c +# mpc_test_2 2 ./examples/C/mpc/benchmarks/kmeans/2pc_kmeans_.c +# mpc_test_2 2 ./examples/C/mpc/benchmarks/gauss/2pc_gauss_inline.c +# mpc_test_2 2 ./examples/C/mpc/benchmarks/db/db_join.c +# mpc_test_2 2 ./examples/C/mpc/benchmarks/db/db_join2.c +# mpc_test_2 2 ./examples/C/mpc/benchmarks/db/db_merge.c +# mpc_test_2 2 ./examples/C/mpc/benchmarks/mnist/mnist_16.c +# mpc_test_2 2 ./examples/C/mpc/benchmarks/mnist/mnist_decomp_main_16.c +# mpc_test_2 2 ./examples/C/mpc/benchmarks/mnist/mnist_decomp_convolution.c +# mpc_test_2 2 ./examples/C/mpc/benchmarks/cryptonets/cryptonets_16.c -# build OPA benchmarks -mpc_test_2 2 ./examples/C/mpc/benchmarks/histogram/histogram.c -mpc_test_2 2 ./examples/C/mpc/benchmarks/gcd/gcd.c -mpc_test_2 2 ./examples/C/mpc/benchmarks/psi/psi.c +# # build OPA benchmarks +# mpc_test_2 2 ./examples/C/mpc/benchmarks/histogram/histogram.c +# mpc_test_2 2 ./examples/C/mpc/benchmarks/gcd/gcd.c +# mpc_test_2 2 ./examples/C/mpc/benchmarks/psi/psi.c # # # mpc_test 2 ./examples/C/mpc/benchmarks/kmeans/2pc_kmeans.c diff --git a/src/ir/opt/cindex.rs b/src/ir/opt/cindex.rs new file mode 100644 index 00000000..731a5f8f --- /dev/null +++ b/src/ir/opt/cindex.rs @@ -0,0 +1,71 @@ +//! Rewrite const indexed store and select terms + +use crate::ir::opt::visit::RewritePass; +use crate::ir::term::*; + +/// ITE cache. +#[derive(Default)] +struct ConstIndexer; + +impl RewritePass for ConstIndexer { + fn visit Vec>( + &mut self, + _computation: &mut Computation, + orig: &Term, + rewritten_children: F, + ) -> Option { + let cs = rewritten_children(); + match &orig.op { + Op::Store => { + let array = cs[0].as_array_opt(); + let index = cs[1].clone(); + let value = cs[2].clone(); + match array { + Some(a) => { + if index.is_const() && value.is_const() { + let i = index.as_value_opt().unwrap(); + let v = value.as_value_opt().unwrap(); + Some(leaf_term(Op::Const(Value::Array(a.clone().store(i.clone(), v.clone()))))) + } else if index.is_const() { + if let Value::BitVector(b) = index.as_value_opt().unwrap() { + let mut array_terms = a.to_terms(); + array_terms[b.as_sint().to_usize().unwrap()] = value; + Some(term(Op::Array, array_terms)) + } else { + None + } + } else { + None + } + }, + None => { + // store on array term? + match &cs[0].op { + Op::Array => { + if index.is_const() { + if let Value::BitVector(b) = index.as_value_opt().unwrap() { + let mut array_terms = cs[0].cs.clone(); + array_terms[b.as_sint().to_usize().unwrap()] = value; + Some(term(Op::Array, array_terms)) + } else { + None + } + } else { + None + } + } + _ => None + } + } + } + } + _ => None, + } + } +} + +/// Binarize (expand) n-ary terms. +pub fn cindex(c: &mut Computation) { + let mut pass = ConstIndexer; + pass.traverse(c); +} diff --git a/src/ir/opt/mod.rs b/src/ir/opt/mod.rs index 455a43e1..0f4f91ce 100644 --- a/src/ir/opt/mod.rs +++ b/src/ir/opt/mod.rs @@ -4,6 +4,7 @@ pub mod cfold; pub mod flat; pub mod inline; pub mod ite; +pub mod cindex; pub mod link; pub mod mem; pub mod scalarize_vars; @@ -41,6 +42,8 @@ pub enum Opt { Inline, /// Ite peephole optimizations Ite, + /// Ite peephole optimizations + ConstantIndex, /// Link function calls Link, /// Eliminate tuples @@ -114,6 +117,9 @@ pub fn opt>(mut fs: Functions, optimizations: I) -> Opt::Ite => { ite::rewrite_ites(comp); } + Opt::ConstantIndex => { + cindex::cindex(comp); + } Opt::Inline => { let public_inputs = comp .metadata diff --git a/src/ir/term/mod.rs b/src/ir/term/mod.rs index e94cc205..ec5351df 100644 --- a/src/ir/term/mod.rs +++ b/src/ir/term/mod.rs @@ -131,6 +131,9 @@ pub enum Op { /// Makes an array equal to `array`, but with `value` at `index`. Store, + /// IR array representation + Array, + /// Assemble n things into a tuple Tuple, /// Get the n'th element of a tuple @@ -252,6 +255,7 @@ impl Op { Op::UbvToPf(_) => Some(1), Op::Select => Some(2), Op::Store => Some(3), + Op::Array => None, Op::Tuple => None, Op::Field(_) => Some(1), Op::Update(_) => Some(2), @@ -296,6 +300,7 @@ impl Display for Op { Op::UbvToPf(a) => write!(f, "(bv2pf {})", a.modulus()), Op::Select => write!(f, "select"), Op::Store => write!(f, "store"), + Op::Array => write!(f, "array"), Op::Tuple => write!(f, "tuple"), Op::Field(i) => write!(f, "(field {})", i), Op::Update(i) => write!(f, "(update {})", i), @@ -786,6 +791,16 @@ impl Array { self.check_idx(idx); self.map.get(idx).unwrap_or(&*self.default).clone() } + + /// to_terms + pub fn to_terms(&self) -> Vec { + let mut ret = Vec::new(); + for i in 0..self.size { + let r = self.map.get(bv_lit(i, 32).as_value_opt().unwrap()).unwrap_or(&*self.default).clone(); + ret.push(leaf_term(Op::Const(r))); + } + return ret; + } } impl Display for Value { diff --git a/src/ir/term/ty.rs b/src/ir/term/ty.rs index 3d901917..f4f7723b 100644 --- a/src/ir/term/ty.rs +++ b/src/ir/term/ty.rs @@ -58,6 +58,7 @@ fn check_dependencies(t: &Term) -> Vec { Op::UbvToPf(_) => Vec::new(), Op::Select => vec![t.cs[0].clone()], Op::Store => vec![t.cs[0].clone()], + Op::Array => vec![t.cs[0].clone()], Op::Tuple => t.cs.clone(), Op::Field(_) => vec![t.cs[0].clone()], Op::Update(_i) => vec![t.cs[0].clone()],