1 /* mpih-mul.c - MPI helper functions
2 * Copyright (C) 1994, 1996, 1998, 1999, 2000,
3 * 2001, 2002 Free Software Foundation, Inc.
5 * This file is part of Libgcrypt.
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
12 * Libgcrypt 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 Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 * Note: This code is heavily based on the GNU MP Library.
22 * Actually it's the same code with only minor changes in the
23 * way the data is stored; this is to support the abstraction
24 * of an optional secure memory allocation which may be used
25 * to avoid revealing of sensitive data due to paging etc.
32 #include "mpi-internal.h"
36 #define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
38 if( (size) < KARATSUBA_THRESHOLD ) \
39 mul_n_basecase (prodp, up, vp, size); \
41 mul_n (prodp, up, vp, size, tspace); \
44 #define MPN_SQR_N_RECURSE(prodp, up, size, tspace) \
46 if ((size) < KARATSUBA_THRESHOLD) \
47 _gcry_mpih_sqr_n_basecase (prodp, up, size); \
49 _gcry_mpih_sqr_n (prodp, up, size, tspace); \
55 /* Multiply the natural numbers u (pointed to by UP) and v (pointed to by VP),
56 * both with SIZE limbs, and store the result at PRODP. 2 * SIZE limbs are
57 * always stored. Return the most significant limb.
59 * Argument constraints:
60 * 1. PRODP != UP and PRODP != VP, i.e. the destination
61 * must be distinct from the multiplier and the multiplicand.
64 * Handle simple cases with traditional multiplication.
66 * This is the most critical code of multiplication. All multiplies rely
67 * on this, both small and huge. Small ones arrive here immediately. Huge
68 * ones arrive here as this is the base case for Karatsuba's recursive
73 mul_n_basecase( mpi_ptr_t prodp, mpi_ptr_t up,
74 mpi_ptr_t vp, mpi_size_t size)
80 /* Multiply by the first limb in V separately, as the result can be
81 * stored (not added) to PROD. We also avoid a loop for zeroing. */
85 MPN_COPY( prodp, up, size );
87 MPN_ZERO( prodp, size );
91 cy = _gcry_mpih_mul_1( prodp, up, size, v_limb );
96 /* For each iteration in the outer loop, multiply one limb from
97 * U with one limb from V, and add it to PROD. */
98 for( i = 1; i < size; i++ ) {
103 cy = _gcry_mpih_add_n(prodp, prodp, up, size);
106 cy = _gcry_mpih_addmul_1(prodp, up, size, v_limb);
117 mul_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_ptr_t vp,
118 mpi_size_t size, mpi_ptr_t tspace )
121 /* The size is odd, and the code below doesn't handle that.
122 * Multiply the least significant (size - 1) limbs with a recursive
123 * call, and handle the most significant limb of S1 and S2
125 * A slightly faster way to do this would be to make the Karatsuba
126 * code below behave as if the size were even, and let it check for
127 * odd size in the end. I.e., in essence move this code to the end.
128 * Doing so would save us a recursive call, and potentially make the
129 * stack grow a lot less.
131 mpi_size_t esize = size - 1; /* even size */
134 MPN_MUL_N_RECURSE( prodp, up, vp, esize, tspace );
135 cy_limb = _gcry_mpih_addmul_1( prodp + esize, up, esize, vp[esize] );
136 prodp[esize + esize] = cy_limb;
137 cy_limb = _gcry_mpih_addmul_1( prodp + esize, vp, size, up[esize] );
138 prodp[esize + size] = cy_limb;
141 /* Anatolij Alekseevich Karatsuba's divide-and-conquer algorithm.
143 * Split U in two pieces, U1 and U0, such that
144 * U = U0 + U1*(B**n),
145 * and V in V1 and V0, such that
146 * V = V0 + V1*(B**n).
148 * UV is then computed recursively using the identity
151 * UV = (B + B )U V + B (U -U )(V -V ) + (B + 1)U V
154 * Where B = 2**BITS_PER_MP_LIMB.
156 mpi_size_t hsize = size >> 1;
160 /* Product H. ________________ ________________
161 * |_____U1 x V1____||____U0 x V0_____|
162 * Put result in upper part of PROD and pass low part of TSPACE
165 MPN_MUL_N_RECURSE(prodp + size, up + hsize, vp + hsize, hsize, tspace);
167 /* Product M. ________________
170 if( _gcry_mpih_cmp(up + hsize, up, hsize) >= 0 ) {
171 _gcry_mpih_sub_n(prodp, up + hsize, up, hsize);
175 _gcry_mpih_sub_n(prodp, up, up + hsize, hsize);
178 if( _gcry_mpih_cmp(vp + hsize, vp, hsize) >= 0 ) {
179 _gcry_mpih_sub_n(prodp + hsize, vp + hsize, vp, hsize);
183 _gcry_mpih_sub_n(prodp + hsize, vp, vp + hsize, hsize);
184 /* No change of NEGFLG. */
186 /* Read temporary operands from low part of PROD.
187 * Put result in low part of TSPACE using upper part of TSPACE
190 MPN_MUL_N_RECURSE(tspace, prodp, prodp + hsize, hsize, tspace + size);
192 /* Add/copy product H. */
193 MPN_COPY (prodp + hsize, prodp + size, hsize);
194 cy = _gcry_mpih_add_n( prodp + size, prodp + size,
195 prodp + size + hsize, hsize);
197 /* Add product M (if NEGFLG M is a negative number) */
199 cy -= _gcry_mpih_sub_n(prodp + hsize, prodp + hsize, tspace, size);
201 cy += _gcry_mpih_add_n(prodp + hsize, prodp + hsize, tspace, size);
203 /* Product L. ________________ ________________
204 * |________________||____U0 x V0_____|
205 * Read temporary operands from low part of PROD.
206 * Put result in low part of TSPACE using upper part of TSPACE
209 MPN_MUL_N_RECURSE(tspace, up, vp, hsize, tspace + size);
211 /* Add/copy Product L (twice) */
213 cy += _gcry_mpih_add_n(prodp + hsize, prodp + hsize, tspace, size);
215 _gcry_mpih_add_1(prodp + hsize + size, prodp + hsize + size, hsize, cy);
217 MPN_COPY(prodp, tspace, hsize);
218 cy = _gcry_mpih_add_n(prodp + hsize, prodp + hsize, tspace + hsize, hsize);
220 _gcry_mpih_add_1(prodp + size, prodp + size, size, 1);
226 _gcry_mpih_sqr_n_basecase( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size )
232 /* Multiply by the first limb in V separately, as the result can be
233 * stored (not added) to PROD. We also avoid a loop for zeroing. */
237 MPN_COPY( prodp, up, size );
239 MPN_ZERO(prodp, size);
243 cy_limb = _gcry_mpih_mul_1( prodp, up, size, v_limb );
245 prodp[size] = cy_limb;
248 /* For each iteration in the outer loop, multiply one limb from
249 * U with one limb from V, and add it to PROD. */
250 for( i=1; i < size; i++) {
255 cy_limb = _gcry_mpih_add_n(prodp, prodp, up, size);
258 cy_limb = _gcry_mpih_addmul_1(prodp, up, size, v_limb);
260 prodp[size] = cy_limb;
267 _gcry_mpih_sqr_n( mpi_ptr_t prodp,
268 mpi_ptr_t up, mpi_size_t size, mpi_ptr_t tspace)
271 /* The size is odd, and the code below doesn't handle that.
272 * Multiply the least significant (size - 1) limbs with a recursive
273 * call, and handle the most significant limb of S1 and S2
275 * A slightly faster way to do this would be to make the Karatsuba
276 * code below behave as if the size were even, and let it check for
277 * odd size in the end. I.e., in essence move this code to the end.
278 * Doing so would save us a recursive call, and potentially make the
279 * stack grow a lot less.
281 mpi_size_t esize = size - 1; /* even size */
284 MPN_SQR_N_RECURSE( prodp, up, esize, tspace );
285 cy_limb = _gcry_mpih_addmul_1( prodp + esize, up, esize, up[esize] );
286 prodp[esize + esize] = cy_limb;
287 cy_limb = _gcry_mpih_addmul_1( prodp + esize, up, size, up[esize] );
289 prodp[esize + size] = cy_limb;
292 mpi_size_t hsize = size >> 1;
295 /* Product H. ________________ ________________
296 * |_____U1 x U1____||____U0 x U0_____|
297 * Put result in upper part of PROD and pass low part of TSPACE
300 MPN_SQR_N_RECURSE(prodp + size, up + hsize, hsize, tspace);
302 /* Product M. ________________
305 if( _gcry_mpih_cmp( up + hsize, up, hsize) >= 0 )
306 _gcry_mpih_sub_n( prodp, up + hsize, up, hsize);
308 _gcry_mpih_sub_n (prodp, up, up + hsize, hsize);
310 /* Read temporary operands from low part of PROD.
311 * Put result in low part of TSPACE using upper part of TSPACE
313 MPN_SQR_N_RECURSE(tspace, prodp, hsize, tspace + size);
315 /* Add/copy product H */
316 MPN_COPY(prodp + hsize, prodp + size, hsize);
317 cy = _gcry_mpih_add_n(prodp + size, prodp + size,
318 prodp + size + hsize, hsize);
320 /* Add product M (if NEGFLG M is a negative number). */
321 cy -= _gcry_mpih_sub_n (prodp + hsize, prodp + hsize, tspace, size);
323 /* Product L. ________________ ________________
324 * |________________||____U0 x U0_____|
325 * Read temporary operands from low part of PROD.
326 * Put result in low part of TSPACE using upper part of TSPACE
328 MPN_SQR_N_RECURSE (tspace, up, hsize, tspace + size);
330 /* Add/copy Product L (twice). */
331 cy += _gcry_mpih_add_n (prodp + hsize, prodp + hsize, tspace, size);
333 _gcry_mpih_add_1(prodp + hsize + size, prodp + hsize + size,
336 MPN_COPY(prodp, tspace, hsize);
337 cy = _gcry_mpih_add_n (prodp + hsize, prodp + hsize, tspace + hsize, hsize);
339 _gcry_mpih_add_1 (prodp + size, prodp + size, size, 1);
344 /* This should be made into an inline function in gmp.h. */
346 _gcry_mpih_mul_n( mpi_ptr_t prodp,
347 mpi_ptr_t up, mpi_ptr_t vp, mpi_size_t size)
352 if( size < KARATSUBA_THRESHOLD )
353 _gcry_mpih_sqr_n_basecase( prodp, up, size );
356 secure = gcry_is_secure( up );
357 tspace = mpi_alloc_limb_space( 2 * size, secure );
358 _gcry_mpih_sqr_n( prodp, up, size, tspace );
359 _gcry_mpi_free_limb_space (tspace, 2 * size );
363 if( size < KARATSUBA_THRESHOLD )
364 mul_n_basecase( prodp, up, vp, size );
367 secure = gcry_is_secure( up ) || gcry_is_secure( vp );
368 tspace = mpi_alloc_limb_space( 2 * size, secure );
369 mul_n (prodp, up, vp, size, tspace);
370 _gcry_mpi_free_limb_space (tspace, 2 * size );
378 _gcry_mpih_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 _gcry_mpi_free_limb_space( ctx->tspace, ctx->tspace_nlimbs );
388 ctx->tspace_nlimbs = 2 * vsize;
389 ctx->tspace = mpi_alloc_limb_space( 2 * vsize,
390 (gcry_is_secure( up )
391 || gcry_is_secure( vp )) );
392 ctx->tspace_size = vsize;
395 MPN_MUL_N_RECURSE( prodp, up, vp, vsize, ctx->tspace );
400 if( usize >= vsize ) {
401 if( !ctx->tp || ctx->tp_size < vsize ) {
403 _gcry_mpi_free_limb_space( ctx->tp, ctx->tp_nlimbs );
404 ctx->tp_nlimbs = 2 * vsize;
405 ctx->tp = mpi_alloc_limb_space( 2 * vsize, gcry_is_secure( up )
406 || gcry_is_secure( vp ) );
407 ctx->tp_size = vsize;
411 MPN_MUL_N_RECURSE( ctx->tp, up, vp, vsize, ctx->tspace );
412 cy = _gcry_mpih_add_n( prodp, prodp, ctx->tp, vsize );
413 _gcry_mpih_add_1( prodp + vsize, ctx->tp + vsize, vsize, cy );
417 } while( usize >= vsize );
421 if( usize < KARATSUBA_THRESHOLD ) {
422 _gcry_mpih_mul( ctx->tspace, vp, vsize, up, usize );
426 ctx->next = gcry_xcalloc( 1, sizeof *ctx );
428 _gcry_mpih_mul_karatsuba_case( ctx->tspace,
434 cy = _gcry_mpih_add_n( prodp, prodp, ctx->tspace, vsize);
435 _gcry_mpih_add_1( prodp + vsize, ctx->tspace + vsize, usize, cy );
441 _gcry_mpih_release_karatsuba_ctx( struct karatsuba_ctx *ctx )
443 struct karatsuba_ctx *ctx2;
446 _gcry_mpi_free_limb_space( ctx->tp, ctx->tp_nlimbs );
448 _gcry_mpi_free_limb_space( ctx->tspace, ctx->tspace_nlimbs );
449 for( ctx=ctx->next; ctx; ctx = ctx2 ) {
452 _gcry_mpi_free_limb_space( ctx->tp, ctx->tp_nlimbs );
454 _gcry_mpi_free_limb_space( ctx->tspace, ctx->tspace_nlimbs );
459 /* Multiply the natural numbers u (pointed to by UP, with USIZE limbs)
460 * and v (pointed to by VP, with VSIZE limbs), and store the result at
461 * PRODP. USIZE + VSIZE limbs are always stored, but if the input
462 * operands are normalized. Return the most significant limb of the
465 * NOTE: The space pointed to by PRODP is overwritten before finished
466 * with U and V, so overlap is an error.
468 * Argument constraints:
470 * 2. PRODP != UP and PRODP != VP, i.e. the destination
471 * must be distinct from the multiplier and the multiplicand.
475 _gcry_mpih_mul( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
476 mpi_ptr_t vp, mpi_size_t vsize)
478 mpi_ptr_t prod_endp = prodp + usize + vsize - 1;
480 struct karatsuba_ctx ctx;
482 if( vsize < KARATSUBA_THRESHOLD ) {
489 /* Multiply by the first limb in V separately, as the result can be
490 * stored (not added) to PROD. We also avoid a loop for zeroing. */
494 MPN_COPY( prodp, up, usize );
496 MPN_ZERO( prodp, usize );
500 cy = _gcry_mpih_mul_1( prodp, up, usize, v_limb );
505 /* For each iteration in the outer loop, multiply one limb from
506 * U with one limb from V, and add it to PROD. */
507 for( i = 1; i < vsize; i++ ) {
512 cy = _gcry_mpih_add_n(prodp, prodp, up, usize);
515 cy = _gcry_mpih_addmul_1(prodp, up, usize, v_limb);
524 memset( &ctx, 0, sizeof ctx );
525 _gcry_mpih_mul_karatsuba_case( prodp, up, usize, vp, vsize, &ctx );
526 _gcry_mpih_release_karatsuba_ctx( &ctx );