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