-
+ 2D6F3C1ECB3331CDFB54C9EFA53246CAC6A6DFA16465D2E354C6801974964229CB173E7AFF19C4C92E896B8C65CFBC6DA79EF03C20022772F5C299F1C3CBC063
mpi/mpih-mul.c
(0 . 0)(1 . 527)
8375 /* mpihelp-mul.c - MPI helper functions
8376 * Copyright (C) 1994, 1996, 1998, 1999,
8377 * 2000 Free Software Foundation, Inc.
8378 *
8379 * This file is part of GnuPG.
8380 *
8381 * GnuPG is free software; you can redistribute it and/or modify
8382 * it under the terms of the GNU General Public License as published by
8383 * the Free Software Foundation; either version 3 of the License, or
8384 * (at your option) any later version.
8385 *
8386 * GnuPG is distributed in the hope that it will be useful,
8387 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8388 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8389 * GNU General Public License for more details.
8390 *
8391 * You should have received a copy of the GNU General Public License
8392 * along with this program; if not, see <http://www.gnu.org/licenses/>.
8393 *
8394 * Note: This code is heavily based on the GNU MP Library.
8395 * Actually it's the same code with only minor changes in the
8396 * way the data is stored; this is to support the abstraction
8397 * of an optional secure memory allocation which may be used
8398 * to avoid revealing of sensitive data due to paging etc.
8399 * The GNU MP Library itself is published under the LGPL;
8400 * however I decided to publish this code under the plain GPL.
8401 */
8402
8403 #include <config.h>
8404 #include <stdio.h>
8405 #include <stdlib.h>
8406 #include <string.h>
8407 #include "mpi-internal.h"
8408 #include "longlong.h"
8409
8410
8411
8412 #define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
8413 do { \
8414 if( (size) < KARATSUBA_THRESHOLD ) \
8415 mul_n_basecase (prodp, up, vp, size); \
8416 else \
8417 mul_n (prodp, up, vp, size, tspace); \
8418 } while (0);
8419
8420 #define MPN_SQR_N_RECURSE(prodp, up, size, tspace) \
8421 do { \
8422 if ((size) < KARATSUBA_THRESHOLD) \
8423 mpih_sqr_n_basecase (prodp, up, size); \
8424 else \
8425 mpih_sqr_n (prodp, up, size, tspace); \
8426 } while (0);
8427
8428
8429
8430
8431 /* Multiply the natural numbers u (pointed to by UP) and v (pointed to by VP),
8432 * both with SIZE limbs, and store the result at PRODP. 2 * SIZE limbs are
8433 * always stored. Return the most significant limb.
8434 *
8435 * Argument constraints:
8436 * 1. PRODP != UP and PRODP != VP, i.e. the destination
8437 * must be distinct from the multiplier and the multiplicand.
8438 *
8439 *
8440 * Handle simple cases with traditional multiplication.
8441 *
8442 * This is the most critical code of multiplication. All multiplies rely
8443 * on this, both small and huge. Small ones arrive here immediately. Huge
8444 * ones arrive here as this is the base case for Karatsuba's recursive
8445 * algorithm below.
8446 */
8447
8448 static mpi_limb_t
8449 mul_n_basecase( mpi_ptr_t prodp, mpi_ptr_t up,
8450 mpi_ptr_t vp, mpi_size_t size)
8451 {
8452 mpi_size_t i;
8453 mpi_limb_t cy;
8454 mpi_limb_t v_limb;
8455
8456 /* Multiply by the first limb in V separately, as the result can be
8457 * stored (not added) to PROD. We also avoid a loop for zeroing. */
8458 v_limb = vp[0];
8459 if( v_limb <= 1 ) {
8460 if( v_limb == 1 )
8461 MPN_COPY( prodp, up, size );
8462 else
8463 MPN_ZERO( prodp, size );
8464 cy = 0;
8465 }
8466 else
8467 cy = mpihelp_mul_1( prodp, up, size, v_limb );
8468
8469 prodp[size] = cy;
8470 prodp++;
8471
8472 /* For each iteration in the outer loop, multiply one limb from
8473 * U with one limb from V, and add it to PROD. */
8474 for( i = 1; i < size; i++ ) {
8475 v_limb = vp[i];
8476 if( v_limb <= 1 ) {
8477 cy = 0;
8478 if( v_limb == 1 )
8479 cy = mpihelp_add_n(prodp, prodp, up, size);
8480 }
8481 else
8482 cy = mpihelp_addmul_1(prodp, up, size, v_limb);
8483
8484 prodp[size] = cy;
8485 prodp++;
8486 }
8487
8488 return cy;
8489 }
8490
8491
8492 static void
8493 mul_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_ptr_t vp,
8494 mpi_size_t size, mpi_ptr_t tspace )
8495 {
8496 if( size & 1 ) {
8497 /* The size is odd, and the code below doesn't handle that.
8498 * Multiply the least significant (size - 1) limbs with a recursive
8499 * call, and handle the most significant limb of S1 and S2
8500 * separately.
8501 * A slightly faster way to do this would be to make the Karatsuba
8502 * code below behave as if the size were even, and let it check for
8503 * odd size in the end. I.e., in essence move this code to the end.
8504 * Doing so would save us a recursive call, and potentially make the
8505 * stack grow a lot less.
8506 */
8507 mpi_size_t esize = size - 1; /* even size */
8508 mpi_limb_t cy_limb;
8509
8510 MPN_MUL_N_RECURSE( prodp, up, vp, esize, tspace );
8511 cy_limb = mpihelp_addmul_1( prodp + esize, up, esize, vp[esize] );
8512 prodp[esize + esize] = cy_limb;
8513 cy_limb = mpihelp_addmul_1( prodp + esize, vp, size, up[esize] );
8514 prodp[esize + size] = cy_limb;
8515 }
8516 else {
8517 /* Anatolij Alekseevich Karatsuba's divide-and-conquer algorithm.
8518 *
8519 * Split U in two pieces, U1 and U0, such that
8520 * U = U0 + U1*(B**n),
8521 * and V in V1 and V0, such that
8522 * V = V0 + V1*(B**n).
8523 *
8524 * UV is then computed recursively using the identity
8525 *
8526 * 2n n n n
8527 * UV = (B + B )U V + B (U -U )(V -V ) + (B + 1)U V
8528 * 1 1 1 0 0 1 0 0
8529 *
8530 * Where B = 2**BITS_PER_MP_LIMB.
8531 */
8532 mpi_size_t hsize = size >> 1;
8533 mpi_limb_t cy;
8534 int negflg;
8535
8536 /* Product H. ________________ ________________
8537 * |_____U1 x V1____||____U0 x V0_____|
8538 * Put result in upper part of PROD and pass low part of TSPACE
8539 * as new TSPACE.
8540 */
8541 MPN_MUL_N_RECURSE(prodp + size, up + hsize, vp + hsize, hsize, tspace);
8542
8543 /* Product M. ________________
8544 * |_(U1-U0)(V0-V1)_|
8545 */
8546 if( mpihelp_cmp(up + hsize, up, hsize) >= 0 ) {
8547 mpihelp_sub_n(prodp, up + hsize, up, hsize);
8548 negflg = 0;
8549 }
8550 else {
8551 mpihelp_sub_n(prodp, up, up + hsize, hsize);
8552 negflg = 1;
8553 }
8554 if( mpihelp_cmp(vp + hsize, vp, hsize) >= 0 ) {
8555 mpihelp_sub_n(prodp + hsize, vp + hsize, vp, hsize);
8556 negflg ^= 1;
8557 }
8558 else {
8559 mpihelp_sub_n(prodp + hsize, vp, vp + hsize, hsize);
8560 /* No change of NEGFLG. */
8561 }
8562 /* Read temporary operands from low part of PROD.
8563 * Put result in low part of TSPACE using upper part of TSPACE
8564 * as new TSPACE.
8565 */
8566 MPN_MUL_N_RECURSE(tspace, prodp, prodp + hsize, hsize, tspace + size);
8567
8568 /* Add/copy product H. */
8569 MPN_COPY (prodp + hsize, prodp + size, hsize);
8570 cy = mpihelp_add_n( prodp + size, prodp + size,
8571 prodp + size + hsize, hsize);
8572
8573 /* Add product M (if NEGFLG M is a negative number) */
8574 if(negflg)
8575 cy -= mpihelp_sub_n(prodp + hsize, prodp + hsize, tspace, size);
8576 else
8577 cy += mpihelp_add_n(prodp + hsize, prodp + hsize, tspace, size);
8578
8579 /* Product L. ________________ ________________
8580 * |________________||____U0 x V0_____|
8581 * Read temporary operands from low part of PROD.
8582 * Put result in low part of TSPACE using upper part of TSPACE
8583 * as new TSPACE.
8584 */
8585 MPN_MUL_N_RECURSE(tspace, up, vp, hsize, tspace + size);
8586
8587 /* Add/copy Product L (twice) */
8588
8589 cy += mpihelp_add_n(prodp + hsize, prodp + hsize, tspace, size);
8590 if( cy )
8591 mpihelp_add_1(prodp + hsize + size, prodp + hsize + size, hsize, cy);
8592
8593 MPN_COPY(prodp, tspace, hsize);
8594 cy = mpihelp_add_n(prodp + hsize, prodp + hsize, tspace + hsize, hsize);
8595 if( cy )
8596 mpihelp_add_1(prodp + size, prodp + size, size, 1);
8597 }
8598 }
8599
8600
8601 void
8602 mpih_sqr_n_basecase( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size )
8603 {
8604 mpi_size_t i;
8605 mpi_limb_t cy_limb;
8606 mpi_limb_t v_limb;
8607
8608 /* Multiply by the first limb in V separately, as the result can be
8609 * stored (not added) to PROD. We also avoid a loop for zeroing. */
8610 v_limb = up[0];
8611 if( v_limb <= 1 ) {
8612 if( v_limb == 1 )
8613 MPN_COPY( prodp, up, size );
8614 else
8615 MPN_ZERO(prodp, size);
8616 cy_limb = 0;
8617 }
8618 else
8619 cy_limb = mpihelp_mul_1( prodp, up, size, v_limb );
8620
8621 prodp[size] = cy_limb;
8622 prodp++;
8623
8624 /* For each iteration in the outer loop, multiply one limb from
8625 * U with one limb from V, and add it to PROD. */
8626 for( i=1; i < size; i++) {
8627 v_limb = up[i];
8628 if( v_limb <= 1 ) {
8629 cy_limb = 0;
8630 if( v_limb == 1 )
8631 cy_limb = mpihelp_add_n(prodp, prodp, up, size);
8632 }
8633 else
8634 cy_limb = mpihelp_addmul_1(prodp, up, size, v_limb);
8635
8636 prodp[size] = cy_limb;
8637 prodp++;
8638 }
8639 }
8640
8641
8642 void
8643 mpih_sqr_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size, mpi_ptr_t tspace)
8644 {
8645 if( size & 1 ) {
8646 /* The size is odd, and the code below doesn't handle that.
8647 * Multiply the least significant (size - 1) limbs with a recursive
8648 * call, and handle the most significant limb of S1 and S2
8649 * separately.
8650 * A slightly faster way to do this would be to make the Karatsuba
8651 * code below behave as if the size were even, and let it check for
8652 * odd size in the end. I.e., in essence move this code to the end.
8653 * Doing so would save us a recursive call, and potentially make the
8654 * stack grow a lot less.
8655 */
8656 mpi_size_t esize = size - 1; /* even size */
8657 mpi_limb_t cy_limb;
8658
8659 MPN_SQR_N_RECURSE( prodp, up, esize, tspace );
8660 cy_limb = mpihelp_addmul_1( prodp + esize, up, esize, up[esize] );
8661 prodp[esize + esize] = cy_limb;
8662 cy_limb = mpihelp_addmul_1( prodp + esize, up, size, up[esize] );
8663
8664 prodp[esize + size] = cy_limb;
8665 }
8666 else {
8667 mpi_size_t hsize = size >> 1;
8668 mpi_limb_t cy;
8669
8670 /* Product H. ________________ ________________
8671 * |_____U1 x U1____||____U0 x U0_____|
8672 * Put result in upper part of PROD and pass low part of TSPACE
8673 * as new TSPACE.
8674 */
8675 MPN_SQR_N_RECURSE(prodp + size, up + hsize, hsize, tspace);
8676
8677 /* Product M. ________________
8678 * |_(U1-U0)(U0-U1)_|
8679 */
8680 if( mpihelp_cmp( up + hsize, up, hsize) >= 0 )
8681 mpihelp_sub_n( prodp, up + hsize, up, hsize);
8682 else
8683 mpihelp_sub_n (prodp, up, up + hsize, hsize);
8684
8685 /* Read temporary operands from low part of PROD.
8686 * Put result in low part of TSPACE using upper part of TSPACE
8687 * as new TSPACE. */
8688 MPN_SQR_N_RECURSE(tspace, prodp, hsize, tspace + size);
8689
8690 /* Add/copy product H */
8691 MPN_COPY(prodp + hsize, prodp + size, hsize);
8692 cy = mpihelp_add_n(prodp + size, prodp + size,
8693 prodp + size + hsize, hsize);
8694
8695 /* Add product M (if NEGFLG M is a negative number). */
8696 cy -= mpihelp_sub_n (prodp + hsize, prodp + hsize, tspace, size);
8697
8698 /* Product L. ________________ ________________
8699 * |________________||____U0 x U0_____|
8700 * Read temporary operands from low part of PROD.
8701 * Put result in low part of TSPACE using upper part of TSPACE
8702 * as new TSPACE. */
8703 MPN_SQR_N_RECURSE (tspace, up, hsize, tspace + size);
8704
8705 /* Add/copy Product L (twice). */
8706 cy += mpihelp_add_n (prodp + hsize, prodp + hsize, tspace, size);
8707 if( cy )
8708 mpihelp_add_1(prodp + hsize + size, prodp + hsize + size,
8709 hsize, cy);
8710
8711 MPN_COPY(prodp, tspace, hsize);
8712 cy = mpihelp_add_n (prodp + hsize, prodp + hsize, tspace + hsize, hsize);
8713 if( cy )
8714 mpihelp_add_1 (prodp + size, prodp + size, size, 1);
8715 }
8716 }
8717
8718
8719 /* This should be made into an inline function in gmp.h. */
8720 void
8721 mpihelp_mul_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_ptr_t vp, mpi_size_t size)
8722 {
8723 int secure;
8724
8725 if( up == vp ) {
8726 if( size < KARATSUBA_THRESHOLD )
8727 mpih_sqr_n_basecase( prodp, up, size );
8728 else {
8729 mpi_ptr_t tspace;
8730 secure = m_is_secure( up );
8731 tspace = mpi_alloc_limb_space( 2 * size, secure );
8732 mpih_sqr_n( prodp, up, size, tspace );
8733 mpi_free_limb_space( tspace );
8734 }
8735 }
8736 else {
8737 if( size < KARATSUBA_THRESHOLD )
8738 mul_n_basecase( prodp, up, vp, size );
8739 else {
8740 mpi_ptr_t tspace;
8741 secure = m_is_secure( up ) || m_is_secure( vp );
8742 tspace = mpi_alloc_limb_space( 2 * size, secure );
8743 mul_n (prodp, up, vp, size, tspace);
8744 mpi_free_limb_space( tspace );
8745 }
8746 }
8747 }
8748
8749
8750
8751 void
8752 mpihelp_mul_karatsuba_case( mpi_ptr_t prodp,
8753 mpi_ptr_t up, mpi_size_t usize,
8754 mpi_ptr_t vp, mpi_size_t vsize,
8755 struct karatsuba_ctx *ctx )
8756 {
8757 mpi_limb_t cy;
8758
8759 if( !ctx->tspace || ctx->tspace_size < vsize ) {
8760 if( ctx->tspace )
8761 mpi_free_limb_space( ctx->tspace );
8762 ctx->tspace = mpi_alloc_limb_space( 2 * vsize,
8763 m_is_secure( up ) || m_is_secure( vp ) );
8764 ctx->tspace_size = vsize;
8765 }
8766
8767 MPN_MUL_N_RECURSE( prodp, up, vp, vsize, ctx->tspace );
8768
8769 prodp += vsize;
8770 up += vsize;
8771 usize -= vsize;
8772 if( usize >= vsize ) {
8773 if( !ctx->tp || ctx->tp_size < vsize ) {
8774 if( ctx->tp )
8775 mpi_free_limb_space( ctx->tp );
8776 ctx->tp = mpi_alloc_limb_space( 2 * vsize, m_is_secure( up )
8777 || m_is_secure( vp ) );
8778 ctx->tp_size = vsize;
8779 }
8780
8781 do {
8782 MPN_MUL_N_RECURSE( ctx->tp, up, vp, vsize, ctx->tspace );
8783 cy = mpihelp_add_n( prodp, prodp, ctx->tp, vsize );
8784 mpihelp_add_1( prodp + vsize, ctx->tp + vsize, vsize, cy );
8785 prodp += vsize;
8786 up += vsize;
8787 usize -= vsize;
8788 } while( usize >= vsize );
8789 }
8790
8791 if( usize ) {
8792 if( usize < KARATSUBA_THRESHOLD ) {
8793 mpihelp_mul( ctx->tspace, vp, vsize, up, usize );
8794 }
8795 else {
8796 if( !ctx->next ) {
8797 ctx->next = xmalloc_clear( sizeof *ctx );
8798 }
8799 mpihelp_mul_karatsuba_case( ctx->tspace,
8800 vp, vsize,
8801 up, usize,
8802 ctx->next );
8803 }
8804
8805 cy = mpihelp_add_n( prodp, prodp, ctx->tspace, vsize);
8806 mpihelp_add_1( prodp + vsize, ctx->tspace + vsize, usize, cy );
8807 }
8808 }
8809
8810
8811 void
8812 mpihelp_release_karatsuba_ctx( struct karatsuba_ctx *ctx )
8813 {
8814 struct karatsuba_ctx *ctx2;
8815
8816 if( ctx->tp )
8817 mpi_free_limb_space( ctx->tp );
8818 if( ctx->tspace )
8819 mpi_free_limb_space( ctx->tspace );
8820 for( ctx=ctx->next; ctx; ctx = ctx2 ) {
8821 ctx2 = ctx->next;
8822 if( ctx->tp )
8823 mpi_free_limb_space( ctx->tp );
8824 if( ctx->tspace )
8825 mpi_free_limb_space( ctx->tspace );
8826 xfree( ctx );
8827 }
8828 }
8829
8830 /* Multiply the natural numbers u (pointed to by UP, with USIZE limbs)
8831 * and v (pointed to by VP, with VSIZE limbs), and store the result at
8832 * PRODP. USIZE + VSIZE limbs are always stored, but if the input
8833 * operands are normalized. Return the most significant limb of the
8834 * result.
8835 *
8836 * NOTE: The space pointed to by PRODP is overwritten before finished
8837 * with U and V, so overlap is an error.
8838 *
8839 * Argument constraints:
8840 * 1. USIZE >= VSIZE.
8841 * 2. PRODP != UP and PRODP != VP, i.e. the destination
8842 * must be distinct from the multiplier and the multiplicand.
8843 */
8844
8845 mpi_limb_t
8846 mpihelp_mul( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
8847 mpi_ptr_t vp, mpi_size_t vsize)
8848 {
8849 mpi_ptr_t prod_endp = prodp + usize + vsize - 1;
8850 mpi_limb_t cy;
8851 struct karatsuba_ctx ctx;
8852
8853 if( vsize < KARATSUBA_THRESHOLD ) {
8854 mpi_size_t i;
8855 mpi_limb_t v_limb;
8856
8857 if( !vsize )
8858 return 0;
8859
8860 /* Multiply by the first limb in V separately, as the result can be
8861 * stored (not added) to PROD. We also avoid a loop for zeroing. */
8862 v_limb = vp[0];
8863 if( v_limb <= 1 ) {
8864 if( v_limb == 1 )
8865 MPN_COPY( prodp, up, usize );
8866 else
8867 MPN_ZERO( prodp, usize );
8868 cy = 0;
8869 }
8870 else
8871 cy = mpihelp_mul_1( prodp, up, usize, v_limb );
8872
8873 prodp[usize] = cy;
8874 prodp++;
8875
8876 /* For each iteration in the outer loop, multiply one limb from
8877 * U with one limb from V, and add it to PROD. */
8878 for( i = 1; i < vsize; i++ ) {
8879 v_limb = vp[i];
8880 if( v_limb <= 1 ) {
8881 cy = 0;
8882 if( v_limb == 1 )
8883 cy = mpihelp_add_n(prodp, prodp, up, usize);
8884 }
8885 else
8886 cy = mpihelp_addmul_1(prodp, up, usize, v_limb);
8887
8888 prodp[usize] = cy;
8889 prodp++;
8890 }
8891
8892 return cy;
8893 }
8894
8895 memset( &ctx, 0, sizeof ctx );
8896 mpihelp_mul_karatsuba_case( prodp, up, usize, vp, vsize, &ctx );
8897 mpihelp_release_karatsuba_ctx( &ctx );
8898 return *prod_endp;
8899 }
8900
8901