-
+ A48F6DCD95712D600A89EFAE6DC7425B5CADE2869B48AA49D6D6FC5C42D63EC462E2A1FCB08CB1DF5468A9A5AD8C8DD419DC7FB8F5758AD0D93007960B19A153
mpi/mpi-bit.c
(0 . 0)(1 . 254)
6311 /* mpi-bit.c - MPI bit level fucntions
6312 * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
6313 *
6314 * This file is part of GnuPG.
6315 *
6316 * GnuPG is free software; you can redistribute it and/or modify
6317 * it under the terms of the GNU General Public License as published by
6318 * the Free Software Foundation; either version 3 of the License, or
6319 * (at your option) any later version.
6320 *
6321 * GnuPG is distributed in the hope that it will be useful,
6322 * but WITHOUT ANY WARRANTY; without even the implied warranty of
6323 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6324 * GNU General Public License for more details.
6325 *
6326 * You should have received a copy of the GNU General Public License
6327 * along with this program; if not, see <http://www.gnu.org/licenses/>.
6328 */
6329
6330 #include <config.h>
6331 #include <stdio.h>
6332 #include <stdlib.h>
6333 #include <assert.h>
6334 #include "mpi-internal.h"
6335 #include "longlong.h"
6336
6337
6338 #ifdef MPI_INTERNAL_NEED_CLZ_TAB
6339 #ifdef __STDC__
6340 const
6341 #endif
6342 unsigned char
6343 __clz_tab[] =
6344 {
6345 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
6346 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6347 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
6348 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
6349 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
6350 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
6351 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
6352 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
6353 };
6354 #endif
6355
6356
6357 #define A_LIMB_1 ((mpi_limb_t)1)
6358
6359
6360 /****************
6361 * Sometimes we have MSL (most significant limbs) which are 0;
6362 * this is for some reasons not good, so this function removes them.
6363 */
6364 void
6365 mpi_normalize( MPI a )
6366 {
6367 if( mpi_is_opaque (a) )
6368 return;
6369
6370 for( ; a->nlimbs && !a->d[a->nlimbs-1]; a->nlimbs-- )
6371 ;
6372 }
6373
6374
6375
6376 /****************
6377 * Return the number of bits in A.
6378 */
6379 unsigned
6380 mpi_get_nbits( MPI a )
6381 {
6382 unsigned n;
6383
6384 mpi_normalize( a );
6385 if( a->nlimbs ) {
6386 mpi_limb_t alimb = a->d[a->nlimbs-1];
6387 if( alimb )
6388 count_leading_zeros( n, alimb );
6389 else
6390 n = BITS_PER_MPI_LIMB;
6391 n = BITS_PER_MPI_LIMB - n + (a->nlimbs-1) * BITS_PER_MPI_LIMB;
6392 }
6393 else
6394 n = 0;
6395 return n;
6396 }
6397
6398
6399 /****************
6400 * Test whether bit N is set.
6401 */
6402 int
6403 mpi_test_bit( MPI a, unsigned n )
6404 {
6405 unsigned limbno, bitno;
6406 mpi_limb_t limb;
6407
6408 limbno = n / BITS_PER_MPI_LIMB;
6409 bitno = n % BITS_PER_MPI_LIMB;
6410
6411 if( limbno >= a->nlimbs )
6412 return 0; /* too far left: this is a 0 */
6413 limb = a->d[limbno];
6414 return (limb & (A_LIMB_1 << bitno))? 1: 0;
6415 }
6416
6417
6418 /****************
6419 * Set bit N of A.
6420 */
6421 void
6422 mpi_set_bit( MPI a, unsigned n )
6423 {
6424 unsigned limbno, bitno;
6425
6426 limbno = n / BITS_PER_MPI_LIMB;
6427 bitno = n % BITS_PER_MPI_LIMB;
6428
6429 if( limbno >= a->nlimbs ) { /* resize */
6430 if( a->alloced >= limbno )
6431 mpi_resize(a, limbno+1 );
6432 a->nlimbs = limbno+1;
6433 }
6434 a->d[limbno] |= (A_LIMB_1<<bitno);
6435 }
6436
6437 /****************
6438 * Set bit N of A. and clear all bits above
6439 */
6440 void
6441 mpi_set_highbit( MPI a, unsigned n )
6442 {
6443 unsigned limbno, bitno;
6444
6445 limbno = n / BITS_PER_MPI_LIMB;
6446 bitno = n % BITS_PER_MPI_LIMB;
6447
6448 if( limbno >= a->nlimbs ) { /* resize */
6449 if( a->alloced >= limbno )
6450 mpi_resize(a, limbno+1 );
6451 a->nlimbs = limbno+1;
6452 }
6453 a->d[limbno] |= (A_LIMB_1<<bitno);
6454 for( bitno++; bitno < BITS_PER_MPI_LIMB; bitno++ )
6455 a->d[limbno] &= ~(A_LIMB_1 << bitno);
6456 a->nlimbs = limbno+1;
6457 }
6458
6459 /****************
6460 * clear bit N of A and all bits above
6461 */
6462 void
6463 mpi_clear_highbit( MPI a, unsigned n )
6464 {
6465 unsigned limbno, bitno;
6466
6467 limbno = n / BITS_PER_MPI_LIMB;
6468 bitno = n % BITS_PER_MPI_LIMB;
6469
6470 if( limbno >= a->nlimbs )
6471 return; /* not allocated, so need to clear bits :-) */
6472
6473 for( ; bitno < BITS_PER_MPI_LIMB; bitno++ )
6474 a->d[limbno] &= ~(A_LIMB_1 << bitno);
6475 a->nlimbs = limbno+1;
6476 }
6477
6478 /****************
6479 * Clear bit N of A.
6480 */
6481 void
6482 mpi_clear_bit( MPI a, unsigned n )
6483 {
6484 unsigned limbno, bitno;
6485
6486 limbno = n / BITS_PER_MPI_LIMB;
6487 bitno = n % BITS_PER_MPI_LIMB;
6488
6489 if( limbno >= a->nlimbs )
6490 return; /* don't need to clear this bit, it's to far to left */
6491 a->d[limbno] &= ~(A_LIMB_1 << bitno);
6492 }
6493
6494
6495 /****************
6496 * Shift A by N bits to the right
6497 * FIXME: should use alloc_limb if X and A are same.
6498 */
6499 void
6500 mpi_rshift( MPI x, MPI a, unsigned n )
6501 {
6502 mpi_ptr_t xp;
6503 mpi_size_t xsize;
6504
6505 xsize = a->nlimbs;
6506 x->sign = a->sign;
6507 RESIZE_IF_NEEDED(x, xsize);
6508 xp = x->d;
6509
6510 if( xsize ) {
6511 mpihelp_rshift( xp, a->d, xsize, n);
6512 MPN_NORMALIZE( xp, xsize);
6513 }
6514 x->nlimbs = xsize;
6515 }
6516
6517
6518 /****************
6519 * Shift A by COUNT limbs to the left
6520 * This is used only within the MPI library
6521 */
6522 void
6523 mpi_lshift_limbs( MPI a, unsigned int count )
6524 {
6525 mpi_ptr_t ap = a->d;
6526 int n = a->nlimbs;
6527 int i;
6528
6529 if( !count || !n )
6530 return;
6531
6532 RESIZE_IF_NEEDED( a, n+count );
6533
6534 for( i = n-1; i >= 0; i-- )
6535 ap[i+count] = ap[i];
6536 for(i=0; i < count; i++ )
6537 ap[i] = 0;
6538 a->nlimbs += count;
6539 }
6540
6541
6542 /****************
6543 * Shift A by COUNT limbs to the right
6544 * This is used only within the MPI library
6545 */
6546 void
6547 mpi_rshift_limbs( MPI a, unsigned int count )
6548 {
6549 mpi_ptr_t ap = a->d;
6550 mpi_size_t n = a->nlimbs;
6551 unsigned int i;
6552
6553 if( count >= n ) {
6554 a->nlimbs = 0;
6555 return;
6556 }
6557
6558 for( i = 0; i < n - count; i++ )
6559 ap[i] = ap[i+count];
6560 ap[i] = 0;
6561 a->nlimbs -= count;
6562 }
6563
6564