/*
    Copyright (C) 2008 Peter Shrimpton
    Copyright (C) 2009 William Hart
    Copyright (C) 2014, 2015 Dana Jacobsen
    Copyright (C) 2015 Kushagra Singh

    This file is part of FLINT.

    FLINT is free software: you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.  See <https://www.gnu.org/licenses/>.
*/

#include <math.h>
#include "ulong_extras.h"

ulong flint_pseudosquares[] = {17, 73, 241, 1009, 2641, 8089, 18001,
          53881, 87481, 117049, 515761, 1083289, 3206641, 3818929, 9257329,
          22000801, 48473881, 48473881, 175244281, 427733329, 427733329,
          898716289u, 2805544681u, 2805544681u, 2805544681u
#ifndef FLINT64
          };
#else
          , 10310263441u, 23616331489u, 85157610409u, 85157610409u,
          196265095009u, 196265095009u, 2871842842801u, 2871842842801u,
          2871842842801u, 26250887023729u, 26250887023729u, 112434732901969u,
          112434732901969u, 112434732901969u, 178936222537081u,
          178936222537081u, 696161110209049u, 696161110209049u,
          2854909648103881u, 6450045516630769u, 6450045516630769u,
          11641399247947921u, 11641399247947921u, 190621428905186449u,
          196640248121928601u, 712624335095093521u, 1773855791877850321u };
#endif

#if FLINT64
#define FLINT_NUM_PSEUDOSQUARES 52
#else
#define FLINT_NUM_PSEUDOSQUARES 25
#endif

int n_is_prime_pseudosquare(ulong n)
{
    unsigned int i, j, m1;
    ulong p, B, NB, exp, mod8;
    const ulong * primes;
    const double * inverses;

    if (n < UWORD(2))
        return 0;
    else if ((n & UWORD(1)) == UWORD(0))
        return (n == UWORD(2));

    primes = n_primes_arr_readonly(FLINT_PSEUDOSQUARES_CUTOFF+1);
    inverses = n_prime_inverses_arr_readonly(FLINT_PSEUDOSQUARES_CUTOFF+1);

    for (i = 0; i < FLINT_PSEUDOSQUARES_CUTOFF; i++)
    {
        double ppre;
        p = primes[i];
        if (p*p > n)
            return 1;
        ppre = inverses[i];
        if (!n_mod2_precomp(n, p, ppre))
            return 0;
    }

    B  = primes[FLINT_PSEUDOSQUARES_CUTOFF];
    NB = (n - 1)/B + 1;
    m1 = 0;

    for (i = 0; i < FLINT_NUM_PSEUDOSQUARES; i++)
        if (flint_pseudosquares[i] > NB)
            break;

    exp = (n - 1)/2;

    for (j = 0; j <= i; j++)
    {
        ulong mod = n_powmod2(primes[j], exp, n);
        if ((mod != UWORD(1)) && (mod != n - 1))
            return 0;
        else if (mod == n - 1)
            m1 = 1;
    }

    mod8 = n % 8;

    if ((mod8 == 3) || (mod8 == 7))
        return 1;
    else if (mod8 == 5)
    {
        ulong mod = n_powmod2(UWORD(2), exp, n);
        if (mod == n - 1)
            return 1;
        else
            flint_throw(FLINT_ERROR, "Whoah, %wu is a probable prime, but not prime, please report!!\n", n);
    }
    else
    {
        if (m1) return 1;
        for (j = i + 1; j < FLINT_NUM_PSEUDOSQUARES + 1; j++)
        {
            ulong mod = n_powmod2(primes[j], exp, n);
            if (mod == n - 1)
                return 1;
            else if (mod != 1)
                flint_throw(FLINT_ERROR, "Whoah, %wu is a probable prime, but not prime, please report!!\n", n);
        }
        flint_throw(FLINT_ERROR, "Whoah, %wu is a probable prime, but not prime, please report!!\n", n);
    }
}
