diff --git a/garbled_circuit/garbled_circuit.cpp b/garbled_circuit/garbled_circuit.cpp index 474cb9c..c3f1c38 100644 --- a/garbled_circuit/garbled_circuit.cpp +++ b/garbled_circuit/garbled_circuit.cpp @@ -84,21 +84,15 @@ int GarbleStr(const string& scd_file_address, const string& p_init_str, garbled_circuit.p_input_size, clock_cycles, &p_init, &p_input)); - block* init_labels = nullptr; - block* input_labels = nullptr; - block* output_labels = nullptr; - short* output_vals = nullptr; - // global key block global_key = RandomBlock(); CHECK(SendData(connfd, &global_key, sizeof(block))); // send global key if (low_mem_foot && clock_cycles > 1) { CHECK( - GarbleBNLowMem(garbled_circuit, p_init, p_input, g_init, g_input, clock_cycles, - output_mask, output_mode, init_labels, input_labels, - output_labels, output_vals, output_bn, R, global_key, - disable_OT, connfd)); + GarbleBNLowMem(garbled_circuit, p_init, p_input, g_init, g_input, + clock_cycles, output_mask, output_mode, output_bn, R, + global_key, disable_OT, connfd)); CHECK( OutputBN2StrLowMem(garbled_circuit, output_bn, clock_cycles, @@ -106,10 +100,9 @@ int GarbleStr(const string& scd_file_address, const string& p_init_str, } else { CHECK( - GarbleBNHighMem(garbled_circuit, p_init, p_input, g_init, g_input, clock_cycles, - output_mask, output_mode, init_labels, input_labels, - output_labels, output_vals, output_bn, R, global_key, - disable_OT, connfd)); + GarbleBNHighMem(garbled_circuit, p_init, p_input, g_init, g_input, + clock_cycles, output_mask, output_mode, output_bn, R, + global_key, disable_OT, connfd)); CHECK( OutputBN2StrHighMem(garbled_circuit, output_bn, clock_cycles, output_mode, output_str)); @@ -120,11 +113,6 @@ int GarbleStr(const string& scd_file_address, const string& p_init_str, BN_free(g_input); BN_free(output_bn); - delete[] init_labels; - delete[] input_labels; - delete[] output_labels; - delete[] output_vals; - RemoveGarbledCircuit(&garbled_circuit); return SUCCESS; @@ -162,30 +150,23 @@ int EvaluateStr(const string& scd_file_address, const string& p_init_str, garbled_circuit.p_input_size, clock_cycles, &p_init, &p_input)); - block* init_labels = nullptr; - block* input_labels = nullptr; - block* output_labels = nullptr; - short* output_vals = nullptr; - // global key block global_key = RandomBlock(); CHECK(RecvData(connfd, &global_key, sizeof(block))); // receive global key if (low_mem_foot && clock_cycles > 1) { CHECK( - EvaluateBNLowMem(garbled_circuit, p_init, p_input, e_init, e_input, clock_cycles, - output_mask, output_mode, init_labels, input_labels, - output_labels, output_vals, output_bn, global_key, - disable_OT, connfd)); + EvaluateBNLowMem(garbled_circuit, p_init, p_input, e_init, e_input, + clock_cycles, output_mask, output_mode, output_bn, + global_key, disable_OT, connfd)); CHECK( OutputBN2StrLowMem(garbled_circuit, output_bn, clock_cycles, output_mode, output_str)); } else { CHECK( - EvaluateBNHighMem(garbled_circuit, p_init, p_input,e_init, e_input, clock_cycles, - output_mask, output_mode, init_labels, input_labels, - output_labels, output_vals, output_bn, global_key, - disable_OT, connfd)); + EvaluateBNHighMem(garbled_circuit, p_init, p_input, e_init, e_input, + clock_cycles, output_mask, output_mode, output_bn, + global_key, disable_OT, connfd)); CHECK( OutputBN2StrHighMem(garbled_circuit, output_bn, clock_cycles, output_mode, output_str)); @@ -196,11 +177,6 @@ int EvaluateStr(const string& scd_file_address, const string& p_init_str, BN_free(e_input); BN_free(output_bn); - delete[] init_labels; - delete[] input_labels; - delete[] output_labels; - delete[] output_vals; - RemoveGarbledCircuit(&garbled_circuit); return SUCCESS; } diff --git a/garbled_circuit/garbled_circuit_high_mem.cpp b/garbled_circuit/garbled_circuit_high_mem.cpp index c777520..36d2795 100644 --- a/garbled_circuit/garbled_circuit_high_mem.cpp +++ b/garbled_circuit/garbled_circuit_high_mem.cpp @@ -50,10 +50,14 @@ int GarbleBNHighMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, BIGNUM* p_input, BIGNUM* g_init, BIGNUM* g_input, uint64_t clock_cycles, const string& output_mask, - OutputMode output_mode, block* init_labels, - block* input_labels, block* output_labels, - short* output_vals, BIGNUM* output_bn, block R, + OutputMode output_mode, BIGNUM* output_bn, block R, block global_key, bool disable_OT, int connfd) { + + block* init_labels = nullptr; + block* input_labels = nullptr; + block* output_labels = nullptr; + short* output_vals = nullptr; + // allocate init and input values and translate form string CHECK( GarbleMakeLabels(garbled_circuit, &init_labels, &input_labels, @@ -83,16 +87,24 @@ int GarbleBNHighMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, GarbleTransferOutput(garbled_circuit, output_labels, output_vals, clock_cycles, output_mask, output_mode, output_bn, connfd)); + delete[] init_labels; + delete[] input_labels; + delete[] output_labels; + delete[] output_vals; return SUCCESS; } int EvaluateBNHighMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, BIGNUM* p_input, BIGNUM* e_init, BIGNUM* e_input, uint64_t clock_cycles, const string& output_mask, - OutputMode output_mode, block* init_labels, - block* input_labels, block* output_labels, - short* output_vals, BIGNUM* output_bn, block global_key, - bool disable_OT, int connfd) { + OutputMode output_mode, BIGNUM* output_bn, + block global_key, bool disable_OT, int connfd) { + + block* init_labels = nullptr; + block* input_labels = nullptr; + block* output_labels = nullptr; + short* output_vals = nullptr; + CHECK( EvaluateMakeLabels(garbled_circuit, &init_labels, &input_labels, &output_labels, &output_vals, clock_cycles)); @@ -120,6 +132,10 @@ int EvaluateBNHighMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, EvaluateTransferOutput(garbled_circuit, output_labels, output_vals, clock_cycles, output_mask, output_mode, output_bn, connfd)); + delete[] init_labels; + delete[] input_labels; + delete[] output_labels; + delete[] output_vals; return SUCCESS; } @@ -173,9 +189,18 @@ int GarbleHighMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, wires_val[dff_bias + i] = 1; } else if (wire_index >= 0 && wire_index < (int64_t) garbled_circuit.get_init_size()) { - wires[dff_bias + i].label0 = init_labels[wire_index * 2 + 0]; - wires[dff_bias + i].label1 = init_labels[wire_index * 2 + 1]; - wires_val[dff_bias + i] = UNKOWN; + if (wire_index < (int64_t) garbled_circuit.get_p_init_hi_index()) { + wires_val[dff_bias + i] = BN_is_bit_set( + p_init, + wire_index - (int64_t) garbled_circuit.get_p_init_lo_index()); + } else { + int64_t label_index = wire_index + - (int64_t) garbled_circuit.get_p_init_hi_index(); + + wires[dff_bias + i].label0 = init_labels[label_index * 2 + 0]; + wires[dff_bias + i].label1 = init_labels[label_index * 2 + 1]; + wires_val[dff_bias + i] = UNKOWN; + } } else { LOG(ERROR) << "Invalid I: " << wire_index << endl; wires_val[dff_bias + i] = 0; // Wire with invalid I values become 0. @@ -201,16 +226,23 @@ int GarbleHighMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, } } // inputs - uint64_t input_bias = garbled_circuit.get_input_lo_index(); - for (uint64_t i = 0; i < garbled_circuit.get_input_size(); i++) { + uint64_t p_input_bias = garbled_circuit.get_p_input_lo_index(); + for (uint64_t i = 0; i < garbled_circuit.p_input_size; i++) { + wires_val[p_input_bias + i] = BN_is_bit_set( + p_input, cid * garbled_circuit.p_input_size + i); + } + uint64_t input_bias = garbled_circuit.get_g_input_lo_index(); + for (uint64_t i = 0; i < garbled_circuit.get_secret_input_size(); i++) { wires[input_bias + i].label0 = input_labels[(cid - * garbled_circuit.get_input_size() + i) * 2 + 0]; + * garbled_circuit.get_secret_input_size() + i) * 2 + 0]; wires[input_bias + i].label1 = input_labels[(cid - * garbled_circuit.get_input_size() + i) * 2 + 1]; + * garbled_circuit.get_secret_input_size() + i) * 2 + 1]; DUMP("input") - << input_labels[(cid * garbled_circuit.get_input_size() + i) * 2 + 0] + << input_labels[(cid * garbled_circuit.get_secret_input_size() + i) + * 2 + 0] << endl; } + for (uint64_t i = 0; i < garbled_circuit.gate_size; i++) { //known value fanout[i] = garbled_circuit.garbledGates[i].fanout; // init fanout @@ -402,8 +434,18 @@ int EvaluateHighMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, wires_val[dff_bias + i] = 1; } else if (wire_index >= 0 && wire_index < (int64_t) garbled_circuit.get_init_size()) { - wires[dff_bias + i] = init_labels[wire_index]; - wires_val[dff_bias + i] = UNKOWN; + + if (wire_index < (int64_t) garbled_circuit.get_p_init_hi_index()) { + wires_val[dff_bias + i] = BN_is_bit_set( + p_init, + wire_index - (int64_t) garbled_circuit.get_p_init_lo_index()); + } else { + int64_t label_index = wire_index + - (int64_t) garbled_circuit.get_p_init_hi_index(); + + wires[dff_bias + i] = init_labels[label_index]; + wires_val[dff_bias + i] = UNKOWN; + } } else { LOG(ERROR) << "Invalid I: " << wire_index << endl; wires_val[dff_bias + i] = 0; @@ -428,12 +470,19 @@ int EvaluateHighMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, } } // inputs - uint64_t input_bias = garbled_circuit.get_input_lo_index(); - for (uint64_t i = 0; i < garbled_circuit.get_input_size(); i++) { + uint64_t p_input_bias = garbled_circuit.get_p_input_lo_index(); + for (uint64_t i = 0; i < garbled_circuit.p_input_size; i++) { + wires_val[p_input_bias + i] = BN_is_bit_set( + p_input, cid * garbled_circuit.p_input_size + i); + } + + uint64_t input_bias = garbled_circuit.get_g_input_lo_index(); + for (uint64_t i = 0; i < garbled_circuit.get_secret_input_size(); i++) { wires[input_bias + i] = input_labels[cid - * garbled_circuit.get_input_size() + i]; - DUMP("input") << input_labels[cid * garbled_circuit.get_input_size() + i] - << endl; + * garbled_circuit.get_secret_input_size() + i]; + DUMP("input") + << input_labels[cid * garbled_circuit.get_secret_input_size() + i] + << endl; } for (uint64_t i = 0; i < garbled_circuit.gate_size; i++) { // known values @@ -570,8 +619,9 @@ int GarbleOT(const GarbledCircuit& garbled_circuit, block* init_labels, + cid * garbled_circuit.e_input_size + i; CHECK_ALLOC(message[idx] = new block[2]); for (uint j = 0; j < 2; j++) { - message[idx][j] = input_labels[(cid * garbled_circuit.get_input_size() - + i + garbled_circuit.g_input_size) * 2 + j]; + message[idx][j] = input_labels[(cid + * garbled_circuit.get_secret_input_size() + i + + garbled_circuit.g_input_size) * 2 + j]; } } } @@ -627,7 +677,7 @@ int EvalauteOT(const GarbledCircuit& garbled_circuit, BIGNUM* e_init, for (uint i = 0; i < garbled_circuit.e_input_size; i++) { uint indx = garbled_circuit.e_init_size + cid * garbled_circuit.e_input_size + i; - input_labels[cid * garbled_circuit.get_input_size() + i + input_labels[cid * garbled_circuit.get_secret_input_size() + i + garbled_circuit.g_input_size] = message[indx]; } } @@ -659,15 +709,15 @@ int GarbleTransferLabels(const GarbledCircuit& garbled_circuit, BIGNUM* g_init, CHECK( SendData( connfd, - &input_labels[(cid * garbled_circuit.get_input_size() + i) * 2 - + 0], + &input_labels[(cid * garbled_circuit.get_secret_input_size() + i) + * 2 + 0], sizeof(block))); } else { CHECK( SendData( connfd, - &input_labels[(cid * garbled_circuit.get_input_size() + i) * 2 - + 1], + &input_labels[(cid * garbled_circuit.get_secret_input_size() + i) + * 2 + 1], sizeof(block))); } } @@ -702,15 +752,15 @@ int GarbleTransferLabels(const GarbledCircuit& garbled_circuit, BIGNUM* g_init, CHECK( SendData( connfd, - &input_labels[(cid * garbled_circuit.get_input_size() + i - + garbled_circuit.g_input_size) * 2 + 0], + &input_labels[(cid * garbled_circuit.get_secret_input_size() + + i + garbled_circuit.g_input_size) * 2 + 0], sizeof(block))); } else { CHECK( SendData( connfd, - &input_labels[(cid * garbled_circuit.get_input_size() + i - + garbled_circuit.g_input_size) * 2 + 1], + &input_labels[(cid * garbled_circuit.get_secret_input_size() + + i + garbled_circuit.g_input_size) * 2 + 1], sizeof(block))); } } @@ -739,9 +789,10 @@ int EvaluateTransferLabels(const GarbledCircuit& garbled_circuit, for (uint cid = 0; cid < clock_cycles; cid++) { for (uint i = 0; i < garbled_circuit.g_input_size; i++) { CHECK( - RecvData(connfd, - &input_labels[cid * garbled_circuit.get_input_size() + i], - sizeof(block))); + RecvData( + connfd, + &input_labels[cid * garbled_circuit.get_secret_input_size() + i], + sizeof(block))); } } @@ -760,7 +811,7 @@ int EvaluateTransferLabels(const GarbledCircuit& garbled_circuit, CHECK( RecvData( connfd, - &input_labels[cid * garbled_circuit.get_input_size() + i + &input_labels[cid * garbled_circuit.get_secret_input_size() + i + garbled_circuit.g_input_size], sizeof(block))); } diff --git a/garbled_circuit/garbled_circuit_high_mem.h b/garbled_circuit/garbled_circuit_high_mem.h index 9bffbe1..525aee1 100644 --- a/garbled_circuit/garbled_circuit_high_mem.h +++ b/garbled_circuit/garbled_circuit_high_mem.h @@ -43,16 +43,12 @@ int GarbleBNHighMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, BIGNUM* p_input, BIGNUM* g_init, BIGNUM* g_input, uint64_t clock_cycles, const string& output_mask, - OutputMode output_mode, block* init_labels, - block* input_labels, block* output_labels, - short* output_vals, BIGNUM* output_bn, block R, + OutputMode output_mode, BIGNUM* output_bn, block R, block global_key, bool disable_OT, int connfd); int EvaluateBNHighMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, BIGNUM* p_input, BIGNUM* e_init, BIGNUM* e_input, uint64_t clock_cycles, const string& output_mask, - OutputMode output_mode, block* init_labels, - block* input_labels, block* output_labels, - short* output_vals, BIGNUM* output_bn, block global_key, + OutputMode output_mode, BIGNUM* output_bn, block global_key, bool disable_OT, int connfd); int GarbleHighMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, BIGNUM* p_input, block* init_labels, block* input_labels, diff --git a/garbled_circuit/garbled_circuit_low_mem.cpp b/garbled_circuit/garbled_circuit_low_mem.cpp index 908a6de..0bae585 100644 --- a/garbled_circuit/garbled_circuit_low_mem.cpp +++ b/garbled_circuit/garbled_circuit_low_mem.cpp @@ -49,13 +49,18 @@ int GarbleBNLowMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, BIGNUM* p_input, BIGNUM* g_init, BIGNUM* g_input, uint64_t clock_cycles, const string& output_mask, - OutputMode output_mode, block* init_labels, - block* input_labels, block* output_labels, - short* output_vals, BIGNUM* output_bn, block R, + OutputMode output_mode, BIGNUM* output_bn, block R, block global_key, bool disable_OT, int connfd) { + uint64_t ot_time = 0; uint64_t garble_time = 0; uint64_t comm_time = 0; + + block* init_labels = nullptr; + block* input_labels = nullptr; + block* output_labels = nullptr; + short* output_vals = nullptr; + BlockPair *wires = nullptr; CHECK_ALLOC(wires = new BlockPair[garbled_circuit.get_wire_size()]); short *wires_val = nullptr; @@ -157,6 +162,10 @@ int GarbleBNLowMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, LOG(INFO) << "Alice communication time (cc) = " << comm_time << endl; LOG(INFO) << "Alice garbling time (cc) = " << garble_time << endl; + delete[] init_labels; + delete[] input_labels; + delete[] output_labels; + delete[] output_vals; delete[] wires; delete[] wires_val; delete[] fanout; @@ -167,13 +176,16 @@ int GarbleBNLowMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, int EvaluateBNLowMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, BIGNUM* p_input, BIGNUM* e_init, BIGNUM* e_input, uint64_t clock_cycles, const string& output_mask, - OutputMode output_mode, block* init_labels, - block* input_labels, block* output_labels, - short* output_vals, BIGNUM* output_bn, block global_key, - bool disable_OT, int connfd) { + OutputMode output_mode, BIGNUM* output_bn, + block global_key, bool disable_OT, int connfd) { uint64_t ot_time = 0; uint64_t eval_time = 0; uint64_t comm_time = 0; + + block* init_labels = nullptr; + block* input_labels = nullptr; + block* output_labels = nullptr; + short* output_vals = nullptr; block *wires = nullptr; CHECK_ALLOC(wires = new block[garbled_circuit.get_wire_size()]); short *wires_val = nullptr; @@ -254,6 +266,10 @@ int EvaluateBNLowMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, LOG(INFO) << "Bob communication time (cc) = " << comm_time << endl; LOG(INFO) << "Bob evaluation time (cc) = " << eval_time << endl; + delete[] init_labels; + delete[] input_labels; + delete[] output_labels; + delete[] output_vals; delete[] wires; delete[] wires_val; delete[] fanout; @@ -285,9 +301,18 @@ uint64_t GarbleLowMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, wires_val[dff_bias + i] = 1; } else if (wire_index >= 0 && wire_index < (int64_t) garbled_circuit.get_init_size()) { - wires[dff_bias + i].label0 = init_labels[wire_index * 2 + 0]; - wires[dff_bias + i].label1 = init_labels[wire_index * 2 + 1]; - wires_val[dff_bias + i] = -1; + if (wire_index < (int64_t) garbled_circuit.get_p_init_hi_index()) { + wires_val[dff_bias + i] = BN_is_bit_set( + p_init, + wire_index - (int64_t) garbled_circuit.get_p_init_lo_index()); + } else { + int64_t label_index = wire_index + - (int64_t) garbled_circuit.get_p_init_hi_index(); + + wires[dff_bias + i].label0 = init_labels[label_index * 2 + 0]; + wires[dff_bias + i].label1 = init_labels[label_index * 2 + 1]; + wires_val[dff_bias + i] = UNKOWN; + } } else { LOG(ERROR) << "Invalid I: " << wire_index << endl; wires_val[dff_bias + i] = 0; @@ -313,10 +338,17 @@ uint64_t GarbleLowMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, } // inputs - uint64_t input_bias = garbled_circuit.get_input_lo_index(); - for (uint64_t i = 0; i < garbled_circuit.get_input_size(); i++) { + uint64_t p_input_bias = garbled_circuit.get_p_input_lo_index(); + for (uint64_t i = 0; i < garbled_circuit.p_input_size; i++) { + wires_val[p_input_bias + i] = BN_is_bit_set( + p_input, cid * garbled_circuit.p_input_size + i); + } + + uint64_t input_bias = garbled_circuit.get_g_input_lo_index(); + for (uint64_t i = 0; i < garbled_circuit.get_secret_input_size(); i++) { wires[input_bias + i].label0 = input_labels[i * 2 + 0]; wires[input_bias + i].label1 = input_labels[i * 2 + 1]; + wires_val[input_bias + i] = UNKOWN; DUMP("input") << input_labels[i * 2 + 0] << endl; } @@ -446,8 +478,17 @@ uint64_t EvaluateLowMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, wires_val[dff_bias + i] = 1; } else if (wire_index >= 0 && wire_index < (int64_t) garbled_circuit.get_init_size()) { - wires[dff_bias + i] = init_labels[wire_index]; - wires_val[dff_bias + i] = -1; + if (wire_index < (int64_t) garbled_circuit.get_p_init_hi_index()) { + wires_val[dff_bias + i] = BN_is_bit_set( + p_init, + wire_index - (int64_t) garbled_circuit.get_p_init_lo_index()); + } else { + int64_t label_index = wire_index + - (int64_t) garbled_circuit.get_p_init_hi_index(); + + wires[dff_bias + i] = init_labels[label_index]; + wires_val[dff_bias + i] = -1; + } } else { LOG(ERROR) << "Invalid I: " << wire_index << endl; wires_val[dff_bias + i] = 0; @@ -472,8 +513,13 @@ uint64_t EvaluateLowMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, } } // inputs - uint64_t input_bias = garbled_circuit.get_input_lo_index(); - for (uint64_t i = 0; i < garbled_circuit.get_input_size(); i++) { + uint64_t p_input_bias = garbled_circuit.get_p_input_lo_index(); + for (uint64_t i = 0; i < garbled_circuit.p_input_size; i++) { + wires_val[p_input_bias + i] = BN_is_bit_set( + p_input, cid * garbled_circuit.p_input_size + i); + } + uint64_t input_bias = garbled_circuit.get_g_input_lo_index(); + for (uint64_t i = 0; i < garbled_circuit.get_secret_input_size(); i++) { wires[input_bias + i] = input_labels[i]; DUMP("input") << input_labels[i] << endl; } @@ -585,17 +631,19 @@ int GarbleAllocLabels(const GarbledCircuit& garbled_circuit, // allocate and generate random init and inputs label pairs (*init_labels) = nullptr; if (garbled_circuit.get_init_size() > 0) { - CHECK_ALLOC((*init_labels) = new block[garbled_circuit.get_init_size() * 2]); - for (uint i = 0; i < garbled_circuit.get_init_size(); i++) { + CHECK_ALLOC( + (*init_labels) = new block[garbled_circuit.get_secret_init_size() * 2]); + for (uint i = 0; i < garbled_circuit.get_secret_init_size(); i++) { (*init_labels)[i * 2 + 0] = RandomBlock(); (*init_labels)[i * 2 + 1] = XorBlock(R, (*init_labels)[i * 2 + 0]); } } (*input_labels) = nullptr; - if (garbled_circuit.get_input_size() > 0) { - CHECK_ALLOC((*input_labels) = - new block[garbled_circuit.get_input_size() * 2]); + if (garbled_circuit.get_secret_input_size() > 0) { + CHECK_ALLOC( + (*input_labels) = + new block[garbled_circuit.get_secret_input_size() * 2]); } (*output_labels) = nullptr; @@ -614,7 +662,7 @@ int GarbleAllocLabels(const GarbledCircuit& garbled_circuit, int GarbleGneInitLabels(const GarbledCircuit& garbled_circuit, block* init_labels, block R) { - for (uint i = 0; i < garbled_circuit.get_init_size(); i++) { + for (uint i = 0; i < garbled_circuit.get_secret_init_size(); i++) { init_labels[i * 2 + 0] = RandomBlock(); init_labels[i * 2 + 1] = XorBlock(R, init_labels[i * 2 + 0]); } @@ -624,8 +672,8 @@ int GarbleGneInitLabels(const GarbledCircuit& garbled_circuit, int GarbleGenInputLabels(const GarbledCircuit& garbled_circuit, block* input_labels, block R) { - if (garbled_circuit.get_input_size() > 0) { - for (uint i = 0; i < garbled_circuit.get_input_size(); i++) { + if (garbled_circuit.get_secret_input_size() > 0) { + for (uint i = 0; i < garbled_circuit.get_secret_input_size(); i++) { input_labels[i * 2 + 0] = RandomBlock(); input_labels[i * 2 + 1] = XorBlock(R, input_labels[i * 2 + 0]); } @@ -638,13 +686,15 @@ int EvaluateAllocLabels(const GarbledCircuit& garbled_circuit, block** output_labels, short** output_vals) { (*init_labels) = nullptr; - if (garbled_circuit.get_init_size() > 0) { - CHECK_ALLOC((*init_labels) = new block[garbled_circuit.get_init_size()]); + if (garbled_circuit.get_secret_init_size() > 0) { + CHECK_ALLOC((*init_labels) = + new block[garbled_circuit.get_secret_init_size()]); } (*input_labels) = nullptr; - if (garbled_circuit.get_input_size() > 0) { - CHECK_ALLOC((*input_labels) = new block[garbled_circuit.get_input_size()]); + if (garbled_circuit.get_secret_input_size() > 0) { + CHECK_ALLOC( + (*input_labels) = new block[garbled_circuit.get_secret_input_size()]); } (*output_labels) = nullptr; diff --git a/garbled_circuit/garbled_circuit_low_mem.h b/garbled_circuit/garbled_circuit_low_mem.h index 238045f..fc506c5 100644 --- a/garbled_circuit/garbled_circuit_low_mem.h +++ b/garbled_circuit/garbled_circuit_low_mem.h @@ -13,35 +13,29 @@ #include "crypto/aes.h" int GarbleBNLowMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, - BIGNUM* p_input,BIGNUM* g_init, - BIGNUM* g_input, uint64_t clock_cycles, - const string& output_mask, OutputMode output_mode, - block* init_labels, block* input_labels, - block* output_labels, short* output_vals, BIGNUM* output_bn, - block R, block global_key, bool disable_OT, int connfd); + BIGNUM* p_input, BIGNUM* g_init, BIGNUM* g_input, + uint64_t clock_cycles, const string& output_mask, + OutputMode output_mode, BIGNUM* output_bn, block R, + block global_key, bool disable_OT, int connfd); int EvaluateBNLowMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, - BIGNUM* p_input,BIGNUM* e_init, - BIGNUM* e_input, uint64_t clock_cycles, - const string& output_mask, OutputMode output_mode, - block* init_labels, block* input_labels, - block* output_labels, short* output_vals, - BIGNUM* output_bn, block global_key, bool disable_OT, - int connfd); + BIGNUM* p_input, BIGNUM* e_init, BIGNUM* e_input, + uint64_t clock_cycles, const string& output_mask, + OutputMode output_mode, BIGNUM* output_bn, + block global_key, bool disable_OT, int connfd); uint64_t GarbleLowMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, - BIGNUM* p_input,block* init_labels, - block* input_labels, block* garbled_tables, - uint64_t *garbled_table_ind, block R, AES_KEY& AES_Key, - uint64_t cid, int connfd, BlockPair *wires, - short* wires_val, int* fanout, + BIGNUM* p_input, block* init_labels, block* input_labels, + block* garbled_tables, uint64_t *garbled_table_ind, + block R, AES_KEY& AES_Key, uint64_t cid, int connfd, + BlockPair *wires, short* wires_val, int* fanout, uint64_t* num_skipped_gates, block* output_labels, short* output_vals); -uint64_t EvaluateLowMem(const GarbledCircuit& garbled_circuit,BIGNUM* p_init, - BIGNUM* p_input, - block* init_labels, block* input_labels, - block* garbled_tables, uint64_t *garbled_table_ind, - AES_KEY& AES_Key, uint64_t cid, int connfd, - block *wires, short* wires_val, int* fanout, - block* output_labels, short* output_vals); +uint64_t EvaluateLowMem(const GarbledCircuit& garbled_circuit, BIGNUM* p_init, + BIGNUM* p_input, block* init_labels, + block* input_labels, block* garbled_tables, + uint64_t *garbled_table_ind, AES_KEY& AES_Key, + uint64_t cid, int connfd, block *wires, + short* wires_val, int* fanout, block* output_labels, + short* output_vals); int GarbleAllocLabels(const GarbledCircuit& garbled_circuit, block** init_labels, block** input_labels, block** output_labels, short** output_vals, block R); diff --git a/garbled_circuit/garbled_circuit_test.cpp b/garbled_circuit/garbled_circuit_test.cpp index 205fbf7..2608d4a 100644 --- a/garbled_circuit/garbled_circuit_test.cpp +++ b/garbled_circuit/garbled_circuit_test.cpp @@ -91,7 +91,7 @@ int Alice(const void* data, int connfd) { if (output_str != gc_data->output) { LOG(ERROR) << "Alice-side equality test failed " "(plain-text's != garble circuit's): " - << output_str << " != " << gc_data->output << endl; + << gc_data->output << " != " << output_str << endl; return FAILURE; } LOG(INFO) << "Equality passed: " << output_str << " == " << gc_data->output @@ -115,7 +115,7 @@ int Bob(const void *data, int connfd) { if (output_str != gc_data->output) { LOG(ERROR) << "Bob's side equality test failed " "(plain-text's != garble circuit's): " - << output_str << " != " << gc_data->output << endl; + << gc_data->output << " != " << output_str << endl; return FAILURE; } LOG(INFO) << "Equality passed: " << output_str << " == " << gc_data->output @@ -629,17 +629,16 @@ MU_TEST(PublicWire8Bit2cc) { string scd_file_address = string(TINYGARBLE_SOURCE_DIR) + "/scd/netlists/public_test_8bit_ncc.scd"; OutputMode output_mode = OutputMode::consecutive; - string p_init_str = "AB"; - string g_init_str = "18"; - string e_init_str = "B2"; - string p_input_str = "C3DE"; - string g_input_str = "1945"; - string e_input_str = "A5C6"; + string p_init_str = "AB"; // 8bit + string g_init_str = "18C3"; //16bit + string e_init_str = "B226B5F2"; //32bit + string p_input_str = "B5C4C3DE95464A5C"; //2*32bit + string g_input_str = "19458C20"; //2*16bit + string e_input_str = "A5C6"; //2*8bit + string output_str; //2*32bit uint64_t clock_cycles = 2; bool disable_OT = false; - bool low_mem_foot = false; - - string output_str = ""; + bool low_mem_foot; LOG(INFO) << "Public Wire test (8-bit 2cc)" << endl; @@ -648,7 +647,10 @@ MU_TEST(PublicWire8Bit2cc) { e_input_str, clock_cycles, output_mode, &output_str); mu_assert(ret == SUCCESS, "EvalauatePlaintextStr"); + LOG(INFO) << "output_str = " << output_str << endl; + // + low_mem_foot = false; GCTestStruct garbler_data = MakeGCTestStruct(scd_file_address, p_init_str, p_input_str, g_init_str, g_input_str, "0", "0", @@ -663,24 +665,36 @@ MU_TEST(PublicWire8Bit2cc) { ret = TcpipTestRun(Alice, (void *) &garbler_data, Bob, (void *) &eval_data); mu_assert(ret == SUCCESS, "TcpipTestRun"); - mu_check(output_str == "235C"); + // + low_mem_foot = true; + garbler_data = MakeGCTestStruct(scd_file_address, p_init_str, p_input_str, + g_init_str, g_input_str, "0", "0", + output_mode, disable_OT, low_mem_foot, + clock_cycles); + eval_data = MakeGCTestStruct(scd_file_address, p_init_str, p_input_str, + e_init_str, e_input_str, output_str, "0", + output_mode, disable_OT, low_mem_foot, + clock_cycles); + + ret = TcpipTestRun(Alice, (void *) &garbler_data, Bob, (void *) &eval_data); + mu_assert(ret == SUCCESS, "TcpipTestRun"); } MU_TEST_SUITE(TestSuite) { MU_SUITE_CONFIGURE(&TestSetup, &TestTeardown); - MU_RUN_TEST(Mux8Bit1cc); - MU_RUN_TEST(Sum1Bit8cc); - MU_RUN_TEST(Sum8Bit1cc); - MU_RUN_TEST(Hamming32Bit1cc); - MU_RUN_TEST(Hamming32Bit8cc); - MU_RUN_TEST(Hamming32Bit8ccDisabledOT); - MU_RUN_TEST(Hamming32Bit8ccWithMask); - MU_RUN_TEST(Hamming32Bit8ccDisabledOTLowMem); - MU_RUN_TEST(Hamming32Bit8ccLowMem); - MU_RUN_TEST(NonSecret8bit3cc); -// MU_RUN_TEST(PublicWire8Bit2cc); +// MU_RUN_TEST(Mux8Bit1cc); +// MU_RUN_TEST(Sum1Bit8cc); +// MU_RUN_TEST(Sum8Bit1cc); +// MU_RUN_TEST(Hamming32Bit1cc); +// MU_RUN_TEST(Hamming32Bit8cc); +// MU_RUN_TEST(Hamming32Bit8ccDisabledOT); +// MU_RUN_TEST(Hamming32Bit8ccWithMask); +// MU_RUN_TEST(Hamming32Bit8ccDisabledOTLowMem); +// MU_RUN_TEST(Hamming32Bit8ccLowMem); +// MU_RUN_TEST(NonSecret8bit3cc); + MU_RUN_TEST(PublicWire8Bit2cc); } diff --git a/garbled_circuit/garbled_circuit_util.h b/garbled_circuit/garbled_circuit_util.h index c135c4d..69a4665 100644 --- a/garbled_circuit/garbled_circuit_util.h +++ b/garbled_circuit/garbled_circuit_util.h @@ -100,6 +100,13 @@ typedef struct GarbledCircuit { return get_init_size() + get_input_size() + dff_size + gate_size; } + inline uint64_t get_secret_init_size() const { + return g_init_size + e_init_size; + } + inline uint64_t get_secret_input_size() const { + return g_input_size + e_input_size; + } + /** * indexing structure: * 0.p_init diff --git a/scd/scd_evaluator_test.cpp b/scd/scd_evaluator_test.cpp index eb073bb..169cd18 100644 --- a/scd/scd_evaluator_test.cpp +++ b/scd/scd_evaluator_test.cpp @@ -212,13 +212,13 @@ MU_TEST(PublicWire8Bit2cc) { string scd_file_address = string(TINYGARBLE_SOURCE_DIR) + "/scd/netlists/public_test_8bit_ncc.scd"; OutputMode output_mode = OutputMode::consecutive; - string p_init_str = "AB"; - string g_init_str = "18"; - string e_init_str = "B2"; - string p_input_str = "C3DE"; - string g_input_str = "1945"; - string e_input_str = "A5C6"; - string output_str; + string p_init_str = "AB"; // 8bit + string g_init_str = "18C3"; //16bit + string e_init_str = "B226B5F2"; //32bit + string p_input_str = "B5C4C3DE95464A5C"; //2*32bit + string g_input_str = "19458C20"; //2*16bit + string e_input_str = "A5C6"; //2*8bit + string output_str; //2*32bit uint64_t clock_cycles = 2; LOG(INFO) << "Public Wire test (8-bit 2cc)" << endl; @@ -230,7 +230,7 @@ MU_TEST(PublicWire8Bit2cc) { mu_assert(ret == SUCCESS, "EvalauatePlaintextStr"); LOG(INFO) << "result: " << output_str << endl; - mu_check(output_str == "235C"); + mu_check(output_str == "92A4B11E27606B20"); }