diff --git a/CMakeLists.txt b/CMakeLists.txt index 539390338f0345be990a76e6b1d161799a045825..d14c3faa9d10b19f060cd468ca55720b8e8894b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,8 @@ option(USE_AES_NI "Compile with AES NI" ON) option(USE_OPENMP "Compile with OpenMP" ON) +option(USE_ABY3_TRUNC1 "Compile with ABY3 truncate 1 algorithm" OFF) + ########################### the project build part ############################### message(STATUS "Using paddlepaddle installation of ${paddle_version}") message(STATUS "paddlepaddle include directory: ${PADDLE_INCLUDE}") @@ -84,6 +86,10 @@ if (USE_OPENMP) find_package(OpenMP REQUIRED) endif(USE_OPENMP) +if (USE_ABY3_TRUNC1) + add_compile_definitions(USE_ABY3_TRUNC1) +endif(USE_ABY3_TRUNC1) + add_subdirectory(core/privc3) add_subdirectory(core/paddlefl_mpc/mpc_protocol) add_subdirectory(core/paddlefl_mpc/operators) diff --git a/core/privc3/fixedpoint_tensor_imp.h b/core/privc3/fixedpoint_tensor_imp.h index cb508288b01c5d5a6625262d24b7d5f1887c5a65..e92bd6840c8789e1ce6558bf51986313ed788897 100644 --- a/core/privc3/fixedpoint_tensor_imp.h +++ b/core/privc3/fixedpoint_tensor_imp.h @@ -166,6 +166,7 @@ void FixedPointTensor::mul(const FixedPointTensor* rhs, mul_trunc(this, rhs, ret, &TensorAdapter::mul); } +#ifdef USE_ABY3_TRUNC1 //use aby3 trunc1 template void FixedPointTensor::truncate(const FixedPointTensor* op, FixedPointTensor* ret, @@ -208,7 +209,20 @@ void FixedPointTensor::truncate(const FixedPointTensor* op, return; } +#else // use truncate3 + // Protocol. `truncate3` +// motivation: +// truncates in aby3 may cause msb error with small probability +// the reason is that before rishft op, its masked value e.g., x' - r' may overflow in int64 +// so that, in `truncate3`, we limit r' in (-2^62, 2^62) to avoid the problem. + +// notice: +// when r' is contrainted in (-2^62, 2^62), +// the SD (statistical distance) of x' - r' between this +// and r' in Z_{2^64} is equal to |X| / (2^63 + |X|) + +// detail protocol: // P2 randomly generates r' \in (-2^62, 2^62), randomly generates r'_0, r_0, r_1 in Z_{2^64}, // P2 compute r'_1 = r' - r'_0, r_2 = r'/2^N - r_0 - r_1, let x2 = r_2 // P2 send r_0, r'_0 to P0, send r_1, r'_1 to P1 @@ -217,7 +231,7 @@ void FixedPointTensor::truncate(const FixedPointTensor* op, // P0 set x0 = r_0 // P0, P1, P2 invoke reshare() with inputs x0, x1, x2 respectively. template -void FixedPointTensor::truncate3(const FixedPointTensor* op, +void FixedPointTensor::truncate(const FixedPointTensor* op, FixedPointTensor* ret, size_t scaling_factor) { if (scaling_factor == 0) { @@ -232,10 +246,6 @@ void FixedPointTensor::truncate3(const FixedPointTensor* op, tensor_factory()->template create(op->shape())); } // r', contraint in (-2^62, 2^62) - // notice : when r' is contrainted in (-2^62, 2^62), - // the SD (statistical distance) of x - r' between this - // and r' in Z_{2^64} is equal to |X| / (2^63 + |X|) - // according to http://yuyu.hk/files/ho2.pdf aby3_ctx()->template gen_random_private(*temp[0]); int64_t contraint_upper = ~((uint64_t) 1 << 62); int64_t contraint_low = (uint64_t) 1 << 62; @@ -307,6 +317,7 @@ void FixedPointTensor::truncate3(const FixedPointTensor* op, tensor_carry_in->scaling_factor() = N; ret->add(tensor_carry_in.get(), ret); } +#endif //USE_ABY3_TRUNC1 template template @@ -345,7 +356,7 @@ void FixedPointTensor::mul_trunc(const FixedPointTensor* lhs, temp->copy(ret_no_trunc->_share[0]); reshare(temp.get(), ret_no_trunc->_share[1]); - truncate3(ret_no_trunc.get(), ret, N); + truncate(ret_no_trunc.get(), ret, N); } template @@ -360,7 +371,7 @@ void FixedPointTensor::mul(const TensorAdapter* rhs, _share[0]->mul(rhs, temp->_share[0]); _share[1]->mul(rhs, temp->_share[1]); - truncate3(temp.get(), ret, rhs->scaling_factor()); + truncate(temp.get(), ret, rhs->scaling_factor()); } template @@ -404,7 +415,7 @@ void FixedPointTensor::mat_mul(const TensorAdapter* rhs, FixedPointTensor* ret) const { _share[0]->mat_mul(rhs, ret->_share[0]); _share[1]->mat_mul(rhs, ret->_share[1]); - truncate3(ret, ret, rhs->scaling_factor()); + truncate(ret, ret, rhs->scaling_factor()); } template< typename T, size_t N> @@ -831,7 +842,7 @@ void FixedPointTensor::long_div(const FixedPointTensor* rhs, } for (size_t i = 1; i <= N; ++i) { - truncate3(&abs_rhs, &sub_rhs, i); + truncate(&abs_rhs, &sub_rhs, i); abs_lhs.gt(&sub_rhs, &cmp_res); cmp_res.mul(&sub_rhs, &sub_rhs); cmp_res.lshift(N - i, &cmp_res); @@ -1184,7 +1195,7 @@ void FixedPointTensor::inverse_square_root(const FixedPointTensor* op, std::shared_ptr> x2 = std::make_shared>(temp[2].get(), temp[3].get()); // x2 = 0.5 * op - truncate3(op, x2.get(), 1); + truncate(op, x2.get(), 1); assign_to_tensor(y->mutable_share(0), (T)(x0 * pow(2, N))); assign_to_tensor(y->mutable_share(1), (T)(x0 * pow(2, N))); diff --git a/core/privc3/fixedpoint_tensor_test.cc b/core/privc3/fixedpoint_tensor_test.cc index 2cf6978294f5542f2a842ad18ea6821609df83e1..8b09cbd735275e7544bb51dc02866386d478f611 100644 --- a/core/privc3/fixedpoint_tensor_test.cc +++ b/core/privc3/fixedpoint_tensor_test.cc @@ -1267,6 +1267,7 @@ TEST_F(FixedTensorTest, mulfixed) { EXPECT_TRUE(test_fixedt_check_tensor_eq(out0.get(), &result)); } +#ifndef USE_ABY3_TRUNC1 //use aby3 trunc1 TEST_F(FixedTensorTest, mulfixed_multi_times) { std::vector shape = {100000, 1}; @@ -1327,6 +1328,7 @@ TEST_F(FixedTensorTest, mulfixed_multi_times) { EXPECT_TRUE(test_fixedt_check_tensor_eq(out1.get(), out2.get())); EXPECT_TRUE(test_fixedt_check_tensor_eq(out0.get(), &result)); } +#endif TEST_F(FixedTensorTest, mulfixed_overflow) {