bn_mp_rshd.c

Go to the documentation of this file.
00001 #include <tommath.h>
00002 #ifdef BN_MP_RSHD_C
00003 /* LibTomMath, multiple-precision integer library -- Tom St Denis
00004  *
00005  * LibTomMath is a library that provides multiple-precision
00006  * integer arithmetic as well as number theoretic functionality.
00007  *
00008  * The library was designed directly after the MPI library by
00009  * Michael Fromberger but has been written from scratch with
00010  * additional optimizations in place.
00011  *
00012  * The library is free for all purposes without any express
00013  * guarantee it works.
00014  *
00015  * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
00016  */
00017 
00018 /* shift right a certain amount of digits */
00019 void mp_rshd (mp_int * a, int b)
00020 {
00021   int     x;
00022 
00023   /* if b <= 0 then ignore it */
00024   if (b <= 0) {
00025     return;
00026   }
00027 
00028   /* if b > used then simply zero it and return */
00029   if (a->used <= b) {
00030     mp_zero (a);
00031     return;
00032   }
00033 
00034   {
00035     register mp_digit *bottom, *top;
00036 
00037     /* shift the digits down */
00038 
00039     /* bottom */
00040     bottom = a->dp;
00041 
00042     /* top [offset into digits] */
00043     top = a->dp + b;
00044 
00045     /* this is implemented as a sliding window where 
00046      * the window is b-digits long and digits from 
00047      * the top of the window are copied to the bottom
00048      *
00049      * e.g.
00050 
00051      b-2 | b-1 | b0 | b1 | b2 | ... | bb |   ---->
00052                  /\                   |      ---->
00053                   \-------------------/      ---->
00054      */
00055     for (x = 0; x < (a->used - b); x++) {
00056       *bottom++ = *top++;
00057     }
00058 
00059     /* zero the top digits */
00060     for (; x < a->used; x++) {
00061       *bottom++ = 0;
00062     }
00063   }
00064   
00065   /* remove excess digits */
00066   a->used -= b;
00067 }
00068 #endif
00069 
00070 /* $Source: /cvsroot/tcl/libtommath/bn_mp_rshd.c,v $ */
00071 /* $Revision: 1.1.1.3 $ */
00072 /* $Date: 2006/12/01 00:08:11 $ */



Generated on Wed Mar 12 12:18:25 2008 by  doxygen 1.5.1