raw
mpi-genesis~            1 /* mpihelp-lshift.c  -	MPI helper functions
mpi-genesis~ 2 * Copyright (C) 1994, 1996, 1998, 2001 Free Software Foundation, Inc.
mpi-genesis~ 3 *
mpi-genesis~ 4 * This file is part of GnuPG.
mpi-genesis~ 5 *
mpi-genesis~ 6 * GnuPG is free software; you can redistribute it and/or modify
mpi-genesis~ 7 * it under the terms of the GNU General Public License as published by
mpi-genesis~ 8 * the Free Software Foundation; either version 3 of the License, or
mpi-genesis~ 9 * (at your option) any later version.
mpi-genesis~ 10 *
mpi-genesis~ 11 * GnuPG is distributed in the hope that it will be useful,
mpi-genesis~ 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mpi-genesis~ 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
mpi-genesis~ 14 * GNU General Public License for more details.
mpi-genesis~ 15 *
mpi-genesis~ 16 * You should have received a copy of the GNU General Public License
mpi-genesis~ 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
mpi-genesis~ 18 *
mpi-genesis~ 19 * Note: This code is heavily based on the GNU MP Library.
mpi-genesis~ 20 * Actually it's the same code with only minor changes in the
mpi-genesis~ 21 * way the data is stored; this is to support the abstraction
mpi-genesis~ 22 * of an optional secure memory allocation which may be used
mpi-genesis~ 23 * to avoid revealing of sensitive data due to paging etc.
mpi-genesis~ 24 * The GNU MP Library itself is published under the LGPL;
mpi-genesis~ 25 * however I decided to publish this code under the plain GPL.
mpi-genesis~ 26 */
mpi-genesis~ 27
mpi-genesis~ 28 #include <config.h>
mpi-genesis~ 29 #include <stdio.h>
mpi-genesis~ 30 #include <stdlib.h>
mpi-genesis~ 31 #include "mpi-internal.h"
mpi-genesis~ 32
mpi-genesis~ 33 /* Shift U (pointed to by UP and USIZE digits long) CNT bits to the left
mpi-genesis~ 34 * and store the USIZE least significant digits of the result at WP.
mpi-genesis~ 35 * Return the bits shifted out from the most significant digit.
mpi-genesis~ 36 *
mpi-genesis~ 37 * Argument constraints:
mpi-genesis~ 38 * 1. 0 < CNT < BITS_PER_MP_LIMB
mpi-genesis~ 39 * 2. If the result is to be written over the input, WP must be >= UP.
mpi-genesis~ 40 */
mpi-genesis~ 41
mpi-genesis~ 42 mpi_limb_t
mpi-genesis~ 43 mpihelp_lshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
mpi-genesis~ 44 unsigned int cnt)
mpi-genesis~ 45 {
mpi-genesis~ 46 mpi_limb_t high_limb, low_limb;
mpi-genesis~ 47 unsigned sh_1, sh_2;
mpi-genesis~ 48 mpi_size_t i;
mpi-genesis~ 49 mpi_limb_t retval;
mpi-genesis~ 50
mpi-genesis~ 51 sh_1 = cnt;
mpi-genesis~ 52 wp += 1;
mpi-genesis~ 53 sh_2 = BITS_PER_MPI_LIMB - sh_1;
mpi-genesis~ 54 i = usize - 1;
mpi-genesis~ 55 low_limb = up[i];
mpi-genesis~ 56 retval = low_limb >> sh_2;
mpi-genesis~ 57 high_limb = low_limb;
mpi-genesis~ 58 while( --i >= 0 ) {
mpi-genesis~ 59 low_limb = up[i];
mpi-genesis~ 60 wp[i] = (high_limb << sh_1) | (low_limb >> sh_2);
mpi-genesis~ 61 high_limb = low_limb;
mpi-genesis~ 62 }
mpi-genesis~ 63 wp[i] = high_limb << sh_1;
mpi-genesis~ 64
mpi-genesis~ 65 return retval;
mpi-genesis~ 66 }
mpi-genesis~ 67
mpi-genesis~ 68