New function and example program computes Fibonacci numbers fairly fast.
[u/mdw/catacomb] / rabin.h
1 /* -*-c-*-
2 *
3 * $Id$
4 *
5 * Miller-Rabin primality test
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
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.
18 *
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.
23 *
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
30 #ifndef CATACOMB_RABIN_H
31 #define CATACOMB_RABIN_H
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /*----- Header files ------------------------------------------------------*/
38
39 #ifndef CATACOMB_MP_H
40 # include "mp.h"
41 #endif
42
43 #ifndef CATACOMB_MPMONT_H
44 # include "mpmont.h"
45 #endif
46
47 #ifndef CATACOMB_PFILT_H
48 # include "pfilt.h"
49 #endif
50
51 /*----- Data structures ---------------------------------------------------*/
52
53 typedef struct rabin {
54 mpmont mm; /* Montgomery arithmetic context */
55 size_t s; /* %$m = 2^s r + 1$% */
56 mp *r; /* %$m = 2^s r + 1$% */
57 mp *m1; /* %$(m - 1)R \bmod m$% */
58 } rabin;
59
60 /*----- Functions provided ------------------------------------------------*/
61
62 /* --- @rabin_create@ --- *
63 *
64 * Arguments: @rabin *r@ = pointer to Rabin-Miller context
65 * @mp *m@ = pointer to number to test
66 *
67 * Returns: Zero on success, nonzero for failure.
68 *
69 * Use: Precomputes some useful values for performing the
70 * Miller-Rabin probabilistic primality test.
71 */
72
73 extern int rabin_create(rabin */*r*/, mp */*m*/);
74
75 /* --- @rabin_destroy@ --- *
76 *
77 * Arguments: @rabin *r@ = pointer to Rabin-Miller context
78 *
79 * Returns: ---
80 *
81 * Use: Disposes of a Rabin-Miller context when it's no longer
82 * needed.
83 */
84
85 extern void rabin_destroy(rabin */*r*/);
86
87 /* --- @rabin_test@, @rabin_rtest@ --- *
88 *
89 * Arguments: @rabin *r@ = pointer to Rabin-Miller context
90 * @mp *g@ = base to test the number against
91 *
92 * Returns: Either @PGEN_FAIL@ if the test failed, or @PGEN_PASS@
93 * if it succeeded.
94 *
95 * Use: Performs a single iteration of the Rabin-Miller primality
96 * test. The @rtest@ variant assumes that %$g$% is either
97 * already in Montgomery representation, or you don't care.
98 */
99
100 extern int rabin_rtest(rabin */*r*/, mp */*g*/);
101 extern int rabin_test(rabin */*r*/, mp */*g*/);
102
103 /* --- @rabin_iters@ --- *
104 *
105 * Arguments: @unsigned len@ = number of bits in value
106 *
107 * Returns: Number of iterations recommended.
108 *
109 * Use: Returns the recommended number of iterations to ensure that a
110 * number with @len@ bits is really prime.
111 */
112
113 extern int rabin_iters(unsigned /*len*/);
114
115 /*----- That's all, folks -------------------------------------------------*/
116
117 #ifdef __cplusplus
118 }
119 #endif
120
121 #endif