inline all function work

This commit is contained in:
Clive2312
2022-07-21 03:10:02 +00:00
22 changed files with 362 additions and 168 deletions

View File

@@ -2,8 +2,8 @@
* Example on how to merge two data sets and to perform various analyses
*/
#define LEN_A 5
#define LEN_B 5
#define LEN_A 50
#define LEN_B 50
#define ATT_A 2 //Number of attributes
#define ATT_B 2
@@ -80,7 +80,7 @@ DT agg_mean(DT *db, int len, int att) {
return mean_with_abort(sum, len);
}
int main(__attribute__((private(0))) int a[LEN_A*ATT_A], __attribute__((private(1))) int b[LEN_B*ATT_B]) {
Output main(__attribute__((private(0))) int a[LEN_A*ATT_A], __attribute__((private(1))) int b[LEN_B*ATT_B]) {
Output res;
InputA INPUT_A;
for (int i = 0; i < LEN_A*ATT_A; i++) {
@@ -103,5 +103,5 @@ int main(__attribute__((private(0))) int a[LEN_A*ATT_A], __attribute__((private(
res.analysis1 = agg_mean_tree(db, LEN_A*LEN_B, ATT);
res.analysis2 = res.analysis1;
// res.analysis2 = variance(db, LEN_A*LEN_B);
return res.joined + res.analysis1 + res.analysis2;
return res;
}

View File

@@ -160,7 +160,7 @@ DT agg_mean_decomposed(DT *db) {
}
}
int main(__attribute__((private(0))) int a[LEN_A*ATT_A], __attribute__((private(1))) int b[LEN_B*ATT_B]) {
Output main(__attribute__((private(0))) int a[LEN_A*ATT_A], __attribute__((private(1))) int b[LEN_B*ATT_B]) {
Output res;
InputA INPUT_A;
for (int i = 0; i < LEN_A*ATT_A; i++) {
@@ -185,5 +185,5 @@ int main(__attribute__((private(0))) int a[LEN_A*ATT_A], __attribute__((private(
// res.analysis2 = agg_variance_decomposed(db, LEN_A*LEN_B, ATT);
res.analysis2 = agg_variance_decomposed(db);
//res.analysis2 = variance(db, LEN_A*LEN_B);
return res.joined + res.analysis1 + res.analysis2;
return res;
}

View File

@@ -45,7 +45,7 @@ DT variance_(DT *db) {
return res / LEN;
}
int main(__attribute__((private(0))) int a[LEN_A*ATT_A], __attribute__((private(1))) int b[LEN_B*ATT_B]) {
Output main(__attribute__((private(0))) int a[LEN_A*ATT_A], __attribute__((private(1))) int b[LEN_B*ATT_B]) {
Output res;
InputA INPUT_A;
for (int i = 0; i < LEN_A*ATT_A; i++) {
@@ -67,6 +67,6 @@ int main(__attribute__((private(0))) int a[LEN_A*ATT_A], __attribute__((private(
// res.analysis2 = variance(db, LEN);
res.analysis2 = variance_(db);
return res.analysis1 + res.analysis2;
return res;
}

View File

@@ -199,7 +199,7 @@ void gaussj_D(DT *m, DT *b, DT *OUTPUT_res) {
// // return out;
}
int main(__attribute__((private(0))) int a[N*N], __attribute__((private(1))) int b[N]) {
Output main(__attribute__((private(0))) int a[N*N], __attribute__((private(1))) int b[N]) {
InputMatrix INPUT_A_m;
InputVector INPUT_B_b;
for (int i = 0; i < N * N; i++) {
@@ -211,5 +211,5 @@ int main(__attribute__((private(0))) int a[N*N], __attribute__((private(1))) int
Output OUTPUT_res;
gaussj_D(INPUT_A_m.m, INPUT_B_b.b, OUTPUT_res.res);
return OUTPUT_res.res[0];
return OUTPUT_res;
}

View File

@@ -8,6 +8,9 @@
#define LEN_OUTER 10
#define LEN_INNER (LEN/LEN_OUTER)
typedef int coord_t;
struct input_a{
int dataA[D*NA];
};
@@ -16,9 +19,11 @@ struct input_b {
int dataB[D*NA];
};
struct output {
int cluster[D*NC];
};
typedef struct
{
coord_t cluster[D*NC];
} Output;
int dist2(int x1, int y1, int x2, int y2) {
return (x1-x2) * (x1-x2) + (y1 - y2) * (y1 - y2);
@@ -179,7 +184,7 @@ void kmeans(int *data, int *OUTPUT_res) {
}
int main(__attribute__((private(0))) int a[20], __attribute__((private(1))) int b[20])
Output main(__attribute__((private(0))) int a[20], __attribute__((private(1))) int b[20])
{
// init data
int data[LEN * D];
@@ -193,13 +198,8 @@ int main(__attribute__((private(0))) int a[20], __attribute__((private(1))) int
data[i + offset] = b[i];
}
struct output output;
Output output;
kmeans(data, output.cluster);
int sum = 0;
for (int i = 0; i < D * NC; i++)
{
sum += output.cluster[i];
}
return sum;
return output;
}

View File

@@ -545,7 +545,7 @@ void sum(DT *OUTPUT_agg, DT* agg, DT *add, int len) {
}
}
int main(
Output main(
__attribute__((private(0))) DT image[IMAGE_WIDTH * IMAGE_WIDTH],
__attribute__((private(1))) DT kernelsL1[OUTPUT_CHANNELS * WINDOW_WIDTH * WINDOW_WIDTH],
__attribute__((private(1))) DT kernelsL2[OUTPUT_CHANNELS * SIZE_KERNELS_2],
@@ -627,12 +627,5 @@ int main(
mmulT_unrolled_2(INPUT_B.kernelsFC2, fc_layer, output.final_layer);
// return output;
// int sum = 0;
// for (int i = 0; i < FINAL_OUTPUT_CHANNELS; i++) {
// sum += output.final_layer[i];
// }
// return sum;
return output.final_layer[1];
return output;
}

View File

@@ -0,0 +1,16 @@
typedef struct
{
int a;
int b;
} Output;
Output main(
__attribute__((private(0))) int a,
__attribute__((private(1))) int b)
{
Output c;
c.a = a;
c.b = b;
return c;
}

View File

@@ -4,5 +4,5 @@ from util import run_tests
from test_suite import *
if __name__ == "__main__":
tests = ts
tests = benchmark_tests
run_tests('c', tests)

View File

@@ -26,10 +26,12 @@ if __name__ == "__main__":
biomatch_tests + \
kmeans_tests_2 + \
gauss_tests + \
db_tests
db_tests + \
mnist_tests
# TODO: add support for return value - int promotion
tests = biomatch_tests
# TODO: add support unsigned + int promotion
# unsigned_arithmetic_tests + \
run_tests('c', tests)
#

View File

@@ -1,3 +1,3 @@
a 0 1 2 3 4 5 6 7 8 9
b 0 1 2 3 4 5 6 7 8 9
res 25
res 10 10 5

View File

@@ -1,3 +1,3 @@
a 2 1 3 3 7 9 0 3 1
b 14 40 6
res -1194
res -1194 0 1536

View File

@@ -1,3 +1,3 @@
a 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
b 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
res 54
res 2 0 52

View File

@@ -1,3 +1,3 @@
a 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
b 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
res 103
res 2 3 7 8 10 11 13 14 17 18

View File

@@ -1,3 +1,3 @@
a 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
b 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
res 882
res 49 833 200

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,3 @@
a 1
b 2
res 1 2

View File

@@ -1,8 +1,8 @@
ts = [
[
"new",
"playground",
"./scripts/aby_tests/test_inputs/test.txt",
"Array index test",
"2pc_array_index",
"./scripts/aby_tests/test_inputs/add.txt",
],
]
@@ -346,12 +346,16 @@ array_tests = [
"2pc_array_sum",
"./scripts/aby_tests/test_inputs/add.txt",
],
# TODO: unsupported in ABY interpreter yet.
# [
# "Array index test",
# "2pc_array_index",
# "./scripts/aby_tests/test_inputs/add.txt",
# ],
[
"Array index test",
"2pc_array_index",
"./scripts/aby_tests/test_inputs/add.txt",
],
[
"Array index test 2",
"2pc_array_index_2",
"./scripts/aby_tests/test_inputs/index.txt",
],
]
c_array_tests = [
@@ -414,6 +418,11 @@ struct_tests = [
"2pc_struct_array_add",
"./scripts/aby_tests/test_inputs/add.txt",
],
[
"Return struct",
"ret_struct",
"./scripts/aby_tests/test_inputs/ret_struct.txt",
],
]
matrix_tests = [
@@ -597,12 +606,12 @@ db_tests = [
"./scripts/aby_tests/test_inputs/db_join.txt",
],
[
"db join",
"db join 2",
"db_join2",
"./scripts/aby_tests/test_inputs/join2.txt",
],
[
"db join",
"db merge",
"db_merge",
"./scripts/aby_tests/test_inputs/merge.txt",
],
@@ -614,11 +623,11 @@ benchmark_tests = [
# "2pc_biomatch",
# "./scripts/aby_tests/test_inputs/biomatch_benchmark_1.txt",
# ],
# [
# "biomatch - 2",
# "2pc_biomatch_",
# "./scripts/aby_tests/test_inputs/biomatch_benchmark_1.txt",
# ],
[
"biomatch - 2",
"2pc_biomatch_",
"./scripts/aby_tests/test_inputs/biomatch_benchmark_1.txt",
],
# [
# "kmeans - 1",
# "2pc_kmeans_",
@@ -630,6 +639,13 @@ benchmark_tests = [
# "./scripts/aby_tests/test_inputs/kmeans.txt",
# ],
]
mnist_tests = [
[
"mnist 16x16",
"mnist",
"./scripts/aby_tests/test_inputs/mnist_16.txt",
]
]
# ilp_benchmark_tests = [
# [

View File

@@ -22,7 +22,7 @@ def get_result(file_path):
for line in lines:
l = line.split()
if l and l[0] == "res":
return l[1]
return l[1:]
raise RuntimeError("Unable to find result: "+file_path)
else:
raise RuntimeError("Unable to open file: "+file_path)
@@ -44,8 +44,8 @@ def run_test(expected: str, server_cmd: List[str], client_cmd: List[str]) -> boo
client_proc = Popen(" ".join(client_cmd),
shell=True, stdout=PIPE, stderr=PIPE)
server_out, server_err = server_proc.communicate(timeout=300)
client_out, client_err = client_proc.communicate(timeout=300)
server_out, server_err = server_proc.communicate(timeout=30)
client_out, client_err = client_proc.communicate(timeout=30)
if server_err:
raise RuntimeError(
@@ -65,10 +65,14 @@ def run_test(expected: str, server_cmd: List[str], client_cmd: List[str]) -> boo
assert server_out == client_out, "server out != client out\nserver_out: " + \
server_out+"\nclient_out: "+client_out
assert server_out == expected, "server_out: "+server_out+"\nexpected: "+expected
# convert server_out to list
server_out_l = server_out.split("\n")
assert server_out_l == expected, "server_out: " + \
str(server_out_l)+"\nexpected: "+str(expected)
return True, ""
except Exception as e:
# print("Exception: ", e)
return False, e

View File

@@ -36,15 +36,22 @@ function mpc_test_3 {
RUST_BACKTRACE=1 measure_time $BIN --parties $parties $cpath mpc --cost-model "hycc" --selection-scheme "gglp"
}
function mpc_test_bool {
parties=$1
cpath=$2
RUST_BACKTRACE=1 measure_time $BIN --parties $parties $cpath mpc --cost-model "hycc" --selection-scheme "b"
}
# mpc_test_3 2 ./examples/C/mpc/playground.c
mpc_test_3 2 ./examples/C/mpc/benchmarks/biomatch/2pc_biomatch_.c
# mpc_test_2 2 ./examples/C/mpc/benchmarks/db/db_join.c
# mpc_test_3 2 ./examples/C/mpc/benchmarks/biomatch/2pc_biomatch_.c
mpc_test_2 2 ./examples/C/mpc/benchmarks/db/db_join.c
# mpc_test 2 ./examples/C/mpc/benchmarks/kmeans/2pc_kmeans_.c
# mpc_test 2 ./examples/C/mpc/benchmarks/kmeans/2pc_kmeans.c
# mpc_test 2 ./examples/C/mpc/benchmarks/biomatch/2pc_biomatch.c
# mpc_test 2 ./examples/C/mpc/benchmarks/biomatch/2pc_biomatch_.c
# mpc_test_2 2 ./examples/C/mpc/benchmarks/mnist/2pc_mnist.c
# mpc_test_3 2 ./examples/C/mpc/benchmarks/mnist/mnist.c
# mpc_test_2 2 ./examples/C/mpc/benchmarks/gauss/2pc_gauss_inline.c
@@ -57,7 +64,6 @@ mpc_test_3 2 ./examples/C/mpc/benchmarks/biomatch/2pc_biomatch_.c
# mpc_test 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
# # 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
@@ -106,8 +112,8 @@ mpc_test_3 2 ./examples/C/mpc/benchmarks/biomatch/2pc_biomatch_.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.c
# mpc_test 2 ./examples/C/mpc/unit_tests/array_tests/2pc_array_index_2.c
# # build circ/compiler array tests
# mpc_test 2 ./examples/C/mpc/unit_tests/c_array_tests/2pc_array.c
@@ -122,6 +128,7 @@ mpc_test_3 2 ./examples/C/mpc/benchmarks/biomatch/2pc_biomatch_.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
@@ -136,17 +143,28 @@ mpc_test_3 2 ./examples/C/mpc/benchmarks/biomatch/2pc_biomatch_.c
# 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 bool-only
# mpc_test_bool 2 ./examples/C/mpc/benchmarks/biomatch/2pc_biomatch.c
# mpc_test_bool 2 ./examples/C/mpc/benchmarks/biomatch/biomatch.c
# mpc_test_bool 2 ./examples/C/mpc/benchmarks/kmeans/2pc_kmeans_.c
# mpc_test_bool 2 ./examples/C/mpc/benchmarks/gauss/2pc_gauss_inline.c
# mpc_test_bool 2 ./examples/C/mpc/benchmarks/db/db_join.c
# mpc_test_bool 2 ./examples/C/mpc/benchmarks/db/db_join2.c
# mpc_test_bool 2 ./examples/C/mpc/benchmarks/db/db_merge.c
# mpc_test_bool 2 ./examples/C/mpc/benchmarks/mnist/mnist.c
# # build hycc benchmarks
# mpc_test 2 ./examples/C/mpc/benchmarks/biomatch/2pc_biomatch.c
# mpc_test_2 2 ./examples/C/mpc/benchmarks/biomatch/biomatch.c
# # # # mpc_test 2 ./examples/C/mpc/benchmarks/kmeans/2pc_kmeans.c
# mpc_test 2 ./examples/C/mpc/benchmarks/kmeans/2pc_kmeans_.c
# # # # mpc_test 2 ./examples/C/mpc/benchmarks/kmeans/2pc_kmeans_og.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.c
# mpc_test_2 2 ./examples/C/mpc/benchmarks/mnist/mnist.c
# # # mpc_test 2 ./examples/C/mpc/benchmarks/kmeans/2pc_kmeans.c
# # # mpc_test 2 ./examples/C/mpc/benchmarks/kmeans/2pc_kmeans_og.c
# # ilp benchmarks
# # mpc_test 2 ./examples/C/mpc/ilp_benchmarks/2pc_ilp_bench_1.c

View File

@@ -113,7 +113,7 @@ fn build_ilp(c: &ComputationSubgraph, costs: &CostModel) -> SharingMap {
// build variables for all term assignments
for (t, i) in terms.iter() {
let mut vars = vec![];
println!("op: {}",&t.op);
// println!("op: {}",&t.op);
match &t.op {
Op::Var(..) | Op::Const(_) => {
for ty in &SHARE_TYPES {
@@ -468,7 +468,7 @@ pub fn calculate_cost(smap: &SharingMap, costs: &CostModel) -> f64 {
cost = cost + 0.0;
}
_ => {
println!("op: {}", t.op);
// println!("op: {}", t.op);
cost = cost + costs.get(&t.op).unwrap().get(to_ty).unwrap();
}
}

View File

@@ -47,6 +47,9 @@ pub struct CostModel {
/// Zero costs
zero: FxHashMap<ShareType, f64>,
/// Zero bool
zero_bool: FxHashMap<ShareType, f64>,
}
impl CostModel {
@@ -58,10 +61,14 @@ impl CostModel {
zero.insert(ShareType::Arithmetic, 0.0);
zero.insert(ShareType::Boolean, 0.0);
zero.insert(ShareType::Yao, 0.0);
let mut zero_bool: FxHashMap<ShareType, f64> = FxHashMap::default();
zero_bool.insert(ShareType::Boolean, 0.0);
CostModel {
conversions,
ops,
zero: zero,
zero,
zero_bool,
}
}
@@ -125,9 +132,8 @@ impl CostModel {
| Op::Field(_)
| Op::Update(..)
| Op::Tuple
| Op::Select
| Op::Store
| Op::Call(..) => Some(&self.zero),
Op::Select | Op::Store => Some(&self.zero_bool),
_ => {
let op_name = match op.clone() {
// assume comparisions are unsigned

View File

@@ -57,6 +57,30 @@ impl fmt::Display for EmbeddedTerm {
}
}
static mut dur_const_arr: std::time::Duration = std::time::Duration::new(0, 0);
static mut num_const_arr: usize = 0;
static mut dur_const_tuple: std::time::Duration = std::time::Duration::new(0, 0);
static mut num_const_tuple: usize = 0;
static mut dur_ite: std::time::Duration = std::time::Duration::new(0, 0);
static mut num_ite: usize = 0;
static mut dur_store: std::time::Duration = std::time::Duration::new(0, 0);
static mut num_store: usize = 0;
static mut dur_field: std::time::Duration = std::time::Duration::new(0, 0);
static mut num_field: usize = 0;
static mut dur_update: std::time::Duration = std::time::Duration::new(0, 0);
static mut num_update: usize = 0;
static mut dur_tuple: std::time::Duration = std::time::Duration::new(0, 0);
static mut num_tuple: usize = 0;
static mut dur_call: std::time::Duration = std::time::Duration::new(0, 0);
static mut num_call: usize = 0;
struct ToABY<'a> {
fs: Functions,
s_map: HashMap<String, SharingMap>,
@@ -139,78 +163,13 @@ impl<'a> ToABY<'a> {
}
}
// fn map_to_shares(&mut self) {
// let mut now = Instant::now();
// let computations = self.fs.computations.clone();
// println!("Time: cloning computations: {:?}", now.elapsed());
// let mut term_to_share_time: std::time::Duration = std::time::Duration::new(0, 0);
// let mut write_line_time: std::time::Duration = std::time::Duration::new(0, 0);
// let mut create_line_time: std::time::Duration = std::time::Duration::new(0, 0);
// let mut format_line_time: std::time::Duration = std::time::Duration::new(0, 0);
// let mut add_line_time: std::time::Duration = std::time::Duration::new(0, 0);
// let share_map_path = get_path(self.path, &self.lang, "share_map");
// let mut share_outputs = Vec::with_capacity(WRITE_SIZE * 2);
// for (name, comp) in computations.iter() {
// println!("mapping {} to shares, total terms: {}", name, comp.terms());
// let share_map = self.get_sharing_map(name);
// for t in comp.terms_postorder() {
// match t.op {
// // these cases are handled dynamically by updating the base array share
// Op::Field(..) | Op::Update(..) | Op::Select | Op::Store | Op::Tuple => continue,
// _ => {}
// }
// now = Instant::now();
// let sort: Sort = check(&t);
// let num_shares = self.get_sort_len(&sort) as i32;
// let shares: Vec<i32> = (0..num_shares)
// .map(|x| x + self.share_cnt)
// .collect::<Vec<i32>>();
// self.share_cnt += num_shares;
// self.term_to_shares.insert(t.clone(), shares.clone());
// term_to_share_time += now.elapsed();
// now = Instant::now();
// // write sharing map
// let share_type = share_map.get(&t).unwrap();
// let share_str = share_type.char();
// create_line_time += now.elapsed();
// for s in shares {
// now = Instant::now();
// let line = format!("{} {}\n", s, share_str);
// format_line_time += now.elapsed();
// now = Instant::now();
// share_outputs.push(line);
// add_line_time += now.elapsed();
// }
// now = Instant::now();
// // buffered write
// if share_outputs.len() >= WRITE_SIZE {
// write_lines(&share_map_path, &share_outputs);
// share_outputs.clear();
// }
// write_line_time += now.elapsed();
// }
// self.s_map.remove(name);
// }
// write_lines(&share_map_path, &share_outputs);
// // clear share map
// self.s_map.clear();
// println!("term to share time: {:?}", term_to_share_time);
// println!("create time: {:?}", create_line_time);
// println!("format time: {:?}", format_line_time);
// println!("add time: {:?}", add_line_time);
// println!("write time: {:?}", write_line_time);
// }
fn shares_to_string(&self, shares: Vec<i32>) -> String {
shares
.iter()
.map(|&i| i.to_string())
.collect::<Vec<String>>()
.join(" ")
}
fn get_md(&self) -> ComputationMetadata {
self.fs
@@ -274,6 +233,7 @@ impl<'a> ToABY<'a> {
}
}
// TODO: Rust ENTRY api on maps
fn get_share(&mut self, t: &Term) -> i32 {
match self.term_to_shares.get(t) {
Some(v) => {
@@ -391,18 +351,15 @@ impl<'a> ToABY<'a> {
if !self.inputs.contains(&t) && md.input_vis.contains_key(name) {
let term_name = ToABY::get_var_name_from_term(&t);
let vis = self.unwrap_vis(name);
let share_cnt = self.get_share(&t);
let s = self.get_share(&t);
let op = "IN";
if vis == PUBLIC {
let bitlen = 1;
let line = format!(
"3 1 {} {} {} {} {}\n",
term_name, vis, bitlen, share_cnt, op
);
let line = format!("3 1 {} {} {} {} {}\n", term_name, vis, bitlen, s, op);
self.bytecode_input.push(line);
} else {
let line = format!("2 1 {} {} {} {}\n", term_name, vis, share_cnt, op);
let line = format!("2 1 {} {} {} {}\n", term_name, vis, s, op);
self.bytecode_input.push(line);
}
self.inputs.push(t.clone());
@@ -519,23 +476,19 @@ impl<'a> ToABY<'a> {
fn embed_bv(&mut self, t: Term) {
match &t.op {
Op::Var(name, Sort::BitVector(_)) => {
let s = self.get_share(&t);
let md = self.get_md();
if !self.inputs.contains(&t) && md.input_vis.contains_key(name) {
let term_name = ToABY::get_var_name_from_term(&t);
let vis = self.unwrap_vis(name);
let share_cnt = self.get_share(&t);
let s = self.get_share(&t);
let op = "IN";
if vis == PUBLIC {
let bitlen = 32;
let line = format!(
"3 1 {} {} {} {} {}\n",
term_name, vis, bitlen, share_cnt, op
);
let line = format!("3 1 {} {} {} {} {}\n", term_name, vis, bitlen, s, op);
self.bytecode_input.push(line);
} else {
let line = format!("2 1 {} {} {} {}\n", term_name, vis, share_cnt, op);
let line = format!("2 1 {} {} {} {}\n", term_name, vis, s, op);
self.bytecode_input.push(line);
}
self.inputs.push(t.clone());
@@ -655,7 +608,21 @@ impl<'a> ToABY<'a> {
.insert(t.clone(), vec![array_shares[idx]]);
self.cache.insert(t.clone(), EmbeddedTerm::Bv);
} else {
panic!("non-const: sel")
let op = "SELECT";
let num_inputs = array_shares.len() + 1;
let index_share = self.get_share(&t.cs[1]);
let output = self.get_share(&t);
let line = format!(
"{} 1 {} {} {} {}\n",
num_inputs,
self.shares_to_string(array_shares),
index_share,
output,
op
);
self.bytecode_output.push(line);
self.term_to_shares.insert(t.clone(), vec![output]);
self.cache.insert(t.clone(), EmbeddedTerm::Bv);
}
}
_ => panic!("Non-field in embed_bv: {:?}", t),
@@ -663,6 +630,7 @@ impl<'a> ToABY<'a> {
}
fn embed_scalar(&mut self, t: Term) {
let now = Instant::now();
match &t.op {
Op::Const(Value::Array(arr)) => {
let shares = self.get_shares(&t);
@@ -687,6 +655,11 @@ impl<'a> ToABY<'a> {
_ => todo!(),
}
}
unsafe {
num_const_arr += 1;
dur_const_arr += now.elapsed();
};
}
Op::Const(Value::Tuple(tup)) => {
let shares = self.get_shares(&t);
@@ -702,6 +675,11 @@ impl<'a> ToABY<'a> {
_ => todo!(),
}
}
unsafe {
num_const_tuple += 1;
dur_const_tuple += now.elapsed();
};
}
Op::Ite => {
let op = "MUX";
@@ -715,12 +693,28 @@ impl<'a> ToABY<'a> {
assert!(shares.len() == a.len());
assert!(shares.len() == b.len());
for ((s_share, a_share), b_share) in shares.iter().zip(a.iter()).zip(b.iter()) {
let line = format!("3 1 {} {} {} {} {}\n", sel, a_share, b_share, s_share, op);
self.bytecode_output.push(line);
}
let num_inputs = 1 + shares.len() * 2;
let num_outputs = shares.len();
let line = format!(
"{} {} {} {} {} {} {}\n",
num_inputs,
num_outputs,
sel,
self.shares_to_string(a),
self.shares_to_string(b),
self.shares_to_string(shares),
op
);
self.bytecode_output.push(line);
self.cache.insert(t.clone(), EmbeddedTerm::Array);
unsafe {
num_ite += 1;
dur_ite += now.elapsed();
};
}
Op::Store => {
assert!(t.cs.len() == 3);
@@ -736,8 +730,29 @@ impl<'a> ToABY<'a> {
self.term_to_shares.insert(t.clone(), array_shares.clone());
self.cache.insert(t.clone(), EmbeddedTerm::Array);
} else {
panic!("non-const: store")
let op = "STORE";
let num_inputs = array_shares.len() + 2;
let outputs = self.get_shares(&t);
let num_outputs = outputs.len();
let index_share = self.get_share(&t.cs[1]);
let line = format!(
"{} {} {} {} {} {} {}\n",
num_inputs,
num_outputs,
self.shares_to_string(array_shares),
index_share,
value_share,
self.shares_to_string(outputs),
op
);
self.bytecode_output.push(line);
}
unsafe {
num_store += 1;
dur_store += now.elapsed();
};
}
Op::Field(i) => {
assert!(t.cs.len() == 1);
@@ -766,6 +781,11 @@ impl<'a> ToABY<'a> {
self.term_to_shares.insert(t.clone(), field_shares.to_vec());
self.cache.insert(t.clone(), EmbeddedTerm::Array);
unsafe {
num_field += 1;
dur_field += now.elapsed();
};
}
Op::Update(i) => {
assert!(t.cs.len() == 2);
@@ -781,6 +801,11 @@ impl<'a> ToABY<'a> {
// store shares
self.term_to_shares.insert(t.clone(), tuple_shares);
self.cache.insert(t.clone(), EmbeddedTerm::Tuple);
unsafe {
num_update += 1;
dur_update += now.elapsed();
};
}
Op::Tuple => {
let mut shares: Vec<i32> = Vec::new();
@@ -789,6 +814,11 @@ impl<'a> ToABY<'a> {
}
self.term_to_shares.insert(t.clone(), shares);
self.cache.insert(t.clone(), EmbeddedTerm::Tuple);
unsafe {
num_tuple += 1;
dur_tuple += now.elapsed();
};
}
Op::Call(name, _arg_names, arg_sorts, ret_sorts) => {
let shares = self.get_shares(&t);
@@ -830,6 +860,11 @@ impl<'a> ToABY<'a> {
);
self.bytecode_output.push(line);
self.cache.insert(t.clone(), EmbeddedTerm::Tuple);
unsafe {
num_call += 1;
dur_call += now.elapsed();
};
}
_ => {
panic!("Non-field in embed_scalar: {}", t.op)
@@ -838,26 +873,125 @@ impl<'a> ToABY<'a> {
}
fn embed(&mut self, t: Term) {
let mut num_bool = 0;
let mut num_bv = 0;
let mut num_scalar = 0;
let mut dur_bool: std::time::Duration = std::time::Duration::new(0, 0);
let mut dur_bv: std::time::Duration = std::time::Duration::new(0, 0);
let mut dur_scalar: std::time::Duration = std::time::Duration::new(0, 0);
unsafe {
dur_const_arr = std::time::Duration::new(0, 0);
num_const_arr = 0;
dur_const_tuple = std::time::Duration::new(0, 0);
num_const_tuple = 0;
dur_ite = std::time::Duration::new(0, 0);
num_ite = 0;
dur_store = std::time::Duration::new(0, 0);
num_store = 0;
dur_field = std::time::Duration::new(0, 0);
num_field = 0;
dur_update = std::time::Duration::new(0, 0);
num_update = 0;
dur_tuple = std::time::Duration::new(0, 0);
num_tuple = 0;
dur_call = std::time::Duration::new(0, 0);
num_call = 0;
}
let mut write_time: std::time::Duration = std::time::Duration::new(0, 0);
for c in PostOrderIter::new(t) {
if self.cache.contains_key(&c) {
continue;
}
let b_now = Instant::now(); // check for tuples are long
match check(&c) {
Sort::Bool => {
let now = Instant::now();
self.embed_bool(c);
num_bool += 1;
dur_bool += now.elapsed();
}
Sort::BitVector(_) => {
let now = Instant::now();
self.embed_bv(c);
num_bv += 1;
dur_bv += now.elapsed();
}
Sort::Array(..) | Sort::Tuple(_) => {
let now = Instant::now();
self.embed_scalar(c);
num_scalar += 1;
dur_scalar += now.elapsed();
}
e => panic!("Unsupported sort in embed: {:?}", e),
}
let now = Instant::now();
self.write_bytecode_output(false);
self.write_const_output(false);
self.write_share_output(false);
write_time += now.elapsed();
}
println!("bool: {}, bv: {}, scalar: {}", num_bool, num_bv, num_scalar);
println!(
"times: bool: {:?}, bv: {:?}, scalar: {:?}",
dur_bool, dur_bv, dur_scalar
);
println!("write time: {:?}", write_time);
if num_bool > 0 && num_bv > 0 && num_scalar > 0 {
println!(
"norm_times: bool: {:?}, bv: {:?}, scalar: {:?}\n",
dur_bool / num_bool,
dur_bv / num_bv,
dur_scalar / num_scalar
);
}
unsafe {
println!("================================");
println!("const_arr: {}, const_tuple: {}, ite: {}, store: {}, field: {}, update: {}, tuple: {}, call: {}", num_const_arr, num_const_tuple, num_ite, num_store, num_field, num_update, num_tuple, num_call);
println!("times: const_arr: {:?}, const_tuple: {:?}, ite: {:?}, store: {:?}, field: {:?}, update: {:?}, tuple: {:?}, call: {:?}", dur_const_arr, dur_const_tuple, dur_ite, dur_store, dur_field, dur_update, dur_tuple, dur_call);
if num_const_arr > 0 {
println!("norm_const_arr: {:?}", dur_const_arr / num_const_arr as u32);
}
if num_const_tuple > 0 {
println!(
"norm_const_tuple: {:?}",
dur_const_tuple / num_const_tuple as u32
);
}
if num_ite > 0 {
println!("norm_ite: {:?}", dur_ite / num_ite as u32);
}
if num_store > 0 {
println!("norm_store: {:?}", dur_store / num_store as u32);
}
if num_field > 0 {
println!("norm_field: {:?}", dur_field / num_field as u32);
}
if num_update > 0 {
println!("norm_update: {:?}", dur_update / num_update as u32);
}
if num_tuple > 0 {
println!("norm_tuple: {:?}", dur_tuple / num_tuple as u32);
}
if num_call > 0 {
println!("norm_call: {:?}", dur_call / num_call as u32);
}
println!("================================\n")
}
}
@@ -873,6 +1007,9 @@ impl<'a> ToABY<'a> {
let mut outputs: Vec<String> = Vec::new();
let mut now = Instant::now();
// set current computation
self.curr_comp = name.to_string();
// create paths
get_path(
self.path,
@@ -881,10 +1018,9 @@ impl<'a> ToABY<'a> {
true,
);
println!("starting: {}", name);
println!("starting: {}, {}", name, comp.terms());
for t in comp.outputs.iter() {
self.curr_comp = name.to_string();
self.embed(t.clone());
let op = "OUT";