mpi-genesis 1
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:
mpi-genesis 21 */
mpi-genesis 22
mpi-genesis 23 #include <stdio.h>
mpi-genesis 24 #include <stdlib.h>
mpi-genesis 25 #include <assert.h>
mpi_second_cut 26
mpi_second_cut 27 #include "knobs.h"
mpi-genesis 28 #include "mpi-internal.h"
mpi-genesis 29 #include "longlong.h"
mpi-genesis 30
mpi-genesis 31
mpi-genesis 32 #ifdef MPI_INTERNAL_NEED_CLZ_TAB
mpi-genesis 33 #ifdef __STDC__
mpi-genesis 34 const
mpi-genesis 35 #endif
mpi-genesis 36 unsigned char
mpi-genesis 37 __clz_tab[] =
mpi-genesis 38 {
mpi-genesis 39 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,
mpi-genesis 40 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,
mpi-genesis 41 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,
mpi-genesis 42 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,
mpi-genesis 43 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,
mpi-genesis 44 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,
mpi-genesis 45 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,
mpi-genesis 46 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,
mpi-genesis 47 };
mpi-genesis 48 #endif
mpi-genesis 49
mpi-genesis 50
mpi-genesis 51 #define A_LIMB_1 ((mpi_limb_t)1)
mpi-genesis 52
mpi-genesis 53
mpi-genesis 54
mpi-genesis 55 * Sometimes we have MSL (most significant limbs) which are 0;
mpi-genesis 56 * this is for some reasons not good, so this function removes them.
mpi-genesis 57 */
mpi-genesis 58 void
mpi-genesis 59 mpi_normalize( MPI a )
mpi-genesis 60 {
mpi-genesis 61 if( mpi_is_opaque (a) )
mpi-genesis 62 return;
mpi-genesis 63
mpi-genesis 64 for( ; a->nlimbs && !a->d[a->nlimbs-1]; a->nlimbs-- )
mpi-genesis 65 ;
mpi-genesis 66 }
mpi-genesis 67
mpi-genesis 68
mpi-genesis 69
mpi-genesis 70
mpi-genesis 71 * Return the number of bits in A.
mpi-genesis 72 */
mpi-genesis 73 unsigned
mpi-genesis 74 mpi_get_nbits( MPI a )
mpi-genesis 75 {
mpi-genesis 76 unsigned n;
mpi-genesis 77
mpi-genesis 78 mpi_normalize( a );
mpi-genesis 79 if( a->nlimbs ) {
mpi-genesis 80 mpi_limb_t alimb = a->d[a->nlimbs-1];
mpi-genesis 81 if( alimb )
mpi-genesis 82 count_leading_zeros( n, alimb );
mpi-genesis 83 else
mpi-genesis 84 n = BITS_PER_MPI_LIMB;
mpi-genesis 85 n = BITS_PER_MPI_LIMB - n + (a->nlimbs-1) * BITS_PER_MPI_LIMB;
mpi-genesis 86 }
mpi-genesis 87 else
mpi-genesis 88 n = 0;
mpi-genesis 89 return n;
mpi-genesis 90 }
mpi-genesis 91
mpi-genesis 92
mpi-genesis 93
mpi-genesis 94 * Test whether bit N is set.
mpi-genesis 95 */
mpi-genesis 96 int
mpi-genesis 97 mpi_test_bit( MPI a, unsigned n )
mpi-genesis 98 {
mpi-genesis 99 unsigned limbno, bitno;
mpi-genesis 100 mpi_limb_t limb;
mpi-genesis 101
mpi-genesis 102 limbno = n / BITS_PER_MPI_LIMB;
mpi-genesis 103 bitno = n % BITS_PER_MPI_LIMB;
mpi-genesis 104
mpi-genesis 105 if( limbno >= a->nlimbs )
mpi-genesis 106 return 0;
mpi-genesis 107 limb = a->d[limbno];
mpi-genesis 108 return (limb & (A_LIMB_1 << bitno))? 1: 0;
mpi-genesis 109 }
mpi-genesis 110
mpi-genesis 111
mpi-genesis 112
mpi-genesis 113 * Set bit N of A.
mpi-genesis 114 */
mpi-genesis 115 void
mpi-genesis 116 mpi_set_bit( MPI a, unsigned n )
mpi-genesis 117 {
mpi-genesis 118 unsigned limbno, bitno;
mpi-genesis 119
mpi-genesis 120 limbno = n / BITS_PER_MPI_LIMB;
mpi-genesis 121 bitno = n % BITS_PER_MPI_LIMB;
mpi-genesis 122
mpi-genesis 123 if( limbno >= a->nlimbs ) {
mpi-genesis 124 if( a->alloced >= limbno )
mpi-genesis 125 mpi_resize(a, limbno+1 );
mpi-genesis 126 a->nlimbs = limbno+1;
mpi-genesis 127 }
mpi-genesis 128 a->d[limbno] |= (A_LIMB_1<<bitno);
mpi-genesis 129 }
mpi-genesis 130
mpi-genesis 131
mpi-genesis 132 * Set bit N of A. and clear all bits above
mpi-genesis 133 */
mpi-genesis 134 void
mpi-genesis 135 mpi_set_highbit( MPI a, unsigned n )
mpi-genesis 136 {
mpi-genesis 137 unsigned limbno, bitno;
mpi-genesis 138
mpi-genesis 139 limbno = n / BITS_PER_MPI_LIMB;
mpi-genesis 140 bitno = n % BITS_PER_MPI_LIMB;
mpi-genesis 141
mpi-genesis 142 if( limbno >= a->nlimbs ) {
mpi-genesis 143 if( a->alloced >= limbno )
mpi-genesis 144 mpi_resize(a, limbno+1 );
mpi-genesis 145 a->nlimbs = limbno+1;
mpi-genesis 146 }
mpi-genesis 147 a->d[limbno] |= (A_LIMB_1<<bitno);
mpi-genesis 148 for( bitno++; bitno < BITS_PER_MPI_LIMB; bitno++ )
mpi-genesis 149 a->d[limbno] &= ~(A_LIMB_1 << bitno);
mpi-genesis 150 a->nlimbs = limbno+1;
mpi-genesis 151 }
mpi-genesis 152
mpi-genesis 153
mpi-genesis 154 * clear bit N of A and all bits above
mpi-genesis 155 */
mpi-genesis 156 void
mpi-genesis 157 mpi_clear_highbit( MPI a, unsigned n )
mpi-genesis 158 {
mpi-genesis 159 unsigned limbno, bitno;
mpi-genesis 160
mpi-genesis 161 limbno = n / BITS_PER_MPI_LIMB;
mpi-genesis 162 bitno = n % BITS_PER_MPI_LIMB;
mpi-genesis 163
mpi-genesis 164 if( limbno >= a->nlimbs )
mpi-genesis 165 return;
mpi-genesis 166
mpi-genesis 167 for( ; bitno < BITS_PER_MPI_LIMB; bitno++ )
mpi-genesis 168 a->d[limbno] &= ~(A_LIMB_1 << bitno);
mpi-genesis 169 a->nlimbs = limbno+1;
mpi-genesis 170 }
mpi-genesis 171
mpi-genesis 172
mpi-genesis 173 * Clear bit N of A.
mpi-genesis 174 */
mpi-genesis 175 void
mpi-genesis 176 mpi_clear_bit( MPI a, unsigned n )
mpi-genesis 177 {
mpi-genesis 178 unsigned limbno, bitno;
mpi-genesis 179
mpi-genesis 180 limbno = n / BITS_PER_MPI_LIMB;
mpi-genesis 181 bitno = n % BITS_PER_MPI_LIMB;
mpi-genesis 182
mpi-genesis 183 if( limbno >= a->nlimbs )
mpi-genesis 184 return;
mpi-genesis 185 a->d[limbno] &= ~(A_LIMB_1 << bitno);
mpi-genesis 186 }
mpi-genesis 187
mpi-genesis 188
mpi-genesis 189
mpi-genesis 190 * Shift A by N bits to the right
mpi-genesis 191 * FIXME: should use alloc_limb if X and A are same.
mpi-genesis 192 */
mpi-genesis 193 void
mpi-genesis 194 mpi_rshift( MPI x, MPI a, unsigned n )
mpi-genesis 195 {
mpi-genesis 196 mpi_ptr_t xp;
mpi-genesis 197 mpi_size_t xsize;
mpi-genesis 198
mpi-genesis 199 xsize = a->nlimbs;
mpi-genesis 200 x->sign = a->sign;
mpi-genesis 201 RESIZE_IF_NEEDED(x, xsize);
mpi-genesis 202 xp = x->d;
mpi-genesis 203
mpi-genesis 204 if( xsize ) {
mpi-genesis 205 mpihelp_rshift( xp, a->d, xsize, n);
mpi-genesis 206 MPN_NORMALIZE( xp, xsize);
mpi-genesis 207 }
mpi-genesis 208 x->nlimbs = xsize;
mpi-genesis 209 }
mpi-genesis 210
mpi-genesis 211
mpi-genesis 212
mpi-genesis 213 * Shift A by COUNT limbs to the left
mpi-genesis 214 * This is used only within the MPI library
mpi-genesis 215 */
mpi-genesis 216 void
mpi-genesis 217 mpi_lshift_limbs( MPI a, unsigned int count )
mpi-genesis 218 {
mpi-genesis 219 mpi_ptr_t ap = a->d;
mpi-genesis 220 int n = a->nlimbs;
mpi-genesis 221 int i;
mpi-genesis 222
mpi-genesis 223 if( !count || !n )
mpi-genesis 224 return;
mpi-genesis 225
mpi-genesis 226 RESIZE_IF_NEEDED( a, n+count );
mpi-genesis 227
mpi-genesis 228 for( i = n-1; i >= 0; i-- )
mpi-genesis 229 ap[i+count] = ap[i];
mpi-genesis 230 for(i=0; i < count; i++ )
mpi-genesis 231 ap[i] = 0;
mpi-genesis 232 a->nlimbs += count;
mpi-genesis 233 }
mpi-genesis 234
mpi-genesis 235
mpi-genesis 236
mpi-genesis 237 * Shift A by COUNT limbs to the right
mpi-genesis 238 * This is used only within the MPI library
mpi-genesis 239 */
mpi-genesis 240 void
mpi-genesis 241 mpi_rshift_limbs( MPI a, unsigned int count )
mpi-genesis 242 {
mpi-genesis 243 mpi_ptr_t ap = a->d;
mpi-genesis 244 mpi_size_t n = a->nlimbs;
mpi-genesis 245 unsigned int i;
mpi-genesis 246
mpi-genesis 247 if( count >= n ) {
mpi-genesis 248 a->nlimbs = 0;
mpi-genesis 249 return;
mpi-genesis 250 }
mpi-genesis 251
mpi-genesis 252 for( i = 0; i < n - count; i++ )
mpi-genesis 253 ap[i] = ap[i+count];
mpi-genesis 254 ap[i] = 0;
mpi-genesis 255 a->nlimbs -= count;
mpi-genesis 256 }
mpi-genesis 257
mpi-genesis 258