gmp_convert.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * File: dclxvi-20130329/gmp_convert.c
  3. * Author: Ruben Niederhagen, Peter Schwabe
  4. * Public Domain
  5. */
  6. #include <math.h>
  7. #include <stdio.h>
  8. #include <gmp.h>
  9. #include "fpe.h"
  10. #include "fp2e.h"
  11. #include "fp6e.h"
  12. #include "fp12e.h"
  13. //#include "parameters.h"
  14. extern const double bn_v;
  15. extern const double bn_v6;
  16. extern const char * bn_pstr;
  17. #ifndef CHECK
  18. static long long ftoll(double arg)
  19. {
  20. return (long long)arg;
  21. }
  22. #endif
  23. void fp2mpz(mpz_t rop, const fpe_t op)
  24. {
  25. mpz_t v, vp, t, p;
  26. mpz_init_set_ui(v, (long)bn_v);
  27. mpz_init_set_str(p, bn_pstr, 10);
  28. mpz_init(t);
  29. mpz_init(vp);
  30. mpz_set_si(rop, ftoll(op->v[0]));
  31. mpz_set_si(t,ftoll(op->v[1]));
  32. mpz_mul(t,t,v);
  33. mpz_mul_si(t,t,6);
  34. mpz_mul(vp,v,v);
  35. mpz_add(rop, rop, t);
  36. mpz_set_si(t,ftoll(op->v[2]));
  37. mpz_mul(t,t,vp);
  38. mpz_mul_si(t,t,6);
  39. mpz_mul(vp,vp,v);
  40. mpz_add(rop, rop, t);
  41. mpz_set_si(t,ftoll(op->v[3]));
  42. mpz_mul(t,t,vp);
  43. mpz_mul_si(t,t,6);
  44. mpz_mul(vp,vp,v);
  45. mpz_add(rop, rop, t);
  46. mpz_set_si(t,ftoll(op->v[4]));
  47. mpz_mul(t,t,vp);
  48. mpz_mul_si(t,t,6);
  49. mpz_mul(vp,vp,v);
  50. mpz_add(rop, rop, t);
  51. mpz_set_si(t,ftoll(op->v[5]));
  52. mpz_mul(t,t,vp);
  53. mpz_mul_si(t,t,6);
  54. mpz_mul(vp,vp,v);
  55. mpz_add(rop, rop, t);
  56. mpz_set_si(t,ftoll(op->v[6]));
  57. mpz_mul(t,t,vp);
  58. mpz_mul_si(t,t,6);
  59. mpz_mul(vp,vp,v);
  60. mpz_add(rop, rop, t);
  61. mpz_set_si(t,ftoll(op->v[7]));
  62. mpz_mul(t,t,vp);
  63. mpz_mul_si(t,t,36);
  64. mpz_mul(vp,vp,v);
  65. mpz_add(rop, rop, t);
  66. mpz_set_si(t,ftoll(op->v[8]));
  67. mpz_mul(t,t,vp);
  68. mpz_mul_si(t,t,36);
  69. mpz_mul(vp,vp,v);
  70. mpz_add(rop, rop, t);
  71. mpz_set_si(t,ftoll(op->v[9]));
  72. mpz_mul(t,t,vp);
  73. mpz_mul_si(t,t,36);
  74. mpz_mul(vp,vp,v);
  75. mpz_add(rop, rop, t);
  76. mpz_set_si(t,ftoll(op->v[10]));
  77. mpz_mul(t,t,vp);
  78. mpz_mul_si(t,t,36);
  79. mpz_mul(vp,vp,v);
  80. mpz_add(rop, rop, t);
  81. mpz_set_si(t,ftoll(op->v[11]));
  82. mpz_mul(t,t,vp);
  83. mpz_mul_si(t,t,36);
  84. mpz_mul(vp,vp,v);
  85. mpz_add(rop, rop, t);
  86. mpz_mod(rop, rop, p);
  87. mpz_clear(v);
  88. mpz_clear(p);
  89. mpz_clear(t);
  90. mpz_clear(vp);
  91. }
  92. void mpz2fp(fpe_t rop, const mpz_t op)
  93. {
  94. mpz_t t, r;
  95. mpz_init(r);
  96. mpz_init_set(t, op);
  97. mpz_tdiv_qr_ui(t, r, t, (long)bn_v6);
  98. rop->v[0] = mpz_get_d(r);
  99. mpz_tdiv_qr_ui(t, r, t, (long)bn_v);
  100. rop->v[1] = mpz_get_d(r);
  101. mpz_tdiv_qr_ui(t, r, t, (long)bn_v);
  102. rop->v[2] = mpz_get_d(r);
  103. mpz_tdiv_qr_ui(t, r, t, (long)bn_v);
  104. rop->v[3] = mpz_get_d(r);
  105. mpz_tdiv_qr_ui(t, r, t, (long)bn_v);
  106. rop->v[4] = mpz_get_d(r);
  107. mpz_tdiv_qr_ui(t, r, t, (long)bn_v);
  108. rop->v[5] = mpz_get_d(r);
  109. mpz_tdiv_qr_ui(t, r, t, (long)bn_v6);
  110. rop->v[6] = mpz_get_d(r);
  111. mpz_tdiv_qr_ui(t, r, t, (long)bn_v);
  112. rop->v[7] = mpz_get_d(r);
  113. mpz_tdiv_qr_ui(t, r, t, (long)bn_v);
  114. rop->v[8] = mpz_get_d(r);
  115. mpz_tdiv_qr_ui(t, r, t, (long)bn_v);
  116. rop->v[9] = mpz_get_d(r);
  117. mpz_tdiv_qr_ui(t, r, t, (long)bn_v);
  118. rop->v[10] = mpz_get_d(r);
  119. //mpz_tdiv_qr_ui(t, r, t, (long)bn_v);
  120. rop->v[11] = mpz_get_d(t);
  121. setmax(rop->v[0],(long)bn_v6/2);
  122. setmax(rop->v[6],(long)bn_v6/2);
  123. setmax(rop->v[1],(long)bn_v/2);
  124. setmax(rop->v[3],(long)bn_v/2);
  125. setmax(rop->v[4],(long)bn_v/2);
  126. setmax(rop->v[7],(long)bn_v/2);
  127. setmax(rop->v[9],(long)bn_v/2);
  128. setmax(rop->v[10],(long)bn_v/2);
  129. setmax(rop->v[2],(long)bn_v);
  130. setmax(rop->v[5],(long)bn_v);
  131. setmax(rop->v[8],(long)bn_v);
  132. setmax(rop->v[11],(long)bn_v);
  133. mpz_clear(r);
  134. mpz_clear(t);
  135. }
  136. void fpe_out_str(FILE *outfile, const fpe_t op)
  137. {
  138. mpz_t t;
  139. mpz_init(t);
  140. fp2mpz(t, op);
  141. mpz_out_str(outfile, 10, t);
  142. mpz_clear(t);
  143. }
  144. void fp2e_out_str(FILE *outfile, const fp2e_t op)
  145. {
  146. fpe_t a, b;
  147. mpz_t ma, mb;
  148. mpz_init(ma);
  149. mpz_init(mb);
  150. int i;
  151. for(i=0;i<12;i++)
  152. {
  153. b->v[i] = op->v[2*i];
  154. a->v[i] = op->v[2*i+1];
  155. }
  156. fp2mpz(ma, a);
  157. fp2mpz(mb, b);
  158. fprintf(outfile, "(");
  159. mpz_out_str(outfile, 10, ma);
  160. fprintf(outfile, "*X + ");
  161. mpz_out_str(outfile, 10, mb);
  162. fprintf(outfile, ")");
  163. mpz_clear(ma);
  164. mpz_clear(mb);
  165. }
  166. void fp6e_out_str(FILE *outfile, const fp6e_t op)
  167. {
  168. fprintf(outfile, "[");
  169. fp2e_out_str(outfile, op->m_a);
  170. fprintf(outfile, " * Y^2 + ");
  171. fp2e_out_str(outfile, op->m_b);
  172. fprintf(outfile, " * Y + ");
  173. fp2e_out_str(outfile, op->m_c);
  174. fprintf(outfile, "]");
  175. }
  176. void fp12e_out_str(FILE *outfile, const fp12e_t op)
  177. {
  178. fp6e_out_str(outfile, op->m_a);
  179. fprintf(outfile, " * Z + ");
  180. fp6e_out_str(outfile, op->m_b);
  181. }
  182. /*
  183. void fp12e_out_magma(FILE *outfile, const fp12e_t op)
  184. {
  185. fpe_t f[12];
  186. fp2e_to_2fpe(f[10],f[11],(op->m_a)->m_a);
  187. fp2e_to_2fpe(f[8],f[9],(op->m_a)->m_b);
  188. fp2e_to_2fpe(f[6],f[7],(op->m_a)->m_c);
  189. fp2e_to_2fpe(f[4],f[5],(op->m_b)->m_a);
  190. fp2e_to_2fpe(f[2],f[3],(op->m_b)->m_b);
  191. fp2e_to_2fpe(f[0],f[1],(op->m_b)->m_c);
  192. int i;
  193. fprintf(outfile, "[");
  194. for(i=0;i<11;i++)
  195. {
  196. fprintf(outfile, "Fp!");
  197. fpe_out_str(outfile, f[i]);
  198. fprintf(outfile,", ");
  199. }
  200. fprintf(outfile, "Fp!");
  201. fpe_out_str(outfile, f[11]);
  202. fprintf(outfile, "]");
  203. }
  204. */
  205. int fp12e_iseq_gmp(const fp12e_t op1, const fp12e_t op2)
  206. {
  207. fpe_t a[12];
  208. fp2e_to_2fpe(a[10],a[11],(op1->m_a)->m_a);
  209. fp2e_to_2fpe(a[8],a[9],(op1->m_a)->m_b);
  210. fp2e_to_2fpe(a[6],a[7],(op1->m_a)->m_c);
  211. fp2e_to_2fpe(a[4],a[5],(op1->m_b)->m_a);
  212. fp2e_to_2fpe(a[2],a[3],(op1->m_b)->m_b);
  213. fp2e_to_2fpe(a[0],a[1],(op1->m_b)->m_c);
  214. fpe_t b[12];
  215. fp2e_to_2fpe(b[10],b[11],(op2->m_a)->m_a);
  216. fp2e_to_2fpe(b[8],b[9],(op2->m_a)->m_b);
  217. fp2e_to_2fpe(b[6],b[7],(op2->m_a)->m_c);
  218. fp2e_to_2fpe(b[4],b[5],(op2->m_b)->m_a);
  219. fp2e_to_2fpe(b[2],b[3],(op2->m_b)->m_b);
  220. fp2e_to_2fpe(b[0],b[1],(op2->m_b)->m_c);
  221. int i;
  222. mpz_t at, bt;
  223. mpz_init(at);
  224. mpz_init(bt);
  225. for(i=0;i<12;i++)
  226. {
  227. fp2mpz(at, a[i]);
  228. fp2mpz(bt, b[i]);
  229. if(mpz_cmp(at,bt)) return 0;
  230. }
  231. mpz_clear(at);
  232. mpz_clear(bt);
  233. return 1;
  234. }
  235. int fp12e_iszero_gmp(const fp12e_t op)
  236. {
  237. fpe_t a[12];
  238. fp2e_to_2fpe(a[10],a[11],(op->m_a)->m_a);
  239. fp2e_to_2fpe(a[8],a[9],(op->m_a)->m_b);
  240. fp2e_to_2fpe(a[6],a[7],(op->m_a)->m_c);
  241. fp2e_to_2fpe(a[4],a[5],(op->m_b)->m_a);
  242. fp2e_to_2fpe(a[2],a[3],(op->m_b)->m_b);
  243. fp2e_to_2fpe(a[0],a[1],(op->m_b)->m_c);
  244. int i;
  245. mpz_t at;
  246. mpz_init(at);
  247. int ret = 1;
  248. for(i=0;i<12;i++)
  249. {
  250. fp2mpz(at, a[i]);
  251. if(mpz_cmp_ui(at,0)) ret = 0;
  252. }
  253. mpz_clear(at);
  254. return ret;
  255. }
  256. int fp12e_isone_gmp(const fp12e_t op)
  257. {
  258. fpe_t a[12];
  259. fp2e_to_2fpe(a[10],a[11],(op->m_a)->m_a);
  260. fp2e_to_2fpe(a[8],a[9],(op->m_a)->m_b);
  261. fp2e_to_2fpe(a[6],a[7],(op->m_a)->m_c);
  262. fp2e_to_2fpe(a[4],a[5],(op->m_b)->m_a);
  263. fp2e_to_2fpe(a[2],a[3],(op->m_b)->m_b);
  264. fp2e_to_2fpe(a[0],a[1],(op->m_b)->m_c);
  265. int i;
  266. mpz_t at;
  267. mpz_init(at);
  268. int ret = 1;
  269. for(i=1;i<12;i++)
  270. {
  271. fp2mpz(at, a[i]);
  272. if(mpz_cmp_ui(at,0)) ret = 0;
  273. }
  274. fp2mpz(at, a[0]);
  275. if(mpz_cmp_ui(at,1)) ret = 0;
  276. mpz_clear(at);
  277. return ret;
  278. }