Documentation fixes.
[BearSSL] / src / int / i32_muladd.c
1 /*
2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include "inner.h"
26
27 /* see inner.h */
28 void
29 br_i32_muladd_small(uint32_t *x, uint32_t z, const uint32_t *m)
30 {
31 uint32_t m_bitlen;
32 size_t u, mlen;
33 uint32_t a0, a1, b0, hi, g, q, tb;
34 uint32_t chf, clow, under, over;
35 uint64_t cc;
36
37 /*
38 * We can test on the modulus bit length since we accept to
39 * leak that length.
40 */
41 m_bitlen = m[0];
42 if (m_bitlen == 0) {
43 return;
44 }
45 if (m_bitlen <= 32) {
46 x[1] = br_rem(x[1], z, m[1]);
47 return;
48 }
49 mlen = (m_bitlen + 31) >> 5;
50
51 /*
52 * Principle: we estimate the quotient (x*2^32+z)/m by
53 * doing a 64/32 division with the high words.
54 *
55 * Let:
56 * w = 2^32
57 * a = (w*a0 + a1) * w^N + a2
58 * b = b0 * w^N + b2
59 * such that:
60 * 0 <= a0 < w
61 * 0 <= a1 < w
62 * 0 <= a2 < w^N
63 * w/2 <= b0 < w
64 * 0 <= b2 < w^N
65 * a < w*b
66 * I.e. the two top words of a are a0:a1, the top word of b is
67 * b0, we ensured that b0 is "full" (high bit set), and a is
68 * such that the quotient q = a/b fits on one word (0 <= q < w).
69 *
70 * If a = b*q + r (with 0 <= r < q), we can estimate q by
71 * doing an Euclidean division on the top words:
72 * a0*w+a1 = b0*u + v (with 0 <= v < w)
73 * Then the following holds:
74 * 0 <= u <= w
75 * u-2 <= q <= u
76 */
77 a0 = br_i32_word(x, m_bitlen - 32);
78 hi = x[mlen];
79 memmove(x + 2, x + 1, (mlen - 1) * sizeof *x);
80 x[1] = z;
81 a1 = br_i32_word(x, m_bitlen - 32);
82 b0 = br_i32_word(m, m_bitlen - 32);
83
84 /*
85 * We estimate a divisor q. If the quotient returned by br_div()
86 * is g:
87 * -- If a0 == b0 then g == 0; we want q = 0xFFFFFFFF.
88 * -- Otherwise:
89 * -- if g == 0 then we set q = 0;
90 * -- otherwise, we set q = g - 1.
91 * The properties described above then ensure that the true
92 * quotient is q-1, q or q+1.
93 */
94 g = br_div(a0, a1, b0);
95 q = MUX(EQ(a0, b0), 0xFFFFFFFF, MUX(EQ(g, 0), 0, g - 1));
96
97 /*
98 * We subtract q*m from x (with the extra high word of value 'hi').
99 * Since q may be off by 1 (in either direction), we may have to
100 * add or subtract m afterwards.
101 *
102 * The 'tb' flag will be true (1) at the end of the loop if the
103 * result is greater than or equal to the modulus (not counting
104 * 'hi' or the carry).
105 */
106 cc = 0;
107 tb = 1;
108 for (u = 1; u <= mlen; u ++) {
109 uint32_t mw, zw, xw, nxw;
110 uint64_t zl;
111
112 mw = m[u];
113 zl = MUL(mw, q) + cc;
114 cc = (uint32_t)(zl >> 32);
115 zw = (uint32_t)zl;
116 xw = x[u];
117 nxw = xw - zw;
118 cc += (uint64_t)GT(nxw, xw);
119 x[u] = nxw;
120 tb = MUX(EQ(nxw, mw), tb, GT(nxw, mw));
121 }
122
123 /*
124 * If we underestimated q, then either cc < hi (one extra bit
125 * beyond the top array word), or cc == hi and tb is true (no
126 * extra bit, but the result is not lower than the modulus). In
127 * these cases we must subtract m once.
128 *
129 * Otherwise, we may have overestimated, which will show as
130 * cc > hi (thus a negative result). Correction is adding m once.
131 */
132 chf = (uint32_t)(cc >> 32);
133 clow = (uint32_t)cc;
134 over = chf | GT(clow, hi);
135 under = ~over & (tb | (~chf & LT(clow, hi)));
136 br_i32_add(x, m, over);
137 br_i32_sub(x, m, under);
138 }