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.
 
 
 
 
 

106 lines
3.3 KiB

  1. /** @brief Decaf global constant table precomputation. */
  2. #define _XOPEN_SOURCE 600 /* for posix_memalign */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "field.h"
  6. #include "f_field.h"
  7. #include "decaf.h"
  8. #define API_NS(_id) $(c_ns)_##_id
  9. static const unsigned char base_point_ser_for_pregen[SER_BYTES] = {
  10. $(ser(msqrt(mont_base,modulus),8))
  11. };
  12. /* To satisfy linker. */
  13. const gf API_NS(precomputed_base_as_fe)[1];
  14. const API_NS(point_t) API_NS(point_base);
  15. struct niels_s;
  16. const gf_s *API_NS(precomputed_wnaf_as_fe);
  17. extern const size_t API_NS(sizeof_precomputed_wnafs);
  18. void API_NS(precompute_wnafs) (
  19. struct niels_s *out,
  20. const API_NS(point_t) base
  21. );
  22. static void field_print(const gf f) { /* UNIFY */
  23. unsigned char ser[X_SER_BYTES];
  24. gf_serialize(ser,f,1);
  25. int b=0, i, comma=0;
  26. unsigned long long limb = 0;
  27. printf("{FIELD_LITERAL(");
  28. for (i=0; i<X_SER_BYTES; i++) {
  29. limb |= ((uint64_t)ser[i])<<b;
  30. b += 8;
  31. if (b >= GF_LIT_LIMB_BITS || i == SER_BYTES-1) {
  32. limb &= (1ull<<GF_LIT_LIMB_BITS) -1;
  33. b -= GF_LIT_LIMB_BITS;
  34. if (comma) printf(",");
  35. comma = 1;
  36. printf("0x%016llx", limb);
  37. limb = ((uint64_t)ser[i])>>(8-b);
  38. }
  39. }
  40. printf(")}");
  41. assert(b<8);
  42. }
  43. int main(int argc, char **argv) {
  44. (void)argc; (void)argv;
  45. API_NS(point_t) real_point_base;
  46. int ret = API_NS(point_decode)(real_point_base,base_point_ser_for_pregen,0);
  47. if (ret != DECAF_SUCCESS) return 1;
  48. API_NS(precomputed_s) *pre;
  49. ret = posix_memalign((void**)&pre, API_NS(alignof_precomputed_s), API_NS(sizeof_precomputed_s));
  50. if (ret || !pre) return 1;
  51. API_NS(precompute)(pre, real_point_base);
  52. struct niels_s *preWnaf;
  53. ret = posix_memalign((void**)&preWnaf, API_NS(alignof_precomputed_s), API_NS(sizeof_precomputed_wnafs));
  54. if (ret || !preWnaf) return 1;
  55. API_NS(precompute_wnafs)(preWnaf, real_point_base);
  56. const gf_s *output;
  57. unsigned i;
  58. printf("/** @warning: this file was automatically generated. */\n");
  59. printf("#include \"field.h\"\n\n");
  60. printf("#include <decaf.h>\n\n");
  61. printf("#define API_NS(_id) $(c_ns)_##_id\n");
  62. output = (const gf_s *)real_point_base;
  63. printf("const API_NS(point_t) API_NS(point_base) = {{\n");
  64. for (i=0; i < sizeof(API_NS(point_t)); i+=sizeof(gf)) {
  65. if (i) printf(",\n ");
  66. field_print(output++);
  67. }
  68. printf("\n}};\n");
  69. output = (const gf_s *)pre;
  70. printf("const gf API_NS(precomputed_base_as_fe)[%d]\n",
  71. (int)(API_NS(sizeof_precomputed_s) / sizeof(gf)));
  72. printf("__attribute__((aligned(%d),visibility(\"hidden\"))) = {\n ", (int)API_NS(alignof_precomputed_s));
  73. for (i=0; i < API_NS(sizeof_precomputed_s); i+=sizeof(gf)) {
  74. if (i) printf(",\n ");
  75. field_print(output++);
  76. }
  77. printf("\n};\n");
  78. output = (const gf_s *)preWnaf;
  79. printf("const gf API_NS(precomputed_wnaf_as_fe)[%d]\n",
  80. (int)(API_NS(sizeof_precomputed_wnafs) / sizeof(gf)));
  81. printf("__attribute__((aligned(%d),visibility(\"hidden\"))) = {\n ", (int)API_NS(alignof_precomputed_s));
  82. for (i=0; i < API_NS(sizeof_precomputed_wnafs); i+=sizeof(gf)) {
  83. if (i) printf(",\n ");
  84. field_print(output++);
  85. }
  86. printf("\n};\n");
  87. return 0;
  88. }