security/nss/lib/freebl/mpi/mpi_hp.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /* This file contains routines that perform vector multiplication. */
michael@0 6
michael@0 7 #include "mpi-priv.h"
michael@0 8 #include <unistd.h>
michael@0 9
michael@0 10 #include <stddef.h>
michael@0 11 /* #include <sys/systeminfo.h> */
michael@0 12 #include <strings.h>
michael@0 13
michael@0 14 extern void multacc512(
michael@0 15 int length, /* doublewords in multiplicand vector. */
michael@0 16 const mp_digit *scalaraddr, /* Address of scalar. */
michael@0 17 const mp_digit *multiplicand, /* The multiplicand vector. */
michael@0 18 mp_digit * result); /* Where to accumulate the result. */
michael@0 19
michael@0 20 extern void maxpy_little(
michael@0 21 int length, /* doublewords in multiplicand vector. */
michael@0 22 const mp_digit *scalaraddr, /* Address of scalar. */
michael@0 23 const mp_digit *multiplicand, /* The multiplicand vector. */
michael@0 24 mp_digit * result); /* Where to accumulate the result. */
michael@0 25
michael@0 26 extern void add_diag_little(
michael@0 27 int length, /* doublewords in input vector. */
michael@0 28 const mp_digit *root, /* The vector to square. */
michael@0 29 mp_digit * result); /* Where to accumulate the result. */
michael@0 30
michael@0 31 void
michael@0 32 s_mpv_sqr_add_prop(const mp_digit *pa, mp_size a_len, mp_digit *ps)
michael@0 33 {
michael@0 34 add_diag_little(a_len, pa, ps);
michael@0 35 }
michael@0 36
michael@0 37 #define MAX_STACK_DIGITS 258
michael@0 38 #define MULTACC512_LEN (512 / MP_DIGIT_BIT)
michael@0 39 #define HP_MPY_ADD_FN (a_len == MULTACC512_LEN ? multacc512 : maxpy_little)
michael@0 40
michael@0 41 /* c = a * b */
michael@0 42 void
michael@0 43 s_mpv_mul_d(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *c)
michael@0 44 {
michael@0 45 mp_digit x[MAX_STACK_DIGITS];
michael@0 46 mp_digit *px = x;
michael@0 47 size_t xSize = 0;
michael@0 48
michael@0 49 if (a == c) {
michael@0 50 if (a_len > MAX_STACK_DIGITS) {
michael@0 51 xSize = sizeof(mp_digit) * (a_len + 2);
michael@0 52 px = malloc(xSize);
michael@0 53 if (!px)
michael@0 54 return;
michael@0 55 }
michael@0 56 memcpy(px, a, a_len * sizeof(*a));
michael@0 57 a = px;
michael@0 58 }
michael@0 59 s_mp_setz(c, a_len + 1);
michael@0 60 HP_MPY_ADD_FN(a_len, &b, a, c);
michael@0 61 if (px != x && px) {
michael@0 62 memset(px, 0, xSize);
michael@0 63 free(px);
michael@0 64 }
michael@0 65 }
michael@0 66
michael@0 67 /* c += a * b, where a is a_len words long. */
michael@0 68 void
michael@0 69 s_mpv_mul_d_add(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *c)
michael@0 70 {
michael@0 71 c[a_len] = 0; /* so carry propagation stops here. */
michael@0 72 HP_MPY_ADD_FN(a_len, &b, a, c);
michael@0 73 }
michael@0 74
michael@0 75 /* c += a * b, where a is y words long. */
michael@0 76 void
michael@0 77 s_mpv_mul_d_add_prop(const mp_digit *a, mp_size a_len, mp_digit b,
michael@0 78 mp_digit *c)
michael@0 79 {
michael@0 80 HP_MPY_ADD_FN(a_len, &b, a, c);
michael@0 81 }
michael@0 82

mercurial