2017-12-01 18:12:25 +00:00
|
|
|
/*
|
2018-01-18 13:12:46 +00:00
|
|
|
* Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2017-12-01 18:12:25 +00:00
|
|
|
* Copyright 2014 Cryptography Research, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*
|
|
|
|
* Originally written by Mike Hamburg
|
2017-11-15 15:27:21 +00:00
|
|
|
*/
|
|
|
|
|
2017-12-05 17:09:39 +00:00
|
|
|
#include "field.h"
|
2017-11-15 15:27:21 +00:00
|
|
|
|
2018-01-18 14:15:46 +00:00
|
|
|
#if (defined(__OPTIMIZE__) && !defined(__OPTIMIZE_SIZE__) && !defined(I_HATE_UNROLLED_LOOPS)) \
|
2017-12-05 16:37:57 +00:00
|
|
|
|| defined(C448_FORCE_UNROLL)
|
2017-12-04 11:38:58 +00:00
|
|
|
# define REPEAT8(_x) _x _x _x _x _x _x _x _x
|
|
|
|
# define FOR_LIMB(_i,_start,_end,_x) do { _i=_start; REPEAT8( if (_i<_end) { _x; } _i++;) } while (0)
|
2017-11-15 15:27:21 +00:00
|
|
|
#else
|
2017-12-04 11:38:58 +00:00
|
|
|
# define FOR_LIMB(_i,_start,_end,_x) do { for (_i=_start; _i<_end; _i++) _x; } while (0)
|
2017-11-15 15:27:21 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-12 14:17:40 +00:00
|
|
|
void gf_mul(gf_s * RESTRICT cs, const gf as, const gf bs)
|
2017-12-04 11:38:58 +00:00
|
|
|
{
|
2017-11-15 15:27:21 +00:00
|
|
|
const uint32_t *a = as->limb, *b = bs->limb;
|
|
|
|
uint32_t *c = cs->limb;
|
|
|
|
uint64_t accum0 = 0, accum1 = 0, accum2 = 0;
|
2017-12-04 11:38:58 +00:00
|
|
|
uint32_t mask = (1 << 28) - 1;
|
2017-11-15 15:27:21 +00:00
|
|
|
uint32_t aa[8], bb[8];
|
2017-12-04 11:38:58 +00:00
|
|
|
int i, j;
|
2017-12-04 13:30:53 +00:00
|
|
|
|
2017-12-04 11:38:58 +00:00
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
aa[i] = a[i] + a[i + 8];
|
|
|
|
bb[i] = b[i] + b[i + 8];
|
|
|
|
}
|
2017-11-15 15:27:21 +00:00
|
|
|
|
2017-12-04 11:38:58 +00:00
|
|
|
FOR_LIMB(j, 0, 8, {
|
|
|
|
accum2 = 0;
|
|
|
|
FOR_LIMB(i, 0, j + 1, {
|
|
|
|
accum2 += widemul(a[j - i], b[i]);
|
|
|
|
accum1 += widemul(aa[j - i], bb[i]);
|
|
|
|
accum0 += widemul(a[8 + j - i], b[8 + i]);
|
|
|
|
}
|
|
|
|
); accum1 -= accum2; accum0 += accum2;
|
|
|
|
accum2 = 0;
|
|
|
|
FOR_LIMB(i, j + 1, 8, {
|
|
|
|
accum0 -=
|
|
|
|
widemul(a[8 + j - i], b[i]);
|
|
|
|
accum2 +=
|
|
|
|
widemul(aa[8 + j - i],
|
|
|
|
bb[i]);
|
|
|
|
accum1 += widemul(a[16 + j - i], b[8 + i]);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
accum1 += accum2;
|
|
|
|
accum0 += accum2;
|
|
|
|
c[j] = ((uint32_t)(accum0)) & mask;
|
|
|
|
c[j + 8] = ((uint32_t)(accum1)) & mask;
|
|
|
|
accum0 >>= 28; accum1 >>= 28;
|
|
|
|
});
|
2017-11-15 15:27:21 +00:00
|
|
|
|
|
|
|
accum0 += accum1;
|
|
|
|
accum0 += c[8];
|
|
|
|
accum1 += c[0];
|
|
|
|
c[8] = ((uint32_t)(accum0)) & mask;
|
|
|
|
c[0] = ((uint32_t)(accum1)) & mask;
|
2017-12-04 11:38:58 +00:00
|
|
|
|
2017-11-15 15:27:21 +00:00
|
|
|
accum0 >>= 28;
|
|
|
|
accum1 >>= 28;
|
|
|
|
c[9] += ((uint32_t)(accum0));
|
|
|
|
c[1] += ((uint32_t)(accum1));
|
|
|
|
}
|
|
|
|
|
2017-12-12 14:17:40 +00:00
|
|
|
void gf_mulw_unsigned(gf_s * RESTRICT cs, const gf as, uint32_t b)
|
2017-12-04 11:38:58 +00:00
|
|
|
{
|
2017-11-15 15:27:21 +00:00
|
|
|
const uint32_t *a = as->limb;
|
|
|
|
uint32_t *c = cs->limb;
|
|
|
|
uint64_t accum0 = 0, accum8 = 0;
|
2017-12-04 11:38:58 +00:00
|
|
|
uint32_t mask = (1 << 28) - 1;
|
2017-11-15 15:27:21 +00:00
|
|
|
int i;
|
2018-01-18 12:55:23 +00:00
|
|
|
|
2017-12-04 11:38:58 +00:00
|
|
|
assert(b < 1 << 28);
|
2018-01-18 12:55:23 +00:00
|
|
|
|
2017-12-04 11:38:58 +00:00
|
|
|
FOR_LIMB(i, 0, 8, {
|
|
|
|
accum0 += widemul(b, a[i]); accum8 += widemul(b, a[i + 8]);
|
|
|
|
c[i] = accum0 & mask; accum0 >>= 28;
|
|
|
|
c[i + 8] = accum8 & mask; accum8 >>= 28;
|
|
|
|
});
|
2017-11-15 15:27:21 +00:00
|
|
|
|
|
|
|
accum0 += accum8 + c[8];
|
2017-12-12 14:17:40 +00:00
|
|
|
c[8] = ((uint32_t)accum0) & mask;
|
|
|
|
c[9] += (uint32_t)(accum0 >> 28);
|
2017-11-15 15:27:21 +00:00
|
|
|
|
|
|
|
accum8 += c[0];
|
2017-12-12 14:17:40 +00:00
|
|
|
c[0] = ((uint32_t)accum8) & mask;
|
|
|
|
c[1] += (uint32_t)(accum8 >> 28);
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 14:17:40 +00:00
|
|
|
void gf_sqr(gf_s * RESTRICT cs, const gf as)
|
2017-12-04 11:38:58 +00:00
|
|
|
{
|
|
|
|
gf_mul(cs, as, as); /* Performs better with a dedicated square */
|
2017-11-15 15:27:21 +00:00
|
|
|
}
|