gmp_convert.c 6.7 KB

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