From c2d1a166c07bcbe015375bc8faa26f014810fc6b Mon Sep 17 00:00:00 2001 From: carlweng Date: Sun, 21 Jan 2024 15:38:02 -0600 Subject: [PATCH] simplify base svole (from ferret) --- emp-zk/emp-vole-f2k/base_svole.h | 94 ++++---------------------------- 1 file changed, 12 insertions(+), 82 deletions(-) diff --git a/emp-zk/emp-vole-f2k/base_svole.h b/emp-zk/emp-vole-f2k/base_svole.h index 35de44e..c095b57 100644 --- a/emp-zk/emp-vole-f2k/base_svole.h +++ b/emp-zk/emp-vole-f2k/base_svole.h @@ -14,102 +14,32 @@ template 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 *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