1 /* mpn_sec_pi1_div_qr, mpn_sec_pi1_div_r -- Compute Q = floor(U / V), U = U
2 mod V. Side-channel silent under the assumption that the used instructions
3 are side-channel silent.
5 Contributed to the GNU project by Torbjörn Granlund.
7 THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES. IT IS ONLY
8 SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
9 GUARANTEED THAT THEY WILL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
11 Copyright 2011-2013 Free Software Foundation, Inc.
13 This file is part of the GNU MP Library.
15 The GNU MP Library is free software; you can redistribute it and/or modify
16 it under the terms of either:
18 * the GNU Lesser General Public License as published by the Free
19 Software Foundation; either version 3 of the License, or (at your
20 option) any later version.
24 * the GNU General Public License as published by the Free Software
25 Foundation; either version 2 of the License, or (at your option) any
28 or both in parallel, as here.
30 The GNU MP Library is distributed in the hope that it will be useful, but
31 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
32 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
35 You should have received copies of the GNU General Public License and the
36 GNU Lesser General Public License along with the GNU MP Library. If not,
37 see https://www.gnu.org/licenses/. */
43 /* This side-channel silent division algorithm reduces the partial remainder by
44 GMP_NUMB_BITS/2 bits at a time, compared to GMP_NUMB_BITS for the main
45 division algorithm. We actually do not insist on reducing by exactly
46 GMP_NUMB_BITS/2, but may leave a partial remainder that is D*B^i to 3D*B^i
47 too large (B is the limb base, D is the divisor, and i is the induction
48 variable); the subsequent step will handle the extra partial remainder bits.
50 With that partial remainder reduction, each step generates a quotient "half
51 limb". The outer loop generates two quotient half limbs, an upper (q1h) and
52 a lower (q0h) which are stored sparsely in separate limb arrays. These
53 arrays are added at the end; using separate arrays avoids data-dependent
54 carry propagation which could else pose a side-channel leakage problem.
56 The quotient half limbs may be between -3 to 0 from the accurate value
57 ("accurate" being the one which corresponds to a reduction to a principal
58 partial remainder). Too small quotient half limbs correspond to too large
59 remainders, which we reduce later, as described above.
61 In order to keep quotients from getting too big, corresponding to a negative
62 partial remainder, we use an inverse which is slightly smaller than usually.
65 #if OPERATION_sec_pi1_div_qr
66 /* Needs (dn + 1) + (nn - dn) + (nn - dn) = 2nn - dn + 1 limbs at tp. */
67 #define FNAME mpn_sec_pi1_div_qr
69 #define RETTYPE mp_limb_t
71 #if OPERATION_sec_pi1_div_r
72 /* Needs (dn + 1) limbs at tp. */
73 #define FNAME mpn_sec_pi1_div_r
80 mp_ptr np
, mp_size_t nn
,
81 mp_srcptr dp
, mp_size_t dn
,
85 mp_limb_t nh
, cy
, q1h
, q0h
, dummy
, cnd
;
88 #if OPERATION_sec_pi1_div_qr
95 ASSERT ((dp
[dn
- 1] & GMP_NUMB_HIGHBIT
) != 0);
99 cy
= mpn_sub_n (np
, np
, dp
, dn
);
100 mpn_cnd_add_n (cy
, np
, np
, dp
, dn
);
101 #if OPERATION_sec_pi1_div_qr
108 /* Create a divisor copy shifted half a limb. */
109 hp
= tp
; /* (dn + 1) limbs */
110 hp
[dn
] = mpn_lshift (hp
, dp
, dn
, GMP_NUMB_BITS
/ 2);
112 #if OPERATION_sec_pi1_div_qr
113 qlp
= tp
+ (dn
+ 1); /* (nn - dn) limbs */
114 qhp
= tp
+ (nn
+ 1); /* (nn - dn) limbs */
120 for (i
= nn
- dn
- 1; i
>= 0; i
--)
124 nh
= (nh
<< GMP_NUMB_BITS
/2) + (np
[dn
] >> GMP_NUMB_BITS
/2);
125 umul_ppmm (q1h
, dummy
, nh
, dinv
);
127 #if OPERATION_sec_pi1_div_qr
130 mpn_submul_1 (np
, hp
, dn
+ 1, q1h
);
133 umul_ppmm (q0h
, dummy
, nh
, dinv
);
135 #if OPERATION_sec_pi1_div_qr
138 nh
-= mpn_submul_1 (np
, dp
, dn
, q0h
);
141 /* 1st adjustment depends on extra high remainder limb. */
142 cnd
= nh
!= 0; /* FIXME: cmp-to-int */
143 #if OPERATION_sec_pi1_div_qr
146 nh
-= mpn_cnd_sub_n (cnd
, np
, np
, dp
, dn
);
148 /* 2nd adjustment depends on remainder/divisor comparison as well as whether
149 extra remainder limb was nullified by previous subtract. */
150 cy
= mpn_sub_n (np
, np
, dp
, dn
);
152 #if OPERATION_sec_pi1_div_qr
155 mpn_cnd_add_n (cy
, np
, np
, dp
, dn
);
157 /* 3rd adjustment depends on remainder/divisor comparison. */
158 cy
= mpn_sub_n (np
, np
, dp
, dn
);
159 #if OPERATION_sec_pi1_div_qr
162 mpn_cnd_add_n (cy
, np
, np
, dp
, dn
);
164 #if OPERATION_sec_pi1_div_qr
165 /* Combine quotient halves into final quotient. */
166 qh
= mpn_lshift (qhp
, qhp
, nn
- dn
, GMP_NUMB_BITS
/2);
167 qh
+= mpn_add_n (qp
, qhp
, qlp
, nn
- dn
);