~ubuntu-branches/ubuntu/raring/libgcrypt11/raring

« back to all changes in this revision

Viewing changes to mpi/mpi-bit.c

  • Committer: Bazaar Package Importer
  • Author(s): Bhavani Shankar
  • Date: 2009-05-16 20:13:32 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090516201332-czkobpu32w318i16
Tags: 1.4.4-2ubuntu1
* Merge from Debian unstable (LP: #364535), remaining changes:
  - Add libgcrypt11-udeb for use by cryptsetup-udeb.
  - Add clean-la.mk, and add a symlink for the .la
  - Install to /lib.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include <config.h>
22
22
#include <stdio.h>
23
23
#include <stdlib.h>
24
 
#include <assert.h>
25
24
#include "mpi-internal.h"
26
25
#include "longlong.h"
27
26
 
188
187
}
189
188
 
190
189
 
 
190
/****************
 
191
 * Shift A by COUNT limbs to the right
 
192
 * This is used only within the MPI library
 
193
 */
 
194
void
 
195
_gcry_mpi_rshift_limbs( gcry_mpi_t a, unsigned int count )
 
196
{
 
197
    mpi_ptr_t ap = a->d;
 
198
    mpi_size_t n = a->nlimbs;
 
199
    unsigned int i;
 
200
 
 
201
    if( count >= n ) {
 
202
        a->nlimbs = 0;
 
203
        return;
 
204
    }
 
205
 
 
206
    for( i = 0; i < n - count; i++ )
 
207
        ap[i] = ap[i+count];
 
208
    ap[i] = 0;
 
209
    a->nlimbs -= count;
 
210
}
 
211
 
 
212
 
191
213
/*
192
214
 * Shift A by N bits to the right.
193
215
 */
277
299
 * This is used only within the MPI library
278
300
 */
279
301
void
280
 
_gcry_mpi_lshift_limbs( gcry_mpi_t a, unsigned int count )
281
 
{
282
 
    mpi_ptr_t ap;
283
 
    int n = a->nlimbs;
284
 
    int i;
285
 
 
286
 
    if( !count || !n )
287
 
        return;
288
 
 
289
 
    RESIZE_IF_NEEDED( a, n+count );
290
 
 
291
 
    ap = a->d;
292
 
    for( i = n-1; i >= 0; i-- )
293
 
        ap[i+count] = ap[i];
294
 
    for(i=0; i < count; i++ )
295
 
        ap[i] = 0;
296
 
    a->nlimbs += count;
297
 
}
298
 
 
299
 
 
300
 
/****************
301
 
 * Shift A by COUNT limbs to the right
302
 
 * This is used only within the MPI library
303
 
 */
304
 
void
305
 
_gcry_mpi_rshift_limbs( gcry_mpi_t a, unsigned int count )
306
 
{
307
 
    mpi_ptr_t ap = a->d;
308
 
    mpi_size_t n = a->nlimbs;
309
 
    unsigned int i;
310
 
 
311
 
    if( count >= n ) {
312
 
        a->nlimbs = 0;
313
 
        return;
314
 
    }
315
 
 
316
 
    for( i = 0; i < n - count; i++ )
317
 
        ap[i] = ap[i+count];
 
302
_gcry_mpi_lshift_limbs (gcry_mpi_t a, unsigned int count)
 
303
{
 
304
  mpi_ptr_t ap;
 
305
  int n = a->nlimbs;
 
306
  int i;
 
307
 
 
308
  if (!count || !n)
 
309
    return;
 
310
 
 
311
  RESIZE_IF_NEEDED (a, n+count);
 
312
 
 
313
  ap = a->d;
 
314
  for (i = n-1; i >= 0; i--)
 
315
    ap[i+count] = ap[i];
 
316
  for (i=0; i < count; i++ )
318
317
    ap[i] = 0;
319
 
    a->nlimbs -= count;
320
 
}
 
318
  a->nlimbs += count;
 
319
}
 
320
 
 
321
 
 
322
/*
 
323
 * Shift A by N bits to the left.
 
324
 */
 
325
void
 
326
gcry_mpi_lshift ( gcry_mpi_t x, gcry_mpi_t a, unsigned int n )
 
327
{
 
328
  unsigned int nlimbs = (n/BITS_PER_MPI_LIMB);
 
329
  unsigned int nbits = (n%BITS_PER_MPI_LIMB);
 
330
 
 
331
  if (x == a && !n)
 
332
    return;  /* In-place shift with an amount of zero.  */
 
333
 
 
334
  if ( x != a )
 
335
    {
 
336
      /* Copy A to X.  */
 
337
      unsigned int alimbs = a->nlimbs;
 
338
      int asign  = a->sign;
 
339
      mpi_ptr_t xp, ap;
 
340
 
 
341
      RESIZE_IF_NEEDED (x, alimbs+nlimbs+1);
 
342
      xp = x->d;
 
343
      ap = a->d;
 
344
      MPN_COPY (xp, ap, alimbs);
 
345
      x->nlimbs = alimbs;
 
346
      x->flags = a->flags;
 
347
      x->sign = asign;
 
348
    }
 
349
 
 
350
  if (nlimbs && !nbits)
 
351
    {
 
352
      /* Shift a full number of limbs.  */
 
353
      _gcry_mpi_lshift_limbs (x, nlimbs);
 
354
    }
 
355
  else if (n)
 
356
    {
 
357
      /* We use a very dump approach: Shift left by the number of
 
358
         limbs plus one and than fix it up by an rshift.  */
 
359
      _gcry_mpi_lshift_limbs (x, nlimbs+1);
 
360
      gcry_mpi_rshift (x, x, BITS_PER_MPI_LIMB - nbits);
 
361
    }
 
362
 
 
363
  MPN_NORMALIZE (x->d, x->nlimbs);
 
364
}
 
365