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