| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: prim.c,v 1.3 2000/10/08 12:11:22 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.3 2000/10/08 12:11:22 mdw |
| 34 | * Use @MP_EQ@ instead of @MP_CMP@. |
| 35 | * |
| 36 | * Revision 1.2 2000/07/29 09:57:42 mdw |
| 37 | * Improve primitive-element testing a lot. Now much more sensible and |
| 38 | * orthogonal: you can find a generator for any given subgroup order by |
| 39 | * putting in the appropriate parameters. |
| 40 | * |
| 41 | * Revision 1.1 1999/12/22 15:58:59 mdw |
| 42 | * Search for primitive elements using prime-search equipment. |
| 43 | * |
| 44 | */ |
| 45 | |
| 46 | /*----- Header files ------------------------------------------------------*/ |
| 47 | |
| 48 | #include "mp.h" |
| 49 | #include "mpint.h" |
| 50 | #include "mpmont.h" |
| 51 | #include "mprand.h" |
| 52 | #include "pgen.h" |
| 53 | #include "prim.h" |
| 54 | |
| 55 | /*----- Main code ---------------------------------------------------------*/ |
| 56 | |
| 57 | /* --- @prim_test@ --- */ |
| 58 | |
| 59 | int prim_test(int rq, pgen_event *ev, void *p) |
| 60 | { |
| 61 | prim_ctx *c = p; |
| 62 | int rc = rq; |
| 63 | |
| 64 | switch (rq) { |
| 65 | case PGEN_BEGIN: |
| 66 | return (PGEN_TRY); |
| 67 | case PGEN_TRY: { |
| 68 | mp *x; |
| 69 | rc = PGEN_FAIL; |
| 70 | |
| 71 | if (!c->exp) |
| 72 | x = mp_copy(ev->m); |
| 73 | else { |
| 74 | x = mpmont_exp(&c->mm, MP_NEW, ev->m, c->exp); |
| 75 | if (MP_EQ(x, MP_ONE)) |
| 76 | goto done; |
| 77 | } |
| 78 | if (c->n == 0) |
| 79 | goto ok; |
| 80 | else { |
| 81 | size_t n = c->n; |
| 82 | mp **f = c->f; |
| 83 | mp *y = MP_NEW; |
| 84 | while (n) { |
| 85 | y = mpmont_exp(&c->mm, y, x, *f); |
| 86 | if (MP_EQ(y, MP_ONE)) { |
| 87 | mp_drop(y); |
| 88 | goto done; |
| 89 | } |
| 90 | n--; f++; |
| 91 | } |
| 92 | mp_drop(y); |
| 93 | } |
| 94 | ok: |
| 95 | rc = PGEN_DONE; |
| 96 | mp_drop(ev->m); |
| 97 | ev->m = x; |
| 98 | break; |
| 99 | done: |
| 100 | mp_drop(x); |
| 101 | } break; |
| 102 | } |
| 103 | |
| 104 | return (rc); |
| 105 | } |
| 106 | |
| 107 | /* --- Trivial stepping functions -----------------------------------------*/ |
| 108 | |
| 109 | /* --- @prim_step@ --- */ |
| 110 | |
| 111 | int prim_step(int rq, pgen_event *ev, void *p) |
| 112 | { |
| 113 | unsigned *i = p; |
| 114 | switch (rq) { |
| 115 | case PGEN_BEGIN: |
| 116 | case PGEN_TRY: |
| 117 | if (*i >= NPRIME) |
| 118 | return PGEN_FAIL; |
| 119 | ev->m = mp_fromint(ev->m, primetab[(*i)++]); |
| 120 | return (PGEN_TRY); |
| 121 | } |
| 122 | return (0); |
| 123 | } |
| 124 | |
| 125 | /*----- That's all, folks -------------------------------------------------*/ |