1 /* mpi-inline.h  -  Internal to the Multi Precision Integers
2  *	Copyright (C) 1994, 1996, 1998, 1999 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
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 3 of the License, or
9  * (at your option) any later version.
10  *
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.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  *
19  * Note: This code is heavily based on the GNU MP Library.
20  *	 Actually it's the same code with only minor changes in the
21  *	 way the data is stored; this is to support the abstraction
22  *	 of an optional secure memory allocation which may be used
23  *	 to avoid revealing of sensitive data due to paging etc.
24  *	 The GNU MP Library itself is published under the LGPL;
25  *	 however I decided to publish this code under the plain GPL.
26  */
27 
28 #ifndef G10_MPI_INLINE_H
29 #define G10_MPI_INLINE_H
30 
31 /* Starting with gcc 4.3 "extern inline" conforms in c99 mode to the
32    c99 semantics.  To keep the useful old semantics we use an
33    attribute.  */
34 #ifndef G10_MPI_INLINE_DECL
35 # ifdef __GNUC_STDC_INLINE__
36 #  define G10_MPI_INLINE_DECL  extern inline __attribute__ ((__gnu_inline__))
37 # else
38 #  define G10_MPI_INLINE_DECL  extern __inline__
39 # endif
40 #endif
41 
42 G10_MPI_INLINE_DECL  mpi_limb_t
mpihelp_add_1(mpi_ptr_t res_ptr,mpi_ptr_t s1_ptr,mpi_size_t s1_size,mpi_limb_t s2_limb)43 mpihelp_add_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
44 	       mpi_size_t s1_size, mpi_limb_t s2_limb)
45 {
46     mpi_limb_t x;
47 
48     x = *s1_ptr++;
49     s2_limb += x;
50     *res_ptr++ = s2_limb;
51     if( s2_limb < x ) { /* sum is less than the left operand: handle carry */
52 	while( --s1_size ) {
53 	    x = *s1_ptr++ + 1;	/* add carry */
54 	    *res_ptr++ = x;	/* and store */
55 	    if( x )		/* not 0 (no overflow): we can stop */
56 		goto leave;
57 	}
58 	return 1; /* return carry (size of s1 to small) */
59     }
60 
61   leave:
62     if( res_ptr != s1_ptr ) { /* not the same variable */
63 	mpi_size_t i;	       /* copy the rest */
64 	for( i=0; i < s1_size-1; i++ )
65 	    res_ptr[i] = s1_ptr[i];
66     }
67     return 0; /* no carry */
68 }
69 
70 
71 
72 G10_MPI_INLINE_DECL mpi_limb_t
mpihelp_add(mpi_ptr_t res_ptr,mpi_ptr_t s1_ptr,mpi_size_t s1_size,mpi_ptr_t s2_ptr,mpi_size_t s2_size)73 mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
74 			       mpi_ptr_t s2_ptr, mpi_size_t s2_size)
75 {
76     mpi_limb_t cy = 0;
77 
78     if( s2_size )
79 	cy = mpihelp_add_n( res_ptr, s1_ptr, s2_ptr, s2_size );
80 
81     if( s1_size - s2_size )
82 	cy = mpihelp_add_1( res_ptr + s2_size, s1_ptr + s2_size,
83 			    s1_size - s2_size, cy);
84     return cy;
85 }
86 
87 
88 G10_MPI_INLINE_DECL mpi_limb_t
mpihelp_sub_1(mpi_ptr_t res_ptr,mpi_ptr_t s1_ptr,mpi_size_t s1_size,mpi_limb_t s2_limb)89 mpihelp_sub_1(mpi_ptr_t res_ptr,  mpi_ptr_t s1_ptr,
90 	      mpi_size_t s1_size, mpi_limb_t s2_limb )
91 {
92     mpi_limb_t x;
93 
94     x = *s1_ptr++;
95     s2_limb = x - s2_limb;
96     *res_ptr++ = s2_limb;
97     if( s2_limb > x ) {
98 	while( --s1_size ) {
99 	    x = *s1_ptr++;
100 	    *res_ptr++ = x - 1;
101 	    if( x )
102 		goto leave;
103 	}
104 	return 1;
105     }
106 
107   leave:
108     if( res_ptr != s1_ptr ) {
109 	mpi_size_t i;
110 	for( i=0; i < s1_size-1; i++ )
111 	    res_ptr[i] = s1_ptr[i];
112     }
113     return 0;
114 }
115 
116 
117 
118 G10_MPI_INLINE_DECL   mpi_limb_t
mpihelp_sub(mpi_ptr_t res_ptr,mpi_ptr_t s1_ptr,mpi_size_t s1_size,mpi_ptr_t s2_ptr,mpi_size_t s2_size)119 mpihelp_sub( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
120 				mpi_ptr_t s2_ptr, mpi_size_t s2_size)
121 {
122     mpi_limb_t cy = 0;
123 
124     if( s2_size )
125 	cy = mpihelp_sub_n(res_ptr, s1_ptr, s2_ptr, s2_size);
126 
127     if( s1_size - s2_size )
128 	cy = mpihelp_sub_1(res_ptr + s2_size, s1_ptr + s2_size,
129 				      s1_size - s2_size, cy);
130     return cy;
131 }
132 
133 #endif /*G10_MPI_INLINE_H*/
134