chiark / gitweb /
Fix daft error in the comment for @gfshare_get@.
[catacomb] / prim.c
1 /* -*-c-*-
2  *
3  * $Id: prim.c,v 1.1 1999/12/22 15:58:59 mdw Exp $
4  *
5  * Finding primitive elements
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 /*----- Revision history --------------------------------------------------* 
31  *
32  * $Log: prim.c,v $
33  * Revision 1.1  1999/12/22 15:58:59  mdw
34  * Search for primitive elements using prime-search equipment.
35  *
36  */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include "mp.h"
41 #include "mpint.h"
42 #include "mpmont.h"
43 #include "mprand.h"
44 #include "pgen.h"
45 #include "prim.h"
46
47 /*----- Main code ---------------------------------------------------------*/
48
49 /* --- @prim_test@ --- */
50
51 int prim_test(int rq, pgen_event *ev, void *p)
52 {
53   prim_ctx *c = p;
54   int rc = rq;
55
56   switch (rq) {
57     case PGEN_BEGIN:
58       return (PGEN_TRY);
59     case PGEN_TRY: {
60       mp *x = MP_NEW;
61       mp *f = c->f;
62       rc = PGEN_FAIL;
63
64       x = mpmont_exp(&c->mm, x, ev->m, f);
65       if (MP_CMP(x, ==, MP_ONE))
66         goto done;
67       if (c->n == 0) {
68         mp_drop(ev->m);
69         ev->m = MP_COPY(x);
70       } else {
71         size_t n = c->n - 1;
72         f++;
73         while (n) {
74           x = mpmont_exp(&c->mm, x, ev->m, f);
75           if (MP_CMP(x, ==, MP_ONE))
76             goto done;
77           n--; f++;
78         }
79       }
80       rc = PGEN_DONE;
81     done:
82       mp_drop(x);
83     } break;
84   }
85
86   return (rc);
87 }
88
89 /* --- Trivial stepping functions -----------------------------------------*/
90
91 /* --- @prim_step@ --- */
92
93 int prim_step(int rq, pgen_event *ev, void *p)
94 {
95   unsigned *i = p;
96   switch (rq) {
97     case PGEN_BEGIN:
98     case PGEN_TRY:
99       if (*i >= NPRIME)
100         return PGEN_FAIL;
101       ev->m = mp_fromint(ev->m, primetab[(*i)++]);
102       return (PGEN_TRY);
103   }
104   return (0);
105 }
106
107 /*----- That's all, folks -------------------------------------------------*/