poulpy/poulpy-backend/src/cpu_spqlios/spqlios-arithmetic/test/spqlios_svp_apply_test.cpp
poulpy-backend/src/cpu_spqlios/spqlios-arithmetic/test/spqlios_svp_apply_test.cppRaw
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
8void 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
46TEST(fft64_svp_apply_dft, svp_apply_dft) { test_fft64_svp_apply_dft(svp_apply_dft); }
47TEST(fft64_svp_apply_dft, fft64_svp_apply_dft_ref) { test_fft64_svp_apply_dft(fft64_svp_apply_dft_ref); }