bilintest.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // g++ -lgmp -lgmpxx -I ../dclxvi-20130329 -I ../update -o ../bin/bilintest ../dclxvi-20130329/bilintest.c ../dclxvi-20130329/fpe.c ../dclxvi-20130329/fp2e.c ../dclxvi-20130329/curvepoint_fp.c ../dclxvi-20130329/twistpoint_fp2.c ../dclxvi-20130329/scalar.c ../dclxvi-20130329/parameters.c ../dclxvi-20130329/mul.c ../dclxvi-20130329/mydouble.c ../update/parameters_512.c ../update/fp2e_512.c ../update/scalar_512.c ../update/complete_addition.c ../update/test_functions.cpp ../dclxvi-20130329/optate.c ../dclxvi-20130329/fp6e.c ../dclxvi-20130329/linefunction.c ../dclxvi-20130329/fp12e.c ../dclxvi-20130329/final_expo.c ../update/final_expo_512.c -DNTESTS=10 && ../bin/bilintest
  2. #include <stdio.h>
  3. #include "test_functions.hpp" //ajouté pour la mise à jour vers une clef 512 bits
  4. #include "mydouble.h"
  5. extern "C" {
  6. #include "fpe.h"
  7. #include "fp2e.h"
  8. #include "fp6e.h"
  9. #include "fp12e.h"
  10. }
  11. #include "curvepoint_fp.h"
  12. #include "twistpoint_fp2.h"
  13. #include "optate.h"
  14. #include <unistd.h> //fonction sleep
  15. extern const curvepoint_fp_t bn_curvegen;
  16. extern const twistpoint_fp2_t bn_twistgen;
  17. extern const scalar_t bn_n;
  18. int main(int argc, char* argv[])
  19. {
  20. titre(Bilintest);
  21. //zout(!0,!1,!456465);
  22. //return 0;
  23. fp12e_t e1, e2, e3;
  24. curvepoint_fp_t p1;
  25. twistpoint_fp2_t p2;
  26. scalar_t s1, s2;
  27. int i;
  28. // Test with neutral element as argument
  29. scalar_setrandom(s1, bn_n);
  30. //scalar_print(stdout, s1);
  31. //exit(0);
  32. curvepoint_fp_set(p1, bn_curvegen);
  33. //curvepoint_fp_print(stdout,p1);
  34. //exit(0);
  35. twistpoint_fp2_setneutral(p2);
  36. fpe_isreduced(p1->m_x);
  37. fpe_isreduced(p1->m_y);
  38. //abc;
  39. //curvepoint_fp_print(stdout,p1);
  40. //scalar_print(stdout,s1);
  41. //where;
  42. curvepoint_fp_scalarmult_vartime(p1, p1, s1); //64 additions
  43. //xyz;
  44. curvepoint_fp_makeaffine(p1);
  45. optate(e1, p2, p1);
  46. if(!fp12e_isone(e1))
  47. printf("Error in optimal ate: e(infty,P) != 1\n");
  48. scalar_setrandom(s2, bn_n);
  49. curvepoint_fp_setneutral(p1);
  50. twistpoint_fp2_set(p2, bn_twistgen);
  51. fp2e_isreduced(p2->m_x);
  52. fp2e_isreduced(p2->m_y);
  53. twistpoint_fp2_scalarmult_vartime(p2, p2, s2);
  54. twistpoint_fp2_makeaffine(p2);
  55. optate(e1, p2, p1);
  56. if(!fp12e_isone(e1))
  57. printf("Error in optimal ate: e(Q,infty) != 1\n");
  58. // Bilinearity test of optimal ate Pairing:
  59. for(i=0;i<NTESTS;i++)
  60. {
  61. #if (NTESTS > 100)
  62. if(!(i%(NTESTS/100)) && i!=0) printf("Number of tests: %d\n",i);
  63. #else
  64. if(i>=0) printf("Number of tests: %d\n",i);
  65. #endif
  66. //ecris(bn_n = );
  67. //scalar_print(stdout,bn_n);
  68. scalar_setrandom(s1, bn_n);
  69. scalar_setrandom(s2, bn_n);
  70. //unsigned long long tab[4]={0x8ae69c5bcb039b07, 0x58549b8019d00d7b, 0x1102df67a4a9ace2, 0xc36d19fe776fac9f};
  71. //scalar_set_lluarray(s1,tab);
  72. //unsigned long long tab2[4]={0xad89b6fb63df1643, 0x354658972be46274, 0x4df1d15eaa4d2322, 0x10bf308f77d3d38d};
  73. //scalar_set_lluarray(s2,tab2);
  74. //unsigned long long tab[4]={8,0,0,0};
  75. //unsigned long long tab2[4]={1,0,0,0};
  76. //scalar_set_lluarray(s1,tab);
  77. //scalar_set_lluarray(s2,tab2);
  78. curvepoint_fp_set(p1, bn_curvegen);
  79. twistpoint_fp2_set(p2, bn_twistgen);
  80. //twistpoint_fp2_print(stdout,bn_twistgen);
  81. fpe_isreduced(p1->m_x);
  82. fpe_isreduced(p1->m_y);
  83. fp2e_isreduced(p2->m_x);
  84. fp2e_isreduced(p2->m_y);
  85. curvepoint_fp_scalarmult_vartime(p1, p1, s1);
  86. curvepoint_fp_makeaffine(p1);
  87. twistpoint_fp2_scalarmult_vartime(p2, p2, s2);
  88. twistpoint_fp2_makeaffine(p2);
  89. //printf("s1p1 affine: ");
  90. //curvepoint_fp_print(stdout,p1);
  91. //printf("\n");
  92. //printf("s2p2 affine: ");
  93. //twistpoint_fp2_print(stdout,p2);
  94. //printf("\n");
  95. optate(e1, p2, p1);
  96. curvepoint_fp_set(p1, bn_curvegen);
  97. twistpoint_fp2_set(p2, bn_twistgen);
  98. fpe_isreduced(p1->m_x);
  99. fpe_isreduced(p1->m_y);
  100. fp2e_isreduced(p2->m_x);
  101. fp2e_isreduced(p2->m_y);
  102. curvepoint_fp_scalarmult_vartime(p1, p1, s2);
  103. curvepoint_fp_makeaffine(p1);
  104. twistpoint_fp2_scalarmult_vartime(p2, p2, s1);
  105. twistpoint_fp2_makeaffine(p2);
  106. //printf("s2p1 affine: ");
  107. //curvepoint_fp_print(stdout,p1);
  108. //printf("\n");
  109. //printf("s1p2 affine: ");
  110. //twistpoint_fp2_print(stdout,p2);
  111. //printf("\n");
  112. optate(e2, p2, p1);
  113. curvepoint_fp_set(p1, bn_curvegen);
  114. twistpoint_fp2_set(p2, bn_twistgen);
  115. optate(e3, p2, p1);
  116. fp12e_pow_vartime(e3, e3, s1);
  117. fp12e_pow_vartime(e3, e3, s2);
  118. if(!fp12e_iseq(e1,e2))
  119. {
  120. printf("Error in optimal ate: e1 != e2\n");
  121. //printf("e1: ");
  122. //fp12e_print(stdout, e1);
  123. //printf("\ne2: ");
  124. //fp12e_print(stdout, e2);
  125. //printf("\nScalars:\n");
  126. //printf("s1= ");
  127. //scalar_print(stdout, s1);
  128. //printf("\ns2= ");
  129. //scalar_print(stdout, s2);
  130. //printf("\n");
  131. }
  132. else if(!fp12e_iseq(e2,e3))
  133. {
  134. printf("Error in optimal ate: e2 != e3\n");
  135. printf("e2: ");
  136. fp12e_print(stdout, e2);
  137. printf("\ne3: ");
  138. fp12e_print(stdout, e3);
  139. printf("\nScalars:\n");
  140. printf("s1= ");
  141. scalar_print(stdout, s1);
  142. printf("\ns2= ");
  143. scalar_print(stdout, s2);
  144. printf("\n");
  145. }
  146. else if(fp12e_iszero(e2))
  147. printf("Error: Pairing value is zero\n");
  148. else if(fp12e_isone(e2))
  149. printf("Warning: Pairing value is one\n");
  150. }
  151. return 0;
  152. }