raw
mpi-genesis~            1 /* mpi-inline.h  -  Internal to the Multi Precision Integers
mpi-genesis~ 2 * Copyright (C) 1994, 1996, 1998, 1999 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 * Note: This code is heavily based on the GNU MP Library.
mpi-genesis~ 20 * Actually it's the same code with only minor changes in the
mpi-genesis~ 21 * way the data is stored; this is to support the abstraction
mpi-genesis~ 22 * of an optional secure memory allocation which may be used
mpi-genesis~ 23 * to avoid revealing of sensitive data due to paging etc.
mpi-genesis~ 24 * The GNU MP Library itself is published under the LGPL;
mpi-genesis~ 25 * however I decided to publish this code under the plain GPL.
mpi-genesis~ 26 */
mpi-genesis~ 27
mpi-genesis~ 28 #ifndef G10_MPI_INLINE_H
mpi-genesis~ 29 #define G10_MPI_INLINE_H
mpi-genesis~ 30
mpi-genesis~ 31 #ifndef G10_MPI_INLINE_DECL
mpi-genesis~ 32 #define G10_MPI_INLINE_DECL extern __inline__
mpi-genesis~ 33 #endif
mpi-genesis~ 34
mpi-genesis~ 35 G10_MPI_INLINE_DECL mpi_limb_t
mpi-genesis~ 36 mpihelp_add_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
mpi-genesis~ 37 mpi_size_t s1_size, mpi_limb_t s2_limb)
mpi-genesis~ 38 {
mpi-genesis~ 39 mpi_limb_t x;
mpi-genesis~ 40
mpi-genesis~ 41 x = *s1_ptr++;
mpi-genesis~ 42 s2_limb += x;
mpi-genesis~ 43 *res_ptr++ = s2_limb;
mpi-genesis~ 44 if( s2_limb < x ) { /* sum is less than the left operand: handle carry */
mpi-genesis~ 45 while( --s1_size ) {
mpi-genesis~ 46 x = *s1_ptr++ + 1; /* add carry */
mpi-genesis~ 47 *res_ptr++ = x; /* and store */
mpi-genesis~ 48 if( x ) /* not 0 (no overflow): we can stop */
mpi-genesis~ 49 goto leave;
mpi-genesis~ 50 }
mpi-genesis~ 51 return 1; /* return carry (size of s1 to small) */
mpi-genesis~ 52 }
mpi-genesis~ 53
mpi-genesis~ 54 leave:
mpi-genesis~ 55 if( res_ptr != s1_ptr ) { /* not the same variable */
mpi-genesis~ 56 mpi_size_t i; /* copy the rest */
mpi-genesis~ 57 for( i=0; i < s1_size-1; i++ )
mpi-genesis~ 58 res_ptr[i] = s1_ptr[i];
mpi-genesis~ 59 }
mpi-genesis~ 60 return 0; /* no carry */
mpi-genesis~ 61 }
mpi-genesis~ 62
mpi-genesis~ 63
mpi-genesis~ 64
mpi-genesis~ 65 G10_MPI_INLINE_DECL mpi_limb_t
mpi-genesis~ 66 mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
mpi-genesis~ 67 mpi_ptr_t s2_ptr, mpi_size_t s2_size)
mpi-genesis~ 68 {
mpi-genesis~ 69 mpi_limb_t cy = 0;
mpi-genesis~ 70
mpi-genesis~ 71 if( s2_size )
mpi-genesis~ 72 cy = mpihelp_add_n( res_ptr, s1_ptr, s2_ptr, s2_size );
mpi-genesis~ 73
mpi-genesis~ 74 if( s1_size - s2_size )
mpi-genesis~ 75 cy = mpihelp_add_1( res_ptr + s2_size, s1_ptr + s2_size,
mpi-genesis~ 76 s1_size - s2_size, cy);
mpi-genesis~ 77 return cy;
mpi-genesis~ 78 }
mpi-genesis~ 79
mpi-genesis~ 80
mpi-genesis~ 81 G10_MPI_INLINE_DECL mpi_limb_t
mpi-genesis~ 82 mpihelp_sub_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
mpi-genesis~ 83 mpi_size_t s1_size, mpi_limb_t s2_limb )
mpi-genesis~ 84 {
mpi-genesis~ 85 mpi_limb_t x;
mpi-genesis~ 86
mpi-genesis~ 87 x = *s1_ptr++;
mpi-genesis~ 88 s2_limb = x - s2_limb;
mpi-genesis~ 89 *res_ptr++ = s2_limb;
mpi-genesis~ 90 if( s2_limb > x ) {
mpi-genesis~ 91 while( --s1_size ) {
mpi-genesis~ 92 x = *s1_ptr++;
mpi-genesis~ 93 *res_ptr++ = x - 1;
mpi-genesis~ 94 if( x )
mpi-genesis~ 95 goto leave;
mpi-genesis~ 96 }
mpi-genesis~ 97 return 1;
mpi-genesis~ 98 }
mpi-genesis~ 99
mpi-genesis~ 100 leave:
mpi-genesis~ 101 if( res_ptr != s1_ptr ) {
mpi-genesis~ 102 mpi_size_t i;
mpi-genesis~ 103 for( i=0; i < s1_size-1; i++ )
mpi-genesis~ 104 res_ptr[i] = s1_ptr[i];
mpi-genesis~ 105 }
mpi-genesis~ 106 return 0;
mpi-genesis~ 107 }
mpi-genesis~ 108
mpi-genesis~ 109
mpi-genesis~ 110
mpi-genesis~ 111 G10_MPI_INLINE_DECL mpi_limb_t
mpi-genesis~ 112 mpihelp_sub( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
mpi-genesis~ 113 mpi_ptr_t s2_ptr, mpi_size_t s2_size)
mpi-genesis~ 114 {
mpi-genesis~ 115 mpi_limb_t cy = 0;
mpi-genesis~ 116
mpi-genesis~ 117 if( s2_size )
mpi-genesis~ 118 cy = mpihelp_sub_n(res_ptr, s1_ptr, s2_ptr, s2_size);
mpi-genesis~ 119
mpi-genesis~ 120 if( s1_size - s2_size )
mpi-genesis~ 121 cy = mpihelp_sub_1(res_ptr + s2_size, s1_ptr + s2_size,
mpi-genesis~ 122 s1_size - s2_size, cy);
mpi-genesis~ 123 return cy;
mpi-genesis~ 124 }
mpi-genesis~ 125
mpi-genesis~ 126 #endif /*G10_MPI_INLINE_H*/