* gpgkeys_hkp.c (send_key), gpgkeys_ldap.c (send_key, send_key_keyserver):
[gnupg.git] / mpi / mpi-inline.h
blobdb2c6a970b7cd0fad67096d3399b655a53d9c7dc
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 * USA.
21 * Note: This code is heavily based on the GNU MP Library.
22 * Actually it's the same code with only minor changes in the
23 * way the data is stored; this is to support the abstraction
24 * of an optional secure memory allocation which may be used
25 * to avoid revealing of sensitive data due to paging etc.
26 * The GNU MP Library itself is published under the LGPL;
27 * however I decided to publish this code under the plain GPL.
30 #ifndef G10_MPI_INLINE_H
31 #define G10_MPI_INLINE_H
33 #ifndef G10_MPI_INLINE_DECL
34 #define G10_MPI_INLINE_DECL extern __inline__
35 #endif
37 G10_MPI_INLINE_DECL mpi_limb_t
38 mpihelp_add_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
39 mpi_size_t s1_size, mpi_limb_t s2_limb)
41 mpi_limb_t x;
43 x = *s1_ptr++;
44 s2_limb += x;
45 *res_ptr++ = s2_limb;
46 if( s2_limb < x ) { /* sum is less than the left operand: handle carry */
47 while( --s1_size ) {
48 x = *s1_ptr++ + 1; /* add carry */
49 *res_ptr++ = x; /* and store */
50 if( x ) /* not 0 (no overflow): we can stop */
51 goto leave;
53 return 1; /* return carry (size of s1 to small) */
56 leave:
57 if( res_ptr != s1_ptr ) { /* not the same variable */
58 mpi_size_t i; /* copy the rest */
59 for( i=0; i < s1_size-1; i++ )
60 res_ptr[i] = s1_ptr[i];
62 return 0; /* no carry */
67 G10_MPI_INLINE_DECL mpi_limb_t
68 mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
69 mpi_ptr_t s2_ptr, mpi_size_t s2_size)
71 mpi_limb_t cy = 0;
73 if( s2_size )
74 cy = mpihelp_add_n( res_ptr, s1_ptr, s2_ptr, s2_size );
76 if( s1_size - s2_size )
77 cy = mpihelp_add_1( res_ptr + s2_size, s1_ptr + s2_size,
78 s1_size - s2_size, cy);
79 return cy;
83 G10_MPI_INLINE_DECL mpi_limb_t
84 mpihelp_sub_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
85 mpi_size_t s1_size, mpi_limb_t s2_limb )
87 mpi_limb_t x;
89 x = *s1_ptr++;
90 s2_limb = x - s2_limb;
91 *res_ptr++ = s2_limb;
92 if( s2_limb > x ) {
93 while( --s1_size ) {
94 x = *s1_ptr++;
95 *res_ptr++ = x - 1;
96 if( x )
97 goto leave;
99 return 1;
102 leave:
103 if( res_ptr != s1_ptr ) {
104 mpi_size_t i;
105 for( i=0; i < s1_size-1; i++ )
106 res_ptr[i] = s1_ptr[i];
108 return 0;
113 G10_MPI_INLINE_DECL mpi_limb_t
114 mpihelp_sub( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
115 mpi_ptr_t s2_ptr, mpi_size_t s2_size)
117 mpi_limb_t cy = 0;
119 if( s2_size )
120 cy = mpihelp_sub_n(res_ptr, s1_ptr, s2_ptr, s2_size);
122 if( s1_size - s2_size )
123 cy = mpihelp_sub_1(res_ptr + s2_size, s1_ptr + s2_size,
124 s1_size - s2_size, cy);
125 return cy;
128 #endif /*G10_MPI_INLINE_H*/