f_impl.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2014-2016 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. #ifndef OSSL_CRYPTO_EC_CURVE448_ARCH_32_F_IMPL_H
  13. # define OSSL_CRYPTO_EC_CURVE448_ARCH_32_F_IMPL_H
  14. # define GF_HEADROOM 2
  15. # define LIMB(x) ((x) & ((1 << 28) - 1)), ((x) >> 28)
  16. # define FIELD_LITERAL(a, b, c, d, e, f, g, h) \
  17. {{LIMB(a), LIMB(b), LIMB(c), LIMB(d), LIMB(e), LIMB(f), LIMB(g), LIMB(h)}}
  18. # define LIMB_PLACE_VALUE(i) 28
  19. void gf_add_RAW(gf out, const gf a, const gf b)
  20. {
  21. unsigned int i;
  22. for (i = 0; i < NLIMBS; i++)
  23. out->limb[i] = a->limb[i] + b->limb[i];
  24. }
  25. void gf_sub_RAW(gf out, const gf a, const gf b)
  26. {
  27. unsigned int i;
  28. for (i = 0; i < NLIMBS; i++)
  29. out->limb[i] = a->limb[i] - b->limb[i];
  30. }
  31. void gf_bias(gf a, int amt)
  32. {
  33. unsigned int i;
  34. uint32_t co1 = ((1 << 28) - 1) * amt, co2 = co1 - amt;
  35. for (i = 0; i < NLIMBS; i++)
  36. a->limb[i] += (i == NLIMBS / 2) ? co2 : co1;
  37. }
  38. void gf_weak_reduce(gf a)
  39. {
  40. uint32_t mask = (1 << 28) - 1;
  41. uint32_t tmp = a->limb[NLIMBS - 1] >> 28;
  42. unsigned int i;
  43. a->limb[NLIMBS / 2] += tmp;
  44. for (i = NLIMBS - 1; i > 0; i--)
  45. a->limb[i] = (a->limb[i] & mask) + (a->limb[i - 1] >> 28);
  46. a->limb[0] = (a->limb[0] & mask) + tmp;
  47. }
  48. #endif /* OSSL_CRYPTO_EC_CURVE448_ARCH_32_F_IMPL_H */