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.3 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_lobit gf_$(gf_shortname)_lobit
  23. #define gf_copy gf_$(gf_shortname)_copy
  24. #define gf_add gf_$(gf_shortname)_add
  25. #define gf_sub gf_$(gf_shortname)_sub
  26. #define gf_add_RAW gf_$(gf_shortname)_add_RAW
  27. #define gf_sub_RAW gf_$(gf_shortname)_sub_RAW
  28. #define gf_bias gf_$(gf_shortname)_bias
  29. #define gf_weak_reduce gf_$(gf_shortname)_weak_reduce
  30. #define gf_strong_reduce gf_$(gf_shortname)_strong_reduce
  31. #define gf_mul gf_$(gf_shortname)_mul
  32. #define gf_sqr gf_$(gf_shortname)_sqr
  33. #define gf_mulw_unsigned gf_$(gf_shortname)_mulw_unsigned
  34. #define gf_isr gf_$(gf_shortname)_isr
  35. #define gf_serialize gf_$(gf_shortname)_serialize
  36. #define gf_deserialize gf_$(gf_shortname)_deserialize
  37. /* RFC 7748 support */
  38. #define X_PUBLIC_BYTES X_SER_BYTES
  39. #define X_PRIVATE_BYTES X_PUBLIC_BYTES
  40. #define X_PRIVATE_BITS $(gf_bits)
  41. #define SQRT_MINUS_ONE P$(gf_shortname)_SQRT_MINUS_ONE /* might not be defined */
  42. #define INLINE_UNUSED __inline__ __attribute__((unused,always_inline))
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /* Defined below in f_impl.h */
  47. static INLINE_UNUSED void gf_copy (gf out, const gf a) { *out = *a; }
  48. static INLINE_UNUSED void gf_add_RAW (gf out, const gf a, const gf b);
  49. static INLINE_UNUSED void gf_sub_RAW (gf out, const gf a, const gf b);
  50. static INLINE_UNUSED void gf_bias (gf inout, int amount);
  51. static INLINE_UNUSED void gf_weak_reduce (gf inout);
  52. void gf_strong_reduce (gf inout);
  53. void gf_add (gf out, const gf a, const gf b);
  54. void gf_sub (gf out, const gf a, const gf b);
  55. void gf_mul (gf_s *__restrict__ out, const gf a, const gf b);
  56. void gf_mulw_unsigned (gf_s *__restrict__ out, const gf a, uint32_t b);
  57. void gf_sqr (gf_s *__restrict__ out, const gf a);
  58. mask_t gf_isr(gf a, const gf x); /** a^2 x = 1, QNR, or 0 if x=0. Return true if successful */
  59. mask_t gf_eq (const gf x, const gf y);
  60. mask_t gf_lobit (const gf x);
  61. mask_t gf_hibit (const gf x);
  62. void gf_serialize (uint8_t *serial, const gf x,int with_highbit);
  63. mask_t gf_deserialize (gf x, const uint8_t serial[SER_BYTES],int with_highbit);
  64. #ifdef __cplusplus
  65. } /* extern "C" */
  66. #endif
  67. #include "f_impl.h" /* Bring in the inline implementations */
  68. #define P_MOD_8 $(modulus % 8)
  69. #if P_MOD_8 == 5
  70. extern const gf SQRT_MINUS_ONE;
  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 }}};