final_expo.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #ifndef NEW_PARAMETERS
  2. /*
  3. * File: dclxvi-20130329/final_expo.c
  4. * Author: Ruben Niederhagen, Peter Schwabe
  5. * Public Domain
  6. */
  7. #include <stdio.h>
  8. #include "final_expo.h"
  9. #include "fpe.h"
  10. extern const scalar_t bn_u;
  11. extern const scalar_t bn_v_scalar;
  12. extern const unsigned long bn_u_bitsize;
  13. static void fp12e_powv_special_square(fp12e_t rop, const fp12e_t op)
  14. {
  15. fp12e_t tmp0, tmp1, tmp2;
  16. //XXX Implement
  17. fp12e_special_square_finexp(tmp0,op);
  18. fp12e_special_square_finexp(tmp0,tmp0);
  19. fp12e_special_square_finexp(tmp0,tmp0); // t0 = op^8
  20. fp12e_special_square_finexp(tmp1,tmp0);
  21. fp12e_special_square_finexp(tmp1,tmp1);
  22. fp12e_special_square_finexp(tmp1,tmp1); // t1 = op^64
  23. fp12e_conjugate(tmp2, tmp0); // t2 = op^-8
  24. fp12e_mul(tmp2,tmp2,op); // t2 = op^-7
  25. fp12e_mul(tmp2,tmp2,tmp1); // tmp2 = op^57
  26. fp12e_special_square_finexp(tmp2,tmp2);
  27. fp12e_special_square_finexp(tmp2,tmp2);
  28. fp12e_special_square_finexp(tmp2,tmp2);
  29. fp12e_special_square_finexp(tmp2,tmp2);
  30. fp12e_special_square_finexp(tmp2,tmp2);
  31. fp12e_special_square_finexp(tmp2,tmp2);
  32. fp12e_special_square_finexp(tmp2,tmp2); // tmp2 = op^(2^7*57) = op^7296
  33. fp12e_mul(tmp2,tmp2,op); // tmp2 = op^7297
  34. fp12e_special_square_finexp(tmp2,tmp2);
  35. fp12e_special_square_finexp(tmp2,tmp2);
  36. fp12e_special_square_finexp(tmp2,tmp2);
  37. fp12e_special_square_finexp(tmp2,tmp2);
  38. fp12e_special_square_finexp(tmp2,tmp2);
  39. fp12e_special_square_finexp(tmp2,tmp2);
  40. fp12e_special_square_finexp(tmp2,tmp2);
  41. fp12e_special_square_finexp(tmp2,tmp2); // tmp2 = op^(7297*256) = op^1868032
  42. fp12e_mul(rop,tmp2,op); // rop = op^v
  43. }
  44. static void fp12e_powu_special_square(fp12e_t rop, const fp12e_t op)
  45. {
  46. fp12e_powv_special_square(rop, op);
  47. fp12e_powv_special_square(rop, rop);
  48. fp12e_powv_special_square(rop, rop);
  49. }
  50. void final_expo(fp12e_t rop)
  51. {
  52. /* This all has to change to support scalar_t instead of mpz_t */
  53. // First part: (p^6 - 1)
  54. fp12e_t dummy1, dummy2, fp, fp2, fp3, fu, fu2, fu3, fu2p, fu3p, y0, y1, y2, y3, y4, y5, y6, t0, t1;
  55. fp12e_set(dummy1, rop);
  56. // This is exactly the p^6-Frobenius action:
  57. fp6e_neg(rop->m_a, rop->m_a);
  58. fp12e_invert(dummy2, dummy1);
  59. fp12e_mul(rop, rop, dummy2);
  60. // After this point, rop has norm 1, so we can use
  61. // special squaring and exponentiation.
  62. // Second part: (p^2 + 1)
  63. fp12e_set(dummy1, rop);
  64. fp12e_frobenius_p2(rop, rop);
  65. fp12e_mul(rop, rop, dummy1);
  66. /* Hard part */
  67. fp12e_frobenius_p(fp, rop);
  68. fp12e_frobenius_p2(fp2, rop);
  69. fp12e_frobenius_p(fp3, fp2);
  70. fp12e_powu_special_square(fu, rop);
  71. fp12e_powu_special_square(fu2, fu);
  72. fp12e_powu_special_square(fu3, fu2);
  73. fp12e_frobenius_p(y3, fu);
  74. fp12e_frobenius_p(fu2p, fu2);
  75. fp12e_frobenius_p(fu3p, fu3);
  76. fp12e_frobenius_p2(y2,fu2);
  77. fp12e_mul(y0, fp, fp2);
  78. fp12e_mul(y0, y0, fp3);
  79. fp12e_conjugate(y1, rop);
  80. fp12e_conjugate(y5, fu2);
  81. fp12e_conjugate(y3, y3);
  82. fp12e_mul(y4, fu, fu2p);
  83. fp12e_conjugate(y4, y4);
  84. fp12e_mul(y6, fu3, fu3p);
  85. fp12e_conjugate(y6, y6);
  86. //t0 := fp12square(y6);
  87. fp12e_special_square_finexp(t0, y6);
  88. //t0 := t0*y4;
  89. fp12e_mul(t0, t0, y4);
  90. //t0 := t0*y5;
  91. fp12e_mul(t0, t0, y5);
  92. //t1 := y3*y5;
  93. fp12e_mul(t1, y3, y5);
  94. //t1 := t1*t0;
  95. fp12e_mul(t1, t1, t0);
  96. //t0 := t0*y2;
  97. fp12e_mul(t0, t0, y2);
  98. //t1 := t1^2;
  99. fp12e_special_square_finexp(t1, t1);
  100. //t1 := t1*t0;
  101. fp12e_mul(t1, t1, t0);
  102. //t1 := t1^2;
  103. fp12e_special_square_finexp(t1, t1);
  104. //t0 := t1*y1;
  105. fp12e_mul(t0, t1, y1);
  106. //t1 := t1*y0;
  107. fp12e_mul(t1, t1, y0);
  108. //t0 := t0^2;
  109. fp12e_special_square_finexp(t0, t0);
  110. //t0 := t0*t1;
  111. fp12e_mul(rop, t0, t1);
  112. }
  113. #endif