1f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#include <tommath.h>
2f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#ifdef BN_MP_LSHD_C
3f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* LibTomMath, multiple-precision integer library -- Tom St Denis
4f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project *
5f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project * LibTomMath is a library that provides multiple-precision
6f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project * integer arithmetic as well as number theoretic functionality.
7f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project *
8f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project * The library was designed directly after the MPI library by
9f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project * Michael Fromberger but has been written from scratch with
10f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project * additional optimizations in place.
11f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project *
12f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project * The library is free for all purposes without any express
13f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project * guarantee it works.
14f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project *
15f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
16f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project */
17f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
18f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* shift left a certain amount of digits */
19f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Projectint mp_lshd (mp_int * a, int b)
20f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project{
21f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project  int     x, res;
22f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
23f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project  /* if its less than zero return */
24f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project  if (b <= 0) {
25f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project    return MP_OKAY;
26f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project  }
27f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
28f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project  /* grow to fit the new digits */
29f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project  if (a->alloc < a->used + b) {
30f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     if ((res = mp_grow (a, a->used + b)) != MP_OKAY) {
31f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       return res;
32f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     }
33f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project  }
34f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
35f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project  {
36f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project    register mp_digit *top, *bottom;
37f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
38f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project    /* increment the used by the shift amount then copy upwards */
39f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project    a->used += b;
40f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
41f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project    /* top */
42f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project    top = a->dp + a->used - 1;
43f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
44f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project    /* base */
45f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project    bottom = a->dp + a->used - 1 - b;
46f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
47f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project    /* much like mp_rshd this is implemented using a sliding window
48f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     * except the window goes the otherway around.  Copying from
49f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     * the bottom to the top.  see bn_mp_rshd.c for more info.
50f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     */
51f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project    for (x = a->used - 1; x >= b; x--) {
52f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      *top-- = *bottom--;
53f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project    }
54f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
55f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project    /* zero the lower digits */
56f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project    top = a->dp;
57f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project    for (x = 0; x < b; x++) {
58f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      *top++ = 0;
59f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project    }
60f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project  }
61f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project  return MP_OKAY;
62f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project}
63f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#endif
64f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
65f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* $Source: /cvs/libtom/libtommath/bn_mp_lshd.c,v $ */
66f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* $Revision: 1.3 $ */
67f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* $Date: 2006/03/31 14:18:44 $ */
68