From 53aa53f91de7d40afdb03f9b38712b0b5aa02787 Mon Sep 17 00:00:00 2001 From: Sandra Guasch Date: Fri, 24 May 2024 17:26:14 +0000 Subject: [PATCH] svp tests --- test/CMakeLists.txt | 2 ++ test/spqlios_svp_product_test.cpp | 28 ++++++++++++++++++ test/spqlios_svp_test.cpp | 47 +++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 test/spqlios_svp_product_test.cpp create mode 100644 test/spqlios_svp_test.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2cf211a..95421f8 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -81,6 +81,8 @@ add_executable(spqlios-test spqlios_vmp_product_test.cpp spqlios_vec_znx_dft_test.cpp spqlios_vec_znx_test.cpp + spqlios_svp_test.cpp + spqlios_svp_product_test.cpp ) target_link_libraries(spqlios-test spqlios-testlib libspqlios ${gtest_libs}) target_include_directories(spqlios-test PRIVATE ${test_incs}) diff --git a/test/spqlios_svp_product_test.cpp b/test/spqlios_svp_product_test.cpp new file mode 100644 index 0000000..db7cb23 --- /dev/null +++ b/test/spqlios_svp_product_test.cpp @@ -0,0 +1,28 @@ +#include + +#include "../spqlios/arithmetic/vec_znx_arithmetic_private.h" +#include "testlib/fft64_dft.h" +#include "testlib/fft64_layouts.h" +#include "testlib/polynomial_vector.h" + +// todo: remove when registered +typedef typeof(fft64_svp_prepare_ref) SVP_PREPARE_F; + +void test_fft64_svp_prepare(SVP_PREPARE_F svp_prepare) { + for (uint64_t n : {2, 4, 8, 64, 128}) { + MODULE* module = new_module_info(n, FFT64); + znx_i64 in = znx_i64::random_log2bound(n, 40); + fft64_svp_ppol_layout out(n); + reim_fft64vec expect = simple_fft64(in); + svp_prepare(module, out.data, in.data()); + const double* ed = (double*)expect.data(); + const double* ac = (double*)out.data; + for (uint64_t i = 0; i < n; ++i) { + ASSERT_LE(abs(ed[i] - ac[i]), 1e-10) << i << n; + } + delete_module_info(module); + } +} + +TEST(svp_prepare, fft64_svp_prepare_ref) { test_fft64_svp_prepare(fft64_svp_prepare_ref); } +TEST(svp_prepare, svp_prepare) { test_fft64_svp_prepare(svp_prepare); } diff --git a/test/spqlios_svp_test.cpp b/test/spqlios_svp_test.cpp new file mode 100644 index 0000000..c75f00e --- /dev/null +++ b/test/spqlios_svp_test.cpp @@ -0,0 +1,47 @@ +#include + +#include "../spqlios/arithmetic/vec_znx_arithmetic_private.h" +#include "testlib/fft64_dft.h" +#include "testlib/fft64_layouts.h" +#include "testlib/polynomial_vector.h" + +void test_fft64_svp_apply_dft(SVP_APPLY_DFT_F svp) { + for (uint64_t n : {2, 4, 8, 64, 128}) { + MODULE* module = new_module_info(n, FFT64); + // poly 1 to multiply - create and prepare + fft64_svp_ppol_layout ppol(n); + ppol.fill_random(1.); + for (uint64_t sa : {3, 5, 8}) { + for (uint64_t sr : {3, 5, 8}) { + uint64_t a_sl = n + uniform_u64_bits(2); + // poly 2 to multiply + znx_vec_i64_layout a(n, sa, a_sl); + a.fill_random(19); + // original operation result + fft64_vec_znx_dft_layout res(n, sr); + thash hash_a_before = a.content_hash(); + thash hash_ppol_before = ppol.content_hash(); + svp(module, res.data, sr, ppol.data, a.data(), sa, a_sl); + ASSERT_EQ(a.content_hash(), hash_a_before); + ASSERT_EQ(ppol.content_hash(), hash_ppol_before); + // create expected value + reim_fft64vec ppo = ppol.get_copy(); + std::vector expect(sr); + for (uint64_t i = 0; i < sr; ++i) { + expect[i] = ppo * simple_fft64(a.get_copy_zext(i)); + } + // this is the largest precision we can safely expect + double prec_expect = n * pow(2., 19 - 52); + for (uint64_t i = 0; i < sr; ++i) { + reim_fft64vec actual = res.get_copy_zext(i); + ASSERT_LE(infty_dist(actual, expect[i]), prec_expect); + } + } + } + + delete_module_info(module); + } +} + +TEST(fft64_svp_apply_dft, svp_apply_dft) { test_fft64_svp_apply_dft(svp_apply_dft); } +TEST(fft64_svp_apply_dft, fft64_svp_apply_dft_ref) { test_fft64_svp_apply_dft(fft64_svp_apply_dft_ref); }