diff --git a/examples/C/mpc/benchmarks/kmeans.c b/examples/C/mpc/benchmarks/kmeans.c new file mode 100644 index 00000000..6a872ce5 --- /dev/null +++ b/examples/C/mpc/benchmarks/kmeans.c @@ -0,0 +1,23 @@ +// #define D 2 // Dimension (fix) +// #define NA 100 // Number of data points from Party A +// #define NB 100 // Number of data points from Party B +// #define NC 5 // Number of clusters +// #define PRECISION 4 + +// #define LEN (NA+NB) +// #define LEN_OUTER 10 +// #define LEN_INNER (LEN/LEN_OUTER) + +int main(__attribute__((private(0))) int[3] a, __attribute__((private(1))) int[3] b) { + // input variables + int D = 2; + int NA = 100; + int NB = 100; + int NC = 5; + int PRECISION = 4; + int LEN = NA + NB; + int LEN_OUTER = 10; + int LEN_INNER = 20; + + int data[LEN * D]; +} diff --git a/examples/C/mpc/unit_tests/array_tests/2pc_array_sum.c b/examples/C/mpc/unit_tests/array_tests/2pc_array_sum.c index bddf3264..2a826e02 100644 --- a/examples/C/mpc/unit_tests/array_tests/2pc_array_sum.c +++ b/examples/C/mpc/unit_tests/array_tests/2pc_array_sum.c @@ -1,6 +1,4 @@ int main(__attribute__((private(0))) int a, __attribute__((private(1))) int b) { - // int c[2][2] = {{a, b}, {a, b}}; - // return c[0][0] + c[1][1]; int arr[2] = {a, b}; return arr[0] + arr[1]; } \ No newline at end of file diff --git a/examples/C/mpc/unit_tests/c_array_tests/2pc_array_sum_c.c b/examples/C/mpc/unit_tests/c_array_tests/2pc_array_sum_c.c new file mode 100644 index 00000000..3b01709d --- /dev/null +++ b/examples/C/mpc/unit_tests/c_array_tests/2pc_array_sum_c.c @@ -0,0 +1,11 @@ +int main(__attribute__((private(0))) int a[2], __attribute__((private(1))) int b[2]) { + int sum[2]; + for (int i = 0; i < 2; i++) { + sum[i] = a[i] + b[i]; + } + int acc = 0; + for (int j = 0; j < 2; j++) { + acc += sum[j]; + } + return acc; +} \ No newline at end of file diff --git a/scripts/aby_tests/test_suite.py b/scripts/aby_tests/test_suite.py index c5eed75e..e8eaec16 100644 --- a/scripts/aby_tests/test_suite.py +++ b/scripts/aby_tests/test_suite.py @@ -490,6 +490,13 @@ c_array_tests = [ {"a": 2, "b": 0}, {"a": 0, "b": 3}, ], + [ + "C array test 3", + 6, + "./third_party/ABY/build/bin/2pc_array_3", + {"a": [1,2], "b": 0}, + {"a": 0, "b": [1,2]}, + ], ] loop_tests = [ diff --git a/scripts/build_mpc_c_test.zsh b/scripts/build_mpc_c_test.zsh index 42441994..f6cf39a3 100755 --- a/scripts/build_mpc_c_test.zsh +++ b/scripts/build_mpc_c_test.zsh @@ -23,50 +23,53 @@ function mpc_test { RUST_BACKTRACE=1 measure_time $BIN -p $parties $zpath } -# build mpc arithmetic tests +# # 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_add_unsigned.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_add_unsigned.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_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 mpc nary arithmetic tests -mpc_test 2 ./examples/C/mpc/unit_tests/nary_arithmetic_tests/2pc_nary_arithmetic_add.c +# # build mpc nary arithmetic tests +# mpc_test 2 ./examples/C/mpc/unit_tests/nary_arithmetic_tests/2pc_nary_arithmetic_add.c -# build mpc 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 mpc 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 mpc 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 mpc 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 mpc nary boolean tests -mpc_test 2 ./examples/C/mpc/unit_tests/nary_boolean_tests/2pc_nary_boolean_and.c +# # build mpc nary boolean tests +# mpc_test 2 ./examples/C/mpc/unit_tests/nary_boolean_tests/2pc_nary_boolean_and.c -# build mpc 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 mpc 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 +# # 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 -# 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 +# # 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 + +# # 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 diff --git a/src/circify/mod.rs b/src/circify/mod.rs index c0a39bc1..59e14a7c 100644 --- a/src/circify/mod.rs +++ b/src/circify/mod.rs @@ -468,6 +468,8 @@ impl Circify { visibility: Option, ) -> Result<()> { let ssa_name = self.declare_env_name(name.clone(), ty)?.clone(); + println!("SSA_NAME: {}", ssa_name); + println!("NAME: {}", name); let t = self.e.declare( &mut self.cir_ctx, ty, @@ -475,6 +477,7 @@ impl Circify { if input { Some(name) } else { None }, visibility, ); + println!("TERM: {:#?}", t); assert!(self.vals.insert(ssa_name, Val::Term(t)).is_none()); Ok(()) } diff --git a/src/front/c/mod.rs b/src/front/c/mod.rs index 1e5c83b5..57815c8d 100644 --- a/src/front/c/mod.rs +++ b/src/front/c/mod.rs @@ -152,7 +152,7 @@ impl CGen { udef: false, }) } - (a, b) => Err(format!("Cannot index {} by {}", b, a)) + (a, b) => Err(format!("[Array Select] cannot index {} by {}", b, a)) } } @@ -165,7 +165,7 @@ impl CGen { mem.store(i, idx_term, new_val); Ok(val.clone()) } - (a, b) => Err(format!("Cannot index {} by {}", b, a)), + (a, b) => Err(format!("[Array Store] cannot index {} by {}", b, a)), } } @@ -588,10 +588,11 @@ impl CGen { for arg in fn_info.args.iter() { let p = &arg.specifiers[0]; let vis = self.interpret_visibility(&p.node); - let ty = d_type_(arg.specifiers[1..].to_vec()); + let base_ty = d_type_(arg.specifiers[1..].to_vec()); let d = &arg.declarator.as_ref().unwrap().node; + let derived_ty = derived_type_(base_ty.unwrap(), d.derived.to_vec()); let name = name_from_decl(d); - let r = self.circ.declare(name.clone(), &ty.unwrap(), true, vis); + let r = self.circ.declare(name.clone(), &derived_ty, true, vis); self.unwrap(r); } self.gen_stmt(fn_info.body.clone()); diff --git a/src/front/c/term.rs b/src/front/c/term.rs index e5f68147..7b764d51 100644 --- a/src/front/c/term.rs +++ b/src/front/c/term.rs @@ -388,7 +388,7 @@ pub struct Ct { values: Option>, } -fn _idx_name(struct_name: &str, idx: usize) -> String { +fn idx_name(struct_name: &str, idx: usize) -> String { format!("{}.{}", struct_name, idx) } @@ -444,7 +444,33 @@ impl Embeddable for Ct { ), udef: false, }, - _ => unimplemented!(), + Ty::Array(n, ty) => { + let v: Vec = (0..n.unwrap()) + .map(|i| { + self.declare( + ctx, + &*ty, + idx_name(&raw_name, i), + user_name.as_ref().map(|u| idx_name(u, i)), + visibility.clone(), + ) + }) + .collect(); + let mut mem = ctx.mem.borrow_mut(); + let id = mem.zero_allocate(n.unwrap(), 32, num_bits(*ty.clone())); + let arr = Self::T { + term: CTermData::CArray( + *ty.clone(), + Some(id), + ), + udef: false, + }; + for (i, t) in v.iter().enumerate() { + let val = t.term.term(&mem); + mem.store(id, bv_lit(i, 32), val); + } + arr + }, } } fn ite(&self, _ctx: &mut CirCtx, cond: Term, t: Self::T, f: Self::T) -> Self::T { diff --git a/src/ir/opt/mem/obliv.rs b/src/ir/opt/mem/obliv.rs index 30f83212..5f1de9f8 100644 --- a/src/ir/opt/mem/obliv.rs +++ b/src/ir/opt/mem/obliv.rs @@ -194,7 +194,6 @@ impl MemVisitor for Replacer { .to_usize() .expect("oversize index"); if k_const < a_seq.len() { - println!("return term: {:#?}", a_seq[k_const]); Some(a_seq[k_const].clone()) } else { panic!("Oversize index: {}", k_const) diff --git a/src/target/aby/trans.rs b/src/target/aby/trans.rs index 24befccb..07d68e4c 100644 --- a/src/target/aby/trans.rs +++ b/src/target/aby/trans.rs @@ -152,6 +152,7 @@ impl ToABY { // Initialize Server inputs self.aby.setup.push("if (role == SERVER) {".to_string()); + // TODO: add in gates based on type / number of inputs for t in server_inputs.iter() { self.aby .setup