Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for svp #22

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
28 changes: 28 additions & 0 deletions test/spqlios_svp_product_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <gtest/gtest.h>

#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); }
47 changes: 47 additions & 0 deletions test/spqlios_svp_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <gtest/gtest.h>

#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<reim_fft64vec> 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); }
Loading