math/gfreduce.[ch]: Fix out-of-bounds memory access.
[u/mdw/catacomb] / math / mpcrt.h
CommitLineData
5ee4c893 1/* -*-c-*-
2 *
5ee4c893 3 * Chinese Remainder Theorem computations (Gauss's algorithm)
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
5ee4c893 9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
45c0fd36 16 *
5ee4c893 17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
45c0fd36 21 *
5ee4c893 22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
5bda60bd 28#ifndef CATACOMB_MPCRT_H
29#define CATACOMB_MPCRT_H
5ee4c893 30
31#ifdef __cplusplus
32 extern "C" {
33#endif
34
35/*----- Header files ------------------------------------------------------*/
36
37#include <stddef.h>
38
5bda60bd 39#ifndef CATACOMB_MP_H
5ee4c893 40# include "mp.h"
41#endif
42
5bda60bd 43#ifndef CATACOMB_MPBARRETT_H
44# include "mpbarrett.h"
5ee4c893 45#endif
46
47/*----- Data structures ---------------------------------------------------*/
48
49typedef struct mpcrt_mod {
50 mp *m; /* %$n_i$% -- the modulus */
51 mp *n; /* %$N_i = n / n_i$% */
52 mp *ni; /* %$M_i = N_i^{-1} \bmod n_i$% */
5bda60bd 53 mp *nni; /* %$N_i M_i \bmod m$% */
5ee4c893 54} mpcrt_mod;
55
56typedef struct mpcrt {
57 size_t k; /* Number of distinct moduli */
5bda60bd 58 mpbarrett mb; /* Barrett context for product */
5ee4c893 59 mpcrt_mod *v; /* Vector of information for each */
60} mpcrt;
61
62/*----- Functions provided ------------------------------------------------*/
63
64/* --- @mpcrt_create@ --- *
65 *
66 * Arguments: @mpcrt *c@ = pointer to CRT context
67 * @mpcrt_mod *v@ = pointer to vector of moduli
68 * @size_t k@ = number of moduli
69 * @mp *n@ = product of all moduli (@MP_NEW@ if unknown)
70 *
71 * Returns: ---
72 *
73 * Use: Initializes a context for solving Chinese Remainder Theorem
74 * problems. The vector of moduli can be incomplete. Omitted
75 * items must be left as null pointers. Not all combinations of
76 * missing things can be coped with, even if there is
77 * technically enough information to cope. For example, if @n@
78 * is unspecified, all the @m@ values must be present, even if
79 * there is one modulus with both @m@ and @n@ (from which the
80 * product of all moduli could clearly be calculated).
81 */
82
83extern void mpcrt_create(mpcrt */*c*/, mpcrt_mod */*v*/,
84 size_t /*k*/, mp */*n*/);
85
86/* --- @mpcrt_destroy@ --- *
87 *
88 * Arguments: @mpcrt *c@ - pointer to CRT context
89 *
90 * Returns: ---
91 *
92 * Use: Destroys a CRT context, releasing all the resources it holds.
93 */
94
95extern void mpcrt_destroy(mpcrt */*c*/);
96
97/* --- @mpcrt_solve@ --- *
98 *
99 * Arguments: @mpcrt *c@ = pointer to CRT context
5bda60bd 100 * @mp *d@ = fake destination
5ee4c893 101 * @mp **v@ = array of residues
102 *
103 * Returns: The unique solution modulo the product of the individual
104 * moduli, which leaves the given residues.
105 *
106 * Use: Constructs a result given its residue modulo an array of
107 * coprime integers. This can be used to improve performance of
108 * RSA encryption or Blum-Blum-Shub generation if the factors
109 * of the modulus are known, since results can be computed mod
110 * each of the individual factors and then combined at the end.
111 * This is rather faster than doing the full-scale modular
112 * exponentiation.
113 */
114
5bda60bd 115extern mp *mpcrt_solve(mpcrt */*c*/, mp */*d*/, mp **/*v*/);
5ee4c893 116
117/*----- That's all, folks -------------------------------------------------*/
118
119#ifdef __cplusplus
120 }
121#endif
122
123#endif