1/*
2 * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2014 Cryptography Research, Inc.
4 *
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
9 *
10 * Originally written by Mike Hamburg
11 */
12
13#include "field.h"
14
15void gf_mul(gf_s * RESTRICT cs, const gf as, const gf bs)
16{
17 const uint32_t *a = as->limb, *b = bs->limb;
18 uint32_t *c = cs->limb;
19 uint64_t accum0 = 0, accum1 = 0, accum2 = 0;
20 uint32_t mask = (1 << 28) - 1;
21 uint32_t aa[8], bb[8];
22 int i, j;
23
24 for (i = 0; i < 8; i++) {
25 aa[i] = a[i] + a[i + 8];
26 bb[i] = b[i] + b[i + 8];
27 }
28
29 for (j = 0; j < 8; j++) {
30 accum2 = 0;
31 for (i = 0; i < j + 1; i++) {
32 accum2 += widemul(a[j - i], b[i]);
33 accum1 += widemul(aa[j - i], bb[i]);
34 accum0 += widemul(a[8 + j - i], b[8 + i]);
35 }
36 accum1 -= accum2;
37 accum0 += accum2;
38 accum2 = 0;
39 for (i = j + 1; i < 8; i++) {
40 accum0 -= widemul(a[8 + j - i], b[i]);
41 accum2 += widemul(aa[8 + j - i], bb[i]);
42 accum1 += widemul(a[16 + j - i], b[8 + i]);
43 }
44 accum1 += accum2;
45 accum0 += accum2;
46 c[j] = ((uint32_t)(accum0)) & mask;
47 c[j + 8] = ((uint32_t)(accum1)) & mask;
48 accum0 >>= 28;
49 accum1 >>= 28;
50 }
51
52 accum0 += accum1;
53 accum0 += c[8];
54 accum1 += c[0];
55 c[8] = ((uint32_t)(accum0)) & mask;
56 c[0] = ((uint32_t)(accum1)) & mask;
57
58 accum0 >>= 28;
59 accum1 >>= 28;
60 c[9] += ((uint32_t)(accum0));
61 c[1] += ((uint32_t)(accum1));
62}
63
64void gf_mulw_unsigned(gf_s * RESTRICT cs, const gf as, uint32_t b)
65{
66 const uint32_t *a = as->limb;
67 uint32_t *c = cs->limb;
68 uint64_t accum0 = 0, accum8 = 0;
69 uint32_t mask = (1 << 28) - 1;
70 int i;
71
72 assert(b <= mask);
73
74 for (i = 0; i < 8; i++) {
75 accum0 += widemul(b, a[i]);
76 accum8 += widemul(b, a[i + 8]);
77 c[i] = accum0 & mask;
78 accum0 >>= 28;
79 c[i + 8] = accum8 & mask;
80 accum8 >>= 28;
81 }
82
83 accum0 += accum8 + c[8];
84 c[8] = ((uint32_t)accum0) & mask;
85 c[9] += (uint32_t)(accum0 >> 28);
86
87 accum8 += c[0];
88 c[0] = ((uint32_t)accum8) & mask;
89 c[1] += (uint32_t)(accum8 >> 28);
90}
91
92void gf_sqr(gf_s * RESTRICT cs, const gf as)
93{
94 gf_mul(cs, as, as); /* Performs better with a dedicated square */
95}
96