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