Skip to content

Commit

Permalink
fix couple of bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
esonghori committed Oct 27, 2015
1 parent 4a7818a commit 1354246
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 122 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scd/netlists/* linguist-vendored
circuit_synthesis/* linguist-vendored
111 changes: 54 additions & 57 deletions garbled_circuit/garbled_circuit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

#include "garbled_circuit/garbled_circuit.h"

#include <openssl/bn.h>
#include "scd/scd.h"
#include "scd/scd_evaluator.h"
#include "util/log.h"
Expand Down Expand Up @@ -779,8 +778,8 @@ int GarbleTransferOutput(const GarbledCircuit& garbled_circuit,
}
}
}
*output_str = OutputBN2Str(output_bn, clock_cycles,
garbled_circuit.output_size, output_mode);
OutputBN2Str(garbled_circuit, output_bn, clock_cycles, output_mode,
output_str);

BN_free(output_mask_bn);
BN_free(output_bn);
Expand Down Expand Up @@ -817,8 +816,8 @@ int EvaluateTransferOutput(const GarbledCircuit& garbled_circuit,
}
}

*output_str = OutputBN2Str(output_bn, clock_cycles,
garbled_circuit.output_size, output_mode);
OutputBN2Str(garbled_circuit, output_bn, clock_cycles, output_mode,
output_str);

BN_free(output_mask_bn);
BN_free(output_bn);
Expand Down Expand Up @@ -872,16 +871,17 @@ int GarbleStr(const string& scd_file_address, const string& init_str,
block global_key = RandomBlock();
CHECK(SendData(connfd, &global_key, sizeof(block))); // send global key

uint64_t g_time = 0;
uint64_t garbling_time = 0;
if (low_mem_foot && clock_cycles > 1) {
BlockPair *wires = nullptr;
CHECK_ALLOC(wires = new BlockPair[garbled_circuit.get_wire_size()]);
BIGNUM* output_bn = BN_new();

CHECK(
GarbleMakeInitLabels(garbled_circuit, &const_labels, &init_labels,
&input_labels, &output_labels, R));
GarbleAllocLabels(garbled_circuit, &const_labels, &init_labels,
&input_labels, &output_labels, R));

(*output_str) = "";
BlockPair *wires = nullptr;
CHECK_ALLOC(wires = new BlockPair[garbled_circuit.get_wire_size()]);
CHECK(GarbleGneInitLabels(garbled_circuit, const_labels, init_labels, R));

CHECK(
GarbleTransferInitLabels(garbled_circuit, const_labels, g_init,
Expand All @@ -891,31 +891,28 @@ int GarbleStr(const string& scd_file_address, const string& init_str,
AESSetEncryptKey((unsigned char *) &(global_key), 128, &AES_Key);
DUMP("r_key") << R << endl;
DUMP("r_key") << global_key << endl;

for (uint64_t cid = 0; cid < clock_cycles; cid++) {
CHECK(GarbleGenInputLabels(garbled_circuit, &input_labels, R));

CHECK(GarbleGenInputLabels(garbled_circuit, input_labels, R));

CHECK(
GarbleTransferInputLabels(garbled_circuit, g_input, input_labels, cid,
disable_OT, connfd));

g_time += GarbleLowMem(garbled_circuit, const_labels, init_labels,
input_labels, R, AES_Key, cid, connfd, wires,
output_labels);

string output_str_cid = "";
garbling_time += GarbleLowMem(garbled_circuit, const_labels, init_labels,
input_labels, R, AES_Key, cid, connfd,
wires, output_labels);
CHECK(
GarbleTransferOutputLowMem(garbled_circuit, output_labels, cid,
output_mask, &output_str_cid, connfd));
if (output_mode == 0) {
(*output_str) += output_str_cid;
} else if (output_mode == 1) {
(*output_str) += output_str_cid + "\n";
} else if (output_mode == 2) {
if (cid == clock_cycles - 1) {
(*output_str) = output_str_cid;
}
}
output_mode, output_mask, output_bn,
connfd));

}
OutputBN2StrLowMem(garbled_circuit, output_bn, clock_cycles, output_mode,
output_str);
BN_free(output_bn);

delete[] wires;

} else {
Expand All @@ -928,8 +925,9 @@ int GarbleStr(const string& scd_file_address, const string& init_str,
g_input, input_labels, clock_cycles, disable_OT,
connfd));

g_time = Garble(garbled_circuit, const_labels, init_labels, input_labels,
global_key, R, clock_cycles, connfd, output_labels);
garbling_time = Garble(garbled_circuit, const_labels, init_labels,
input_labels, global_key, R, clock_cycles, connfd,
output_labels);
CHECK(
GarbleTransferOutput(garbled_circuit, output_labels, clock_cycles,
output_mask, output_mode, output_str, connfd));
Expand All @@ -941,10 +939,11 @@ int GarbleStr(const string& scd_file_address, const string& init_str,
delete[] input_labels;
delete[] output_labels;

LOG(INFO) << "Total Garbling time (cc) = " << g_time
<< "\tGarbling time per gate (cc/gate) = "
<< (g_time) / ((double) garbled_circuit.gate_size * clock_cycles)
<< endl;
LOG(INFO)
<< "Total Garbling time (cc) = " << garbling_time
<< "\tGarbling time per gate (cc/gate) = "
<< (garbling_time) / ((double) garbled_circuit.gate_size * clock_cycles)
<< endl;
RemoveGarbledCircuit(&garbled_circuit);
ServerClose(connfd);

Expand Down Expand Up @@ -982,15 +981,15 @@ int EvaluateStr(const string& scd_file_address, const string& init_str,
block global_key = RandomBlock();
CHECK(RecvData(connfd, &global_key, sizeof(block))); // receive global key

uint64_t e_time = 0;
uint64_t evaluation_time = 0;
if (low_mem_foot && clock_cycles > 1) {
(*output_str) = "";
block *wires = nullptr;
CHECK_ALLOC(wires = new block[garbled_circuit.get_wire_size()]);
BIGNUM* output_bn = BN_new();

CHECK(
EvaluateMakeInitLabels(garbled_circuit, &const_labels, &init_labels,
&input_labels, &output_labels));
EvaluateAllocLabels(garbled_circuit, &const_labels, &init_labels,
&input_labels, &output_labels));

CHECK(
EvaluateTransferInitLabels(garbled_circuit, const_labels, e_init,
Expand All @@ -999,30 +998,26 @@ int EvaluateStr(const string& scd_file_address, const string& init_str,
AES_KEY AES_Key;
AESSetEncryptKey((unsigned char *) &(global_key), 128, &AES_Key);
DUMP("r_key") << global_key << endl;

for (uint64_t cid = 0; cid < clock_cycles; cid++) {

CHECK(
EvaluateTransferInputLabels(garbled_circuit, e_input, input_labels,
cid, disable_OT, connfd));

e_time += EvaluateLowMem(garbled_circuit, const_labels, init_labels,
input_labels, AES_Key, cid, connfd, wires,
output_labels);
evaluation_time += EvaluateLowMem(garbled_circuit, const_labels,
init_labels, input_labels, AES_Key, cid,
connfd, wires, output_labels);

string output_str_cid = "";
CHECK(
EvaluateTransferOutputLowMem(garbled_circuit, output_labels, cid,
output_mask, &output_str_cid, connfd));
if (output_mode == 0) {
(*output_str) += output_str_cid;
} else if (output_mode == 1) {
(*output_str) += output_str_cid + "\n";
} else if (output_mode == 2) {
if (cid == clock_cycles - 1) {
(*output_str) = output_str_cid;
}
}
output_mode, output_mask, output_bn,
connfd));
}
OutputBN2StrLowMem(garbled_circuit, output_bn, clock_cycles, output_mode,
output_str);
BN_free(output_bn);

delete[] wires;
} else {
CHECK(
Expand All @@ -1034,8 +1029,9 @@ int EvaluateStr(const string& scd_file_address, const string& init_str,
init_labels, e_input, input_labels, clock_cycles,
disable_OT, connfd));

e_time = Evaluate(garbled_circuit, const_labels, init_labels, input_labels,
global_key, clock_cycles, connfd, output_labels);
evaluation_time = Evaluate(garbled_circuit, const_labels, init_labels,
input_labels, global_key, clock_cycles, connfd,
output_labels);

CHECK(
EvaluateTransferOutput(garbled_circuit, output_labels, clock_cycles,
Expand All @@ -1049,10 +1045,11 @@ int EvaluateStr(const string& scd_file_address, const string& init_str,
delete[] init_labels;
delete[] input_labels;
delete[] output_labels;
LOG(INFO) << "Total Evaluating time (cc) = " << e_time
<< "\tEvaluate time per gate (cc/gate) = "
<< (e_time) / ((double) garbled_circuit.gate_size * clock_cycles)
<< endl;
LOG(INFO)
<< "Total Evaluating time (cc) = " << evaluation_time
<< "\tEvaluate time per gate (cc/gate) = "
<< (evaluation_time) / ((double) garbled_circuit.gate_size * clock_cycles)
<< endl;
RemoveGarbledCircuit(&garbled_circuit);
ClientClose(connfd);

Expand Down
4 changes: 4 additions & 0 deletions garbled_circuit/garbled_circuit.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

#include <cstdint>
#include <string>
#include <openssl/bn.h>
#include "crypto/block.h"

using std::string;
Expand Down Expand Up @@ -165,6 +166,9 @@ typedef struct GarbledCircuit {

} GarbledCircuit;

int OutputBN2Str(const GarbledCircuit& garbled_circuit, BIGNUM* outputs,
uint64_t clock_cycles, int output_mode, string *output_str);

int GarbleStr(const string& scd_file_address, const string& init_str,
const string& input_str, uint64_t clock_cycles,
const string& output_mask, int output_mode, bool disable_OT,
Expand Down
82 changes: 53 additions & 29 deletions garbled_circuit/garbled_circuit_low_mem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,9 @@ uint64_t EvaluateLowMem(const GarbledCircuit& garbled_circuit,
return (end_time - start_time);
}

int GarbleMakeInitLabels(const GarbledCircuit& garbled_circuit,
block** const_labels, block** init_labels,
block** input_labels, block** output_labels, block R) {
int GarbleAllocLabels(const GarbledCircuit& garbled_circuit,
block** const_labels, block** init_labels,
block** input_labels, block** output_labels, block R) {

(*const_labels) = nullptr;
CHECK_ALLOC((*const_labels) = new block[2 * 2]);
Expand Down Expand Up @@ -425,20 +425,36 @@ int GarbleMakeInitLabels(const GarbledCircuit& garbled_circuit,
return SUCCESS;
}

int GarbleGneInitLabels(const GarbledCircuit& garbled_circuit,
block* const_labels, block* init_labels, block R) {

for (uint i = 0; i < 2; i++) {
const_labels[i * 2 + 0] = RandomBlock();
const_labels[i * 2 + 1] = XorBlock(R, const_labels[i * 2 + 0]);
}

for (uint i = 0; i < garbled_circuit.get_init_size(); i++) {
init_labels[i * 2 + 0] = RandomBlock();
init_labels[i * 2 + 1] = XorBlock(R, init_labels[i * 2 + 0]);
}

return SUCCESS;
}

int GarbleGenInputLabels(const GarbledCircuit& garbled_circuit,
block** input_labels, block R) {
block* input_labels, block R) {
if (garbled_circuit.get_input_size() > 0) {
for (uint i = 0; i < garbled_circuit.get_input_size(); i++) {
(*input_labels)[i * 2 + 0] = RandomBlock();
(*input_labels)[i * 2 + 1] = XorBlock(R, (*input_labels)[i * 2 + 0]);
input_labels[i * 2 + 0] = RandomBlock();
input_labels[i * 2 + 1] = XorBlock(R, input_labels[i * 2 + 0]);
}
}
return SUCCESS;
}

int EvaluateMakeInitLabels(const GarbledCircuit& garbled_circuit,
block** const_labels, block** init_labels,
block** input_labels, block** output_labels) {
int EvaluateAllocLabels(const GarbledCircuit& garbled_circuit,
block** const_labels, block** init_labels,
block** input_labels, block** output_labels) {
(*const_labels) = nullptr;
CHECK_ALLOC((*const_labels) = new block[2]);

Expand Down Expand Up @@ -742,12 +758,20 @@ int EvaluateTransferInputLabels(const GarbledCircuit& garbled_circuit,

int GarbleTransferOutputLowMem(const GarbledCircuit& garbled_circuit,
block* output_labels, uint64_t cid,
const string& output_mask, string* output_str,
int connfd) {
int output_mode, const string& output_mask,
BIGNUM* output_bn, int connfd) {
BIGNUM* output_mask_bn = BN_new();
BN_hex2bn(&output_mask_bn, output_mask.c_str());

BIGNUM* output_bn = BN_new();
uint64_t output_bit_offset = 0;
if (output_mode == 0) { // normal mode, keep all the bits.
output_bit_offset = cid * garbled_circuit.output_size;
} else if (output_mode == 1) { // Separated by clock mode, keep all the bits.
output_bit_offset = cid * garbled_circuit.output_size;
} else if (output_mode == 2) { // keep the last cycle, overwrite the bits.
output_bit_offset = 0;
}

for (uint64_t i = 0; i < garbled_circuit.output_size; i++) {
short garble_output_type = get_LSB(output_labels[(i) * 2 + 0]);
short eval_output_type;
Expand All @@ -756,33 +780,37 @@ int GarbleTransferOutputLowMem(const GarbledCircuit& garbled_circuit,
|| BN_is_bit_set(output_mask_bn, cid * garbled_circuit.output_size + i)
== 0) {
CHECK(SendData(connfd, &garble_output_type, sizeof(short)));
BN_clear_bit(output_bn, i);
BN_clear_bit(output_bn, output_bit_offset + i);
} else {
CHECK(RecvData(connfd, &eval_output_type, sizeof(short)));
if (eval_output_type != garble_output_type) {
BN_set_bit(output_bn, i);
BN_set_bit(output_bn, output_bit_offset + i);
} else {
BN_clear_bit(output_bn, i);
BN_clear_bit(output_bn, output_bit_offset + i);
}
}
}

const char* output_c = BN_bn2hex(output_bn);
*output_str = output_c;

BN_free(output_mask_bn);
BN_free(output_bn);
return SUCCESS;
}

int EvaluateTransferOutputLowMem(const GarbledCircuit& garbled_circuit,
block* output_labels, uint64_t cid,
const string& output_mask, string* output_str,
int connfd) {
int output_mode, const string& output_mask,
BIGNUM* output_bn, int connfd) {
BIGNUM* output_mask_bn = BN_new();
BN_hex2bn(&output_mask_bn, output_mask.c_str());

BIGNUM* output_bn = BN_new();
uint64_t output_bit_offset = 0;
if (output_mode == 0) { // normal mode, keep all the bits.
output_bit_offset = cid * garbled_circuit.output_size;
} else if (output_mode == 1) { // Separated by clock mode, keep all the bits.
output_bit_offset = cid * garbled_circuit.output_size;
} else if (output_mode == 2) { // keep the last cycle, overwrite the bits.
output_bit_offset = 0;
}

for (uint64_t i = 0; i < garbled_circuit.output_size; i++) {
short garble_output_type;
short eval_output_type = get_LSB(output_labels[i]);
Expand All @@ -792,21 +820,17 @@ int EvaluateTransferOutputLowMem(const GarbledCircuit& garbled_circuit,
== 0) {
CHECK(RecvData(connfd, &garble_output_type, sizeof(short)));
if (eval_output_type != garble_output_type) {
BN_set_bit(output_bn, i);
BN_set_bit(output_bn, output_bit_offset + i);
} else {
BN_clear_bit(output_bn, i);
BN_clear_bit(output_bn, output_bit_offset + i);
}
} else {
CHECK(SendData(connfd, &eval_output_type, sizeof(short)));
BN_clear_bit(output_bn, i);
BN_clear_bit(output_bn, output_bit_offset + i);
}
}

const char* output_c = BN_bn2hex(output_bn);
*output_str = output_c;

BN_free(output_mask_bn);
BN_free(output_bn);
return SUCCESS;
}

Loading

0 comments on commit 1354246

Please sign in to comment.