fp6e.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * File: dclxvi-20130329/fp6e.c
  3. * Author: Ruben Niederhagen, Peter Schwabe
  4. * Public Domain
  5. */
  6. #include <stdio.h>
  7. #include <assert.h>
  8. //AVANT
  9. //#include "fp6e.h"
  10. //APRES
  11. #include "mul.h"
  12. extern "C" {
  13. #include "fpe.h"
  14. #include "fp2e.h"
  15. #include "fp6e.h"
  16. }
  17. extern const fp2e_t bn_ypminus1;
  18. extern const fp2e_t bn_ypminus1_squ;
  19. extern const fpe_t bn_zeta;
  20. extern const fpe_t bn_zeta2;
  21. void fp6e_short_coeffred(fp6e_t rop)
  22. {
  23. fp2e_short_coeffred(rop->m_a);
  24. fp2e_short_coeffred(rop->m_b);
  25. fp2e_short_coeffred(rop->m_c);
  26. }
  27. // Set fp6e_t rop to given value:
  28. void fp6e_set(fp6e_t rop, const fp6e_t op)
  29. {
  30. fp2e_set(rop->m_a, op->m_a);
  31. fp2e_set(rop->m_b, op->m_b);
  32. fp2e_set(rop->m_c, op->m_c);
  33. }
  34. // Initialize an fp6e, set to value given in three fp2es
  35. void fp6e_set_fp2e(fp6e_t rop, const fp2e_t a, const fp2e_t b, const fp2e_t c)
  36. {
  37. fp2e_set(rop->m_a, a);
  38. fp2e_set(rop->m_b, b);
  39. fp2e_set(rop->m_c, c);
  40. }
  41. // Set rop to one:
  42. void fp6e_setone(fp6e_t rop)
  43. {
  44. fp2e_setzero(rop->m_a);
  45. fp2e_setzero(rop->m_b);
  46. fp2e_setone(rop->m_c);
  47. }
  48. // Set rop to zero:
  49. void fp6e_setzero(fp6e_t rop)
  50. {
  51. fp2e_setzero(rop->m_a);
  52. fp2e_setzero(rop->m_b);
  53. fp2e_setzero(rop->m_c);
  54. }
  55. // Compare for equality:
  56. int fp6e_iseq(const fp6e_t op1, const fp6e_t op2)
  57. {
  58. int ret = fp2e_iseq(op1->m_a, op2->m_a);
  59. ret = ret && fp2e_iseq(op1->m_b, op2->m_b);
  60. ret = ret && fp2e_iseq(op1->m_c, op2->m_c);
  61. return ret;
  62. }
  63. int fp6e_isone(const fp6e_t op)
  64. {
  65. int ret = fp2e_iszero(op->m_a);
  66. ret = ret && fp2e_iszero(op->m_b);
  67. ret = ret && fp2e_isone(op->m_c);
  68. return ret;
  69. }
  70. int fp6e_iszero(const fp6e_t op)
  71. {
  72. int ret = fp2e_iszero(op->m_a);
  73. ret = ret && fp2e_iszero(op->m_b);
  74. ret = ret && fp2e_iszero(op->m_c);
  75. return ret;
  76. }
  77. void fp6e_cmov(fp6e_t rop, const fp6e_t op, int c)
  78. {
  79. fp2e_cmov(rop->m_a, op->m_a, c);
  80. fp2e_cmov(rop->m_b, op->m_b, c);
  81. fp2e_cmov(rop->m_c, op->m_c, c);
  82. }
  83. // Add two fp6e, store result in rop:
  84. void fp6e_add(fp6e_t rop, const fp6e_t op1, const fp6e_t op2)
  85. {
  86. fp2e_add(rop->m_a, op1->m_a, op2->m_a);
  87. fp2e_add(rop->m_b, op1->m_b, op2->m_b);
  88. fp2e_add(rop->m_c, op1->m_c, op2->m_c);
  89. }
  90. // Subtract op2 from op1, store result in rop:
  91. void fp6e_sub(fp6e_t rop, const fp6e_t op1, const fp6e_t op2)
  92. {
  93. fp2e_sub(rop->m_a, op1->m_a, op2->m_a);
  94. fp2e_sub(rop->m_b, op1->m_b, op2->m_b);
  95. fp2e_sub(rop->m_c, op1->m_c, op2->m_c);
  96. }
  97. // Subtract op2 from op1, store result in rop:
  98. void fp6e_neg(fp6e_t rop, const fp6e_t op)
  99. {
  100. fp2e_neg(rop->m_a, op->m_a);
  101. fp2e_neg(rop->m_b, op->m_b);
  102. fp2e_neg(rop->m_c, op->m_c);
  103. }
  104. // Multiply two fp6e, store result in rop:
  105. void fp6e_mul(fp6e_t rop, const fp6e_t op1, const fp6e_t op2)
  106. {
  107. fp2e_t tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; // Needed for intermediary values
  108. // See "Multiplication and Squaring in Pairing-Friendly Fields", section 4, Karatsuba method
  109. fp2e_mul(tmp3, op1->m_a, op2->m_a);
  110. fp2e_mul(tmp2, op1->m_b, op2->m_b);
  111. fp2e_mul(tmp1, op1->m_c, op2->m_c);
  112. fp2e_add(tmp4, op1->m_a, op1->m_b);
  113. //fp2e_short_coeffred(tmp4);
  114. fp2e_add(tmp5, op2->m_a, op2->m_b);
  115. //fp2e_short_coeffred(tmp5);
  116. fp2e_mul(tmp6, tmp4, tmp5);
  117. fp2e_sub2(tmp6, tmp2);
  118. //fp2e_short_coeffred(tmp6);
  119. fp2e_sub2(tmp6, tmp3);
  120. //fp2e_short_coeffred(tmp6);
  121. fp2e_mulxi(tmp6, tmp6);
  122. fp2e_add2(tmp6, tmp1);
  123. fp2e_add(tmp4, op1->m_b, op1->m_c);
  124. //fp2e_short_coeffred(tmp4);
  125. fp2e_add(tmp5, op2->m_b, op2->m_c);
  126. //fp2e_short_coeffred(tmp5);
  127. fp2e_mul(rop->m_b, tmp4, tmp5);
  128. fp2e_sub2(rop->m_b, tmp1);
  129. fp2e_sub2(rop->m_b, tmp2);
  130. //fp2e_short_coeffred(rop->m_b);
  131. fp2e_mulxi(tmp4, tmp3);
  132. fp2e_add2(rop->m_b, tmp4);
  133. fp2e_short_coeffred(rop->m_b);
  134. fp2e_add(tmp4, op1->m_a, op1->m_c);
  135. //fp2e_short_coeffred(tmp4);
  136. fp2e_add(tmp5, op2->m_a, op2->m_c);
  137. //fp2e_short_coeffred(tmp5);
  138. fp2e_set(rop->m_c, tmp6);
  139. fp2e_short_coeffred(rop->m_c);
  140. fp2e_mul(rop->m_a, tmp4, tmp5);
  141. fp2e_sub2(rop->m_a, tmp1);
  142. fp2e_add2(rop->m_a, tmp2);
  143. fp2e_sub2(rop->m_a, tmp3);
  144. fp2e_short_coeffred(rop->m_a);
  145. }
  146. // Compute the double of the Square of an fp6e, store result in rop, uses Chung-Hasan (CH-SQR3x in pairing-friendly fields)
  147. void fp6e_squaredouble(fp6e_t rop, const fp6e_t op)
  148. {
  149. //fp6e_mul(rop, op, op); //XXX make faster!
  150. fp2e_t s0, s1, s2, s3, s4, t;
  151. fp2e_square(s0,op->m_c);
  152. fp2e_add(t, op->m_a, op->m_c);
  153. fp2e_add(s1, t, op->m_b);
  154. fp2e_short_coeffred(s1);
  155. fp2e_square(s1, s1);
  156. fp2e_sub(s2, t, op->m_b);
  157. fp2e_short_coeffred(s2);
  158. fp2e_square(s2, s2);
  159. fp2e_mul(s3, op->m_a, op->m_b);
  160. fp2e_double(s3, s3);
  161. fp2e_square(s4, op->m_a);
  162. fp2e_mulxi(rop->m_c, s3);
  163. fp2e_add(rop->m_c, rop->m_c, s0);
  164. fp2e_double(rop->m_c, rop->m_c);
  165. fp2e_short_coeffred(rop->m_c);
  166. fp2e_mulxi(rop->m_b, s4);
  167. fp2e_sub(rop->m_b, s3, rop->m_b);
  168. fp2e_double(rop->m_b, rop->m_b);
  169. fp2e_sub(rop->m_b, s1, rop->m_b);
  170. fp2e_sub(rop->m_b, rop->m_b, s2);
  171. fp2e_short_coeffred(rop->m_b);
  172. fp2e_add(rop->m_a, s0, s4);
  173. fp2e_double(rop->m_a, rop->m_a);
  174. fp2e_sub(rop->m_a, s1, rop->m_a);
  175. fp2e_add(rop->m_a, rop->m_a, s2);
  176. fp2e_short_coeffred(rop->m_a);
  177. }
  178. // Multiply with tau:
  179. void fp6e_multau(fp6e_t rop, const fp6e_t op)
  180. {
  181. fp2e_t tmp1;
  182. fp2e_set(tmp1, op->m_b);
  183. fp2e_set(rop->m_b, op->m_c);
  184. fp2e_mulxi(rop->m_c, op->m_a);
  185. fp2e_set(rop->m_a, tmp1);
  186. }
  187. void fp6e_mul_fpe(fp6e_t rop, const fp6e_t op1, const fpe_t op2)
  188. {
  189. fp2e_mul_fpe(rop->m_a, op1->m_a, op2);
  190. fp2e_mul_fpe(rop->m_b, op1->m_b, op2);
  191. fp2e_mul_fpe(rop->m_c, op1->m_c, op2);
  192. }
  193. void fp6e_mul_fp2e(fp6e_t rop, const fp6e_t op1, const fp2e_t op2)
  194. {
  195. fp2e_mul(rop->m_a, op1->m_a, op2);
  196. fp2e_mul(rop->m_b, op1->m_b, op2);
  197. fp2e_mul(rop->m_c, op1->m_c, op2);
  198. }
  199. // Multiply an fp6e by a short fp6e, store result in rop:
  200. // the short fp6e op2 has a2 = 0, i.e. op2 = b2*tau + c2.
  201. void fp6e_mul_shortfp6e(fp6e_t rop, const fp6e_t op1, const fp6e_t op2)
  202. {
  203. fp2e_t tmp1, tmp2, tmp3, tmp4, tmp5; // Needed for intermediary values
  204. fp2e_mul(tmp2, op1->m_b, op2->m_b); // tmp2 = b1*b2
  205. fp2e_mul(tmp1, op1->m_c, op2->m_c); // tmp1 = c1*c2
  206. fp2e_mul(tmp3, op1->m_a, op2->m_b); // tmp3 = a1*b2
  207. fp2e_mulxi(tmp3, tmp3); // tmp3 = a1*b2*xi
  208. fp2e_add(tmp5, tmp3, tmp1); // tmp5 = c1*c2 + a1*b2*xi
  209. fp2e_add(tmp4, op1->m_b, op1->m_c); // tmp4 = b1+c1
  210. //fp2e_short_coeffred(tmp4);
  211. fp2e_add(tmp3, op2->m_b, op2->m_c); // tmp3 = b2+c2
  212. //fp2e_short_coeffred(tmp3);
  213. fp2e_mul(rop->m_b, tmp4, tmp3); // b3 = (b1+c1)*(b2+c2)
  214. fp2e_sub2(rop->m_b, tmp1);
  215. fp2e_sub2(rop->m_b, tmp2); // b3 = b1*c2 + b2*c1
  216. fp2e_short_coeffred(rop->m_b);
  217. fp2e_mul(rop->m_a, op1->m_a, op2->m_c); // a3 = a1*c2
  218. fp2e_add2(rop->m_a, tmp2); // a3 = a1*c2 + b1*b2
  219. fp2e_set(rop->m_c, tmp5); // c3 = c1*c2 + a1*b2*xi
  220. }
  221. void fp6e_invert(fp6e_t rop, const fp6e_t op)
  222. {
  223. fp2e_t tmp1, tmp2, tmp3, tmp4, tmp5; // Needed to store intermediary results
  224. // See "Implementing cryptographic pairings"
  225. fp2e_square(tmp1, op->m_c);
  226. fp2e_mul(tmp5, op->m_a, op->m_b);
  227. fp2e_mulxi(tmp5, tmp5);
  228. fp2e_sub2(tmp1, tmp5); // A
  229. fp2e_short_coeffred(tmp1);
  230. fp2e_square(tmp2, op->m_a);
  231. fp2e_mulxi(tmp2, tmp2);
  232. fp2e_mul(tmp5, op->m_b, op->m_c);
  233. fp2e_sub2(tmp2, tmp5); // B
  234. fp2e_short_coeffred(tmp2);
  235. fp2e_square(tmp3, op->m_b);
  236. fp2e_mul(tmp5, op->m_a, op->m_c);
  237. fp2e_sub2(tmp3, tmp5); // C
  238. //fp2e_short_coeffred(tmp3);
  239. fp2e_mul(tmp4, tmp3, op->m_b);
  240. fp2e_mulxi(tmp4, tmp4);
  241. fp2e_mul(tmp5, tmp1, op->m_c);
  242. fp2e_add2(tmp4, tmp5);
  243. fp2e_mul(tmp5, tmp2, op->m_a);
  244. fp2e_mulxi(tmp5, tmp5);
  245. fp2e_add2(tmp4, tmp5); // F
  246. fp2e_short_coeffred(tmp4);
  247. fp2e_invert(tmp4, tmp4);
  248. fp2e_mul(rop->m_a, tmp3, tmp4);
  249. fp2e_mul(rop->m_b, tmp2, tmp4);
  250. fp2e_mul(rop->m_c, tmp1, tmp4);
  251. }
  252. void fp6e_frobenius_p(fp6e_t rop, const fp6e_t op)
  253. {
  254. fp6e_set(rop, op);
  255. fp2e_conjugate(rop->m_a, rop->m_a);
  256. fp2e_conjugate(rop->m_b, rop->m_b);
  257. fp2e_conjugate(rop->m_c, rop->m_c);
  258. fp2e_mul(rop->m_b, rop->m_b, bn_ypminus1);
  259. fp2e_mul(rop->m_a, rop->m_a, bn_ypminus1_squ);
  260. }
  261. void fp6e_frobenius_p2(fp6e_t rop, const fp6e_t op)
  262. {
  263. fp2e_set(rop->m_c, op->m_c);
  264. fp2e_mul_fpe(rop->m_b, op->m_b, bn_zeta2);
  265. fp2e_mul_fpe(rop->m_a, op->m_a, bn_zeta);
  266. }
  267. // Print the fp6e:
  268. void fp6e_print(FILE * outfile, const fp6e_t op)
  269. {
  270. fprintf(outfile, "[");
  271. fp2e_print(outfile, op->m_a);
  272. fprintf(outfile, " * Y^2\n + ");
  273. fp2e_print(outfile, op->m_b);
  274. fprintf(outfile, " * Y\n + ");
  275. fp2e_print(outfile, op->m_c);
  276. fprintf(outfile, "]");
  277. }