Path: blob/main/contrib/bearssl/src/int/i31_montmul.c
39482 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_montymul(uint32_t *d, const uint32_t *x, const uint32_t *y,29const uint32_t *m, uint32_t m0i)30{31/*32* Each outer loop iteration computes:33* d <- (d + xu*y + f*m) / 2^3134* We have xu <= 2^31-1 and f <= 2^31-1.35* Thus, if d <= 2*m-1 on input, then:36* 2*m-1 + 2*(2^31-1)*m <= (2^32)*m-137* and the new d value is less than 2*m.38*39* We represent d over 31-bit words, with an extra word 'dh'40* which can thus be only 0 or 1.41*/42size_t len, len4, u, v;43uint32_t dh;4445len = (m[0] + 31) >> 5;46len4 = len & ~(size_t)3;47br_i31_zero(d, m[0]);48dh = 0;49for (u = 0; u < len; u ++) {50/*51* The carry for each operation fits on 32 bits:52* d[v+1] <= 2^31-153* xu*y[v+1] <= (2^31-1)*(2^31-1)54* f*m[v+1] <= (2^31-1)*(2^31-1)55* r <= 2^32-156* (2^31-1) + 2*(2^31-1)*(2^31-1) + (2^32-1) = 2^63 - 2^3157* After division by 2^31, the new r is then at most 2^32-158*59* Using a 32-bit carry has performance benefits on 32-bit60* systems; however, on 64-bit architectures, we prefer to61* keep the carry (r) in a 64-bit register, thus avoiding some62* "clear high bits" operations.63*/64uint32_t f, xu;65#if BR_6466uint64_t r;67#else68uint32_t r;69#endif7071xu = x[u + 1];72f = MUL31_lo((d[1] + MUL31_lo(x[u + 1], y[1])), m0i);7374r = 0;75for (v = 0; v < len4; v += 4) {76uint64_t z;7778z = (uint64_t)d[v + 1] + MUL31(xu, y[v + 1])79+ MUL31(f, m[v + 1]) + r;80r = z >> 31;81d[v + 0] = (uint32_t)z & 0x7FFFFFFF;82z = (uint64_t)d[v + 2] + MUL31(xu, y[v + 2])83+ MUL31(f, m[v + 2]) + r;84r = z >> 31;85d[v + 1] = (uint32_t)z & 0x7FFFFFFF;86z = (uint64_t)d[v + 3] + MUL31(xu, y[v + 3])87+ MUL31(f, m[v + 3]) + r;88r = z >> 31;89d[v + 2] = (uint32_t)z & 0x7FFFFFFF;90z = (uint64_t)d[v + 4] + MUL31(xu, y[v + 4])91+ MUL31(f, m[v + 4]) + r;92r = z >> 31;93d[v + 3] = (uint32_t)z & 0x7FFFFFFF;94}95for (; v < len; v ++) {96uint64_t z;9798z = (uint64_t)d[v + 1] + MUL31(xu, y[v + 1])99+ MUL31(f, m[v + 1]) + r;100r = z >> 31;101d[v] = (uint32_t)z & 0x7FFFFFFF;102}103104/*105* Since the new dh can only be 0 or 1, the addition of106* the old dh with the carry MUST fit on 32 bits, and107* thus can be done into dh itself.108*/109dh += r;110d[len] = dh & 0x7FFFFFFF;111dh >>= 31;112}113114/*115* We must write back the bit length because it was overwritten in116* the loop (not overwriting it would require a test in the loop,117* which would yield bigger and slower code).118*/119d[0] = m[0];120121/*122* d[] may still be greater than m[] at that point; notably, the123* 'dh' word may be non-zero.124*/125br_i31_sub(d, m, NEQ(dh, 0) | NOT(br_i31_sub(d, m, 0)));126}127128129