Makefile: Link tests against stuff like -lm.
[u/mdw/catacomb] / keycheck-mp.c
CommitLineData
600127f0 1/* -*-c-*-
2 *
b817bfc6 3 * $Id: keycheck-mp.c,v 1.2 2004/04/08 01:36:15 mdw Exp $
600127f0 4 *
5 * Key consistency checking tools for large integers
6 *
7 * (c) 2001 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
600127f0 11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
45c0fd36 18 *
600127f0 19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
45c0fd36 23 *
600127f0 24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
600127f0 30/*----- Header files ------------------------------------------------------*/
31
32#include <mLib/dstr.h>
33
34#include "fibrand.h"
35#include "grand.h"
36#include "keycheck.h"
37#include "mp.h"
38#include "mprand.h"
39#include "pfilt.h"
40#include "pgen.h"
41#include "rabin.h"
42#include "rand.h"
43
44/*----- Main code ---------------------------------------------------------*/
45
46/* --- @keycheck_prime@ --- *
47 *
48 * Arguments: @keycheck *kc@ = keycheck state
49 * @unsigned sev@ = severity if not prime
50 * @mp *m@ = a number to check for primality
51 * @const char *name@ = name of this number
52 *
53 * Returns: Zero if OK, or return status from function.
54 *
55 * Use: Checks that a number is prime.
56 */
57
58int keycheck_prime(keycheck *kc, unsigned sev, mp *m, const char *name)
59{
60 int rc;
61
62 rc = pfilt_smallfactor(m);
63 if (rc == PGEN_TRY) {
64 rabin rn;
65 grand *r = fibrand_create(0);
66 unsigned n;
67 mp *x = MP_NEW;
68
69 r->ops->misc(r, GRAND_SEEDRAND, &rand_global);
70 n = rabin_iters(mp_bits(m));
71 rabin_create(&rn, m);
72 do {
73 x = mprand_range(x, m, r, 0);
74 rc = rabin_test(&rn, x);
75 n--;
76 } while (n && rc == PGEN_PASS);
77 rabin_destroy(&rn);
78 mp_drop(x);
79 r->ops->destroy(r);
80 }
81 if (rc != PGEN_FAIL)
82 rc = 0;
83 else
84 rc = keycheck_report(kc, sev, "%s not prime", name);
85 return (rc);
86}
87
88/*----- That's all, folks -------------------------------------------------*/