optate.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * File: dclxvi-20130329/optate.c
  3. * Author: Ruben Niederhagen, Peter Schwabe
  4. * Public Domain
  5. */
  6. #include <stdio.h>
  7. //AVANT
  8. //#include "fp2e.h"
  9. //#include "fp6e.h"
  10. //#include "fp12e.h"
  11. //APRES
  12. #include "mul.h"
  13. extern "C" {
  14. #include "fpe.h"
  15. #include "fp2e.h"
  16. #include "fp6e.h"
  17. #include "fp12e.h"
  18. }
  19. #include "curvepoint_fp.h"
  20. #include "twistpoint_fp2.h"
  21. #include "linefunction.h"
  22. #include "optate.h"
  23. #include "final_expo.h"
  24. #include "zout.hpp"
  25. //#include "parameters.h"
  26. extern const unsigned long bn_naflen_6uplus2;
  27. extern const scalar_t bn_6uplus2;
  28. extern const fpe_t bn_zeta2;
  29. extern const fp2e_t bn_z2p;
  30. extern const fp2e_t bn_z3p;
  31. extern const signed char bn_6uplus2_naf[66];
  32. void optate_miller(fp12e_t rop, const twistpoint_fp2_t op1, const curvepoint_fp_t op2)
  33. {
  34. // op1 and op2 are assumed to be in affine coordinates!
  35. twistpoint_fp2_t q1, q2;//, q3;
  36. fp12e_setone(rop);
  37. fp2e_t dummy1, dummy2, dummy3;
  38. fp2e_t tfp2e1, tfp2e2;
  39. twistpoint_fp2_t r, t, mop1;
  40. twistpoint_fp2_set(r, op1);
  41. twistpoint_fp2_neg(mop1, op1);
  42. fp2e_setone(r->m_t); /* As r has to be in affine coordinates this is ok */
  43. fp2e_setone(t->m_t); /* As t has to be in affine coordinates this is ok */
  44. fp2e_t r2;
  45. fp2e_square(r2, op1->m_y);
  46. unsigned int i;
  47. /*
  48. for(i = bn_bitlen_6uplus2 - 1; i > 0; i--)
  49. {
  50. linefunction_double_ate(dummy1, dummy2, dummy3, r, r, op2);
  51. if(i != bn_bitlen_6uplus2 -1) fp12e_square(rop, rop);
  52. fp12e_mul_line(rop, rop, dummy1, dummy2, dummy3);
  53. if (scalar_getbit(bn_6uplus2, i - 1))
  54. {
  55. linefunction_add_ate(dummy1, dummy2, dummy3, r, r, op1, op2, r2);
  56. fp12e_mul_line(rop, rop, dummy1, dummy2, dummy3);
  57. }
  58. }
  59. */
  60. for(i = bn_naflen_6uplus2-1; i > 0; i--)
  61. {
  62. linefunction_double_ate(dummy1, dummy2, dummy3, r, r, op2);
  63. if(i != bn_naflen_6uplus2 -1) fp12e_square(rop, rop);
  64. fp12e_mul_line(rop, rop, dummy1, dummy2, dummy3);
  65. if (bn_6uplus2_naf[i-1]==1)
  66. {
  67. linefunction_add_ate(dummy1, dummy2, dummy3, r, r, op1, op2, r2);
  68. fp12e_mul_line(rop, rop, dummy1, dummy2, dummy3);
  69. }
  70. if (bn_6uplus2_naf[i-1]==-1)
  71. {
  72. linefunction_add_ate(dummy1, dummy2, dummy3, r, r, mop1, op2, r2);
  73. fp12e_mul_line(rop, rop, dummy1, dummy2, dummy3);
  74. }
  75. }
  76. /* Compute Q2 */
  77. fp2e_mul_fpe(tfp2e1, op1->m_x, bn_zeta2);
  78. twistpoint_fp2_affineset_fp2e(q2, tfp2e1, op1->m_y);
  79. /* Compute Q1 */
  80. fp2e_set(tfp2e1, op1->m_x);
  81. fp2e_conjugate(tfp2e1, tfp2e1);
  82. fp2e_mul(tfp2e1, tfp2e1, bn_z2p);
  83. /*
  84. printf("\n");
  85. fp2e_print(stdout, bn_z2p);
  86. printf("\n");
  87. */
  88. fp2e_set(tfp2e2, op1->m_y);
  89. fp2e_conjugate(tfp2e2, tfp2e2);
  90. fp2e_mul(tfp2e2, tfp2e2, bn_z3p);
  91. twistpoint_fp2_affineset_fp2e(q1, tfp2e1, tfp2e2);
  92. /* Compute Q3 */
  93. //fp2e_mul_fpe(tfp2e3, tfp2e1, bn_zeta2);
  94. //fp2e_neg(tfp2e2, tfp2e2);
  95. //twistpoint_fp2_affineset_fp2e(q3, tfp2e3, tfp2e2);
  96. /* Remaining line functions */
  97. fp2e_square(r2, q1->m_y);
  98. linefunction_add_ate(dummy1, dummy2, dummy3, t, r, q1, op2, r2);
  99. fp12e_mul_line(rop, rop, dummy1, dummy2, dummy3);
  100. fp2e_square(r2, q2->m_y);
  101. linefunction_add_ate(dummy1, dummy2, dummy3, t, t, q2, op2, r2);
  102. fp12e_mul_line(rop, rop, dummy1, dummy2, dummy3);
  103. //fp2e_square(r2, q3->m_y);
  104. //linefunction_add_ate(dummy1, dummy2, dummy3, t, t, q3, op2, r2);
  105. //fp12e_mul_line(rop, rop, dummy1, dummy2, dummy3);
  106. }
  107. void optate(fp12e_t rop, const twistpoint_fp2_t op1, const curvepoint_fp_t op2)
  108. {
  109. int retone;
  110. fp12e_t d;
  111. fp12e_setone(d);
  112. optate_miller(rop, op1, op2);
  113. final_expo(rop);
  114. retone = fp2e_iszero(op1->m_z);
  115. retone |= fpe_iszero(op2->m_z);
  116. fp12e_cmov(rop, d, retone);
  117. }