3
Usually, modular reduction is accomplished by long division, using the
4
mp_div() or mp_mod() functions. However, when performing modular
5
exponentiation, you spend a lot of time reducing by the same modulus
6
again and again. For this purpose, doing a full division for each
7
multiplication is quite inefficient.
9
For this reason, the mp_exptmod() function does not perform modular
10
reductions in the usual way, but instead takes advantage of an
11
algorithm due to Barrett, as described by Menezes, Oorschot and
12
VanStone in their book _Handbook of Applied Cryptography_, published
13
by the CRC Press (see Chapter 14 for details). This method reduces
14
most of the computation of reduction to efficient shifting and masking
15
operations, and avoids the multiple-precision division entirely.
17
Here is a brief synopsis of Barrett reduction, as it is implemented in
20
Let b denote the radix of the computation (one more than the maximum
21
value that can be denoted by an mp_digit). Let m be the modulus, and
22
let k be the number of significant digits of m. Let x be the value to
23
be reduced modulo m. By the Division Theorem, there exist unique
24
integers Q and R such that:
26
x = Qm + R, 0 <= R < m
28
Barrett reduction takes advantage of the fact that you can easily
29
approximate Q to within two, given a value M such that:
36
Computation of M requires a full-precision division step, so if you
37
are only doing a single reduction by m, you gain no advantage.
38
However, when multiple reductions by the same m are required, this
39
division need only be done once, beforehand. Using this, we can use
40
the following equation to compute Q', an approximation of Q:
46
Q' = floor( ----------------- )
50
The divisions by b^(k-1) and b^(k+1) and the floor() functions can be
51
efficiently implemented with shifts and masks, leaving only a single
52
multiplication to be performed to get this approximation. It can be
53
shown that Q - 2 <= Q' <= Q, so in the worst case, we can get out with
54
two additional subtractions to bring the value into line with the
57
Once we've got Q', we basically multiply that by m and subtract from
60
x - Q'm = Qm + R - Q'm
62
Since we know the constraint on Q', this is one of:
68
Since R < m by the Division Theorem, we can simply subtract off m
69
until we get a value in the correct range, which will happen with no
70
more than 2 subtractions:
79
In random performance trials, modular exponentiation using this method
80
of reduction gave around a 40% speedup over using the division for
83
------------------------------------------------------------------
84
The contents of this file are subject to the Mozilla Public
85
License Version 1.1 (the "License"); you may not use this file
86
except in compliance with the License. You may obtain a copy of
87
the License at http://www.mozilla.org/MPL/
89
Software distributed under the License is distributed on an "AS
90
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
91
implied. See the License for the specific language governing
92
rights and limitations under the License.
94
The Original Code is the MPI Arbitrary Precision Integer Arithmetic
97
The Initial Developer of the Original Code is
98
Michael J. Fromberger <sting@linguist.dartmouth.edu>
100
Portions created by Michael J. Fromberger are
101
Copyright (C) 1998, 2000 Michael J. Fromberger. All Rights Reserved.
105
Alternatively, the contents of this file may be used under the
106
terms of the GNU General Public License Version 2 or later (the
107
"GPL"), in which case the provisions of the GPL are applicable
108
instead of those above. If you wish to allow use of your
109
version of this file only under the terms of the GPL and not to
110
allow others to use your version of this file under the MPL,
111
indicate your decision by deleting the provisions above and
112
replace them with the notice and other provisions required by
113
the GPL. If you do not delete the provisions above, a recipient
114
may use your version of this file under either the MPL or the GPL.
116
$Id: redux.txt,v 1.1 2000/07/14 00:44:36 nelsonb%netscape.com Exp $