xref: /freebsd/contrib/bearssl/src/int/i31_muladd.c (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
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_i31_muladd_small(uint32_t *x, uint32_t z, const uint32_t *m)
30 {
31 	uint32_t m_bitlen;
32 	unsigned mblr;
33 	size_t u, mlen;
34 	uint32_t a0, a1, b0, hi, g, q, tb;
35 	uint32_t under, over;
36 	uint32_t cc;
37 
38 	/*
39 	 * We can test on the modulus bit length since we accept to
40 	 * leak that length.
41 	 */
42 	m_bitlen = m[0];
43 	if (m_bitlen == 0) {
44 		return;
45 	}
46 	if (m_bitlen <= 31) {
47 		uint32_t lo;
48 
49 		hi = x[1] >> 1;
50 		lo = (x[1] << 31) | z;
51 		x[1] = br_rem(hi, lo, m[1]);
52 		return;
53 	}
54 	mlen = (m_bitlen + 31) >> 5;
55 	mblr = (unsigned)m_bitlen & 31;
56 
57 	/*
58 	 * Principle: we estimate the quotient (x*2^31+z)/m by
59 	 * doing a 64/32 division with the high words.
60 	 *
61 	 * Let:
62 	 *   w = 2^31
63 	 *   a = (w*a0 + a1) * w^N + a2
64 	 *   b = b0 * w^N + b2
65 	 * such that:
66 	 *   0 <= a0 < w
67 	 *   0 <= a1 < w
68 	 *   0 <= a2 < w^N
69 	 *   w/2 <= b0 < w
70 	 *   0 <= b2 < w^N
71 	 *   a < w*b
72 	 * I.e. the two top words of a are a0:a1, the top word of b is
73 	 * b0, we ensured that b0 is "full" (high bit set), and a is
74 	 * such that the quotient q = a/b fits on one word (0 <= q < w).
75 	 *
76 	 * If a = b*q + r (with 0 <= r < q), we can estimate q by
77 	 * doing an Euclidean division on the top words:
78 	 *   a0*w+a1 = b0*u + v  (with 0 <= v < b0)
79 	 * Then the following holds:
80 	 *   0 <= u <= w
81 	 *   u-2 <= q <= u
82 	 */
83 	hi = x[mlen];
84 	if (mblr == 0) {
85 		a0 = x[mlen];
86 		memmove(x + 2, x + 1, (mlen - 1) * sizeof *x);
87 		x[1] = z;
88 		a1 = x[mlen];
89 		b0 = m[mlen];
90 	} else {
91 		a0 = ((x[mlen] << (31 - mblr)) | (x[mlen - 1] >> mblr))
92 			& 0x7FFFFFFF;
93 		memmove(x + 2, x + 1, (mlen - 1) * sizeof *x);
94 		x[1] = z;
95 		a1 = ((x[mlen] << (31 - mblr)) | (x[mlen - 1] >> mblr))
96 			& 0x7FFFFFFF;
97 		b0 = ((m[mlen] << (31 - mblr)) | (m[mlen - 1] >> mblr))
98 			& 0x7FFFFFFF;
99 	}
100 
101 	/*
102 	 * We estimate a divisor q. If the quotient returned by br_div()
103 	 * is g:
104 	 * -- If a0 == b0 then g == 0; we want q = 0x7FFFFFFF.
105 	 * -- Otherwise:
106 	 *    -- if g == 0 then we set q = 0;
107 	 *    -- otherwise, we set q = g - 1.
108 	 * The properties described above then ensure that the true
109 	 * quotient is q-1, q or q+1.
110 	 *
111 	 * Take care that a0, a1 and b0 are 31-bit words, not 32-bit. We
112 	 * must adjust the parameters to br_div() accordingly.
113 	 */
114 	g = br_div(a0 >> 1, a1 | (a0 << 31), b0);
115 	q = MUX(EQ(a0, b0), 0x7FFFFFFF, MUX(EQ(g, 0), 0, g - 1));
116 
117 	/*
118 	 * We subtract q*m from x (with the extra high word of value 'hi').
119 	 * Since q may be off by 1 (in either direction), we may have to
120 	 * add or subtract m afterwards.
121 	 *
122 	 * The 'tb' flag will be true (1) at the end of the loop if the
123 	 * result is greater than or equal to the modulus (not counting
124 	 * 'hi' or the carry).
125 	 */
126 	cc = 0;
127 	tb = 1;
128 	for (u = 1; u <= mlen; u ++) {
129 		uint32_t mw, zw, xw, nxw;
130 		uint64_t zl;
131 
132 		mw = m[u];
133 		zl = MUL31(mw, q) + cc;
134 		cc = (uint32_t)(zl >> 31);
135 		zw = (uint32_t)zl & (uint32_t)0x7FFFFFFF;
136 		xw = x[u];
137 		nxw = xw - zw;
138 		cc += nxw >> 31;
139 		nxw &= 0x7FFFFFFF;
140 		x[u] = nxw;
141 		tb = MUX(EQ(nxw, mw), tb, GT(nxw, mw));
142 	}
143 
144 	/*
145 	 * If we underestimated q, then either cc < hi (one extra bit
146 	 * beyond the top array word), or cc == hi and tb is true (no
147 	 * extra bit, but the result is not lower than the modulus). In
148 	 * these cases we must subtract m once.
149 	 *
150 	 * Otherwise, we may have overestimated, which will show as
151 	 * cc > hi (thus a negative result). Correction is adding m once.
152 	 */
153 	over = GT(cc, hi);
154 	under = ~over & (tb | LT(cc, hi));
155 	br_i31_add(x, m, over);
156 	br_i31_sub(x, m, under);
157 }
158