raw
mpi-genesis             1 /* mpi-scan.c  -  MPI functions
mpi-genesis 2 * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
mpi-genesis 3 *
mpi-genesis 4 * This file is part of GnuPG.
mpi-genesis 5 *
mpi-genesis 6 * GnuPG is free software; you can redistribute it and/or modify
mpi-genesis 7 * it under the terms of the GNU General Public License as published by
mpi-genesis 8 * the Free Software Foundation; either version 3 of the License, or
mpi-genesis 9 * (at your option) any later version.
mpi-genesis 10 *
mpi-genesis 11 * GnuPG is distributed in the hope that it will be useful,
mpi-genesis 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mpi-genesis 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
mpi-genesis 14 * GNU General Public License for more details.
mpi-genesis 15 *
mpi-genesis 16 * You should have received a copy of the GNU General Public License
mpi-genesis 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
mpi-genesis 18 */
mpi-genesis 19
mpi-genesis 20 #include <config.h>
mpi-genesis 21 #include <stdio.h>
mpi-genesis 22 #include <stdlib.h>
mpi-genesis 23 #include "mpi-internal.h"
mpi-genesis 24 #include "longlong.h"
mpi-genesis 25
mpi-genesis 26 /****************
mpi-genesis 27 * Scan through an mpi and return byte for byte. a -1 is returned to indicate
mpi-genesis 28 * the end of the mpi. Scanning is done from the lsb to the msb, returned
mpi-genesis 29 * values are in the range of 0 .. 255.
mpi-genesis 30 *
mpi-genesis 31 * FIXME: This code is VERY ugly!
mpi-genesis 32 */
mpi-genesis 33 #if 0 /* Code is not used */
mpi-genesis 34 int
mpi-genesis 35 mpi_getbyte( MPI a, unsigned idx )
mpi-genesis 36 {
mpi-genesis 37 int i, j;
mpi-genesis 38 unsigned n;
mpi-genesis 39 mpi_ptr_t ap;
mpi-genesis 40 mpi_limb_t limb;
mpi-genesis 41
mpi-genesis 42 ap = a->d;
mpi-genesis 43 for(n=0,i=0; i < a->nlimbs; i++ ) {
mpi-genesis 44 limb = ap[i];
mpi-genesis 45 for( j=0; j < BYTES_PER_MPI_LIMB; j++, n++ )
mpi-genesis 46 if( n == idx )
mpi-genesis 47 return (limb >> j*8) & 0xff;
mpi-genesis 48 }
mpi-genesis 49 return -1;
mpi-genesis 50 }
mpi-genesis 51 #endif /* Code is not used */
mpi-genesis 52
mpi-genesis 53
mpi-genesis 54 /****************
mpi-genesis 55 * Put a value at position IDX into A. idx counts from lsb to msb
mpi-genesis 56 */
mpi-genesis 57 /* FIXME: There is a problem with the long constants which should have
mpi-genesis 58 a LL prefix or better the macros we use at other places. */
mpi-genesis 59 #if 0 /* Code is not used */
mpi-genesis 60 void
mpi-genesis 61 mpi_putbyte( MPI a, unsigned idx, int xc )
mpi-genesis 62 {
mpi-genesis 63
mpi-genesis 64 int i, j;
mpi-genesis 65 unsigned n;
mpi-genesis 66 mpi_ptr_t ap;
mpi-genesis 67 mpi_limb_t limb, c;
mpi-genesis 68
mpi-genesis 69 c = xc & 0xff;
mpi-genesis 70 ap = a->d;
mpi-genesis 71 for(n=0,i=0; i < a->alloced; i++ ) {
mpi-genesis 72 limb = ap[i];
mpi-genesis 73 for( j=0; j < BYTES_PER_MPI_LIMB; j++, n++ )
mpi-genesis 74 if( n == idx ) {
mpi-genesis 75 #if BYTES_PER_MPI_LIMB == 4
mpi-genesis 76 if( j == 0 )
mpi-genesis 77 limb = (limb & 0xffffff00) | c;
mpi-genesis 78 else if( j == 1 )
mpi-genesis 79 limb = (limb & 0xffff00ff) | (c<<8);
mpi-genesis 80 else if( j == 2 )
mpi-genesis 81 limb = (limb & 0xff00ffff) | (c<<16);
mpi-genesis 82 else
mpi-genesis 83 limb = (limb & 0x00ffffff) | (c<<24);
mpi-genesis 84 #elif BYTES_PER_MPI_LIMB == 8
mpi-genesis 85 if( j == 0 )
mpi-genesis 86 limb = (limb & 0xffffffffffffff00) | c;
mpi-genesis 87 else if( j == 1 )
mpi-genesis 88 limb = (limb & 0xffffffffffff00ff) | (c<<8);
mpi-genesis 89 else if( j == 2 )
mpi-genesis 90 limb = (limb & 0xffffffffff00ffff) | (c<<16);
mpi-genesis 91 else if( j == 3 )
mpi-genesis 92 limb = (limb & 0xffffffff00ffffff) | (c<<24);
mpi-genesis 93 else if( j == 4 )
mpi-genesis 94 limb = (limb & 0xffffff00ffffffff) | (c<<32);
mpi-genesis 95 else if( j == 5 )
mpi-genesis 96 limb = (limb & 0xffff00ffffffffff) | (c<<40);
mpi-genesis 97 else if( j == 6 )
mpi-genesis 98 limb = (limb & 0xff00ffffffffffff) | (c<<48);
mpi-genesis 99 else
mpi-genesis 100 limb = (limb & 0x00ffffffffffffff) | (c<<56);
mpi-genesis 101 #else
mpi-genesis 102 #error please enhance this function, its ugly - i know.
mpi-genesis 103 #endif
mpi-genesis 104 if( a->nlimbs <= i )
mpi-genesis 105 a->nlimbs = i+1;
mpi-genesis 106 ap[i] = limb;
mpi-genesis 107 return;
mpi-genesis 108 }
mpi-genesis 109 }
mpi-genesis 110 abort(); /* index out of range */
mpi-genesis 111 }
mpi-genesis 112 #endif /* Code is not used */
mpi-genesis 113
mpi-genesis 114
mpi-genesis 115 /****************
mpi-genesis 116 * Count the number of zerobits at the low end of A
mpi-genesis 117 */
mpi-genesis 118 unsigned int
mpi-genesis 119 mpi_trailing_zeros( MPI a )
mpi-genesis 120 {
mpi-genesis 121 unsigned n, count = 0;
mpi-genesis 122
mpi-genesis 123 for(n=0; n < a->nlimbs; n++ ) {
mpi-genesis 124 if( a->d[n] ) {
mpi-genesis 125 unsigned nn;
mpi-genesis 126 mpi_limb_t alimb = a->d[n];
mpi-genesis 127
mpi-genesis 128 count_trailing_zeros( nn, alimb );
mpi-genesis 129 count += nn;
mpi-genesis 130 break;
mpi-genesis 131 }
mpi-genesis 132 count += BITS_PER_MPI_LIMB;
mpi-genesis 133 }
mpi-genesis 134 return count;
mpi-genesis 135
mpi-genesis 136 }
mpi-genesis 137
mpi-genesis 138