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.
 
 
 
 
 

172 lines
3.3 KiB

  1. /* Copyright (c) 2014 Cryptography Research, Inc.
  2. * Released under the MIT License. See LICENSE.txt for license information.
  3. */
  4. #ifndef __P448_H__
  5. #define __P448_H__ 1
  6. #include "word.h"
  7. #include <stdint.h>
  8. #include <assert.h>
  9. typedef struct p448_t {
  10. uint32_t limb[16];
  11. } __attribute__((aligned(32))) p448_t;
  12. #define LIMBPERM(x) (((x)<<1 | (x)>>3) & 15)
  13. #define USE_NEON_PERM 1
  14. #define LIMBHI(x) ((x##ull)>>LBITS)
  15. #define LIMBLO(x) ((x##ull)&((1ull<<LBITS)-1))
  16. # define FIELD_LITERAL(a,b,c,d,e,f,g,h) \
  17. LIMBLO(a),LIMBLO(e), LIMBHI(a),LIMBHI(e), \
  18. LIMBLO(b),LIMBLO(f), LIMBHI(b),LIMBHI(f), \
  19. LIMBLO(c),LIMBLO(g), LIMBHI(c),LIMBHI(g), \
  20. LIMBLO(d),LIMBLO(h), LIMBHI(d),LIMBHI(h)
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. static __inline__ void
  25. p448_add_RAW (
  26. p448_t *out,
  27. const p448_t *a,
  28. const p448_t *b
  29. ) __attribute__((unused,always_inline));
  30. static __inline__ void
  31. p448_sub_RAW (
  32. p448_t *out,
  33. const p448_t *a,
  34. const p448_t *b
  35. ) __attribute__((unused,always_inline));
  36. static __inline__ void
  37. p448_copy (
  38. p448_t *out,
  39. const p448_t *a
  40. ) __attribute__((unused,always_inline));
  41. static __inline__ void
  42. p448_weak_reduce (
  43. p448_t *inout
  44. ) __attribute__((unused,always_inline));
  45. void
  46. p448_strong_reduce (
  47. p448_t *inout
  48. );
  49. static __inline__ void
  50. p448_bias (
  51. p448_t *inout,
  52. int amount
  53. ) __attribute__((unused,always_inline));
  54. void
  55. p448_mul (
  56. p448_t *__restrict__ out,
  57. const p448_t *a,
  58. const p448_t *b
  59. );
  60. void
  61. p448_mulw (
  62. p448_t *__restrict__ out,
  63. const p448_t *a,
  64. uint64_t b
  65. );
  66. void
  67. p448_sqr (
  68. p448_t *__restrict__ out,
  69. const p448_t *a
  70. );
  71. void
  72. p448_serialize (
  73. uint8_t *serial,
  74. const struct p448_t *x
  75. );
  76. mask_t
  77. p448_deserialize (
  78. p448_t *x,
  79. const uint8_t serial[56]
  80. );
  81. /* -------------- Inline functions begin here -------------- */
  82. void
  83. p448_add_RAW (
  84. p448_t *out,
  85. const p448_t *a,
  86. const p448_t *b
  87. ) {
  88. unsigned int i;
  89. for (i=0; i<sizeof(*out)/sizeof(uint32xn_t); i++) {
  90. ((uint32xn_t*)out)[i] = ((const uint32xn_t*)a)[i] + ((const uint32xn_t*)b)[i];
  91. }
  92. }
  93. void
  94. p448_sub_RAW (
  95. p448_t *out,
  96. const p448_t *a,
  97. const p448_t *b
  98. ) {
  99. unsigned int i;
  100. for (i=0; i<sizeof(*out)/sizeof(uint32xn_t); i++) {
  101. ((uint32xn_t*)out)[i] = ((const uint32xn_t*)a)[i] - ((const uint32xn_t*)b)[i];
  102. }
  103. /*
  104. unsigned int i;
  105. for (i=0; i<sizeof(*out)/sizeof(out->limb[0]); i++) {
  106. out->limb[i] = a->limb[i] - b->limb[i];
  107. }
  108. */
  109. }
  110. void
  111. p448_copy (
  112. p448_t *out,
  113. const p448_t *a
  114. ) {
  115. *out = *a;
  116. }
  117. void
  118. p448_bias (
  119. p448_t *a,
  120. int amt
  121. ) {
  122. uint32_t co1 = ((1ull<<28)-1)*amt, co2 = co1-amt;
  123. uint32x4_t lo = {co1,co2,co1,co1}, hi = {co1,co1,co1,co1};
  124. uint32x4_t *aa = (uint32x4_t*) a;
  125. aa[0] += lo;
  126. aa[1] += hi;
  127. aa[2] += hi;
  128. aa[3] += hi;
  129. }
  130. void
  131. p448_weak_reduce (
  132. p448_t *a
  133. ) {
  134. uint32x2_t *aa = (uint32x2_t*) a, vmask = {(1ull<<28)-1, (1ull<<28)-1}, vm2 = {0,-1},
  135. tmp = vshr_n_u32(aa[7],28);
  136. int i;
  137. for (i=7; i>=1; i--) {
  138. aa[i] = vsra_n_u32(aa[i] & vmask, aa[i-1], 28);
  139. }
  140. aa[0] = (aa[0] & vmask) + vrev64_u32(tmp) + (tmp&vm2);
  141. }
  142. #ifdef __cplusplus
  143. }; /* extern "C" */
  144. #endif
  145. #endif /* __P448_H__ */