Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* mpihelp-lshift.c  -  MPI helper functions
0003  * Copyright (C) 1994, 1996, 1998, 2001 Free Software Foundation, Inc.
0004  *
0005  * This file is part of GnuPG.
0006  *
0007  * Note: This code is heavily based on the GNU MP Library.
0008  *   Actually it's the same code with only minor changes in the
0009  *   way the data is stored; this is to support the abstraction
0010  *   of an optional secure memory allocation which may be used
0011  *   to avoid revealing of sensitive data due to paging etc.
0012  *   The GNU MP Library itself is published under the LGPL;
0013  *   however I decided to publish this code under the plain GPL.
0014  */
0015 
0016 #include "mpi-internal.h"
0017 
0018 /* Shift U (pointed to by UP and USIZE digits long) CNT bits to the left
0019  * and store the USIZE least significant digits of the result at WP.
0020  * Return the bits shifted out from the most significant digit.
0021  *
0022  * Argument constraints:
0023  * 1. 0 < CNT < BITS_PER_MP_LIMB
0024  * 2. If the result is to be written over the input, WP must be >= UP.
0025  */
0026 
0027 mpi_limb_t
0028 mpihelp_lshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize, unsigned int cnt)
0029 {
0030     mpi_limb_t high_limb, low_limb;
0031     unsigned sh_1, sh_2;
0032     mpi_size_t i;
0033     mpi_limb_t retval;
0034 
0035     sh_1 = cnt;
0036     wp += 1;
0037     sh_2 = BITS_PER_MPI_LIMB - sh_1;
0038     i = usize - 1;
0039     low_limb = up[i];
0040     retval = low_limb >> sh_2;
0041     high_limb = low_limb;
0042     while (--i >= 0) {
0043         low_limb = up[i];
0044         wp[i] = (high_limb << sh_1) | (low_limb >> sh_2);
0045         high_limb = low_limb;
0046     }
0047     wp[i] = high_limb << sh_1;
0048 
0049     return retval;
0050 }