3 * Miller-Rabin primality test
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 #ifndef CATACOMB_RABIN_H
29 #define CATACOMB_RABIN_H
35 /*----- Header files ------------------------------------------------------*/
41 #ifndef CATACOMB_MPMONT_H
45 #ifndef CATACOMB_PFILT_H
49 /*----- Data structures ---------------------------------------------------*/
51 typedef struct rabin {
52 mpmont mm; /* Montgomery arithmetic context */
53 size_t s; /* %$m = 2^s r + 1$% */
54 mp *r; /* %$m = 2^s r + 1$% */
55 mp *m1; /* %$(m - 1)R \bmod m$% */
58 /*----- Functions provided ------------------------------------------------*/
60 /* --- @rabin_create@ --- *
62 * Arguments: @rabin *r@ = pointer to Rabin-Miller context
63 * @mp *m@ = pointer to number to test
65 * Returns: Zero on success, nonzero for failure.
67 * Use: Precomputes some useful values for performing the
68 * Miller-Rabin probabilistic primality test.
71 extern int rabin_create(rabin */*r*/, mp */*m*/);
73 /* --- @rabin_destroy@ --- *
75 * Arguments: @rabin *r@ = pointer to Rabin-Miller context
79 * Use: Disposes of a Rabin-Miller context when it's no longer
83 extern void rabin_destroy(rabin */*r*/);
85 /* --- @rabin_test@, @rabin_rtest@ --- *
87 * Arguments: @rabin *r@ = pointer to Rabin-Miller context
88 * @mp *g@ = base to test the number against
90 * Returns: Either @PGEN_FAIL@ if the test failed, or @PGEN_PASS@
93 * Use: Performs a single iteration of the Rabin-Miller primality
94 * test. The @rtest@ variant assumes that %$g$% is either
95 * already in Montgomery representation, or you don't care.
98 extern int rabin_rtest(rabin */*r*/, mp */*g*/);
99 extern int rabin_test(rabin */*r*/, mp */*g*/);
101 /* --- @rabin_iters@ --- *
103 * Arguments: @unsigned len@ = number of bits in value
105 * Returns: Number of iterations recommended.
107 * Use: Returns the recommended number of iterations to ensure that a
108 * number with @len@ bits is really prime.
111 extern int rabin_iters(unsigned /*len*/);
113 /*----- That's all, folks -------------------------------------------------*/