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.
 
 
 
 
 

176 lines
5.6 KiB

  1. /* Copyright (c) 2015 Cryptography Research, Inc.
  2. * Released under the MIT License. See LICENSE.txt for license information.
  3. */
  4. /**
  5. * @file decaf_precompute.c
  6. * @author Mike Hamburg
  7. * @brief Decaf global constant table precomputation.
  8. */
  9. #define _XOPEN_SOURCE 600 /* for posix_memalign */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include "field.h"
  13. #include "decaf.h"
  14. #include "decaf_config.h"
  15. #define GEN_TABLES
  16. #include "curve_data.inc.c"
  17. /* To satisfy linker. */
  18. const gf API_NS(precomputed_base_as_fe)[1];
  19. const API_NS(scalar_t) API_NS(precomputed_scalarmul_adjustment);
  20. const API_NS(scalar_t) API_NS(point_scalarmul_adjustment);
  21. const API_NS(scalar_t) API_NS(sc_r2) = {{{0}}};
  22. const decaf_word_t API_NS(MONTGOMERY_FACTOR) = 0;
  23. const API_NS(point_t) API_NS(point_base);
  24. struct niels_s;
  25. const gf_s *API_NS(precomputed_wnaf_as_fe);
  26. extern const size_t API_NS2(sizeof,precomputed_wnafs);
  27. void API_NS(precompute_wnafs) (
  28. struct niels_s *out,
  29. const API_NS(point_t) base
  30. );
  31. static void scalar_print(const char *name, const API_NS(scalar_t) sc) { /* UNIFY */
  32. printf("const API_NS(scalar_t) %s = {{{\n", name);
  33. const int SCALAR_BYTES = (SCALAR_BITS + 7) / 8;
  34. unsigned char ser[SCALAR_BYTES];
  35. API_NS(scalar_encode)(ser,sc);
  36. int b=0, i, comma=0;
  37. unsigned long long limb = 0;
  38. for (i=0; i<SCALAR_BYTES; i++) {
  39. limb |= ((uint64_t)ser[i])<<b;
  40. b += 8;
  41. if (b == 64 || i==SCALAR_BYTES-1) {
  42. b = 0;
  43. if (comma) printf(",");
  44. comma = 1;
  45. printf("SC_LIMB(0x%016llx)", limb);
  46. limb = ((uint64_t)ser[i])>>(8-b);
  47. }
  48. }
  49. printf("}}};\n\n");
  50. }
  51. static void field_print(const gf f) { /* UNIFY */
  52. const int GF_SER_BYTES = (GF_BITS + 7) / 8;
  53. unsigned char ser[GF_SER_BYTES];
  54. gf_serialize(ser,f);
  55. int b=0, i, comma=0;
  56. unsigned long long limb = 0;
  57. printf("{FIELD_LITERAL(");
  58. for (i=0; i<GF_SER_BYTES; i++) {
  59. limb |= ((uint64_t)ser[i])<<b;
  60. b += 8;
  61. if (b >= GF_LIT_LIMB_BITS || i == GF_SER_BYTES-1) {
  62. limb &= (1ull<<GF_LIT_LIMB_BITS) -1;
  63. b -= GF_LIT_LIMB_BITS;
  64. if (comma) printf(",");
  65. comma = 1;
  66. printf("0x%016llx", limb);
  67. limb = ((uint64_t)ser[i])>>(8-b);
  68. }
  69. }
  70. printf(")}");
  71. assert(b<8);
  72. }
  73. int main(int argc, char **argv) {
  74. (void)argc; (void)argv;
  75. API_NS(point_t) real_point_base;
  76. int ret = API_NS(point_decode)(real_point_base,base_point_ser_for_pregen,0);
  77. if (ret != DECAF_SUCCESS) return 1;
  78. API_NS(precomputed_s) *pre;
  79. ret = posix_memalign((void**)&pre, API_NS2(alignof,precomputed_s), API_NS2(sizeof,precomputed_s));
  80. if (ret || !pre) return 1;
  81. API_NS(precompute)(pre, real_point_base);
  82. struct niels_s *preWnaf;
  83. ret = posix_memalign((void**)&preWnaf, API_NS2(alignof,precomputed_s), API_NS2(sizeof,precomputed_wnafs));
  84. if (ret || !preWnaf) return 1;
  85. API_NS(precompute_wnafs)(preWnaf, real_point_base);
  86. const gf_s *output;
  87. unsigned i;
  88. printf("/** @warning: this file was automatically generated. */\n");
  89. printf("#include \"field.h\"\n\n");
  90. printf("#include <decaf.h>\n\n");
  91. printf("#define API_NS(_id) %s_##_id\n", API_NAME);
  92. printf("#define API_NS2(_pref,_id) _pref##_%s_##_id\n", API_NAME);
  93. output = (const gf_s *)real_point_base;
  94. printf("const API_NS(point_t) API_NS(point_base) = {{\n");
  95. for (i=0; i < sizeof(API_NS(point_t)); i+=sizeof(gf)) {
  96. if (i) printf(",\n ");
  97. field_print(output++);
  98. }
  99. printf("\n}};\n");
  100. output = (const gf_s *)pre;
  101. printf("const gf API_NS(precomputed_base_as_fe)[%d]\n",
  102. (int)(API_NS2(sizeof,precomputed_s) / sizeof(gf)));
  103. printf("__attribute__((aligned(%d),visibility(\"hidden\"))) = {\n ", (int)API_NS2(alignof,precomputed_s));
  104. for (i=0; i < API_NS2(sizeof,precomputed_s); i+=sizeof(gf)) {
  105. if (i) printf(",\n ");
  106. field_print(output++);
  107. }
  108. printf("\n};\n");
  109. output = (const gf_s *)preWnaf;
  110. printf("const gf API_NS(precomputed_wnaf_as_fe)[%d]\n",
  111. (int)(API_NS2(sizeof,precomputed_wnafs) / sizeof(gf)));
  112. printf("__attribute__((aligned(%d),visibility(\"hidden\"))) = {\n ", (int)API_NS2(alignof,precomputed_s));
  113. for (i=0; i < API_NS2(sizeof,precomputed_wnafs); i+=sizeof(gf)) {
  114. if (i) printf(",\n ");
  115. field_print(output++);
  116. }
  117. printf("\n};\n");
  118. API_NS(scalar_t) smadj;
  119. API_NS(scalar_copy)(smadj,API_NS(scalar_one));
  120. for (i=0; i<DECAF_COMBS_N*DECAF_COMBS_T*DECAF_COMBS_S; i++) {
  121. API_NS(scalar_add)(smadj,smadj,smadj);
  122. }
  123. API_NS(scalar_sub)(smadj, smadj, API_NS(scalar_one));
  124. scalar_print("API_NS(precomputed_scalarmul_adjustment)", smadj);
  125. API_NS(scalar_copy)(smadj,API_NS(scalar_one));
  126. for (i=0; i<SCALAR_BITS-1 + DECAF_WINDOW_BITS
  127. - ((SCALAR_BITS-1) % DECAF_WINDOW_BITS); i++) {
  128. API_NS(scalar_add)(smadj,smadj,smadj);
  129. }
  130. API_NS(scalar_sub)(smadj, smadj, API_NS(scalar_one));
  131. scalar_print("API_NS(point_scalarmul_adjustment)", smadj);
  132. API_NS(scalar_copy)(smadj,API_NS(scalar_one));
  133. for (i=0; i<sizeof(API_NS(scalar_t))*8*2; i++) {
  134. API_NS(scalar_add)(smadj,smadj,smadj);
  135. }
  136. scalar_print("API_NS(sc_r2)", smadj);
  137. API_NS(scalar_sub)(smadj,API_NS(scalar_zero),API_NS(scalar_one)); /* get p-1 */
  138. unsigned long long w = 1, plo = smadj->limb[0]+1;
  139. #if DECAF_WORD_BITS == 32
  140. plo |= ((unsigned long long)smadj->limb[1]) << 32;
  141. #endif
  142. for (i=0; i<6; i++) {
  143. w *= w*plo + 2;
  144. }
  145. printf("const decaf_word_t API_NS(MONTGOMERY_FACTOR) = (decaf_word_t)0x%016llxull;\n\n", w);
  146. return 0;
  147. }