Path: blob/main/contrib/bearssl/src/int/i32_montmul.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_i32_montymul(uint32_t *d, const uint32_t *x, const uint32_t *y,29const uint32_t *m, uint32_t m0i)30{31size_t len, u, v;32uint64_t dh;3334len = (m[0] + 31) >> 5;35br_i32_zero(d, m[0]);36dh = 0;37for (u = 0; u < len; u ++) {38uint32_t f, xu;39uint64_t r1, r2, zh;4041xu = x[u + 1];42f = (d[1] + x[u + 1] * y[1]) * m0i;43r1 = 0;44r2 = 0;45for (v = 0; v < len; v ++) {46uint64_t z;47uint32_t t;4849z = (uint64_t)d[v + 1] + MUL(xu, y[v + 1]) + r1;50r1 = z >> 32;51t = (uint32_t)z;52z = (uint64_t)t + MUL(f, m[v + 1]) + r2;53r2 = z >> 32;54if (v != 0) {55d[v] = (uint32_t)z;56}57}58zh = dh + r1 + r2;59d[len] = (uint32_t)zh;60dh = zh >> 32;61}6263/*64* d[] may still be greater than m[] at that point; notably, the65* 'dh' word may be non-zero.66*/67br_i32_sub(d, m, NEQ(dh, 0) | NOT(br_i32_sub(d, m, 0)));68}697071