FLINT3.7.0-dev

mpn_mod_is_square, mpn_mod_sqrt

Synopsis#include <flint/mpn_mod.h>
truth_t mpn_mod_is_square(nn_srcptr x, gr_ctx_t ctx)source
int mpn_mod_sqrt(nn_ptr res, nn_srcptr x, gr_ctx_t ctx)source

Test whether x is a square, and compute a square root of x.

00 and 11 are squares for every modulus, and mpn_mod_sqrt returns them unchanged. Otherwise both functions use the Jacobi symbol (xn)\left(\frac{x}{n}\right) for odd nn (flint_mpn_is_square_mod). A symbol of −1-1 proves that xx is not a square, whether or not nn is prime: mpn_mod_is_square returns T_FALSE and mpn_mod_sqrt sets res to zero and returns GR_DOMAIN. A symbol of +1+1 gives T_TRUE if the context is flagged as prime and T_UNKNOWN otherwise; for even nn, mpn_mod_is_square returns T_UNKNOWN.

mpn_mod_sqrt computes roots only for odd moduli flagged as prime (gr_ctx_set_is_field), with flint_mpn_sqrtmod_preinv and the precomputed inverse of the context (an exponentiation, Atkin's formula, Tonelli–Shanks or Müller's variant of the Cipolla–Lehmer algorithm depending on the 2-adic valuation of n−1n - 1; see Square roots modulo a prime). For even moduli, and for odd moduli not flagged as prime when the Jacobi symbol is +1+1, it returns GR_UNABLE. If the algorithm fails, which can only happen if the flag was wrong, it also returns GR_UNABLE.

Cite this page

The FLINT team, “mpn_mod_is_square, mpn_mod_sqrt (mpn_mod.h),” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/mpn_mod/mpn_mod_is_square.html (generated 2026-11-02).