Skip to content

Commit 40e25df

Browse files
authored
Merge pull request #22 from tfhe/svp_tests
Add tests for svp
2 parents 9bd0910 + 53aa53f commit 40e25df

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

test/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ add_executable(spqlios-test
8181
spqlios_vmp_product_test.cpp
8282
spqlios_vec_znx_dft_test.cpp
8383
spqlios_vec_znx_test.cpp
84+
spqlios_svp_test.cpp
85+
spqlios_svp_product_test.cpp
8486
)
8587
target_link_libraries(spqlios-test spqlios-testlib libspqlios ${gtest_libs})
8688
target_include_directories(spqlios-test PRIVATE ${test_incs})

test/spqlios_svp_product_test.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <gtest/gtest.h>
2+
3+
#include "../spqlios/arithmetic/vec_znx_arithmetic_private.h"
4+
#include "testlib/fft64_dft.h"
5+
#include "testlib/fft64_layouts.h"
6+
#include "testlib/polynomial_vector.h"
7+
8+
// todo: remove when registered
9+
typedef typeof(fft64_svp_prepare_ref) SVP_PREPARE_F;
10+
11+
void test_fft64_svp_prepare(SVP_PREPARE_F svp_prepare) {
12+
for (uint64_t n : {2, 4, 8, 64, 128}) {
13+
MODULE* module = new_module_info(n, FFT64);
14+
znx_i64 in = znx_i64::random_log2bound(n, 40);
15+
fft64_svp_ppol_layout out(n);
16+
reim_fft64vec expect = simple_fft64(in);
17+
svp_prepare(module, out.data, in.data());
18+
const double* ed = (double*)expect.data();
19+
const double* ac = (double*)out.data;
20+
for (uint64_t i = 0; i < n; ++i) {
21+
ASSERT_LE(abs(ed[i] - ac[i]), 1e-10) << i << n;
22+
}
23+
delete_module_info(module);
24+
}
25+
}
26+
27+
TEST(svp_prepare, fft64_svp_prepare_ref) { test_fft64_svp_prepare(fft64_svp_prepare_ref); }
28+
TEST(svp_prepare, svp_prepare) { test_fft64_svp_prepare(svp_prepare); }

test/spqlios_svp_test.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <gtest/gtest.h>
2+
3+
#include "../spqlios/arithmetic/vec_znx_arithmetic_private.h"
4+
#include "testlib/fft64_dft.h"
5+
#include "testlib/fft64_layouts.h"
6+
#include "testlib/polynomial_vector.h"
7+
8+
void test_fft64_svp_apply_dft(SVP_APPLY_DFT_F svp) {
9+
for (uint64_t n : {2, 4, 8, 64, 128}) {
10+
MODULE* module = new_module_info(n, FFT64);
11+
// poly 1 to multiply - create and prepare
12+
fft64_svp_ppol_layout ppol(n);
13+
ppol.fill_random(1.);
14+
for (uint64_t sa : {3, 5, 8}) {
15+
for (uint64_t sr : {3, 5, 8}) {
16+
uint64_t a_sl = n + uniform_u64_bits(2);
17+
// poly 2 to multiply
18+
znx_vec_i64_layout a(n, sa, a_sl);
19+
a.fill_random(19);
20+
// original operation result
21+
fft64_vec_znx_dft_layout res(n, sr);
22+
thash hash_a_before = a.content_hash();
23+
thash hash_ppol_before = ppol.content_hash();
24+
svp(module, res.data, sr, ppol.data, a.data(), sa, a_sl);
25+
ASSERT_EQ(a.content_hash(), hash_a_before);
26+
ASSERT_EQ(ppol.content_hash(), hash_ppol_before);
27+
// create expected value
28+
reim_fft64vec ppo = ppol.get_copy();
29+
std::vector<reim_fft64vec> expect(sr);
30+
for (uint64_t i = 0; i < sr; ++i) {
31+
expect[i] = ppo * simple_fft64(a.get_copy_zext(i));
32+
}
33+
// this is the largest precision we can safely expect
34+
double prec_expect = n * pow(2., 19 - 52);
35+
for (uint64_t i = 0; i < sr; ++i) {
36+
reim_fft64vec actual = res.get_copy_zext(i);
37+
ASSERT_LE(infty_dist(actual, expect[i]), prec_expect);
38+
}
39+
}
40+
}
41+
42+
delete_module_info(module);
43+
}
44+
}
45+
46+
TEST(fft64_svp_apply_dft, svp_apply_dft) { test_fft64_svp_apply_dft(svp_apply_dft); }
47+
TEST(fft64_svp_apply_dft, fft64_svp_apply_dft_ref) { test_fft64_svp_apply_dft(fft64_svp_apply_dft_ref); }

0 commit comments

Comments
 (0)