From 7190dc428829a71cfdbdb979c98814c7eb789a75 Mon Sep 17 00:00:00 2001 From: Xiao Wang Date: Sun, 5 Sep 2021 00:02:00 -0500 Subject: [PATCH] Improvedlpn (#15) * improved lpnb * update * update perf * update perf * reduce to on thread in test Co-authored-by: Ubuntu --- README.md | 15 ++--- emp-zk/emp-vole/lpn.h | 152 ++++++++++++++++++++++++++---------------- test/vole/lpn.cpp | 3 +- 3 files changed, 105 insertions(+), 65 deletions(-) diff --git a/README.md b/README.md index eeb1f09..3cd8ba2 100644 --- a/README.md +++ b/README.md @@ -46,17 +46,16 @@ All values are for "million gates per second". ##### Boolean circuits |Threads|10 Mbps|20 Mbps|30 Mbps|50 Mbps|Localhost| |-------|-------|-------|-------|-------|---------| -|1|4.4|6.2|7.0|7.5|7.6| -|2|5.3|8.1|9.9|11.8|11.8| -|3|5.7|9.1|11.4|13.9|14.3| -|4|5.8|9.9|12.2|14.9|15.8| +|1|5.1|7.8|8.6|8.6|8.6| +|2|6|10|12.9|14.3|13.6| +|3|6.3|10.9|14.5|17.3|18| +|4|6.4|11.4|15.1|19|19.4| ##### Arithmetic circuits |Threads|100 Mbps|500 Mbps|1 Gbps|2 Gbps|Localhost| |-------|-------|-------|-------|-------|---------| -|1|1.2|3.4|4.2|4.8|4.8| -|2|1.3|4.4|6.1|7.0|7.1| -|3|1.4|4.9|7.2|8.4|8.4| -|4|1.4|5.0|7.5|8.9|8.9| +|1|1.4|4.8|6.8|7.8|7.8| +|2|1.4|5.6|8.7|10.2|10.4| +|3|1.4|5.9|9.3|11.7|12.5| Question diff --git a/emp-zk/emp-vole/lpn.h b/emp-zk/emp-vole/lpn.h index 8f14b4f..a8f68bf 100644 --- a/emp-zk/emp-vole/lpn.h +++ b/emp-zk/emp-vole/lpn.h @@ -12,108 +12,148 @@ class LpnFp { public: int threads; block seed; - int round, leftover; - __uint128_t *M; const __uint128_t *preM, *prex; __uint128_t *K; const __uint128_t *preK; uint32_t k_mask; - LpnFp (int n, int k, ThreadPool * pool, int threads, block seed = zero_block) { this->k = k; this->n = n; this->pool = pool; this->threads = threads; this->seed = seed; - - round = d / 4; - leftover = d % 4; - this->k_mask = k_mask_gen(k); + k_mask = 1; + while(k_mask < (uint32_t)k) { + k_mask <<=1; + k_mask = k_mask | 0x1; + } } - uint32_t k_mask_gen(int kin) { - int ksz = kin; - int sz = 0; - while(ksz > 1) { - sz++; - ksz = ksz>>1; - } - return (1< add_func) { - block tmp[30]; - for(int m = 0; m < 30; ++m) + + void __compute4(int i, PRP *prp, std::function add_func) { + block tmp[10]; + for(int m = 0; m < 10; ++m) tmp[m] = makeBlock(i, m); - prp->permute_block(tmp, 30); - uint32_t* r = (uint32_t*)(tmp); - uint64_t* mult = (uint64_t*)(tmp+10); - for(int m = 0; m < 4; ++m) { - int index[d]; - for (int j = 0; j < d; ++j) { - index[j] = r[m*d+j]&k_mask; - mult[m*d+j] = mod(mult[m*d+j]); - } - add_func(i+m, index, mult+m*d); + prp->permute_block(tmp, 10); + int* index = (int*)(tmp); + for(int j = 0; j < 4*d; ++j) { + index[j] = index[j]&k_mask; + index[j] = index[j] >= k? index[j]-k:index[j]; } + add_func(i, index); } - void __compute1(int i, PRP *prp, std::function add_func) { - block tmp[8]; - for(int m = 0; m < 8; ++m) + void __compute1(int i, PRP *prp, std::function add_func) { + block tmp[3]; + for(int m = 0; m < 3; ++m) tmp[m] = makeBlock(i, m); - prp->permute_block(tmp, 8); + prp->permute_block(tmp, 3); uint32_t* r = (uint32_t*)(tmp); - uint64_t* mult = (uint64_t*)(tmp+3); - int index[d]; for (int j = 0; j < d; ++j) { index[j] = r[j]&k_mask; - mult[j] = mod(mult[j]); + index[j] = index[j] >= k? index[j]-k:index[j]; } - add_func(i, index, mult); + add_func(i, index); } void task(int start, int end) { PRP prp(seed); int j = start; if(party == 1) { - std::function add_func1 = std::bind(&LpnFp::add1, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); + std::function add_func1 = std::bind(&LpnFp::add1, this, std::placeholders::_1, std::placeholders::_2); + std::function add_func1s = std::bind(&LpnFp::add1_single, this, std::placeholders::_1, std::placeholders::_2); for(; j < end-4; j+=4) __compute4(j, &prp, add_func1); for(; j < end; ++j) - __compute1(j, &prp, add_func1); + __compute1(j, &prp, add_func1s); } else { - std::function add_func2 = std::bind(&LpnFp::add2, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); + std::function add_func2 = std::bind(&LpnFp::add2, this, std::placeholders::_1, std::placeholders::_2); + std::function add_func2s = std::bind(&LpnFp::add2_single, this, std::placeholders::_1, std::placeholders::_2); for(; j < end-4; j+=4) __compute4(j, &prp, add_func2); for(; j < end; ++j) - __compute1(j, &prp, add_func2); + __compute1(j, &prp, add_func2s); } } diff --git a/test/vole/lpn.cpp b/test/vole/lpn.cpp index da1fbe6..bc7ae47 100644 --- a/test/vole/lpn.cpp +++ b/test/vole/lpn.cpp @@ -35,8 +35,9 @@ void test_lpn(NetIO *io, int party) { Delta = Delta & ((__uint128_t)0xFFFFFFFFFFFFFFFFLL); Delta = mod(Delta, pr); + //test cases reduced for github action int test_n = 1016832/2; - int test_k = 15800; + int test_k = 158000/10; __uint128_t *mac1 = new __uint128_t[test_n]; __uint128_t *mac2 = new __uint128_t[test_k];