1 /* mpi-inline.h - Internal to the Multi Precision Integers
2 * Copyright (C) 1994, 1996, 1998, 1999 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 * Note: This code is heavily based on the GNU MP Library.
21 * Actually it's the same code with only minor changes in the
22 * way the data is stored; this is to support the abstraction
23 * of an optional secure memory allocation which may be used
24 * to avoid revealing of sensitive data due to paging etc.
25 * The GNU MP Library itself is published under the LGPL;
26 * however I decided to publish this code under the plain GPL.
29 #ifndef G10_MPI_INLINE_H
30 #define G10_MPI_INLINE_H
32 #ifndef G10_MPI_INLINE_DECL
33 #define G10_MPI_INLINE_DECL extern __inline__
36 G10_MPI_INLINE_DECL mpi_limb_t
37 mpihelp_add_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
38 mpi_size_t s1_size, mpi_limb_t s2_limb)
45 if( s2_limb < x ) { /* sum is less than the left operand: handle carry */
47 x = *s1_ptr++ + 1; /* add carry */
48 *res_ptr++ = x; /* and store */
49 if( x ) /* not 0 (no overflow): we can stop */
52 return 1; /* return carry (size of s1 to small) */
56 if( res_ptr != s1_ptr ) { /* not the same variable */
57 mpi_size_t i; /* copy the rest */
58 for( i=0; i < s1_size-1; i++ )
59 res_ptr[i] = s1_ptr[i];
61 return 0; /* no carry */
66 G10_MPI_INLINE_DECL mpi_limb_t
67 mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
68 mpi_ptr_t s2_ptr, mpi_size_t s2_size)
73 cy = mpihelp_add_n( res_ptr, s1_ptr, s2_ptr, s2_size );
75 if( s1_size - s2_size )
76 cy = mpihelp_add_1( res_ptr + s2_size, s1_ptr + s2_size,
77 s1_size - s2_size, cy);
82 G10_MPI_INLINE_DECL mpi_limb_t
83 mpihelp_sub_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
84 mpi_size_t s1_size, mpi_limb_t s2_limb )
89 s2_limb = x - s2_limb;
102 if( res_ptr != s1_ptr ) {
104 for( i=0; i < s1_size-1; i++ )
105 res_ptr[i] = s1_ptr[i];
112 G10_MPI_INLINE_DECL mpi_limb_t
113 mpihelp_sub( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
114 mpi_ptr_t s2_ptr, mpi_size_t s2_size)
119 cy = mpihelp_sub_n(res_ptr, s1_ptr, s2_ptr, s2_size);
121 if( s1_size - s2_size )
122 cy = mpihelp_sub_1(res_ptr + s2_size, s1_ptr + s2_size,
123 s1_size - s2_size, cy);
128 #endif /*G10_MPI_INLINE_H*/