raw
ch1_mpi                 1 /* mpi-inline.h  -  Internal to the Multi Precision Integers
ch1_mpi 2 * Modified by No Such Labs. (C) 2015. See README.
ch1_mpi 3 *
ch1_mpi 4 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
ch1_mpi 5 * SHA256(gnupg-1.4.10.tar.gz):
ch1_mpi 6 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
ch1_mpi 7 * (C) 1994-2005 Free Software Foundation, Inc.
ch1_mpi 8 *
ch1_mpi 9 * This program is free software: you can redistribute it and/or modify
ch1_mpi 10 * it under the terms of the GNU General Public License as published by
ch1_mpi 11 * the Free Software Foundation, either version 3 of the License, or
ch1_mpi 12 * (at your option) any later version.
ch1_mpi 13 *
ch1_mpi 14 * This program is distributed in the hope that it will be useful,
ch1_mpi 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ch1_mpi 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ch1_mpi 17 * GNU General Public License for more details.
ch1_mpi 18 *
ch1_mpi 19 * You should have received a copy of the GNU General Public License
ch1_mpi 20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
ch1_mpi 21 */
ch1_mpi 22
ch1_mpi 23 #ifndef G10_MPI_INLINE_H
ch1_mpi 24 #define G10_MPI_INLINE_H
ch1_mpi 25
ch1_mpi 26 #ifndef G10_MPI_INLINE_DECL
ch1_mpi 27 #define G10_MPI_INLINE_DECL extern __inline__
ch1_mpi 28 #endif
ch1_mpi 29
ch1_mpi 30 G10_MPI_INLINE_DECL mpi_limb_t
ch1_mpi 31 mpihelp_add_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
ch1_mpi 32 mpi_size_t s1_size, mpi_limb_t s2_limb)
ch1_mpi 33 {
ch1_mpi 34 mpi_limb_t x;
ch1_mpi 35
ch1_mpi 36 x = *s1_ptr++;
ch1_mpi 37 s2_limb += x;
ch1_mpi 38 *res_ptr++ = s2_limb;
ch1_mpi 39 if( s2_limb < x ) { /* sum is less than the left operand: handle carry */
ch1_mpi 40 while( --s1_size ) {
ch1_mpi 41 x = *s1_ptr++ + 1; /* add carry */
ch1_mpi 42 *res_ptr++ = x; /* and store */
ch1_mpi 43 if( x ) /* not 0 (no overflow): we can stop */
ch1_mpi 44 goto leave;
ch1_mpi 45 }
ch1_mpi 46 return 1; /* return carry (size of s1 to small) */
ch1_mpi 47 }
ch1_mpi 48
ch1_mpi 49 leave:
ch1_mpi 50 if( res_ptr != s1_ptr ) { /* not the same variable */
ch1_mpi 51 mpi_size_t i; /* copy the rest */
ch1_mpi 52 for( i=0; i < s1_size-1; i++ )
ch1_mpi 53 res_ptr[i] = s1_ptr[i];
ch1_mpi 54 }
ch1_mpi 55 return 0; /* no carry */
ch1_mpi 56 }
ch1_mpi 57
ch1_mpi 58
ch1_mpi 59
ch1_mpi 60 G10_MPI_INLINE_DECL mpi_limb_t
ch1_mpi 61 mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
ch1_mpi 62 mpi_ptr_t s2_ptr, mpi_size_t s2_size)
ch1_mpi 63 {
ch1_mpi 64 mpi_limb_t cy = 0;
ch1_mpi 65
ch1_mpi 66 if( s2_size )
ch1_mpi 67 cy = mpihelp_add_n( res_ptr, s1_ptr, s2_ptr, s2_size );
ch1_mpi 68
ch1_mpi 69 if( s1_size - s2_size )
ch1_mpi 70 cy = mpihelp_add_1( res_ptr + s2_size, s1_ptr + s2_size,
ch1_mpi 71 s1_size - s2_size, cy);
ch1_mpi 72 return cy;
ch1_mpi 73 }
ch1_mpi 74
ch1_mpi 75
ch1_mpi 76 G10_MPI_INLINE_DECL mpi_limb_t
ch1_mpi 77 mpihelp_sub_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
ch1_mpi 78 mpi_size_t s1_size, mpi_limb_t s2_limb )
ch1_mpi 79 {
ch1_mpi 80 mpi_limb_t x;
ch1_mpi 81
ch1_mpi 82 x = *s1_ptr++;
ch1_mpi 83 s2_limb = x - s2_limb;
ch1_mpi 84 *res_ptr++ = s2_limb;
ch1_mpi 85 if( s2_limb > x ) {
ch1_mpi 86 while( --s1_size ) {
ch1_mpi 87 x = *s1_ptr++;
ch1_mpi 88 *res_ptr++ = x - 1;
ch1_mpi 89 if( x )
ch1_mpi 90 goto leave;
ch1_mpi 91 }
ch1_mpi 92 return 1;
ch1_mpi 93 }
ch1_mpi 94
ch1_mpi 95 leave:
ch1_mpi 96 if( res_ptr != s1_ptr ) {
ch1_mpi 97 mpi_size_t i;
ch1_mpi 98 for( i=0; i < s1_size-1; i++ )
ch1_mpi 99 res_ptr[i] = s1_ptr[i];
ch1_mpi 100 }
ch1_mpi 101 return 0;
ch1_mpi 102 }
ch1_mpi 103
ch1_mpi 104
ch1_mpi 105
ch1_mpi 106 G10_MPI_INLINE_DECL mpi_limb_t
ch1_mpi 107 mpihelp_sub( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
ch1_mpi 108 mpi_ptr_t s2_ptr, mpi_size_t s2_size)
ch1_mpi 109 {
ch1_mpi 110 mpi_limb_t cy = 0;
ch1_mpi 111
ch1_mpi 112 if( s2_size )
ch1_mpi 113 cy = mpihelp_sub_n(res_ptr, s1_ptr, s2_ptr, s2_size);
ch1_mpi 114
ch1_mpi 115 if( s1_size - s2_size )
ch1_mpi 116 cy = mpihelp_sub_1(res_ptr + s2_size, s1_ptr + s2_size,
ch1_mpi 117 s1_size - s2_size, cy);
ch1_mpi 118 return cy;
ch1_mpi 119 }
ch1_mpi 120
ch1_mpi 121 #endif /*G10_MPI_INLINE_H*/