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.
 
 
 
 
 

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