| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: mpcrt.h,v 1.2 1999/12/10 23:22:32 mdw Exp $ |
| 4 | * |
| 5 | * Chinese Remainder Theorem computations (Gauss's algorithm) |
| 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: mpcrt.h,v $ |
| 33 | * Revision 1.2 1999/12/10 23:22:32 mdw |
| 34 | * Interface changes for suggested destinations. Use Barrett reduction. |
| 35 | * |
| 36 | * Revision 1.1 1999/11/22 20:50:57 mdw |
| 37 | * Add support for solving Chinese Remainder Theorem problems. |
| 38 | * |
| 39 | */ |
| 40 | |
| 41 | #ifndef CATACOMB_MPCRT_H |
| 42 | #define CATACOMB_MPCRT_H |
| 43 | |
| 44 | #ifdef __cplusplus |
| 45 | extern "C" { |
| 46 | #endif |
| 47 | |
| 48 | /*----- Header files ------------------------------------------------------*/ |
| 49 | |
| 50 | #include <stddef.h> |
| 51 | |
| 52 | #ifndef CATACOMB_MP_H |
| 53 | # include "mp.h" |
| 54 | #endif |
| 55 | |
| 56 | #ifndef CATACOMB_MPBARRETT_H |
| 57 | # include "mpbarrett.h" |
| 58 | #endif |
| 59 | |
| 60 | /*----- Data structures ---------------------------------------------------*/ |
| 61 | |
| 62 | typedef struct mpcrt_mod { |
| 63 | mp *m; /* %$n_i$% -- the modulus */ |
| 64 | mp *n; /* %$N_i = n / n_i$% */ |
| 65 | mp *ni; /* %$M_i = N_i^{-1} \bmod n_i$% */ |
| 66 | mp *nni; /* %$N_i M_i \bmod m$% */ |
| 67 | } mpcrt_mod; |
| 68 | |
| 69 | typedef struct mpcrt { |
| 70 | size_t k; /* Number of distinct moduli */ |
| 71 | mpbarrett mb; /* Barrett context for product */ |
| 72 | mpcrt_mod *v; /* Vector of information for each */ |
| 73 | } mpcrt; |
| 74 | |
| 75 | /*----- Functions provided ------------------------------------------------*/ |
| 76 | |
| 77 | /* --- @mpcrt_create@ --- * |
| 78 | * |
| 79 | * Arguments: @mpcrt *c@ = pointer to CRT context |
| 80 | * @mpcrt_mod *v@ = pointer to vector of moduli |
| 81 | * @size_t k@ = number of moduli |
| 82 | * @mp *n@ = product of all moduli (@MP_NEW@ if unknown) |
| 83 | * |
| 84 | * Returns: --- |
| 85 | * |
| 86 | * Use: Initializes a context for solving Chinese Remainder Theorem |
| 87 | * problems. The vector of moduli can be incomplete. Omitted |
| 88 | * items must be left as null pointers. Not all combinations of |
| 89 | * missing things can be coped with, even if there is |
| 90 | * technically enough information to cope. For example, if @n@ |
| 91 | * is unspecified, all the @m@ values must be present, even if |
| 92 | * there is one modulus with both @m@ and @n@ (from which the |
| 93 | * product of all moduli could clearly be calculated). |
| 94 | */ |
| 95 | |
| 96 | extern void mpcrt_create(mpcrt */*c*/, mpcrt_mod */*v*/, |
| 97 | size_t /*k*/, mp */*n*/); |
| 98 | |
| 99 | /* --- @mpcrt_destroy@ --- * |
| 100 | * |
| 101 | * Arguments: @mpcrt *c@ - pointer to CRT context |
| 102 | * |
| 103 | * Returns: --- |
| 104 | * |
| 105 | * Use: Destroys a CRT context, releasing all the resources it holds. |
| 106 | */ |
| 107 | |
| 108 | extern void mpcrt_destroy(mpcrt */*c*/); |
| 109 | |
| 110 | /* --- @mpcrt_solve@ --- * |
| 111 | * |
| 112 | * Arguments: @mpcrt *c@ = pointer to CRT context |
| 113 | * @mp *d@ = fake destination |
| 114 | * @mp **v@ = array of residues |
| 115 | * |
| 116 | * Returns: The unique solution modulo the product of the individual |
| 117 | * moduli, which leaves the given residues. |
| 118 | * |
| 119 | * Use: Constructs a result given its residue modulo an array of |
| 120 | * coprime integers. This can be used to improve performance of |
| 121 | * RSA encryption or Blum-Blum-Shub generation if the factors |
| 122 | * of the modulus are known, since results can be computed mod |
| 123 | * each of the individual factors and then combined at the end. |
| 124 | * This is rather faster than doing the full-scale modular |
| 125 | * exponentiation. |
| 126 | */ |
| 127 | |
| 128 | extern mp *mpcrt_solve(mpcrt */*c*/, mp */*d*/, mp **/*v*/); |
| 129 | |
| 130 | /*----- That's all, folks -------------------------------------------------*/ |
| 131 | |
| 132 | #ifdef __cplusplus |
| 133 | } |
| 134 | #endif |
| 135 | |
| 136 | #endif |