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.
 
 
 
 
 

242 lines
4.1 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. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. static __inline__ void
  16. p448_set_ui (
  17. p448_t *out,
  18. uint64_t x
  19. ) __attribute__((unused,always_inline));
  20. static __inline__ void
  21. p448_add (
  22. p448_t *out,
  23. const p448_t *a,
  24. const p448_t *b
  25. ) __attribute__((unused,always_inline));
  26. static __inline__ void
  27. p448_sub (
  28. p448_t *out,
  29. const p448_t *a,
  30. const p448_t *b
  31. ) __attribute__((unused,always_inline));
  32. static __inline__ void
  33. p448_neg (
  34. p448_t *out,
  35. const p448_t *a
  36. ) __attribute__((unused,always_inline));
  37. static __inline__ void
  38. p448_addw (
  39. p448_t *a,
  40. uint32_t x
  41. ) __attribute__((unused,always_inline));
  42. static __inline__ void
  43. p448_subw (
  44. p448_t *a,
  45. uint32_t x
  46. ) __attribute__((unused,always_inline));
  47. static __inline__ void
  48. p448_copy (
  49. p448_t *out,
  50. const p448_t *a
  51. ) __attribute__((unused,always_inline));
  52. static __inline__ void
  53. p448_weak_reduce (
  54. p448_t *inout
  55. ) __attribute__((unused,always_inline));
  56. void
  57. p448_strong_reduce (
  58. p448_t *inout
  59. );
  60. mask_t
  61. p448_is_zero (
  62. const p448_t *in
  63. );
  64. static __inline__ void
  65. p448_bias (
  66. p448_t *inout,
  67. int amount
  68. ) __attribute__((unused,always_inline));
  69. void
  70. p448_mul (
  71. p448_t *__restrict__ out,
  72. const p448_t *a,
  73. const p448_t *b
  74. );
  75. void
  76. p448_mulw (
  77. p448_t *__restrict__ out,
  78. const p448_t *a,
  79. uint64_t b
  80. );
  81. void
  82. p448_sqr (
  83. p448_t *__restrict__ out,
  84. const p448_t *a
  85. );
  86. void
  87. p448_serialize (
  88. uint8_t *serial,
  89. const struct p448_t *x
  90. );
  91. mask_t
  92. p448_deserialize (
  93. p448_t *x,
  94. const uint8_t serial[56]
  95. );
  96. /* -------------- Inline functions begin here -------------- */
  97. void
  98. p448_set_ui (
  99. p448_t *out,
  100. uint64_t x
  101. ) {
  102. int i;
  103. out->limb[0] = x & ((1<<28)-1);
  104. out->limb[1] = x>>28;
  105. for (i=2; i<16; i++) {
  106. out->limb[i] = 0;
  107. }
  108. }
  109. void
  110. p448_add (
  111. p448_t *out,
  112. const p448_t *a,
  113. const p448_t *b
  114. ) {
  115. unsigned int i;
  116. for (i=0; i<sizeof(*out)/sizeof(uint32xn_t); i++) {
  117. ((uint32xn_t*)out)[i] = ((const uint32xn_t*)a)[i] + ((const uint32xn_t*)b)[i];
  118. }
  119. /*
  120. unsigned int i;
  121. for (i=0; i<sizeof(*out)/sizeof(out->limb[0]); i++) {
  122. out->limb[i] = a->limb[i] + b->limb[i];
  123. }
  124. */
  125. }
  126. void
  127. p448_sub (
  128. p448_t *out,
  129. const p448_t *a,
  130. const p448_t *b
  131. ) {
  132. unsigned int i;
  133. for (i=0; i<sizeof(*out)/sizeof(uint32xn_t); i++) {
  134. ((uint32xn_t*)out)[i] = ((const uint32xn_t*)a)[i] - ((const uint32xn_t*)b)[i];
  135. }
  136. /*
  137. unsigned int i;
  138. for (i=0; i<sizeof(*out)/sizeof(out->limb[0]); i++) {
  139. out->limb[i] = a->limb[i] - b->limb[i];
  140. }
  141. */
  142. }
  143. void
  144. p448_neg (
  145. p448_t *out,
  146. const p448_t *a
  147. ) {
  148. unsigned int i;
  149. for (i=0; i<sizeof(*out)/sizeof(uint32xn_t); i++) {
  150. ((uint32xn_t*)out)[i] = -((const uint32xn_t*)a)[i];
  151. }
  152. /*
  153. unsigned int i;
  154. for (i=0; i<sizeof(*out)/sizeof(out->limb[0]); i++) {
  155. out->limb[i] = -a->limb[i];
  156. }
  157. */
  158. }
  159. void
  160. p448_addw (
  161. p448_t *a,
  162. uint32_t x
  163. ) {
  164. a->limb[0] += x;
  165. }
  166. void
  167. p448_subw (
  168. p448_t *a,
  169. uint32_t x
  170. ) {
  171. a->limb[0] -= x;
  172. }
  173. void
  174. p448_copy (
  175. p448_t *out,
  176. const p448_t *a
  177. ) {
  178. *out = *a;
  179. }
  180. void
  181. p448_bias (
  182. p448_t *a,
  183. int amt
  184. ) {
  185. uint32_t co1 = ((1ull<<28)-1)*amt, co2 = co1-amt;
  186. uint32x4_t lo = {co1,co1,co1,co1}, hi = {co2,co1,co1,co1};
  187. uint32x4_t *aa = (uint32x4_t*) a;
  188. aa[0] += lo;
  189. aa[1] += lo;
  190. aa[2] += hi;
  191. aa[3] += lo;
  192. }
  193. void
  194. p448_weak_reduce (
  195. p448_t *a
  196. ) {
  197. uint64_t mask = (1ull<<28) - 1;
  198. uint64_t tmp = a->limb[15] >> 28;
  199. int i;
  200. a->limb[8] += tmp;
  201. for (i=15; i>0; i--) {
  202. a->limb[i] = (a->limb[i] & mask) + (a->limb[i-1]>>28);
  203. }
  204. a->limb[0] = (a->limb[0] & mask) + tmp;
  205. }
  206. #ifdef __cplusplus
  207. }; /* extern "C" */
  208. #endif
  209. #endif /* __P448_H__ */