Line | Count | Source |
1 | | /* mpn_cnd_sub_n -- Compute R = U - V if CND != 0 or R = U if CND == 0. |
2 | | Both cases should take the same time and perform the exact same memory |
3 | | accesses, since this function is intended to be used where side-channel |
4 | | attack resilience is relevant. |
5 | | |
6 | | Copyright 1992-1994, 1996, 2000, 2002, 2008, 2009, 2011, 2013 Free Software |
7 | | Foundation, Inc. |
8 | | |
9 | | This file is part of the GNU MP Library. |
10 | | |
11 | | The GNU MP Library is free software; you can redistribute it and/or modify |
12 | | it under the terms of either: |
13 | | |
14 | | * the GNU Lesser General Public License as published by the Free |
15 | | Software Foundation; either version 3 of the License, or (at your |
16 | | option) any later version. |
17 | | |
18 | | or |
19 | | |
20 | | * the GNU General Public License as published by the Free Software |
21 | | Foundation; either version 2 of the License, or (at your option) any |
22 | | later version. |
23 | | |
24 | | or both in parallel, as here. |
25 | | |
26 | | The GNU MP Library is distributed in the hope that it will be useful, but |
27 | | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
28 | | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
29 | | for more details. |
30 | | |
31 | | You should have received copies of the GNU General Public License and the |
32 | | GNU Lesser General Public License along with the GNU MP Library. If not, |
33 | | see https://www.gnu.org/licenses/. */ |
34 | | |
35 | | #include "gmp-impl.h" |
36 | | |
37 | | mp_limb_t |
38 | | mpn_cnd_sub_n (mp_limb_t cnd, mp_ptr rp, mp_srcptr up, mp_srcptr vp, mp_size_t n) |
39 | 758M | { |
40 | 758M | mp_limb_t ul, vl, sl, rl, cy, cy1, cy2, mask; |
41 | | |
42 | 758M | ASSERT (n >= 1); |
43 | 758M | ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n)); |
44 | 758M | ASSERT (MPN_SAME_OR_SEPARATE_P (rp, vp, n)); |
45 | | |
46 | 758M | mask = -(mp_limb_t) (cnd != 0); |
47 | 758M | cy = 0; |
48 | 758M | do |
49 | 19.7G | { |
50 | 19.7G | ul = *up++; |
51 | 19.7G | vl = *vp++ & mask; |
52 | 19.7G | #if GMP_NAIL_BITS == 0 |
53 | 19.7G | sl = ul - vl; |
54 | 19.7G | cy1 = sl > ul; |
55 | 19.7G | rl = sl - cy; |
56 | 19.7G | cy2 = rl > sl; |
57 | 19.7G | cy = cy1 | cy2; |
58 | 19.7G | *rp++ = rl; |
59 | | #else |
60 | | rl = ul - vl; |
61 | | rl -= cy; |
62 | | cy = rl >> (GMP_LIMB_BITS - 1); |
63 | | *rp++ = rl & GMP_NUMB_MASK; |
64 | | #endif |
65 | 19.7G | } |
66 | 19.7G | while (--n != 0); |
67 | | |
68 | 758M | return cy; |
69 | 758M | } |