1 /* mpihelp-mul.c - MPI helper functions
2 * Copyright (C) 1994, 1996, 1998, 1999,
3 * 2000 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 * Note: This code is heavily based on the GNU MP Library.
21 * Actually it's the same code with only minor changes in the
22 * way the data is stored; this is to support the abstraction
23 * of an optional secure memory allocation which may be used
24 * to avoid revealing of sensitive data due to paging etc.
25 * The GNU MP Library itself is published under the LGPL;
26 * however I decided to publish this code under the plain GPL.
33 #include "mpi-internal.h"
38 #define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
40 if( (size) < KARATSUBA_THRESHOLD ) \
41 mul_n_basecase (prodp, up, vp, size); \
43 mul_n (prodp, up, vp, size, tspace); \
46 #define MPN_SQR_N_RECURSE(prodp, up, size, tspace) \
48 if ((size) < KARATSUBA_THRESHOLD) \
49 mpih_sqr_n_basecase (prodp, up, size); \
51 mpih_sqr_n (prodp, up, size, tspace); \
57 /* Multiply the natural numbers u (pointed to by UP) and v (pointed to by VP),
58 * both with SIZE limbs, and store the result at PRODP. 2 * SIZE limbs are
59 * always stored. Return the most significant limb.
61 * Argument constraints:
62 * 1. PRODP != UP and PRODP != VP, i.e. the destination
63 * must be distinct from the multiplier and the multiplicand.
66 * Handle simple cases with traditional multiplication.
68 * This is the most critical code of multiplication. All multiplies rely
69 * on this, both small and huge. Small ones arrive here immediately. Huge
70 * ones arrive here as this is the base case for Karatsuba's recursive
75 mul_n_basecase( mpi_ptr_t prodp, mpi_ptr_t up,
76 mpi_ptr_t vp, mpi_size_t size)
82 /* Multiply by the first limb in V separately, as the result can be
83 * stored (not added) to PROD. We also avoid a loop for zeroing. */
87 MPN_COPY( prodp, up, size );
89 MPN_ZERO( prodp, size );
93 cy = mpihelp_mul_1( prodp, up, size, v_limb );
98 /* For each iteration in the outer loop, multiply one limb from
99 * U with one limb from V, and add it to PROD. */
100 for( i = 1; i < size; i++ ) {
105 cy = mpihelp_add_n(prodp, prodp, up, size);
108 cy = mpihelp_addmul_1(prodp, up, size, v_limb);
119 mul_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_ptr_t vp,
120 mpi_size_t size, mpi_ptr_t tspace )
123 /* The size is odd, and the code below doesn't handle that.
124 * Multiply the least significant (size - 1) limbs with a recursive
125 * call, and handle the most significant limb of S1 and S2
127 * A slightly faster way to do this would be to make the Karatsuba
128 * code below behave as if the size were even, and let it check for
129 * odd size in the end. I.e., in essence move this code to the end.
130 * Doing so would save us a recursive call, and potentially make the
131 * stack grow a lot less.
133 mpi_size_t esize = size - 1; /* even size */
136 MPN_MUL_N_RECURSE( prodp, up, vp, esize, tspace );
137 cy_limb = mpihelp_addmul_1( prodp + esize, up, esize, vp[esize] );
138 prodp[esize + esize] = cy_limb;
139 cy_limb = mpihelp_addmul_1( prodp + esize, vp, size, up[esize] );
140 prodp[esize + size] = cy_limb;
143 /* Anatolij Alekseevich Karatsuba's divide-and-conquer algorithm.
145 * Split U in two pieces, U1 and U0, such that
146 * U = U0 + U1*(B**n),
147 * and V in V1 and V0, such that
148 * V = V0 + V1*(B**n).
150 * UV is then computed recursively using the identity
153 * UV = (B + B )U V + B (U -U )(V -V ) + (B + 1)U V
156 * Where B = 2**BITS_PER_MP_LIMB.
158 mpi_size_t hsize = size >> 1;
162 /* Product H. ________________ ________________
163 * |_____U1 x V1____||____U0 x V0_____|
164 * Put result in upper part of PROD and pass low part of TSPACE
167 MPN_MUL_N_RECURSE(prodp + size, up + hsize, vp + hsize, hsize, tspace);
169 /* Product M. ________________
172 if( mpihelp_cmp(up + hsize, up, hsize) >= 0 ) {
173 mpihelp_sub_n(prodp, up + hsize, up, hsize);
177 mpihelp_sub_n(prodp, up, up + hsize, hsize);
180 if( mpihelp_cmp(vp + hsize, vp, hsize) >= 0 ) {
181 mpihelp_sub_n(prodp + hsize, vp + hsize, vp, hsize);
185 mpihelp_sub_n(prodp + hsize, vp, vp + hsize, hsize);
186 /* No change of NEGFLG. */
188 /* Read temporary operands from low part of PROD.
189 * Put result in low part of TSPACE using upper part of TSPACE
192 MPN_MUL_N_RECURSE(tspace, prodp, prodp + hsize, hsize, tspace + size);
194 /* Add/copy product H. */
195 MPN_COPY (prodp + hsize, prodp + size, hsize);
196 cy = mpihelp_add_n( prodp + size, prodp + size,
197 prodp + size + hsize, hsize);
199 /* Add product M (if NEGFLG M is a negative number) */
201 cy -= mpihelp_sub_n(prodp + hsize, prodp + hsize, tspace, size);
203 cy += mpihelp_add_n(prodp + hsize, prodp + hsize, tspace, size);
205 /* Product L. ________________ ________________
206 * |________________||____U0 x V0_____|
207 * Read temporary operands from low part of PROD.
208 * Put result in low part of TSPACE using upper part of TSPACE
211 MPN_MUL_N_RECURSE(tspace, up, vp, hsize, tspace + size);
213 /* Add/copy Product L (twice) */
215 cy += mpihelp_add_n(prodp + hsize, prodp + hsize, tspace, size);
217 mpihelp_add_1(prodp + hsize + size, prodp + hsize + size, hsize, cy);
219 MPN_COPY(prodp, tspace, hsize);
220 cy = mpihelp_add_n(prodp + hsize, prodp + hsize, tspace + hsize, hsize);
222 mpihelp_add_1(prodp + size, prodp + size, size, 1);
228 mpih_sqr_n_basecase( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size )
234 /* Multiply by the first limb in V separately, as the result can be
235 * stored (not added) to PROD. We also avoid a loop for zeroing. */
239 MPN_COPY( prodp, up, size );
241 MPN_ZERO(prodp, size);
245 cy_limb = mpihelp_mul_1( prodp, up, size, v_limb );
247 prodp[size] = cy_limb;
250 /* For each iteration in the outer loop, multiply one limb from
251 * U with one limb from V, and add it to PROD. */
252 for( i=1; i < size; i++) {
257 cy_limb = mpihelp_add_n(prodp, prodp, up, size);
260 cy_limb = mpihelp_addmul_1(prodp, up, size, v_limb);
262 prodp[size] = cy_limb;
269 mpih_sqr_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size, mpi_ptr_t tspace)
272 /* The size is odd, and the code below doesn't handle that.
273 * Multiply the least significant (size - 1) limbs with a recursive
274 * call, and handle the most significant limb of S1 and S2
276 * A slightly faster way to do this would be to make the Karatsuba
277 * code below behave as if the size were even, and let it check for
278 * odd size in the end. I.e., in essence move this code to the end.
279 * Doing so would save us a recursive call, and potentially make the
280 * stack grow a lot less.
282 mpi_size_t esize = size - 1; /* even size */
285 MPN_SQR_N_RECURSE( prodp, up, esize, tspace );
286 cy_limb = mpihelp_addmul_1( prodp + esize, up, esize, up[esize] );
287 prodp[esize + esize] = cy_limb;
288 cy_limb = mpihelp_addmul_1( prodp + esize, up, size, up[esize] );
290 prodp[esize + size] = cy_limb;
293 mpi_size_t hsize = size >> 1;
296 /* Product H. ________________ ________________
297 * |_____U1 x U1____||____U0 x U0_____|
298 * Put result in upper part of PROD and pass low part of TSPACE
301 MPN_SQR_N_RECURSE(prodp + size, up + hsize, hsize, tspace);
303 /* Product M. ________________
306 if( mpihelp_cmp( up + hsize, up, hsize) >= 0 )
307 mpihelp_sub_n( prodp, up + hsize, up, hsize);
309 mpihelp_sub_n (prodp, up, up + hsize, hsize);
311 /* Read temporary operands from low part of PROD.
312 * Put result in low part of TSPACE using upper part of TSPACE
314 MPN_SQR_N_RECURSE(tspace, prodp, hsize, tspace + size);
316 /* Add/copy product H */
317 MPN_COPY(prodp + hsize, prodp + size, hsize);
318 cy = mpihelp_add_n(prodp + size, prodp + size,
319 prodp + size + hsize, hsize);
321 /* Add product M (if NEGFLG M is a negative number). */
322 cy -= mpihelp_sub_n (prodp + hsize, prodp + hsize, tspace, size);
324 /* Product L. ________________ ________________
325 * |________________||____U0 x U0_____|
326 * Read temporary operands from low part of PROD.
327 * Put result in low part of TSPACE using upper part of TSPACE
329 MPN_SQR_N_RECURSE (tspace, up, hsize, tspace + size);
331 /* Add/copy Product L (twice). */
332 cy += mpihelp_add_n (prodp + hsize, prodp + hsize, tspace, size);
334 mpihelp_add_1(prodp + hsize + size, prodp + hsize + size,
337 MPN_COPY(prodp, tspace, hsize);
338 cy = mpihelp_add_n (prodp + hsize, prodp + hsize, tspace + hsize, hsize);
340 mpihelp_add_1 (prodp + size, prodp + size, size, 1);
345 /* This should be made into an inline function in gmp.h. */
347 mpihelp_mul_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_ptr_t vp, mpi_size_t size)
352 if( size < KARATSUBA_THRESHOLD )
353 mpih_sqr_n_basecase( prodp, up, size );
356 secure = m_is_secure( up );
357 tspace = mpi_alloc_limb_space( 2 * size, secure );
358 mpih_sqr_n( prodp, up, size, tspace );
359 mpi_free_limb_space( tspace );
363 if( size < KARATSUBA_THRESHOLD )
364 mul_n_basecase( prodp, up, vp, size );
367 secure = m_is_secure( up ) || m_is_secure( vp );
368 tspace = mpi_alloc_limb_space( 2 * size, secure );
369 mul_n (prodp, up, vp, size, tspace);
370 mpi_free_limb_space( tspace );
378 mpihelp_mul_karatsuba_case( mpi_ptr_t prodp,
379 mpi_ptr_t up, mpi_size_t usize,
380 mpi_ptr_t vp, mpi_size_t vsize,
381 struct karatsuba_ctx *ctx )
385 if( !ctx->tspace || ctx->tspace_size < vsize ) {
387 mpi_free_limb_space( ctx->tspace );
388 ctx->tspace = mpi_alloc_limb_space( 2 * vsize,
389 m_is_secure( up ) || m_is_secure( vp ) );
390 ctx->tspace_size = vsize;
393 MPN_MUL_N_RECURSE( prodp, up, vp, vsize, ctx->tspace );
398 if( usize >= vsize ) {
399 if( !ctx->tp || ctx->tp_size < vsize ) {
401 mpi_free_limb_space( ctx->tp );
402 ctx->tp = mpi_alloc_limb_space( 2 * vsize, m_is_secure( up )
403 || m_is_secure( vp ) );
404 ctx->tp_size = vsize;
408 MPN_MUL_N_RECURSE( ctx->tp, up, vp, vsize, ctx->tspace );
409 cy = mpihelp_add_n( prodp, prodp, ctx->tp, vsize );
410 mpihelp_add_1( prodp + vsize, ctx->tp + vsize, vsize, cy );
414 } while( usize >= vsize );
418 if( usize < KARATSUBA_THRESHOLD ) {
419 mpihelp_mul( ctx->tspace, vp, vsize, up, usize );
423 ctx->next = xmalloc_clear( sizeof *ctx );
425 mpihelp_mul_karatsuba_case( ctx->tspace,
431 cy = mpihelp_add_n( prodp, prodp, ctx->tspace, vsize);
432 mpihelp_add_1( prodp + vsize, ctx->tspace + vsize, usize, cy );
438 mpihelp_release_karatsuba_ctx( struct karatsuba_ctx *ctx )
440 struct karatsuba_ctx *ctx2;
443 mpi_free_limb_space( ctx->tp );
445 mpi_free_limb_space( ctx->tspace );
446 for( ctx=ctx->next; ctx; ctx = ctx2 ) {
449 mpi_free_limb_space( ctx->tp );
451 mpi_free_limb_space( ctx->tspace );
456 /* Multiply the natural numbers u (pointed to by UP, with USIZE limbs)
457 * and v (pointed to by VP, with VSIZE limbs), and store the result at
458 * PRODP. USIZE + VSIZE limbs are always stored, but if the input
459 * operands are normalized. Return the most significant limb of the
462 * NOTE: The space pointed to by PRODP is overwritten before finished
463 * with U and V, so overlap is an error.
465 * Argument constraints:
467 * 2. PRODP != UP and PRODP != VP, i.e. the destination
468 * must be distinct from the multiplier and the multiplicand.
472 mpihelp_mul( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
473 mpi_ptr_t vp, mpi_size_t vsize)
475 mpi_ptr_t prod_endp = prodp + usize + vsize - 1;
477 struct karatsuba_ctx ctx;
479 if( vsize < KARATSUBA_THRESHOLD ) {
486 /* Multiply by the first limb in V separately, as the result can be
487 * stored (not added) to PROD. We also avoid a loop for zeroing. */
491 MPN_COPY( prodp, up, usize );
493 MPN_ZERO( prodp, usize );
497 cy = mpihelp_mul_1( prodp, up, usize, v_limb );
502 /* For each iteration in the outer loop, multiply one limb from
503 * U with one limb from V, and add it to PROD. */
504 for( i = 1; i < vsize; i++ ) {
509 cy = mpihelp_add_n(prodp, prodp, up, usize);
512 cy = mpihelp_addmul_1(prodp, up, usize, v_limb);
521 memset( &ctx, 0, sizeof ctx );
522 mpihelp_mul_karatsuba_case( prodp, up, usize, vp, vsize, &ctx );
523 mpihelp_release_karatsuba_ctx( &ctx );