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