smg_comms_c_wrappers 1
smg_comms_c_wrappers 2 * Modified by No Such Labs. (C) 2015. See README.
smg_comms_c_wrappers 3 * Modified by S.MG, 2018. Added mpi_get_alloced(MPI a)
smg_comms_c_wrappers 4 *
smg_comms_c_wrappers 5 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
smg_comms_c_wrappers 6 * SHA256(gnupg-1.4.10.tar.gz):
smg_comms_c_wrappers 7 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
smg_comms_c_wrappers 8 * (C) 1994-2005 Free Software Foundation, Inc.
smg_comms_c_wrappers 9 *
smg_comms_c_wrappers 10 * This program is free software: you can redistribute it and/or modify
smg_comms_c_wrappers 11 * it under the terms of the GNU General Public License as published by
smg_comms_c_wrappers 12 * the Free Software Foundation, either version 3 of the License, or
smg_comms_c_wrappers 13 * (at your option) any later version.
smg_comms_c_wrappers 14 *
smg_comms_c_wrappers 15 * This program is distributed in the hope that it will be useful,
smg_comms_c_wrappers 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
smg_comms_c_wrappers 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
smg_comms_c_wrappers 18 * GNU General Public License for more details.
smg_comms_c_wrappers 19 *
smg_comms_c_wrappers 20 * You should have received a copy of the GNU General Public License
smg_comms_c_wrappers 21 * along with this program. If not, see <http:
smg_comms_c_wrappers 22 */
smg_comms_c_wrappers 23
smg_comms_c_wrappers 24 #include <stdio.h>
smg_comms_c_wrappers 25 #include <stdlib.h>
smg_comms_c_wrappers 26 #include <string.h>
smg_comms_c_wrappers 27 #include <assert.h>
smg_comms_c_wrappers 28
smg_comms_c_wrappers 29 #include "knobs.h"
smg_comms_c_wrappers 30 #include "mpi.h"
smg_comms_c_wrappers 31 #include "mpi-internal.h"
smg_comms_c_wrappers 32 #include "memory.h"
smg_comms_c_wrappers 33 #include "util.h"
smg_comms_c_wrappers 34
smg_comms_c_wrappers 35
smg_comms_c_wrappers 36 #ifdef M_DEBUG
smg_comms_c_wrappers 37 #undef mpi_alloc
smg_comms_c_wrappers 38 #undef mpi_alloc_secure
smg_comms_c_wrappers 39 #undef mpi_free
smg_comms_c_wrappers 40 #endif
smg_comms_c_wrappers 41
smg_comms_c_wrappers 42
smg_comms_c_wrappers 43 * Note: It was a bad idea to use the number of limbs to allocate
smg_comms_c_wrappers 44 * because on a alpha the limbs are large but we normally need
smg_comms_c_wrappers 45 * integers of n bits - So we should chnage this to bits (or bytes).
smg_comms_c_wrappers 46 *
smg_comms_c_wrappers 47 * But mpi_alloc is used in a lot of places :-)
smg_comms_c_wrappers 48 */
smg_comms_c_wrappers 49 MPI
smg_comms_c_wrappers 50 #ifdef M_DEBUG
smg_comms_c_wrappers 51 mpi_debug_alloc( unsigned nlimbs, const char *info )
smg_comms_c_wrappers 52 #else
smg_comms_c_wrappers 53 mpi_alloc( unsigned nlimbs )
smg_comms_c_wrappers 54 #endif
smg_comms_c_wrappers 55 {
smg_comms_c_wrappers 56 MPI a;
smg_comms_c_wrappers 57
smg_comms_c_wrappers 58 if( DBG_MEMORY )
smg_comms_c_wrappers 59 log_debug("mpi_alloc(%u)\n", nlimbs*BITS_PER_MPI_LIMB );
smg_comms_c_wrappers 60 #ifdef M_DEBUG
smg_comms_c_wrappers 61 a = m_debug_alloc( sizeof *a, info );
smg_comms_c_wrappers 62 a->d = nlimbs? mpi_debug_alloc_limb_space( nlimbs, 0, info ) : NULL;
smg_comms_c_wrappers 63 #else
smg_comms_c_wrappers 64 a = xmalloc( sizeof *a );
smg_comms_c_wrappers 65 a->d = nlimbs? mpi_alloc_limb_space( nlimbs, 0 ) : NULL;
smg_comms_c_wrappers 66 #endif
smg_comms_c_wrappers 67 a->alloced = nlimbs;
smg_comms_c_wrappers 68 a->nlimbs = 0;
smg_comms_c_wrappers 69 a->sign = 0;
smg_comms_c_wrappers 70 a->flags = 0;
smg_comms_c_wrappers 71 a->nbits = 0;
smg_comms_c_wrappers 72 return a;
smg_comms_c_wrappers 73 }
smg_comms_c_wrappers 74
smg_comms_c_wrappers 75 void
smg_comms_c_wrappers 76 mpi_m_check( MPI a )
smg_comms_c_wrappers 77 {
smg_comms_c_wrappers 78 m_check(a);
smg_comms_c_wrappers 79 m_check(a->d);
smg_comms_c_wrappers 80 }
smg_comms_c_wrappers 81
smg_comms_c_wrappers 82 MPI
smg_comms_c_wrappers 83 #ifdef M_DEBUG
smg_comms_c_wrappers 84 mpi_debug_alloc_secure( unsigned nlimbs, const char *info )
smg_comms_c_wrappers 85 #else
smg_comms_c_wrappers 86 mpi_alloc_secure( unsigned nlimbs )
smg_comms_c_wrappers 87 #endif
smg_comms_c_wrappers 88 {
smg_comms_c_wrappers 89 MPI a;
smg_comms_c_wrappers 90
smg_comms_c_wrappers 91 if( DBG_MEMORY )
smg_comms_c_wrappers 92 log_debug("mpi_alloc_secure(%u)\n", nlimbs*BITS_PER_MPI_LIMB );
smg_comms_c_wrappers 93 #ifdef M_DEBUG
smg_comms_c_wrappers 94 a = m_debug_alloc( sizeof *a, info );
smg_comms_c_wrappers 95 a->d = nlimbs? mpi_debug_alloc_limb_space( nlimbs, 1, info ) : NULL;
smg_comms_c_wrappers 96 #else
smg_comms_c_wrappers 97 a = xmalloc( sizeof *a );
smg_comms_c_wrappers 98 a->d = nlimbs? mpi_alloc_limb_space( nlimbs, 1 ) : NULL;
smg_comms_c_wrappers 99 #endif
smg_comms_c_wrappers 100 a->alloced = nlimbs;
smg_comms_c_wrappers 101 a->flags = 1;
smg_comms_c_wrappers 102 a->nlimbs = 0;
smg_comms_c_wrappers 103 a->sign = 0;
smg_comms_c_wrappers 104 a->nbits = 0;
smg_comms_c_wrappers 105 return a;
smg_comms_c_wrappers 106 }
smg_comms_c_wrappers 107
smg_comms_c_wrappers 108
smg_comms_c_wrappers 109 #if 0
smg_comms_c_wrappers 110 static void *unused_limbs_5;
smg_comms_c_wrappers 111 static void *unused_limbs_32;
smg_comms_c_wrappers 112 static void *unused_limbs_64;
smg_comms_c_wrappers 113 #endif
smg_comms_c_wrappers 114
smg_comms_c_wrappers 115 mpi_ptr_t
smg_comms_c_wrappers 116 #ifdef M_DEBUG
smg_comms_c_wrappers 117 mpi_debug_alloc_limb_space( unsigned nlimbs, int secure, const char *info )
smg_comms_c_wrappers 118 #else
smg_comms_c_wrappers 119 mpi_alloc_limb_space( unsigned nlimbs, int secure )
smg_comms_c_wrappers 120 #endif
smg_comms_c_wrappers 121 {
smg_comms_c_wrappers 122 size_t len = nlimbs * sizeof(mpi_limb_t);
smg_comms_c_wrappers 123 mpi_ptr_t p;
smg_comms_c_wrappers 124
smg_comms_c_wrappers 125 if( DBG_MEMORY )
smg_comms_c_wrappers 126 log_debug("mpi_alloc_limb_space(%u)\n", (unsigned)len*8 );
smg_comms_c_wrappers 127 #if 0
smg_comms_c_wrappers 128 if( !secure ) {
smg_comms_c_wrappers 129 if( nlimbs == 5 && unused_limbs_5 ) {
smg_comms_c_wrappers 130 p = unused_limbs_5;
smg_comms_c_wrappers 131 unused_limbs_5 = *p;
smg_comms_c_wrappers 132 return p;
smg_comms_c_wrappers 133 }
smg_comms_c_wrappers 134 else if( nlimbs == 32 && unused_limbs_32 ) {
smg_comms_c_wrappers 135 p = unused_limbs_32;
smg_comms_c_wrappers 136 unused_limbs_32 = *p;
smg_comms_c_wrappers 137 return p;
smg_comms_c_wrappers 138 }
smg_comms_c_wrappers 139 else if( nlimbs == 64 && unused_limbs_64 ) {
smg_comms_c_wrappers 140 p = unused_limbs_64;
smg_comms_c_wrappers 141 unused_limbs_64 = *p;
smg_comms_c_wrappers 142 return p;
smg_comms_c_wrappers 143 }
smg_comms_c_wrappers 144 }
smg_comms_c_wrappers 145 #endif
smg_comms_c_wrappers 146
smg_comms_c_wrappers 147 #ifdef M_DEBUG
smg_comms_c_wrappers 148 p = secure? m_debug_alloc_secure(len, info):m_debug_alloc( len, info );
smg_comms_c_wrappers 149 #else
smg_comms_c_wrappers 150 p = secure? xmalloc_secure( len ):xmalloc( len );
smg_comms_c_wrappers 151 #endif
smg_comms_c_wrappers 152
smg_comms_c_wrappers 153 return p;
smg_comms_c_wrappers 154 }
smg_comms_c_wrappers 155
smg_comms_c_wrappers 156 void
smg_comms_c_wrappers 157 #ifdef M_DEBUG
smg_comms_c_wrappers 158 mpi_debug_free_limb_space( mpi_ptr_t a, const char *info )
smg_comms_c_wrappers 159 #else
smg_comms_c_wrappers 160 mpi_free_limb_space( mpi_ptr_t a )
smg_comms_c_wrappers 161 #endif
smg_comms_c_wrappers 162 {
smg_comms_c_wrappers 163 if( !a )
smg_comms_c_wrappers 164 return;
smg_comms_c_wrappers 165 if( DBG_MEMORY )
smg_comms_c_wrappers 166 log_debug("mpi_free_limb_space of size %lu\n", (ulong)m_size(a)*8 );
smg_comms_c_wrappers 167
smg_comms_c_wrappers 168 #if 0
smg_comms_c_wrappers 169 if( !m_is_secure(a) ) {
smg_comms_c_wrappers 170 size_t nlimbs = m_size(a) / 4 ;
smg_comms_c_wrappers 171 void *p = a;
smg_comms_c_wrappers 172
smg_comms_c_wrappers 173 if( nlimbs == 5 ) {
smg_comms_c_wrappers 174 *a = unused_limbs_5;
smg_comms_c_wrappers 175 unused_limbs_5 = a;
smg_comms_c_wrappers 176 return;
smg_comms_c_wrappers 177 }
smg_comms_c_wrappers 178 else if( nlimbs == 32 ) {
smg_comms_c_wrappers 179 *a = unused_limbs_32;
smg_comms_c_wrappers 180 unused_limbs_32 = a;
smg_comms_c_wrappers 181 return;
smg_comms_c_wrappers 182 }
smg_comms_c_wrappers 183 else if( nlimbs == 64 ) {
smg_comms_c_wrappers 184 *a = unused_limbs_64;
smg_comms_c_wrappers 185 unused_limbs_64 = a;
smg_comms_c_wrappers 186 return;
smg_comms_c_wrappers 187 }
smg_comms_c_wrappers 188 }
smg_comms_c_wrappers 189 #endif
smg_comms_c_wrappers 190
smg_comms_c_wrappers 191 xfree(a);
smg_comms_c_wrappers 192 }
smg_comms_c_wrappers 193
smg_comms_c_wrappers 194
smg_comms_c_wrappers 195 void
smg_comms_c_wrappers 196 mpi_assign_limb_space( MPI a, mpi_ptr_t ap, unsigned nlimbs )
smg_comms_c_wrappers 197 {
smg_comms_c_wrappers 198 mpi_free_limb_space(a->d);
smg_comms_c_wrappers 199 a->d = ap;
smg_comms_c_wrappers 200 a->alloced = nlimbs;
smg_comms_c_wrappers 201 }
smg_comms_c_wrappers 202
smg_comms_c_wrappers 203
smg_comms_c_wrappers 204
smg_comms_c_wrappers 205
smg_comms_c_wrappers 206 * Resize the array of A to NLIMBS. the additional space is cleared
smg_comms_c_wrappers 207 * (set to 0) [done by xrealloc()]
smg_comms_c_wrappers 208 */
smg_comms_c_wrappers 209 void
smg_comms_c_wrappers 210 #ifdef M_DEBUG
smg_comms_c_wrappers 211 mpi_debug_resize( MPI a, unsigned nlimbs, const char *info )
smg_comms_c_wrappers 212 #else
smg_comms_c_wrappers 213 mpi_resize( MPI a, unsigned nlimbs )
smg_comms_c_wrappers 214 #endif
smg_comms_c_wrappers 215 {
smg_comms_c_wrappers 216 if( nlimbs <= a->alloced )
smg_comms_c_wrappers 217 return;
smg_comms_c_wrappers 218
smg_comms_c_wrappers 219 * take care of it. Maybe we should drop a->secure completely
smg_comms_c_wrappers 220 * and rely on a mpi_is_secure function, which would be
smg_comms_c_wrappers 221 * a wrapper around m_is_secure
smg_comms_c_wrappers 222 */
smg_comms_c_wrappers 223 #ifdef M_DEBUG
smg_comms_c_wrappers 224 if( a->d )
smg_comms_c_wrappers 225 a->d = m_debug_realloc(a->d, nlimbs * sizeof(mpi_limb_t), info );
smg_comms_c_wrappers 226 else
smg_comms_c_wrappers 227 a->d = m_debug_alloc_clear( nlimbs * sizeof(mpi_limb_t), info );
smg_comms_c_wrappers 228 #else
smg_comms_c_wrappers 229 if( a->d )
smg_comms_c_wrappers 230 a->d = xrealloc(a->d, nlimbs * sizeof(mpi_limb_t) );
smg_comms_c_wrappers 231 else
smg_comms_c_wrappers 232 a->d = xmalloc_clear( nlimbs * sizeof(mpi_limb_t) );
smg_comms_c_wrappers 233 #endif
smg_comms_c_wrappers 234 a->alloced = nlimbs;
smg_comms_c_wrappers 235 }
smg_comms_c_wrappers 236
smg_comms_c_wrappers 237 void
smg_comms_c_wrappers 238 mpi_clear( MPI a )
smg_comms_c_wrappers 239 {
smg_comms_c_wrappers 240 a->nlimbs = 0;
smg_comms_c_wrappers 241 a->nbits = 0;
smg_comms_c_wrappers 242 a->flags = 0;
smg_comms_c_wrappers 243 }
smg_comms_c_wrappers 244
smg_comms_c_wrappers 245
smg_comms_c_wrappers 246 void
smg_comms_c_wrappers 247 #ifdef M_DEBUG
smg_comms_c_wrappers 248 mpi_debug_free( MPI a, const char *info )
smg_comms_c_wrappers 249 #else
smg_comms_c_wrappers 250 mpi_free( MPI a )
smg_comms_c_wrappers 251 #endif
smg_comms_c_wrappers 252 {
smg_comms_c_wrappers 253 if( !a )
smg_comms_c_wrappers 254 return;
smg_comms_c_wrappers 255 if( DBG_MEMORY )
smg_comms_c_wrappers 256 log_debug("mpi_free\n" );
smg_comms_c_wrappers 257 if( a->flags & 4 )
smg_comms_c_wrappers 258 xfree( a->d );
smg_comms_c_wrappers 259 else {
smg_comms_c_wrappers 260 #ifdef M_DEBUG
smg_comms_c_wrappers 261 mpi_debug_free_limb_space(a->d, info);
smg_comms_c_wrappers 262 #else
smg_comms_c_wrappers 263 mpi_free_limb_space(a->d);
smg_comms_c_wrappers 264 #endif
smg_comms_c_wrappers 265 }
smg_comms_c_wrappers 266 if( a->flags & ~7 )
smg_comms_c_wrappers 267 log_bug("invalid flag value in mpi\n");
smg_comms_c_wrappers 268 xfree(a);
smg_comms_c_wrappers 269 }
smg_comms_c_wrappers 270
smg_comms_c_wrappers 271
smg_comms_c_wrappers 272 void
smg_comms_c_wrappers 273 mpi_set_secure( MPI a )
smg_comms_c_wrappers 274 {
smg_comms_c_wrappers 275 mpi_ptr_t ap, bp;
smg_comms_c_wrappers 276
smg_comms_c_wrappers 277 if( (a->flags & 1) )
smg_comms_c_wrappers 278 return;
smg_comms_c_wrappers 279 a->flags |= 1;
smg_comms_c_wrappers 280 ap = a->d;
smg_comms_c_wrappers 281 if( !a->nlimbs ) {
smg_comms_c_wrappers 282 assert(!ap);
smg_comms_c_wrappers 283 return;
smg_comms_c_wrappers 284 }
smg_comms_c_wrappers 285 #ifdef M_DEBUG
smg_comms_c_wrappers 286 bp = mpi_debug_alloc_limb_space( a->nlimbs, 1, "set_secure" );
smg_comms_c_wrappers 287 #else
smg_comms_c_wrappers 288 bp = mpi_alloc_limb_space( a->nlimbs, 1 );
smg_comms_c_wrappers 289 #endif
smg_comms_c_wrappers 290 MPN_COPY( bp, ap, a->nlimbs );
smg_comms_c_wrappers 291 a->d = bp;
smg_comms_c_wrappers 292 #ifdef M_DEBUG
smg_comms_c_wrappers 293 mpi_debug_free_limb_space(ap, "set_secure");
smg_comms_c_wrappers 294 #else
smg_comms_c_wrappers 295 mpi_free_limb_space(ap);
smg_comms_c_wrappers 296 #endif
smg_comms_c_wrappers 297 }
smg_comms_c_wrappers 298
smg_comms_c_wrappers 299
smg_comms_c_wrappers 300 MPI
smg_comms_c_wrappers 301 mpi_set_opaque( MPI a, void *p, unsigned int len )
smg_comms_c_wrappers 302 {
smg_comms_c_wrappers 303 if( !a ) {
smg_comms_c_wrappers 304 #ifdef M_DEBUG
smg_comms_c_wrappers 305 a = mpi_debug_alloc(0,"alloc_opaque");
smg_comms_c_wrappers 306 #else
smg_comms_c_wrappers 307 a = mpi_alloc(0);
smg_comms_c_wrappers 308 #endif
smg_comms_c_wrappers 309 }
smg_comms_c_wrappers 310
smg_comms_c_wrappers 311 if( a->flags & 4 )
smg_comms_c_wrappers 312 xfree( a->d );
smg_comms_c_wrappers 313 else {
smg_comms_c_wrappers 314 #ifdef M_DEBUG
smg_comms_c_wrappers 315 mpi_debug_free_limb_space(a->d, "alloc_opaque");
smg_comms_c_wrappers 316 #else
smg_comms_c_wrappers 317 mpi_free_limb_space(a->d);
smg_comms_c_wrappers 318 #endif
smg_comms_c_wrappers 319 }
smg_comms_c_wrappers 320
smg_comms_c_wrappers 321 a->d = p;
smg_comms_c_wrappers 322 a->alloced = 0;
smg_comms_c_wrappers 323 a->nlimbs = 0;
smg_comms_c_wrappers 324 a->nbits = len;
smg_comms_c_wrappers 325 a->flags = 4;
smg_comms_c_wrappers 326 return a;
smg_comms_c_wrappers 327 }
smg_comms_c_wrappers 328
smg_comms_c_wrappers 329
smg_comms_c_wrappers 330 void *
smg_comms_c_wrappers 331 mpi_get_opaque( MPI a, unsigned int *len )
smg_comms_c_wrappers 332 {
smg_comms_c_wrappers 333 if( !(a->flags & 4) )
smg_comms_c_wrappers 334 log_bug("mpi_get_opaque on normal mpi\n");
smg_comms_c_wrappers 335 if( len )
smg_comms_c_wrappers 336 *len = a->nbits;
smg_comms_c_wrappers 337 return a->d;
smg_comms_c_wrappers 338 }
smg_comms_c_wrappers 339
smg_comms_c_wrappers 340
smg_comms_c_wrappers 341
smg_comms_c_wrappers 342 * Note: This copy function should not interpret the MPI
smg_comms_c_wrappers 343 * but copy it transparently.
smg_comms_c_wrappers 344 */
smg_comms_c_wrappers 345 MPI
smg_comms_c_wrappers 346 #ifdef M_DEBUG
smg_comms_c_wrappers 347 mpi_debug_copy( MPI a, const char *info )
smg_comms_c_wrappers 348 #else
smg_comms_c_wrappers 349 mpi_copy( MPI a )
smg_comms_c_wrappers 350 #endif
smg_comms_c_wrappers 351 {
smg_comms_c_wrappers 352 int i;
smg_comms_c_wrappers 353 MPI b;
smg_comms_c_wrappers 354
smg_comms_c_wrappers 355 if( a && (a->flags & 4) ) {
smg_comms_c_wrappers 356 void *p = m_is_secure(a->d)? xmalloc_secure( a->nbits )
smg_comms_c_wrappers 357 : xmalloc( a->nbits );
smg_comms_c_wrappers 358 memcpy( p, a->d, a->nbits );
smg_comms_c_wrappers 359 b = mpi_set_opaque( NULL, p, a->nbits );
smg_comms_c_wrappers 360 }
smg_comms_c_wrappers 361 else if( a ) {
smg_comms_c_wrappers 362 #ifdef M_DEBUG
smg_comms_c_wrappers 363 b = mpi_is_secure(a)? mpi_debug_alloc_secure( a->nlimbs, info )
smg_comms_c_wrappers 364 : mpi_debug_alloc( a->nlimbs, info );
smg_comms_c_wrappers 365 #else
smg_comms_c_wrappers 366 b = mpi_is_secure(a)? mpi_alloc_secure( a->nlimbs )
smg_comms_c_wrappers 367 : mpi_alloc( a->nlimbs );
smg_comms_c_wrappers 368 #endif
smg_comms_c_wrappers 369 b->nlimbs = a->nlimbs;
smg_comms_c_wrappers 370 b->sign = a->sign;
smg_comms_c_wrappers 371 b->flags = a->flags;
smg_comms_c_wrappers 372 b->nbits = a->nbits;
smg_comms_c_wrappers 373 for(i=0; i < b->nlimbs; i++ )
smg_comms_c_wrappers 374 b->d[i] = a->d[i];
smg_comms_c_wrappers 375 }
smg_comms_c_wrappers 376 else
smg_comms_c_wrappers 377 b = NULL;
smg_comms_c_wrappers 378 return b;
smg_comms_c_wrappers 379 }
smg_comms_c_wrappers 380
smg_comms_c_wrappers 381
smg_comms_c_wrappers 382
smg_comms_c_wrappers 383 * This function allocates an MPI which is optimized to hold
smg_comms_c_wrappers 384 * a value as large as the one given in the argument and allocates it
smg_comms_c_wrappers 385 * with the same flags as A.
smg_comms_c_wrappers 386 */
smg_comms_c_wrappers 387 MPI
smg_comms_c_wrappers 388 #ifdef M_DEBUG
smg_comms_c_wrappers 389 mpi_debug_alloc_like( MPI a, const char *info )
smg_comms_c_wrappers 390 #else
smg_comms_c_wrappers 391 mpi_alloc_like( MPI a )
smg_comms_c_wrappers 392 #endif
smg_comms_c_wrappers 393 {
smg_comms_c_wrappers 394 MPI b;
smg_comms_c_wrappers 395
smg_comms_c_wrappers 396 if( a && (a->flags & 4) ) {
smg_comms_c_wrappers 397 void *p = m_is_secure(a->d)? xmalloc_secure( a->nbits )
smg_comms_c_wrappers 398 : xmalloc( a->nbits );
smg_comms_c_wrappers 399 memcpy( p, a->d, a->nbits );
smg_comms_c_wrappers 400 b = mpi_set_opaque( NULL, p, a->nbits );
smg_comms_c_wrappers 401 }
smg_comms_c_wrappers 402 else if( a ) {
smg_comms_c_wrappers 403 #ifdef M_DEBUG
smg_comms_c_wrappers 404 b = mpi_is_secure(a)? mpi_debug_alloc_secure( a->nlimbs, info )
smg_comms_c_wrappers 405 : mpi_debug_alloc( a->nlimbs, info );
smg_comms_c_wrappers 406 #else
smg_comms_c_wrappers 407 b = mpi_is_secure(a)? mpi_alloc_secure( a->nlimbs )
smg_comms_c_wrappers 408 : mpi_alloc( a->nlimbs );
smg_comms_c_wrappers 409 #endif
smg_comms_c_wrappers 410 b->nlimbs = 0;
smg_comms_c_wrappers 411 b->sign = 0;
smg_comms_c_wrappers 412 b->flags = a->flags;
smg_comms_c_wrappers 413 b->nbits = 0;
smg_comms_c_wrappers 414 }
smg_comms_c_wrappers 415 else
smg_comms_c_wrappers 416 b = NULL;
smg_comms_c_wrappers 417 return b;
smg_comms_c_wrappers 418 }
smg_comms_c_wrappers 419
smg_comms_c_wrappers 420
smg_comms_c_wrappers 421 void
smg_comms_c_wrappers 422 mpi_set( MPI w, MPI u)
smg_comms_c_wrappers 423 {
smg_comms_c_wrappers 424 mpi_ptr_t wp, up;
smg_comms_c_wrappers 425 mpi_size_t usize = u->nlimbs;
smg_comms_c_wrappers 426 int usign = u->sign;
smg_comms_c_wrappers 427
smg_comms_c_wrappers 428 RESIZE_IF_NEEDED(w, usize);
smg_comms_c_wrappers 429 wp = w->d;
smg_comms_c_wrappers 430 up = u->d;
smg_comms_c_wrappers 431 MPN_COPY( wp, up, usize );
smg_comms_c_wrappers 432 w->nlimbs = usize;
smg_comms_c_wrappers 433 w->nbits = u->nbits;
smg_comms_c_wrappers 434 w->flags = u->flags;
smg_comms_c_wrappers 435 w->sign = usign;
smg_comms_c_wrappers 436 }
smg_comms_c_wrappers 437
smg_comms_c_wrappers 438
smg_comms_c_wrappers 439 void
smg_comms_c_wrappers 440 mpi_set_ui( MPI w, unsigned long u)
smg_comms_c_wrappers 441 {
smg_comms_c_wrappers 442 RESIZE_IF_NEEDED(w, 1);
smg_comms_c_wrappers 443 w->d[0] = u;
smg_comms_c_wrappers 444 w->nlimbs = u? 1:0;
smg_comms_c_wrappers 445 w->sign = 0;
smg_comms_c_wrappers 446 w->nbits = 0;
smg_comms_c_wrappers 447 w->flags = 0;
smg_comms_c_wrappers 448 }
smg_comms_c_wrappers 449
smg_comms_c_wrappers 450
smg_comms_c_wrappers 451 MPI
smg_comms_c_wrappers 452 mpi_alloc_set_ui( unsigned long u)
smg_comms_c_wrappers 453 {
smg_comms_c_wrappers 454 #ifdef M_DEBUG
smg_comms_c_wrappers 455 MPI w = mpi_debug_alloc(1,"alloc_set_ui");
smg_comms_c_wrappers 456 #else
smg_comms_c_wrappers 457 MPI w = mpi_alloc(1);
smg_comms_c_wrappers 458 #endif
smg_comms_c_wrappers 459 w->d[0] = u;
smg_comms_c_wrappers 460 w->nlimbs = u? 1:0;
smg_comms_c_wrappers 461 w->sign = 0;
smg_comms_c_wrappers 462 return w;
smg_comms_c_wrappers 463 }
smg_comms_c_wrappers 464
smg_comms_c_wrappers 465
smg_comms_c_wrappers 466 void
smg_comms_c_wrappers 467 mpi_swap( MPI a, MPI b)
smg_comms_c_wrappers 468 {
smg_comms_c_wrappers 469 struct gcry_mpi tmp;
smg_comms_c_wrappers 470
smg_comms_c_wrappers 471 tmp = *a; *a = *b; *b = tmp;
smg_comms_c_wrappers 472 }
smg_comms_c_wrappers 473
smg_comms_c_wrappers 474
smg_comms_c_wrappers 475 int
smg_comms_c_wrappers 476 mpi_get_nlimbs (MPI a)
smg_comms_c_wrappers 477 {
smg_comms_c_wrappers 478 return a->nlimbs;
smg_comms_c_wrappers 479 }
smg_comms_c_wrappers 480
smg_comms_c_wrappers 481
smg_comms_c_wrappers 482 * Returns the allocated space for the given MPI, as number of limbs.
smg_comms_c_wrappers 483 */
smg_comms_c_wrappers 484 int
smg_comms_c_wrappers 485 mpi_get_alloced (MPI a)
smg_comms_c_wrappers 486 {
smg_comms_c_wrappers 487 return a->alloced;
smg_comms_c_wrappers 488 }
smg_comms_c_wrappers 489
smg_comms_c_wrappers 490 int
smg_comms_c_wrappers 491 mpi_is_neg (MPI a)
smg_comms_c_wrappers 492 {
smg_comms_c_wrappers 493 return a->sign;
smg_comms_c_wrappers 494 }
smg_comms_c_wrappers 495
smg_comms_c_wrappers 496
smg_comms_c_wrappers 497
smg_comms_c_wrappers 498 the number of bytes to represent it. */
smg_comms_c_wrappers 499 unsigned int
smg_comms_c_wrappers 500 mpi_nlimb_hint_from_nbytes (unsigned int nbytes)
smg_comms_c_wrappers 501 {
smg_comms_c_wrappers 502 return (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
smg_comms_c_wrappers 503 }
smg_comms_c_wrappers 504
smg_comms_c_wrappers 505
smg_comms_c_wrappers 506 the number of bytes to represent it. */
smg_comms_c_wrappers 507 unsigned int
smg_comms_c_wrappers 508 mpi_nlimb_hint_from_nbits (unsigned int nbits)
smg_comms_c_wrappers 509 {
smg_comms_c_wrappers 510 return (nbits+BITS_PER_MPI_LIMB-1) / BITS_PER_MPI_LIMB;
smg_comms_c_wrappers 511 }
smg_comms_c_wrappers 512
smg_comms_c_wrappers 513 unsigned int
smg_comms_c_wrappers 514 mpi_get_flags (MPI a)
smg_comms_c_wrappers 515 {
smg_comms_c_wrappers 516 return a->flags;
smg_comms_c_wrappers 517 }