validation_XY_PRIME.c (1643B)
1 /* 2 This file is part of Anastasis 3 Copyright (C) 2020, 2021 Anastasis SARL 4 5 Anastasis is free software; you can redistribute it and/or modify it under the 6 terms of the GNU General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along with 14 Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file reducer/validation_XY_PRIME.c 18 * @brief anastasis reducer api 19 * @author Christian Grothoff 20 * @author Dominik Meister 21 * @author Dennis Neufeld 22 */ 23 #include <string.h> 24 #include <stdbool.h> 25 #include <stdio.h> 26 #include <gcrypt.h> 27 28 /** 29 * Function to validate a prime number. 30 * 31 * @param pr_number prime number to validate (input) 32 * @return true if pr_number is a prime, else false 33 */ 34 bool 35 XY_PRIME_check (const char *pr_number); 36 37 /* declaration to fix compiler warning */ 38 bool 39 XY_PRIME_check (const char *pr_number) 40 { 41 unsigned long long n; 42 char dummy; 43 gcry_mpi_t p; 44 bool is_prime; 45 46 if (1 != sscanf (pr_number, 47 "%llu%c", 48 &n, 49 &dummy)) 50 return false; 51 p = gcry_mpi_set_ui (NULL, 52 (unsigned long) n); 53 is_prime = (0 == gcry_prime_check (p, 54 0)); 55 gcry_mpi_release (p); 56 return is_prime; 57 }