Path: blob/main/contrib/bearssl/src/int/i31_muladd.c
39483 views
/*1* Copyright (c) 2016 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include "inner.h"2526/* see inner.h */27void28br_i31_muladd_small(uint32_t *x, uint32_t z, const uint32_t *m)29{30uint32_t m_bitlen;31unsigned mblr;32size_t u, mlen;33uint32_t a0, a1, b0, hi, g, q, tb;34uint32_t under, over;35uint32_t cc;3637/*38* We can test on the modulus bit length since we accept to39* leak that length.40*/41m_bitlen = m[0];42if (m_bitlen == 0) {43return;44}45if (m_bitlen <= 31) {46uint32_t lo;4748hi = x[1] >> 1;49lo = (x[1] << 31) | z;50x[1] = br_rem(hi, lo, m[1]);51return;52}53mlen = (m_bitlen + 31) >> 5;54mblr = (unsigned)m_bitlen & 31;5556/*57* Principle: we estimate the quotient (x*2^31+z)/m by58* doing a 64/32 division with the high words.59*60* Let:61* w = 2^3162* a = (w*a0 + a1) * w^N + a263* b = b0 * w^N + b264* such that:65* 0 <= a0 < w66* 0 <= a1 < w67* 0 <= a2 < w^N68* w/2 <= b0 < w69* 0 <= b2 < w^N70* a < w*b71* I.e. the two top words of a are a0:a1, the top word of b is72* b0, we ensured that b0 is "full" (high bit set), and a is73* such that the quotient q = a/b fits on one word (0 <= q < w).74*75* If a = b*q + r (with 0 <= r < q), we can estimate q by76* doing an Euclidean division on the top words:77* a0*w+a1 = b0*u + v (with 0 <= v < b0)78* Then the following holds:79* 0 <= u <= w80* u-2 <= q <= u81*/82hi = x[mlen];83if (mblr == 0) {84a0 = x[mlen];85memmove(x + 2, x + 1, (mlen - 1) * sizeof *x);86x[1] = z;87a1 = x[mlen];88b0 = m[mlen];89} else {90a0 = ((x[mlen] << (31 - mblr)) | (x[mlen - 1] >> mblr))91& 0x7FFFFFFF;92memmove(x + 2, x + 1, (mlen - 1) * sizeof *x);93x[1] = z;94a1 = ((x[mlen] << (31 - mblr)) | (x[mlen - 1] >> mblr))95& 0x7FFFFFFF;96b0 = ((m[mlen] << (31 - mblr)) | (m[mlen - 1] >> mblr))97& 0x7FFFFFFF;98}99100/*101* We estimate a divisor q. If the quotient returned by br_div()102* is g:103* -- If a0 == b0 then g == 0; we want q = 0x7FFFFFFF.104* -- Otherwise:105* -- if g == 0 then we set q = 0;106* -- otherwise, we set q = g - 1.107* The properties described above then ensure that the true108* quotient is q-1, q or q+1.109*110* Take care that a0, a1 and b0 are 31-bit words, not 32-bit. We111* must adjust the parameters to br_div() accordingly.112*/113g = br_div(a0 >> 1, a1 | (a0 << 31), b0);114q = MUX(EQ(a0, b0), 0x7FFFFFFF, MUX(EQ(g, 0), 0, g - 1));115116/*117* We subtract q*m from x (with the extra high word of value 'hi').118* Since q may be off by 1 (in either direction), we may have to119* add or subtract m afterwards.120*121* The 'tb' flag will be true (1) at the end of the loop if the122* result is greater than or equal to the modulus (not counting123* 'hi' or the carry).124*/125cc = 0;126tb = 1;127for (u = 1; u <= mlen; u ++) {128uint32_t mw, zw, xw, nxw;129uint64_t zl;130131mw = m[u];132zl = MUL31(mw, q) + cc;133cc = (uint32_t)(zl >> 31);134zw = (uint32_t)zl & (uint32_t)0x7FFFFFFF;135xw = x[u];136nxw = xw - zw;137cc += nxw >> 31;138nxw &= 0x7FFFFFFF;139x[u] = nxw;140tb = MUX(EQ(nxw, mw), tb, GT(nxw, mw));141}142143/*144* If we underestimated q, then either cc < hi (one extra bit145* beyond the top array word), or cc == hi and tb is true (no146* extra bit, but the result is not lower than the modulus). In147* these cases we must subtract m once.148*149* Otherwise, we may have overestimated, which will show as150* cc > hi (thus a negative result). Correction is adding m once.151*/152over = GT(cc, hi);153under = ~over & (tb | LT(cc, hi));154br_i31_add(x, m, over);155br_i31_sub(x, m, under);156}157158159