You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

94 lines
3.4 KiB

  1. /** @brief Field-specific code for $(gf_desc). */
  2. #include "constant_time.h"
  3. #include <string.h>
  4. #include <assert.h>
  5. #include "word.h"
  6. #define __DECAF_$(gf_shortname)_GF_DEFINED__ 1
  7. #define NLIMBS ($(gf_impl_bits/8)/sizeof(word_t))
  8. #define X_SER_BYTES $(((gf_bits-1)/8 + 1))
  9. #define SER_BYTES $(((gf_bits-2)/8 + 1))
  10. typedef struct gf_$(gf_shortname)_s {
  11. word_t limb[NLIMBS];
  12. } __attribute__((aligned(32))) gf_$(gf_shortname)_s, gf_$(gf_shortname)_t[1];
  13. #define GF_LIT_LIMB_BITS $(gf_lit_limb_bits)
  14. #define GF_BITS $(gf_bits)
  15. #define ZERO gf_$(gf_shortname)_ZERO
  16. #define ONE gf_$(gf_shortname)_ONE
  17. #define MODULUS gf_$(gf_shortname)_MODULUS
  18. #define gf gf_$(gf_shortname)_t
  19. #define gf_s gf_$(gf_shortname)_s
  20. #define gf_eq gf_$(gf_shortname)_eq
  21. #define gf_hibit gf_$(gf_shortname)_hibit
  22. #define gf_copy gf_$(gf_shortname)_copy
  23. #define gf_add gf_$(gf_shortname)_add
  24. #define gf_sub gf_$(gf_shortname)_sub
  25. #define gf_add_RAW gf_$(gf_shortname)_add_RAW
  26. #define gf_sub_RAW gf_$(gf_shortname)_sub_RAW
  27. #define gf_bias gf_$(gf_shortname)_bias
  28. #define gf_weak_reduce gf_$(gf_shortname)_weak_reduce
  29. #define gf_strong_reduce gf_$(gf_shortname)_strong_reduce
  30. #define gf_mul gf_$(gf_shortname)_mul
  31. #define gf_sqr gf_$(gf_shortname)_sqr
  32. #define gf_mulw_unsigned gf_$(gf_shortname)_mulw_unsigned
  33. #define gf_isr gf_$(gf_shortname)_isr
  34. #define gf_serialize gf_$(gf_shortname)_serialize
  35. #define gf_deserialize gf_$(gf_shortname)_deserialize
  36. /* RFC 7748 support */
  37. #define X_PUBLIC_BYTES X_SER_BYTES
  38. #define X_PRIVATE_BYTES X_PUBLIC_BYTES
  39. #define X_PRIVATE_BITS $(gf_bits)
  40. #define SQRT_MINUS_ONE P$(gf_shortname)_SQRT_MINUS_ONE /* might not be defined */
  41. #define INLINE_UNUSED __inline__ __attribute__((unused,always_inline))
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. /* Defined below in f_impl.h */
  46. static INLINE_UNUSED void gf_copy (gf out, const gf a) { *out = *a; }
  47. static INLINE_UNUSED void gf_add_RAW (gf out, const gf a, const gf b);
  48. static INLINE_UNUSED void gf_sub_RAW (gf out, const gf a, const gf b);
  49. static INLINE_UNUSED void gf_bias (gf inout, int amount);
  50. static INLINE_UNUSED void gf_weak_reduce (gf inout);
  51. void gf_strong_reduce (gf inout);
  52. void gf_add (gf out, const gf a, const gf b);
  53. void gf_sub (gf out, const gf a, const gf b);
  54. void gf_mul (gf_s *__restrict__ out, const gf a, const gf b);
  55. void gf_mulw_unsigned (gf_s *__restrict__ out, const gf a, uint32_t b);
  56. void gf_sqr (gf_s *__restrict__ out, const gf a);
  57. mask_t gf_isr(gf a, const gf x); /** a^2 x = 1, QNR, or 0 if x=0. Return true if successful */
  58. mask_t gf_eq (const gf x, const gf y);
  59. mask_t gf_hibit (const gf x);
  60. void gf_serialize (uint8_t *serial, const gf x,int with_highbit);
  61. mask_t gf_deserialize (gf x, const uint8_t serial[SER_BYTES],int with_highbit);
  62. #ifdef __cplusplus
  63. } /* extern "C" */
  64. #endif
  65. #include "f_impl.h" /* Bring in the inline implementations */
  66. #define P_MOD_8 $(modulus % 8)
  67. #if P_MOD_8 == 5
  68. static const gf SQRT_MINUS_ONE = {FIELD_LITERAL( /* TODO make not static */
  69. $(ser(msqrt(-1,modulus),gf_lit_limb_bits) if modulus % 4 == 1 else "/* NOPE */")
  70. )};
  71. #endif
  72. #ifndef LIMBPERM
  73. #define LIMBPERM(i) (i)
  74. #endif
  75. #define LIMB_MASK(i) (((1ull)<<LIMB_PLACE_VALUE(i))-1)
  76. static const gf ZERO = {{{0}}}, ONE = {{{ [LIMBPERM(0)] = 1 }}};