smg_comms_c_wrappers 1
smg_comms_c_wrappers 2 * Modified by No Such Labs. (C) 2015. See README.
smg_comms_c_wrappers 3 *
smg_comms_c_wrappers 4 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
smg_comms_c_wrappers 5 * SHA256(gnupg-1.4.10.tar.gz):
smg_comms_c_wrappers 6 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
smg_comms_c_wrappers 7 * (C) 1994-2005 Free Software Foundation, Inc.
smg_comms_c_wrappers 8 *
smg_comms_c_wrappers 9 * This program is free software: you can redistribute it and/or modify
smg_comms_c_wrappers 10 * it under the terms of the GNU General Public License as published by
smg_comms_c_wrappers 11 * the Free Software Foundation, either version 3 of the License, or
smg_comms_c_wrappers 12 * (at your option) any later version.
smg_comms_c_wrappers 13 *
smg_comms_c_wrappers 14 * This program is distributed in the hope that it will be useful,
smg_comms_c_wrappers 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
smg_comms_c_wrappers 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
smg_comms_c_wrappers 17 * GNU General Public License for more details.
smg_comms_c_wrappers 18 *
smg_comms_c_wrappers 19 * You should have received a copy of the GNU General Public License
smg_comms_c_wrappers 20 * along with this program. If not, see <http:
smg_comms_c_wrappers 21 */
smg_comms_c_wrappers 22
smg_comms_c_wrappers 23 #include <stdio.h>
smg_comms_c_wrappers 24 #include <stdlib.h>
smg_comms_c_wrappers 25 #include <assert.h>
smg_comms_c_wrappers 26
smg_comms_c_wrappers 27 #include "knobs.h"
smg_comms_c_wrappers 28 #include "mpi-internal.h"
smg_comms_c_wrappers 29 #include "longlong.h"
smg_comms_c_wrappers 30
smg_comms_c_wrappers 31
smg_comms_c_wrappers 32 #ifdef MPI_INTERNAL_NEED_CLZ_TAB
smg_comms_c_wrappers 33 #ifdef __STDC__
smg_comms_c_wrappers 34 const
smg_comms_c_wrappers 35 #endif
smg_comms_c_wrappers 36 unsigned char
smg_comms_c_wrappers 37 __clz_tab[] =
smg_comms_c_wrappers 38 {
smg_comms_c_wrappers 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,
smg_comms_c_wrappers 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,
smg_comms_c_wrappers 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,
smg_comms_c_wrappers 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,
smg_comms_c_wrappers 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,
smg_comms_c_wrappers 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,
smg_comms_c_wrappers 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,
smg_comms_c_wrappers 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,
smg_comms_c_wrappers 47 };
smg_comms_c_wrappers 48 #endif
smg_comms_c_wrappers 49
smg_comms_c_wrappers 50
smg_comms_c_wrappers 51 #define A_LIMB_1 ((mpi_limb_t)1)
smg_comms_c_wrappers 52
smg_comms_c_wrappers 53
smg_comms_c_wrappers 54
smg_comms_c_wrappers 55 * Sometimes we have MSL (most significant limbs) which are 0;
smg_comms_c_wrappers 56 * this is for some reasons not good, so this function removes them.
smg_comms_c_wrappers 57 */
smg_comms_c_wrappers 58 void
smg_comms_c_wrappers 59 mpi_normalize( MPI a )
smg_comms_c_wrappers 60 {
smg_comms_c_wrappers 61 if( mpi_is_opaque (a) )
smg_comms_c_wrappers 62 return;
smg_comms_c_wrappers 63
smg_comms_c_wrappers 64 for( ; a->nlimbs && !a->d[a->nlimbs-1]; a->nlimbs-- )
smg_comms_c_wrappers 65 ;
smg_comms_c_wrappers 66 }
smg_comms_c_wrappers 67
smg_comms_c_wrappers 68
smg_comms_c_wrappers 69
smg_comms_c_wrappers 70
smg_comms_c_wrappers 71 * Return the number of bits in A.
smg_comms_c_wrappers 72 */
smg_comms_c_wrappers 73 unsigned
smg_comms_c_wrappers 74 mpi_get_nbits( MPI a )
smg_comms_c_wrappers 75 {
smg_comms_c_wrappers 76 unsigned n;
smg_comms_c_wrappers 77
smg_comms_c_wrappers 78 mpi_normalize( a );
smg_comms_c_wrappers 79 if( a->nlimbs ) {
smg_comms_c_wrappers 80 mpi_limb_t alimb = a->d[a->nlimbs-1];
smg_comms_c_wrappers 81 if( alimb )
smg_comms_c_wrappers 82 count_leading_zeros( n, alimb );
smg_comms_c_wrappers 83 else
smg_comms_c_wrappers 84 n = BITS_PER_MPI_LIMB;
smg_comms_c_wrappers 85 n = BITS_PER_MPI_LIMB - n + (a->nlimbs-1) * BITS_PER_MPI_LIMB;
smg_comms_c_wrappers 86 }
smg_comms_c_wrappers 87 else
smg_comms_c_wrappers 88 n = 0;
smg_comms_c_wrappers 89 return n;
smg_comms_c_wrappers 90 }
smg_comms_c_wrappers 91
smg_comms_c_wrappers 92
smg_comms_c_wrappers 93
smg_comms_c_wrappers 94 * Test whether bit N is set.
smg_comms_c_wrappers 95 */
smg_comms_c_wrappers 96 int
smg_comms_c_wrappers 97 mpi_test_bit( MPI a, unsigned n )
smg_comms_c_wrappers 98 {
smg_comms_c_wrappers 99 unsigned limbno, bitno;
smg_comms_c_wrappers 100 mpi_limb_t limb;
smg_comms_c_wrappers 101
smg_comms_c_wrappers 102 limbno = n / BITS_PER_MPI_LIMB;
smg_comms_c_wrappers 103 bitno = n % BITS_PER_MPI_LIMB;
smg_comms_c_wrappers 104
smg_comms_c_wrappers 105 if( limbno >= a->nlimbs )
smg_comms_c_wrappers 106 return 0;
smg_comms_c_wrappers 107 limb = a->d[limbno];
smg_comms_c_wrappers 108 return (limb & (A_LIMB_1 << bitno))? 1: 0;
smg_comms_c_wrappers 109 }
smg_comms_c_wrappers 110
smg_comms_c_wrappers 111
smg_comms_c_wrappers 112
smg_comms_c_wrappers 113 * Set bit N of A.
smg_comms_c_wrappers 114 */
smg_comms_c_wrappers 115 void
smg_comms_c_wrappers 116 mpi_set_bit( MPI a, unsigned n )
smg_comms_c_wrappers 117 {
smg_comms_c_wrappers 118 unsigned limbno, bitno;
smg_comms_c_wrappers 119
smg_comms_c_wrappers 120 limbno = n / BITS_PER_MPI_LIMB;
smg_comms_c_wrappers 121 bitno = n % BITS_PER_MPI_LIMB;
smg_comms_c_wrappers 122
smg_comms_c_wrappers 123 if( limbno >= a->nlimbs ) {
smg_comms_c_wrappers 124 if( a->alloced >= limbno )
smg_comms_c_wrappers 125 mpi_resize(a, limbno+1 );
smg_comms_c_wrappers 126 a->nlimbs = limbno+1;
smg_comms_c_wrappers 127 }
smg_comms_c_wrappers 128 a->d[limbno] |= (A_LIMB_1<<bitno);
smg_comms_c_wrappers 129 }
smg_comms_c_wrappers 130
smg_comms_c_wrappers 131
smg_comms_c_wrappers 132 * Set bit N of A. and clear all bits above
smg_comms_c_wrappers 133 */
smg_comms_c_wrappers 134 void
smg_comms_c_wrappers 135 mpi_set_highbit( MPI a, unsigned n )
smg_comms_c_wrappers 136 {
smg_comms_c_wrappers 137 unsigned limbno, bitno;
smg_comms_c_wrappers 138
smg_comms_c_wrappers 139 limbno = n / BITS_PER_MPI_LIMB;
smg_comms_c_wrappers 140 bitno = n % BITS_PER_MPI_LIMB;
smg_comms_c_wrappers 141
smg_comms_c_wrappers 142 if( limbno >= a->nlimbs ) {
smg_comms_c_wrappers 143 if( a->alloced >= limbno )
smg_comms_c_wrappers 144 mpi_resize(a, limbno+1 );
smg_comms_c_wrappers 145 a->nlimbs = limbno+1;
smg_comms_c_wrappers 146 }
smg_comms_c_wrappers 147 a->d[limbno] |= (A_LIMB_1<<bitno);
smg_comms_c_wrappers 148 for( bitno++; bitno < BITS_PER_MPI_LIMB; bitno++ )
smg_comms_c_wrappers 149 a->d[limbno] &= ~(A_LIMB_1 << bitno);
smg_comms_c_wrappers 150 a->nlimbs = limbno+1;
smg_comms_c_wrappers 151 }
smg_comms_c_wrappers 152
smg_comms_c_wrappers 153
smg_comms_c_wrappers 154 * clear bit N of A and all bits above
smg_comms_c_wrappers 155 */
smg_comms_c_wrappers 156 void
smg_comms_c_wrappers 157 mpi_clear_highbit( MPI a, unsigned n )
smg_comms_c_wrappers 158 {
smg_comms_c_wrappers 159 unsigned limbno, bitno;
smg_comms_c_wrappers 160
smg_comms_c_wrappers 161 limbno = n / BITS_PER_MPI_LIMB;
smg_comms_c_wrappers 162 bitno = n % BITS_PER_MPI_LIMB;
smg_comms_c_wrappers 163
smg_comms_c_wrappers 164 if( limbno >= a->nlimbs )
smg_comms_c_wrappers 165 return;
smg_comms_c_wrappers 166
smg_comms_c_wrappers 167 for( ; bitno < BITS_PER_MPI_LIMB; bitno++ )
smg_comms_c_wrappers 168 a->d[limbno] &= ~(A_LIMB_1 << bitno);
smg_comms_c_wrappers 169
smg_comms_c_wrappers 170
smg_comms_c_wrappers 171 a->nlimbs = limbno+1;
smg_comms_c_wrappers 172 for( ; a->nlimbs && !a->d[a->nlimbs-1]; a->nlimbs-- );
smg_comms_c_wrappers 173
smg_comms_c_wrappers 174 }
smg_comms_c_wrappers 175
smg_comms_c_wrappers 176
smg_comms_c_wrappers 177 * Clear bit N of A.
smg_comms_c_wrappers 178 */
smg_comms_c_wrappers 179 void
smg_comms_c_wrappers 180 mpi_clear_bit( MPI a, unsigned n )
smg_comms_c_wrappers 181 {
smg_comms_c_wrappers 182 unsigned limbno, bitno;
smg_comms_c_wrappers 183
smg_comms_c_wrappers 184 limbno = n / BITS_PER_MPI_LIMB;
smg_comms_c_wrappers 185 bitno = n % BITS_PER_MPI_LIMB;
smg_comms_c_wrappers 186
smg_comms_c_wrappers 187 if( limbno >= a->nlimbs )
smg_comms_c_wrappers 188 return;
smg_comms_c_wrappers 189 a->d[limbno] &= ~(A_LIMB_1 << bitno);
smg_comms_c_wrappers 190 }
smg_comms_c_wrappers 191
smg_comms_c_wrappers 192
smg_comms_c_wrappers 193
smg_comms_c_wrappers 194 * Shift A by N bits to the right
smg_comms_c_wrappers 195 * FIXME: should use alloc_limb if X and A are same.
smg_comms_c_wrappers 196 */
smg_comms_c_wrappers 197 void
smg_comms_c_wrappers 198 mpi_rshift( MPI x, MPI a, unsigned n )
smg_comms_c_wrappers 199 {
smg_comms_c_wrappers 200 mpi_ptr_t xp;
smg_comms_c_wrappers 201 mpi_size_t xsize;
smg_comms_c_wrappers 202
smg_comms_c_wrappers 203 xsize = a->nlimbs;
smg_comms_c_wrappers 204 x->sign = a->sign;
smg_comms_c_wrappers 205 RESIZE_IF_NEEDED(x, xsize);
smg_comms_c_wrappers 206 xp = x->d;
smg_comms_c_wrappers 207
smg_comms_c_wrappers 208 if( xsize ) {
smg_comms_c_wrappers 209 mpihelp_rshift( xp, a->d, xsize, n);
smg_comms_c_wrappers 210 MPN_NORMALIZE( xp, xsize);
smg_comms_c_wrappers 211 }
smg_comms_c_wrappers 212 x->nlimbs = xsize;
smg_comms_c_wrappers 213 }
smg_comms_c_wrappers 214
smg_comms_c_wrappers 215
smg_comms_c_wrappers 216
smg_comms_c_wrappers 217 * Shift A by COUNT limbs to the left
smg_comms_c_wrappers 218 * This is used only within the MPI library
smg_comms_c_wrappers 219 */
smg_comms_c_wrappers 220 void
smg_comms_c_wrappers 221 mpi_lshift_limbs( MPI a, unsigned int count )
smg_comms_c_wrappers 222 {
smg_comms_c_wrappers 223 mpi_ptr_t ap = a->d;
smg_comms_c_wrappers 224 int n = a->nlimbs;
smg_comms_c_wrappers 225 int i;
smg_comms_c_wrappers 226
smg_comms_c_wrappers 227 if( !count || !n )
smg_comms_c_wrappers 228 return;
smg_comms_c_wrappers 229
smg_comms_c_wrappers 230 RESIZE_IF_NEEDED( a, n+count );
smg_comms_c_wrappers 231
smg_comms_c_wrappers 232 for( i = n-1; i >= 0; i-- )
smg_comms_c_wrappers 233 ap[i+count] = ap[i];
smg_comms_c_wrappers 234 for(i=0; i < count; i++ )
smg_comms_c_wrappers 235 ap[i] = 0;
smg_comms_c_wrappers 236 a->nlimbs += count;
smg_comms_c_wrappers 237 }
smg_comms_c_wrappers 238
smg_comms_c_wrappers 239
smg_comms_c_wrappers 240
smg_comms_c_wrappers 241 * Shift A by COUNT limbs to the right
smg_comms_c_wrappers 242 * This is used only within the MPI library
smg_comms_c_wrappers 243 */
smg_comms_c_wrappers 244 void
smg_comms_c_wrappers 245 mpi_rshift_limbs( MPI a, unsigned int count )
smg_comms_c_wrappers 246 {
smg_comms_c_wrappers 247 mpi_ptr_t ap = a->d;
smg_comms_c_wrappers 248 mpi_size_t n = a->nlimbs;
smg_comms_c_wrappers 249 unsigned int i;
smg_comms_c_wrappers 250
smg_comms_c_wrappers 251 if( count >= n ) {
smg_comms_c_wrappers 252 a->nlimbs = 0;
smg_comms_c_wrappers 253 return;
smg_comms_c_wrappers 254 }
smg_comms_c_wrappers 255
smg_comms_c_wrappers 256 for( i = 0; i < n - count; i++ )
smg_comms_c_wrappers 257 ap[i] = ap[i+count];
smg_comms_c_wrappers 258 ap[i] = 0;
smg_comms_c_wrappers 259 a->nlimbs -= count;
smg_comms_c_wrappers 260 }
smg_comms_c_wrappers 261
smg_comms_c_wrappers 262