1 /* mpi-scan.c - MPI functions
2 * Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
4 * This file is part of Libgcrypt.
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
24 #include "mpi-internal.h"
28 * Scan through an mpi and return byte for byte. a -1 is returned to indicate
29 * the end of the mpi. Scanning is done from the lsb to the msb, returned
30 * values are in the range of 0 .. 255.
32 * FIXME: This code is VERY ugly!
35 _gcry_mpi_getbyte( gcry_mpi_t a, unsigned idx )
43 for(n=0,i=0; i < a->nlimbs; i++ ) {
45 for( j=0; j < BYTES_PER_MPI_LIMB; j++, n++ )
47 return (limb >> j*8) & 0xff;
54 * Put a value at position IDX into A. idx counts from lsb to msb
57 _gcry_mpi_putbyte( gcry_mpi_t a, unsigned idx, int xc )
66 for(n=0,i=0; i < a->alloced; i++ ) {
68 for( j=0; j < BYTES_PER_MPI_LIMB; j++, n++ )
70 #if BYTES_PER_MPI_LIMB == 4
72 limb = (limb & 0xffffff00) | c;
74 limb = (limb & 0xffff00ff) | (c<<8);
76 limb = (limb & 0xff00ffff) | (c<<16);
78 limb = (limb & 0x00ffffff) | (c<<24);
79 #elif BYTES_PER_MPI_LIMB == 8
81 limb = (limb & 0xffffffffffffff00) | c;
83 limb = (limb & 0xffffffffffff00ff) | (c<<8);
85 limb = (limb & 0xffffffffff00ffff) | (c<<16);
87 limb = (limb & 0xffffffff00ffffff) | (c<<24);
89 limb = (limb & 0xffffff00ffffffff) | (c<<32);
91 limb = (limb & 0xffff00ffffffffff) | (c<<40);
93 limb = (limb & 0xff00ffffffffffff) | (c<<48);
95 limb = (limb & 0x00ffffffffffffff) | (c<<56);
97 #error please enhance this function, its ugly - i know.
105 abort(); /* index out of range */
110 * Count the number of zerobits at the low end of A
113 _gcry_mpi_trailing_zeros( gcry_mpi_t a )
115 unsigned n, count = 0;
117 for(n=0; n < a->nlimbs; n++ ) {
120 mpi_limb_t alimb = a->d[n];
122 count_trailing_zeros( nn, alimb );
126 count += BITS_PER_MPI_LIMB;