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