2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2014-2016 Cryptography Research, Inc.
5 * Licensed under the OpenSSL license (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
10 * Originally written by Mike Hamburg
13 #define GF_HEADROOM 60
14 #define FIELD_LITERAL(a,b,c,d,e,f,g,h) {{a,b,c,d,e,f,g,h}}
15 #define LIMB_PLACE_VALUE(i) 56
17 void gf_add_RAW(gf out, const gf a, const gf b)
19 for (unsigned int i = 0; i < sizeof(*out) / sizeof(uint64xn_t); i++) {
20 ((uint64xn_t *) out)[i] =
21 ((const uint64xn_t *)a)[i] + ((const uint64xn_t *)b)[i];
25 void gf_sub_RAW(gf out, const gf a, const gf b)
27 for (unsigned int i = 0; i < sizeof(*out) / sizeof(uint64xn_t); i++) {
28 ((uint64xn_t *) out)[i] =
29 ((const uint64xn_t *)a)[i] - ((const uint64xn_t *)b)[i];
33 void gf_bias(gf a, int amt)
35 uint64_t co1 = ((1ull << 56) - 1) * amt, co2 = co1 - amt;
38 uint64x4_t lo = { co1, co1, co1, co1 }, hi = {
40 uint64x4_t *aa = (uint64x4_t *) a;
44 uint64x2_t lo = { co1, co1 }, hi = {
46 uint64x2_t *aa = (uint64x2_t *) a;
52 for (unsigned int i = 0; i < sizeof(*a) / sizeof(uint64_t); i++) {
53 a->limb[i] += (i == 4) ? co2 : co1;
58 void gf_weak_reduce(gf a)
60 /* PERF: use pshufb/palignr if anyone cares about speed of this */
61 uint64_t mask = (1ull << 56) - 1;
62 uint64_t tmp = a->limb[7] >> 56;
65 for (unsigned int i = 7; i > 0; i--) {
66 a->limb[i] = (a->limb[i] & mask) + (a->limb[i - 1] >> 56);
68 a->limb[0] = (a->limb[0] & mask) + tmp;