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
2.8 KiB

  1. #include "<%=name.toLowerCase()+".h"%>"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <gmp.h>
  5. void <%=name%>_str2element(P<%=name%>Element pE, char *s) {
  6. mpz_t r;
  7. mpz_init(r);
  8. mpz_set_str(r, s, 10);
  9. pE->type = <%=name%>_LONG;
  10. for (int i=0; i<<%=name%>_N64; i++) pE->longVal[i] = 0;
  11. mpz_export((void *)pE->longVal, NULL, -1, 8, -1, 0, r);
  12. }
  13. char *<%=name%>_element2str(P<%=name%>Element pE) {
  14. mpz_t r;
  15. mpz_t q;
  16. if (!(pE->type & <%=name%>_LONG)) {
  17. if (pE->shortVal>=0) {
  18. char *r = new char[32];
  19. sprintf(r, "%d", pE->shortVal);
  20. return r;
  21. } else {
  22. mpz_init(q);
  23. mpz_import(q, <%=name%>_N64, -1, 8, -1, 0, (const void *)Fr_q.longVal);
  24. mpz_init_set_si(r, pE->shortVal);
  25. mpz_add(r, r, q);
  26. mpz_clear(q);
  27. }
  28. } else {
  29. <%=name%>_toNormal(pE);
  30. mpz_init(r);
  31. mpz_import(r, <%=name%>_N64, -1, 8, -1, 0, (const void *)pE->longVal);
  32. }
  33. char *res = mpz_get_str (0, 10, r);
  34. mpz_clear(r);
  35. return res;
  36. }
  37. void <%=name%>_toMpz(mpz_t r, P<%=name%>Element pE) {
  38. mpz_t q;
  39. <%=name%>_toNormal(pE);
  40. if (!(pE->type & <%=name%>_LONG)) {
  41. mpz_set_si(r, pE->shortVal);
  42. if (pE->shortVal<0) {
  43. mpz_init(q);
  44. mpz_import(q, <%=name%>_N64, -1, 8, -1, 0, (const void *)Fr_q.longVal);
  45. mpz_add(r, r, q);
  46. }
  47. } else {
  48. mpz_import(r, <%=name%>_N64, -1, 8, -1, 0, (const void *)pE->longVal);
  49. }
  50. }
  51. void <%=name%>_fromMpz(P<%=name%>Element pE, mpz_t v) {
  52. if (mpz_fits_sint_p(v)) {
  53. pE->type = <%=name%>_SHORT;
  54. pE->shortVal = mpz_get_si(v);
  55. } else {
  56. pE->type = <%=name%>_LONG;
  57. for (int i=0; i<<%=name%>_N64; i++) pE->longVal[i] = 0;
  58. mpz_export((void *)(pE->longVal), NULL, -1, 8, -1, 0, v);
  59. }
  60. }
  61. void <%=name%>_idiv(P<%=name%>Element r, P<%=name%>Element a, P<%=name%>Element b) {
  62. mpz_t ma;
  63. mpz_t mb;
  64. mpz_t mr;
  65. mpz_init(ma);
  66. mpz_init(mb);
  67. mpz_init(mr);
  68. <%=name%>_toMpz(ma, a);
  69. // char *s1 = mpz_get_str (0, 10, ma);
  70. // printf("s1 %s\n", s1);
  71. <%=name%>_toMpz(mb, b);
  72. // char *s2 = mpz_get_str (0, 10, mb);
  73. // printf("s2 %s\n", s2);
  74. mpz_fdiv_q(mr, ma, mb);
  75. // char *sr = mpz_get_str (0, 10, mr);
  76. // printf("r %s\n", sr);
  77. <%=name%>_fromMpz(r, mr);
  78. }
  79. void <%=name%>_inv(P<%=name%>Element r, P<%=name%>Element a) {
  80. mpz_t ma;
  81. mpz_t mr;
  82. mpz_t q;
  83. mpz_init(ma);
  84. mpz_init(mr);
  85. mpz_init(q);
  86. mpz_import(q, <%=name%>_N64, -1, 8, -1, 0, (const void *)Fr_q.longVal);
  87. <%=name%>_toMpz(ma, a);
  88. mpz_invert(mr, ma, q);
  89. <%=name%>_fromMpz(r, mr);
  90. }
  91. void <%=name%>_div(P<%=name%>Element r, P<%=name%>Element a, P<%=name%>Element b) {
  92. <%=name%>Element tmp;
  93. <%=name%>_inv(&tmp, b);
  94. <%=name%>_mul(r, a, &tmp);
  95. }