fp6e.c 8.1 KB

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