f_impl32.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright 2017-2023 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. #include "internal/e_os.h"
  13. #include <openssl/macros.h>
  14. #include "internal/numbers.h"
  15. #ifdef UINT128_MAX
  16. /* We have support for 128 bit ints, so do nothing here */
  17. NON_EMPTY_TRANSLATION_UNIT
  18. #else
  19. # include "../field.h"
  20. void ossl_gf_mul(gf_s * RESTRICT cs, const gf as, const gf bs)
  21. {
  22. const uint32_t *a = as->limb, *b = bs->limb;
  23. uint32_t *c = cs->limb;
  24. uint64_t accum0 = 0, accum1 = 0, accum2 = 0;
  25. uint32_t mask = (1 << 28) - 1;
  26. uint32_t aa[8], bb[8];
  27. int i, j;
  28. for (i = 0; i < 8; i++) {
  29. aa[i] = a[i] + a[i + 8];
  30. bb[i] = b[i] + b[i + 8];
  31. }
  32. for (j = 0; j < 8; j++) {
  33. accum2 = 0;
  34. for (i = 0; i < j + 1; i++) {
  35. accum2 += widemul(a[j - i], b[i]);
  36. accum1 += widemul(aa[j - i], bb[i]);
  37. accum0 += widemul(a[8 + j - i], b[8 + i]);
  38. }
  39. accum1 -= accum2;
  40. accum0 += accum2;
  41. accum2 = 0;
  42. for (i = j + 1; i < 8; i++) {
  43. accum0 -= widemul(a[8 + j - i], b[i]);
  44. accum2 += widemul(aa[8 + j - i], bb[i]);
  45. accum1 += widemul(a[16 + j - i], b[8 + i]);
  46. }
  47. accum1 += accum2;
  48. accum0 += accum2;
  49. c[j] = ((uint32_t)(accum0)) & mask;
  50. c[j + 8] = ((uint32_t)(accum1)) & mask;
  51. accum0 >>= 28;
  52. accum1 >>= 28;
  53. }
  54. accum0 += accum1;
  55. accum0 += c[8];
  56. accum1 += c[0];
  57. c[8] = ((uint32_t)(accum0)) & mask;
  58. c[0] = ((uint32_t)(accum1)) & mask;
  59. accum0 >>= 28;
  60. accum1 >>= 28;
  61. c[9] += ((uint32_t)(accum0));
  62. c[1] += ((uint32_t)(accum1));
  63. }
  64. void ossl_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. assert(b <= mask);
  72. for (i = 0; i < 8; i++) {
  73. accum0 += widemul(b, a[i]);
  74. accum8 += widemul(b, a[i + 8]);
  75. c[i] = accum0 & mask;
  76. accum0 >>= 28;
  77. c[i + 8] = accum8 & mask;
  78. accum8 >>= 28;
  79. }
  80. accum0 += accum8 + c[8];
  81. c[8] = ((uint32_t)accum0) & mask;
  82. c[9] += (uint32_t)(accum0 >> 28);
  83. accum8 += c[0];
  84. c[0] = ((uint32_t)accum8) & mask;
  85. c[1] += (uint32_t)(accum8 >> 28);
  86. }
  87. void ossl_gf_sqr(gf_s * RESTRICT cs, const gf as)
  88. {
  89. ossl_gf_mul(cs, as, as); /* Performs better with a dedicated square */
  90. }
  91. #endif