Skip to content

Commit

Permalink
Allow seed and choice input to Ferret
Browse files Browse the repository at this point in the history
  • Loading branch information
radhika1601 committed Aug 24, 2024
1 parent eb0daf2 commit 31bd147
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
14 changes: 10 additions & 4 deletions emp-ot/ferret/base_cot.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class BaseCot { public:
}
}

void cot_gen(block *ot_data, int64_t size) {
void cot_gen(block *ot_data, int64_t size, bool * pre_bool = nullptr) {
if (this->party == ALICE) {
iknp->send_cot(ot_data, size);
io->flush();
Expand All @@ -59,7 +59,10 @@ class BaseCot { public:
} else {
PRG prg;
bool *pre_bool_ini = new bool[size];
prg.random_bool(pre_bool_ini, size);
if(pre_bool && !malicious)
memcpy(pre_bool_ini, pre_bool, size);
else
prg.random_bool(pre_bool_ini, size);
iknp->recv_cot(ot_data, pre_bool_ini, size);
block ch[2];
ch[0] = zero_block;
Expand All @@ -71,7 +74,7 @@ class BaseCot { public:
}
}

void cot_gen(OTPre<IO> *pre_ot, int64_t size) {
void cot_gen(OTPre<IO> *pre_ot, int64_t size, bool * pre_bool = nullptr) {
block *ot_data = new block[size];
if (this->party == ALICE) {
iknp->send_cot(ot_data, size);
Expand All @@ -82,7 +85,10 @@ class BaseCot { public:
} else {
PRG prg;
bool *pre_bool_ini = new bool[size];
prg.random_bool(pre_bool_ini, size);
if(pre_bool && !malicious)
memcpy(pre_bool_ini, pre_bool, size);
else
prg.random_bool(pre_bool_ini, size);
iknp->recv_cot(ot_data, pre_bool_ini, size);
block ch[2];
ch[0] = zero_block;
Expand Down
8 changes: 4 additions & 4 deletions emp-ot/ferret/ferret_cot.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ PrimalLPNParameter param = ferret_b13, std::string pre_file="");

~FerretCOT();

void setup(block Deltain, std::string pre_file = "");
void setup(block Deltain, std::string pre_file = "", bool *choice=nullptr, block seed=zero_block);

void setup(std::string pre_file = "");
void setup(std::string pre_file = "", bool *choice = nullptr, block seed= zero_block);

void send_cot(block * data, int64_t length) override;

void recv_cot(block* data, const bool * b, int64_t length) override;

void rcot(block *data, int64_t num);

int64_t rcot_inplace(block *ot_buffer, int64_t length);
int64_t rcot_inplace(block *ot_buffer, int64_t length, block seed = zero_block);

int64_t byte_memory_need_inplace(int64_t ot_need);

Expand Down Expand Up @@ -81,7 +81,7 @@ PrimalLPNParameter param = ferret_b13, std::string pre_file="");
void extend_initialization();

void extend(block* ot_output, MpcotReg<T> *mpfss, OTPre<T> *preot,
LpnF2<T, 10> *lpn, block *ot_input);
LpnF2<T, 10> *lpn, block *ot_input, block seed = zero_block);

void extend_f2k(block *ot_buffer);

Expand Down
32 changes: 21 additions & 11 deletions emp-ot/ferret/ferret_cot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ void FerretCOT<T>::extend_initialization() {
// extend f2k in detail
template<typename T>
void FerretCOT<T>::extend(block* ot_output, MpcotReg<T> *mpcot, OTPre<T> *preot,
LpnF2<T, 10> *lpn, block *ot_input) {
LpnF2<T, 10> *lpn, block *ot_input, block seed) {
if(party == ALICE) mpcot->sender_init(Delta);
else mpcot->recver_init();
mpcot->mpcot(ot_output, preot, ot_input);
lpn->compute(ot_output, ot_input+mpcot->consist_check_cot_num);
lpn->compute(ot_output, ot_input+mpcot->consist_check_cot_num, seed);
}

// extend f2k (customized location)
Expand All @@ -81,14 +81,15 @@ void FerretCOT<T>::extend_f2k() {
}

template<typename T>
void FerretCOT<T>::setup(block Deltain, std::string pre_file) {
void FerretCOT<T>::setup(block Deltain, std::string pre_file, bool *choice, block seed) {
this->Delta = Deltain;
setup(pre_file);
if(this->is_malicious) seed = zero_block;
setup(pre_file, choice, seed);
ch[1] = Delta;
}

template<typename T>
void FerretCOT<T>::setup(std::string pre_file) {
void FerretCOT<T>::setup(std::string pre_file, bool *choice, block seed) {
if(pre_file != "") pre_ot_filename = pre_file;
else {
pre_ot_filename=(party==ALICE?PRE_OT_DATA_REG_SEND_FILE:PRE_OT_DATA_REG_RECV_FILE);
Expand Down Expand Up @@ -123,10 +124,18 @@ void FerretCOT<T>::setup(std::string pre_file) {

block *pre_data_ini = new block[param.k_pre+mpcot_ini.consist_check_cot_num];
memset(this->ot_pre_data, 0, param.n_pre*16);

base_cot->cot_gen(&pre_ot_ini, pre_ot_ini.n);
base_cot->cot_gen(pre_data_ini, param.k_pre+mpcot_ini.consist_check_cot_num);
extend(ot_pre_data, &mpcot_ini, &pre_ot_ini, &lpn, pre_data_ini);
if(this->is_malicious){
seed = zero_block;
choice = nullptr;
}
if(choice){
base_cot->cot_gen(&pre_ot_ini, pre_ot_ini.n, choice);
base_cot->cot_gen(pre_data_ini, param.k_pre + mpcot_ini.consist_check_cot_num, choice+pre_ot_ini.n);
}else {
base_cot->cot_gen(&pre_ot_ini, pre_ot_ini.n);
base_cot->cot_gen(pre_data_ini, param.k_pre + mpcot_ini.consist_check_cot_num);
}
extend(ot_pre_data, &mpcot_ini, &pre_ot_ini, &lpn, pre_data_ini, seed);
delete[] pre_data_ini;
}

Expand Down Expand Up @@ -221,7 +230,7 @@ int64_t FerretCOT<T>::byte_memory_need_inplace(int64_t ot_need) {
// parameter "length" should be the return of "byte_memory_need_inplace"
// output the number of COTs that can be used
template<typename T>
int64_t FerretCOT<T>::rcot_inplace(block *ot_buffer, int64_t byte_space) {
int64_t FerretCOT<T>::rcot_inplace(block *ot_buffer, int64_t byte_space, block seed) {
if(byte_space < param.n) error("space not enough");
if((byte_space - M) % ot_limit != 0) error("call byte_memory_need_inplace \
to get the correct length of memory space");
Expand All @@ -232,7 +241,8 @@ int64_t FerretCOT<T>::rcot_inplace(block *ot_buffer, int64_t byte_space) {
if(party == ALICE)
pre_ot->send_pre(ot_pre_data, Delta);
else pre_ot->recv_pre(ot_pre_data);
extend(pt, mpcot, pre_ot, lpn_f2, ot_pre_data);
if(this->is_malicious) seed = zero_block;
extend(pt, mpcot, pre_ot, lpn_f2, ot_pre_data, seed);
pt += ot_limit;
memcpy(ot_pre_data, pt, M*sizeof(block));
}
Expand Down
5 changes: 3 additions & 2 deletions emp-ot/ferret/lpn_f2.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ class LpnF2 { public:
__compute1(nn, kk, j, &prp);
}

void compute(block * nn, const block * kk) {
void compute(block * nn, const block * kk, block s = zero_block) {
vector<std::future<void>> fut;
int64_t width = n/threads;
seed = seed_gen();
if(!cmpBlock(&s, &zero_block, 1)) seed = s;
else seed = seed_gen();
for(int i = 0; i < threads - 1; ++i) {
int64_t start = i * width;
int64_t end = min((i+1)* width, n);
Expand Down

0 comments on commit 31bd147

Please sign in to comment.