File: test_babai.cpp

package info (click to toggle)
fplll 5.5.0-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,984 kB
  • sloc: cpp: 21,104; javascript: 1,284; sh: 1,050; makefile: 198; perl: 46; python: 42
file content (137 lines) | stat: -rw-r--r-- 4,596 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* Copyright (C) 2022 Martin R. Albrecht

   This file is part of fplll. fplll is free software: you
   can redistribute it and/or modify it under the terms of the GNU Lesser
   General Public License as published by the Free Software Foundation,
   either version 2.1 of the License, or (at your option) any later version.

   fplll is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with fplll. If not, see <http://www.gnu.org/licenses/>. */

#include <fplll/fplll.h>
#include <vector>

#include <cfloat>  // Needed for precision macros.

using namespace fplll;

template <class ZT, class FT>
int test_intrel(long n, long bits, bool shouldfail = false, int seed = 0)
{
  gmp_randseed_ui(RandGen::get_gmp_state(), seed);
  mpz_t status;
  mpz_init(status);

  ZZ_mat<ZT> B, U, UT;
  B.resize(n, n + 1);
  B.gen_intrel(bits);

  vector<Z_NR<ZT>> t = vector<Z_NR<ZT>>(n + 1);

  for (long i = 0; i < n; i++)
  {
    mpz_urandomb(status, RandGen::get_gmp_state(), 1);
    if (mpz_cmp_si(status, 1))
    {
      t[0].add(t[0], B[i][0]);
    }
  }
  mpz_clear(status);

  vector<Z_NR<ZT>> v = vector<Z_NR<ZT>>(t);

  lll_reduction(B);

  MatGSO<Z_NR<ZT>, FP_NR<FT>> M(B, U, UT, GSO_DEFAULT);
  M.update_gso();
  M.babai(v, 0, n, false);

  vector<Z_NR<ZT>> w = vector<Z_NR<ZT>>(n + 1);

  Z_NR<ZT> tmp;

  for (long i = 0; i < B.get_rows(); i++)
  {
    for (long j = 0; j < B.get_cols(); j++)
    {
      tmp.mul(v[i], B[i][j]);
      w[j].add(w[j], tmp);
    }
  }

  if ((w[0] == t[0]) - shouldfail)
  {
    return 0;
  }
  else
  {
    std::cerr << "n:" << n << ", bits: " << bits << ", shouldfail:" << shouldfail << std::endl;
    std::cerr << "t:" << t << std::endl;
    std::cerr << "w:" << w << std::endl;
    return 1;
  }
}

int main(int argc, char *argv[])
{
  int status = 0;
  RandGen::init_with_seed(0);
  status += test_intrel<mpz_t, double>(10, 20);
  status += test_intrel<mpz_t, double>(10, 30);
  status += test_intrel<mpz_t, double>(10, 40);
  status += test_intrel<mpz_t, double>(10, 50);
  status += test_intrel<mpz_t, double>(10, 60, true);

#ifdef FPLLL_WITH_LONG_DOUBLE
  // long double has various types of platform and compiler specific behaviour.
  // This makes this test case platform specific.
  // These cases are (at least):
  // (1) Some platforms have sizeof(double) == sizeof(long double), because the C standard
  //     only requires long double to be at least as big as double. This means that
  //     in some situations long double may simply alias double (e.g MSVC).
  // (2) Some platforms make long double an alias for IEEE quadruple precision types. This
  //     may mean that long double is actually rather accurate (more than, say, 60 bits of
  //     precision).
  // (3) On x86-64, some compilers may treat long double as the 80-bit extended precision
  //     x87 type. This means that there's more precision than a regular double. In this situation,
  //     the compiler may also make sizeof(long double) == 16, meaning that we cannot detect this
  //     easily based on the size of long double alone.
  //
  // To circumvent these issues, we check how many elements are in the mantissa of long double,
  // using LDBL_MANT_DIG. Specifically, if LDBL_MANT_DIG == DBL_MANT_DIG, then we are in case (1).
  // If they are different, then if LDBL_MANT_DIG < 70 then the second test should also fail.
  status += test_intrel<mpz_t, long double>(10, 60, LDBL_MANT_DIG == DBL_MANT_DIG);
  status += test_intrel<mpz_t, long double>(10, 70, LDBL_MANT_DIG < 70);
#endif
#ifdef FPLLL_WITH_QD
  // QD needs to have the round-to-double flag set on x86 systems. This function
  // does nothing on non-x86 machines (see the readme for the QD library for more).
  // This is also already done in the wrapper.
  unsigned old_cw;
  fpu_fix_start(&old_cw);
  status += test_intrel<mpz_t, dd_real>(10, 110);
  status += test_intrel<mpz_t, dd_real>(10, 120, true);
  // status += test_intrel<mpz_t, qd_real>(10, 100);
  // status += test_intrel<mpz_t, qd_real>(10, 230, true);
  fpu_fix_end(&old_cw);
#endif
  FP_NR<mpfr_t>::set_prec(100);
  status += test_intrel<mpz_t, mpfr_t>(10, 100);
  FP_NR<mpfr_t>::set_prec(200);
  status += test_intrel<mpz_t, mpfr_t>(10, 200);

  if (status == 0)
  {
    std::cerr << "All tests passed." << std::endl;
    return 0;
  }
  else
  {
    return -1;
  }
};