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