#include #include "dpf.hpp" #include "helpers/eval_common_data.hpp" #include namespace { template auto recon(const A & a, const B & b) { if constexpr (dpf::is_secret_share_v> && dpf::is_secret_share_v>) return dpf::reconstruct(a, b); else return a - b; } } // namespace template struct EvalPointTest : public testing::Test { public: using input_type = typename std::tuple_element_t<0, T>; using output_type = typename std::tuple_element_t<1, T>; using integral_type = dpf::utils::integral_type_from_bitlength_t>; using dpf_type = dpf::utils::dpf_type_t; protected: EvalPointTest() : params{std::get>(allParams)}, range{(std::size_t(1) << std::min(dpf::utils::bitlength_of_v, std::size_t(10))-1)-1}, zero_output{from_integral_type_output(0)} { } void SetUp() override { } void TearDown() override { } input_type get_start(const input_type & x) { integral_type x_int = to_integral_type(x), min_int = to_integral_type(std::numeric_limits::min()), max_int = to_integral_type(std::numeric_limits::max()), start_int; // set start_int so that the tested range is centered around x_int if possible // use start_int = min_int if x_int smaller than min_int+range // or start_int = max_int-2*range if x_int larger than max_int-range // range is selected to be at most 1 less than half the maximum range for input_type // this ensures there are no overflow issues // for signed integral types, since the MSB is internally flipped, // needed additional check that x_int was in the correct range for the given // conditionals (note that these added checks are always try for unsigned types) if (x_int < min_int + range && x_int >= min_int) { start_int = min_int; } else if (x_int > max_int - range && x_int <= max_int) { start_int = max_int - (range << 1); } else { start_int = x_int - range; } return from_integral_type(start_int); } template void assert_wrapper(const input_type & x, const output_type & y, UnaryFunction0 f0, UnaryFunction1 f1) { input_type cur = get_start(x); for (std::size_t i = 0; i <= range<<1; ++i, cur = next_domain_point(cur)) { auto y0 = *f0(cur); auto y1 = *f1(cur); const output_type got = recon(y0, y1); if (cur == x) { ASSERT_EQ(got, y); } else { ASSERT_EQ(got, zero_output); } } } static constexpr auto to_integral_type = dpf::utils::to_integral_type{}; static constexpr auto from_integral_type = dpf::utils::make_from_integral_value{}; static constexpr auto from_integral_type_output = dpf::utils::make_from_integral_value{}; std::vector params; std::size_t range; output_type zero_output; }; TYPED_TEST_SUITE_P(EvalPointTest); TYPED_TEST_P(EvalPointTest, Basic) { using input_type = typename TestFixture::input_type; for (auto [x, y] : this->params) { auto [dpf0, dpf1] = dpf::make_dpf(x, y); this->assert_wrapper(x, y, [&dpf0](input_type cur) { return dpf::eval_point(dpf0, cur); }, [&dpf1](input_type cur) { return dpf::eval_point(dpf1, cur); } ); } } TYPED_TEST_P(EvalPointTest, BasicPathMemoizer) { using input_type = typename TestFixture::input_type; for (auto [x, y] : this->params) { auto [dpf0, dpf1] = dpf::make_dpf(x, y); using key0_t = std::decay_t; using key1_t = std::decay_t; auto memo0 = dpf::make_basic_path_memoizer(), memo1 = dpf::make_basic_path_memoizer(); this->assert_wrapper(x, y, [&dpf0, &memo0](input_type cur) { return dpf::eval_point(dpf0, cur, memo0); }, [&dpf1, &memo1](input_type cur) { return dpf::eval_point(dpf1, cur, memo1); } ); } } TYPED_TEST_P(EvalPointTest, NonmemoizingPathMemoizer) { using input_type = typename TestFixture::input_type; for (auto [x, y] : this->params) { auto [dpf0, dpf1] = dpf::make_dpf(x, y); using key0_t = std::decay_t; using key1_t = std::decay_t; auto memo0 = dpf::make_nonmemoizing_path_memoizer(), memo1 = dpf::make_nonmemoizing_path_memoizer(); this->assert_wrapper(x, y, [&dpf0, &memo0](input_type cur) { return dpf::eval_point(dpf0, cur, memo0); }, [&dpf1, &memo1](input_type cur) { return dpf::eval_point(dpf1, cur, memo1); } ); } } REGISTER_TYPED_TEST_SUITE_P(EvalPointTest, Basic, BasicPathMemoizer, NonmemoizingPathMemoizer); using Types = testing::Types < // base test test_type, // test input types test_type, test_type, test_type, test_type, test_type, test_type, uint64_t>, test_type, uint64_t>, test_type, uint64_t>, test_type, uint64_t>, test_type, uint64_t>, // test output types test_type, test_type, test_type, test_type, test_type, test_type>, test_type>, test_type>, test_type>, // custom types test_type, test_type, test_type, test_type >; INSTANTIATE_TYPED_TEST_SUITE_P(EvalPointTestInstantiation, EvalPointTest, Types);