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.

184 lines
4.2 KiB

  1. #include "<%=name.toLowerCase()+".h"%>"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <gmp.h>
  5. #include <assert.h>
  6. mpz_t q;
  7. mpz_t zero;
  8. mpz_t one;
  9. mpz_t mask;
  10. size_t nBits;
  11. void <%=name%>_toMpz(mpz_t r, P<%=name%>Element pE) {
  12. <%=name%>_toNormal(pE);
  13. if (!(pE->type & <%=name%>_LONG)) {
  14. mpz_set_si(r, pE->shortVal);
  15. if (pE->shortVal<0) {
  16. mpz_add(r, r, q);
  17. }
  18. } else {
  19. mpz_import(r, <%=name%>_N64, -1, 8, -1, 0, (const void *)pE->longVal);
  20. }
  21. }
  22. void <%=name%>_fromMpz(P<%=name%>Element pE, mpz_t v) {
  23. if (mpz_fits_sint_p(v)) {
  24. pE->type = <%=name%>_SHORT;
  25. pE->shortVal = mpz_get_si(v);
  26. } else {
  27. pE->type = <%=name%>_LONG;
  28. for (int i=0; i<<%=name%>_N64; i++) pE->longVal[i] = 0;
  29. mpz_export((void *)(pE->longVal), NULL, -1, 8, -1, 0, v);
  30. }
  31. }
  32. void <%=name%>_init() {
  33. mpz_init(q);
  34. mpz_import(q, <%=name%>_N64, -1, 8, -1, 0, (const void *)Fr_q.longVal);
  35. mpz_init_set_ui(zero, 0);
  36. mpz_init_set_ui(one, 1);
  37. nBits = mpz_sizeinbase (q, 2);
  38. mpz_init(mask);
  39. mpz_mul_2exp(mask, one, nBits-1);
  40. mpz_sub(mask, mask, one);
  41. }
  42. void <%=name%>_str2element(P<%=name%>Element pE, char const *s) {
  43. mpz_t mr;
  44. mpz_init_set_str(mr, s, 10);
  45. <%=name%>_fromMpz(pE, mr);
  46. }
  47. char *<%=name%>_element2str(P<%=name%>Element pE) {
  48. mpz_t r;
  49. if (!(pE->type & <%=name%>_LONG)) {
  50. if (pE->shortVal>=0) {
  51. char *r = new char[32];
  52. sprintf(r, "%d", pE->shortVal);
  53. return r;
  54. } else {
  55. mpz_init_set_si(r, pE->shortVal);
  56. mpz_add(r, r, q);
  57. }
  58. } else {
  59. <%=name%>_toNormal(pE);
  60. mpz_init(r);
  61. mpz_import(r, <%=name%>_N64, -1, 8, -1, 0, (const void *)pE->longVal);
  62. }
  63. char *res = mpz_get_str (0, 10, r);
  64. mpz_clear(r);
  65. return res;
  66. }
  67. void <%=name%>_idiv(P<%=name%>Element r, P<%=name%>Element a, P<%=name%>Element b) {
  68. mpz_t ma;
  69. mpz_t mb;
  70. mpz_t mr;
  71. mpz_init(ma);
  72. mpz_init(mb);
  73. mpz_init(mr);
  74. <%=name%>_toMpz(ma, a);
  75. // char *s1 = mpz_get_str (0, 10, ma);
  76. // printf("s1 %s\n", s1);
  77. <%=name%>_toMpz(mb, b);
  78. // char *s2 = mpz_get_str (0, 10, mb);
  79. // printf("s2 %s\n", s2);
  80. mpz_fdiv_q(mr, ma, mb);
  81. // char *sr = mpz_get_str (0, 10, mr);
  82. // printf("r %s\n", sr);
  83. <%=name%>_fromMpz(r, mr);
  84. }
  85. void <%=name%>_mod(P<%=name%>Element r, P<%=name%>Element a, P<%=name%>Element b) {
  86. mpz_t ma;
  87. mpz_t mb;
  88. mpz_t mr;
  89. mpz_init(ma);
  90. mpz_init(mb);
  91. mpz_init(mr);
  92. <%=name%>_toMpz(ma, a);
  93. <%=name%>_toMpz(mb, b);
  94. mpz_fdiv_r(mr, ma, mb);
  95. <%=name%>_fromMpz(r, mr);
  96. }
  97. void <%=name%>_shl(P<%=name%>Element r, P<%=name%>Element a, P<%=name%>Element b) {
  98. mpz_t ma;
  99. mpz_t mb;
  100. mpz_t mr;
  101. mpz_init(ma);
  102. mpz_init(mb);
  103. mpz_init(mr);
  104. <%=name%>_toMpz(ma, a);
  105. <%=name%>_toMpz(mb, b);
  106. if (mpz_cmp_ui(mb, nBits) >= 0) {
  107. mpz_set(mr, zero);
  108. } else {
  109. mpz_mul_2exp(mr, ma, mpz_get_ui(mb));
  110. mpz_and(mr, mr, mask);
  111. }
  112. <%=name%>_fromMpz(r, mr);
  113. }
  114. void <%=name%>_shr(P<%=name%>Element r, P<%=name%>Element a, P<%=name%>Element b) {
  115. mpz_t ma;
  116. mpz_t mb;
  117. mpz_t mr;
  118. mpz_init(ma);
  119. mpz_init(mb);
  120. mpz_init(mr);
  121. <%=name%>_toMpz(ma, a);
  122. <%=name%>_toMpz(mb, b);
  123. if (mpz_cmp_ui(mb, nBits) >= 0) {
  124. mpz_set(mr, zero);
  125. } else {
  126. mpz_tdiv_q_2exp(mr, ma, mpz_get_ui(mb));
  127. mpz_and(mr, mr, mask);
  128. }
  129. <%=name%>_fromMpz(r, mr);
  130. }
  131. void <%=name%>_pow(P<%=name%>Element r, P<%=name%>Element a, P<%=name%>Element b) {
  132. mpz_t ma;
  133. mpz_t mb;
  134. mpz_t mr;
  135. mpz_init(ma);
  136. mpz_init(mb);
  137. mpz_init(mr);
  138. <%=name%>_toMpz(ma, a);
  139. <%=name%>_toMpz(mb, b);
  140. mpz_powm(mr, ma, mb, q);
  141. <%=name%>_fromMpz(r, mr);
  142. }
  143. void <%=name%>_inv(P<%=name%>Element r, P<%=name%>Element a) {
  144. mpz_t ma;
  145. mpz_t mr;
  146. mpz_init(ma);
  147. mpz_init(mr);
  148. <%=name%>_toMpz(ma, a);
  149. mpz_invert(mr, ma, q);
  150. <%=name%>_fromMpz(r, mr);
  151. }
  152. void <%=name%>_div(P<%=name%>Element r, P<%=name%>Element a, P<%=name%>Element b) {
  153. <%=name%>Element tmp;
  154. <%=name%>_inv(&tmp, b);
  155. <%=name%>_mul(r, a, &tmp);
  156. }
  157. void <%=name%>_fail() {
  158. assert(false);
  159. }