Path: blob/main/contrib/bearssl/src/int/i15_montmul.c
39482 views
/*1* Copyright (c) 2017 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_i15_montymul(uint16_t *d, const uint16_t *x, const uint16_t *y,29const uint16_t *m, uint16_t m0i)30{31size_t len, len4, u, v;32uint32_t dh;3334len = (m[0] + 15) >> 4;35len4 = len & ~(size_t)3;36br_i15_zero(d, m[0]);37dh = 0;38for (u = 0; u < len; u ++) {39uint32_t f, xu, r, zh;4041xu = x[u + 1];42f = MUL15((d[1] + MUL15(x[u + 1], y[1])) & 0x7FFF, m0i)43& 0x7FFF;44#if BR_ARMEL_CORTEXM_GCC45if (len4 != 0) {46uint16_t *limit;4748limit = d + len4;49asm volatile (50"\n\51@ carry: r=r2 \n\52@ multipliers: xu=r3 f=r4 \n\53@ base registers: d+v=r5 y+v=r6 m+v=r7 \n\54@ r8 contains 0x7FFF \n\55@ r9 contains d+len4 \n\56ldr r0, %[limit] \n\57ldr r3, %[xu] \n\58mov r9, r0 \n\59ldr r4, %[f] \n\60eor r2, r2 \n\61ldr r5, %[d] \n\62sub r1, r2, #1 \n\63ldr r6, %[y] \n\64lsr r1, r1, #17 \n\65ldr r7, %[m] \n\66mov r8, r1 \n\67loop%=: \n\68ldrh r0, [r6, #2] \n\69ldrh r1, [r7, #2] \n\70mul r0, r3 \n\71mul r1, r4 \n\72add r2, r0, r2 \n\73ldrh r0, [r5, #2] \n\74add r2, r1, r2 \n\75mov r1, r8 \n\76add r2, r0, r2 \n\77and r1, r2 \n\78lsr r2, r2, #15 \n\79strh r1, [r5, #0] \n\80\n\81ldrh r0, [r6, #4] \n\82ldrh r1, [r7, #4] \n\83mul r0, r3 \n\84mul r1, r4 \n\85add r2, r0, r2 \n\86ldrh r0, [r5, #4] \n\87add r2, r1, r2 \n\88mov r1, r8 \n\89add r2, r0, r2 \n\90and r1, r2 \n\91lsr r2, r2, #15 \n\92strh r1, [r5, #2] \n\93\n\94ldrh r0, [r6, #6] \n\95ldrh r1, [r7, #6] \n\96mul r0, r3 \n\97mul r1, r4 \n\98add r2, r0, r2 \n\99ldrh r0, [r5, #6] \n\100add r2, r1, r2 \n\101mov r1, r8 \n\102add r2, r0, r2 \n\103and r1, r2 \n\104lsr r2, r2, #15 \n\105strh r1, [r5, #4] \n\106\n\107ldrh r0, [r6, #8] \n\108ldrh r1, [r7, #8] \n\109mul r0, r3 \n\110mul r1, r4 \n\111add r2, r0, r2 \n\112ldrh r0, [r5, #8] \n\113add r2, r1, r2 \n\114mov r1, r8 \n\115add r2, r0, r2 \n\116and r1, r2 \n\117lsr r2, r2, #15 \n\118strh r1, [r5, #6] \n\119\n\120add r5, r5, #8 \n\121add r6, r6, #8 \n\122add r7, r7, #8 \n\123cmp r5, r9 \n\124bne loop%= \n\125\n\126str r2, %[carry] \n\127"128: [carry] "=m" (r)129: [xu] "m" (xu), [f] "m" (f), [d] "m" (d), [y] "m" (y),130[m] "m" (m), [limit] "m" (limit)131: "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9" );132} else {133r = 0;134}135v = len4;136#else137r = 0;138for (v = 0; v < len4; v += 4) {139uint32_t z;140141z = d[v + 1] + MUL15(xu, y[v + 1])142+ MUL15(f, m[v + 1]) + r;143r = z >> 15;144d[v + 0] = z & 0x7FFF;145z = d[v + 2] + MUL15(xu, y[v + 2])146+ MUL15(f, m[v + 2]) + r;147r = z >> 15;148d[v + 1] = z & 0x7FFF;149z = d[v + 3] + MUL15(xu, y[v + 3])150+ MUL15(f, m[v + 3]) + r;151r = z >> 15;152d[v + 2] = z & 0x7FFF;153z = d[v + 4] + MUL15(xu, y[v + 4])154+ MUL15(f, m[v + 4]) + r;155r = z >> 15;156d[v + 3] = z & 0x7FFF;157}158#endif159for (; v < len; v ++) {160uint32_t z;161162z = d[v + 1] + MUL15(xu, y[v + 1])163+ MUL15(f, m[v + 1]) + r;164r = z >> 15;165d[v + 0] = z & 0x7FFF;166}167168zh = dh + r;169d[len] = zh & 0x7FFF;170dh = zh >> 15;171}172173/*174* Restore the bit length (it was overwritten in the loop above).175*/176d[0] = m[0];177178/*179* d[] may be greater than m[], but it is still lower than twice180* the modulus.181*/182br_i15_sub(d, m, NEQ(dh, 0) | NOT(br_i15_sub(d, m, 0)));183}184185186