Skip to content

Commit

Permalink
simplify base svole (from ferret)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlweng committed Jan 21, 2024
1 parent 9816549 commit c2d1a16
Showing 1 changed file with 12 additions and 82 deletions.
94 changes: 12 additions & 82 deletions emp-zk/emp-vole-f2k/base_svole.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,102 +14,32 @@ template <typename IO> class BaseSVoleF2k {

GaloisFieldPacking pack;

int ferret_cnt, f2k_cnt;
block *ferret_buffer = nullptr;
block *f2k_val_buffer = nullptr;
block *f2k_mac_buffer = nullptr;

int64_t FERRET_BUFFER_MEM_SZ;
int64_t FERRET_BUFFER_SZ;
int64_t F2K_BUFFER_SZ;

BaseSVoleF2k(int party, IO **ios, FerretCOT<IO> *ferret)
: party(party), ios(ios), ferret(ferret) {
FERRET_BUFFER_MEM_SZ = ferret_b13.n;
FERRET_BUFFER_SZ = ferret_b13.buf_sz();
F2K_BUFFER_SZ = ferret_b13.buf_sz() / 128;

if (party == BOB)
delta = ferret->Delta;
io = ios[0];

ferret_cnt = 0;
f2k_cnt = 0;

ferret_buffer = new block[FERRET_BUFFER_MEM_SZ];
if (party == ALICE) {
f2k_val_buffer = new block[F2K_BUFFER_SZ];
}
f2k_mac_buffer = new block[F2K_BUFFER_SZ];

ferret_buffer_refill();
pre_f2k_buffer_refill();
}

~BaseSVoleF2k() {
delete[] ferret_buffer;
if (party == ALICE)
delete[] f2k_val_buffer;
delete[] f2k_mac_buffer;
}

void extend(block *val, block *mac, int num) {
int buf_left = F2K_BUFFER_SZ - f2k_cnt;
int rd1_n = (buf_left >= num) ? num : buf_left;
int rd_n = 0, rdext_n = 0;
if (rd1_n < num) {
int tmp = num - rd1_n;
rd_n = tmp / F2K_BUFFER_SZ;
rdext_n = tmp % F2K_BUFFER_SZ;
}
int pt = rd1_n;
if (party == ALICE) {
memcpy(val, f2k_val_buffer + f2k_cnt, rd1_n * sizeof(block));
memcpy(mac, f2k_mac_buffer + f2k_cnt, rd1_n * sizeof(block));
for (int i = 0; i < rd_n; ++i) {
pre_f2k_buffer_refill();
memcpy(val + pt, f2k_val_buffer, F2K_BUFFER_SZ * sizeof(block));
memcpy(mac + pt, f2k_mac_buffer, F2K_BUFFER_SZ * sizeof(block));
pt += F2K_BUFFER_SZ;
block *ferret_buffer = new block[num*128];
ferret->rcot(ferret_buffer, num*128);
std::size_t j = 0;
for(std::size_t i = 0; i < num; ++i) {
bool val_b[128];
if(party == ALICE) {
for(int i = 0; i < 128; ++i)
val_b[i] = getLSB(ferret_buffer[j+i]);
val[i] = bool_to_block(val_b);
}
pre_f2k_buffer_refill();
memcpy(val + pt, f2k_val_buffer, rdext_n * sizeof(block));
memcpy(mac + pt, f2k_mac_buffer, rdext_n * sizeof(block));
} else {
memcpy(mac, f2k_mac_buffer + f2k_cnt, rd1_n * sizeof(block));
for (int i = 0; i < rd_n; ++i) {
pre_f2k_buffer_refill();
memcpy(mac + pt, f2k_mac_buffer, F2K_BUFFER_SZ * sizeof(block));
pt += F2K_BUFFER_SZ;
}
pre_f2k_buffer_refill();
memcpy(mac + pt, f2k_mac_buffer, rdext_n * sizeof(block));
}
f2k_cnt = rdext_n;
}

void pre_f2k_buffer_refill() {
int f2k_cnt_tmp = 0;
while (f2k_cnt_tmp < F2K_BUFFER_SZ) {
if ((ferret_cnt + 128) > FERRET_BUFFER_SZ) {
ferret_buffer_refill();
}
if (party == ALICE) {
bool val[128];
for (int i = 0; i < 128; ++i)
val[i] = getLSB(ferret_buffer[ferret_cnt + i]);
f2k_val_buffer[f2k_cnt_tmp] = bool_to_block(val);
}
pack.packing(f2k_mac_buffer + f2k_cnt_tmp, ferret_buffer + ferret_cnt);
ferret_cnt += 128;
f2k_cnt_tmp++;
pack.packing(mac+i, ferret_buffer+j);
j += 128;
}
f2k_cnt = 0;
}

void ferret_buffer_refill() {
ferret->rcot_inplace(ferret_buffer, FERRET_BUFFER_MEM_SZ);
ferret_cnt = 0;
delete[] ferret_buffer;
}

// DEBUG
Expand Down

0 comments on commit c2d1a16

Please sign in to comment.