is_prime.c 699 B

123456789101112131415161718192021222324252627
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. *
  9. * Tom St Denis, [email protected], http://libtomcrypt.org
  10. */
  11. #include "mycrypt.h"
  12. #ifdef MPI
  13. /* figures out if a number is prime (MR test) */
  14. int is_prime(mp_int *N, int *result)
  15. {
  16. int err;
  17. _ARGCHK(N != NULL);
  18. _ARGCHK(result != NULL);
  19. if ((err = mp_prime_is_prime(N, mp_prime_rabin_miller_trials(mp_count_bits(N)), result)) != MP_OKAY) {
  20. return mpi_to_ltc_error(err);
  21. }
  22. return CRYPT_OK;
  23. }
  24. #endif