speedtest.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * File: dclxvi-20130329/speedtest.c
  3. * Author: Ruben Niederhagen, Peter Schwabe
  4. * Public Domain
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include "curvepoint_fp.h"
  9. #include "twistpoint_fp2.h"
  10. #include "fp12e.h"
  11. #include "optate.h"
  12. #include "linefunction.h"
  13. #define NTESTS 20
  14. #define REP 50
  15. #ifdef __x86_64__
  16. #define mycpucycles(RES) \
  17. __asm__ volatile("rdtsc;shlq $32,%%rdx;orq %%rdx,%%rax" : "=a" (RES) :: "%rdx");
  18. #else
  19. #define mycpucycles(RES) \
  20. __asm__ volatile(".byte 15;.byte 49" : "=A" (RES));
  21. #endif
  22. extern const curvepoint_fp_t bn_curvegen;
  23. extern const twistpoint_fp2_t bn_twistgen;
  24. extern const scalar_t bn_n;
  25. /*
  26. static int cmp_llu(const void *a, const void*b)
  27. {
  28. if(*(unsigned long long *)a < *(unsigned long long *)b) return -1;
  29. if(*(unsigned long long *)a > *(unsigned long long *)b) return 1;
  30. return 0;
  31. }
  32. static unsigned long long median(unsigned long long *l, size_t llen)
  33. {
  34. qsort(l,llen,sizeof(unsigned long long),cmp_llu);
  35. if(llen%2) return l[llen/2];
  36. else return (l[llen/2-1]+l[llen/2])/2;
  37. }
  38. */
  39. static void print_bench(unsigned long long *l, size_t llen)
  40. {
  41. size_t i;
  42. for(i=0;i<llen-1;i++)
  43. {
  44. l[i] = l[i+1]-l[i];
  45. printf("%llu", l[i]/REP);
  46. if(i < llen-2) printf(" ");
  47. }
  48. printf("\n");
  49. }
  50. fp12e_t e1;
  51. curvepoint_fp_t p1;
  52. twistpoint_fp2_t p2;
  53. twistpoint_fp2_t p3;
  54. twistpoint_fp2_t rq;
  55. fp2e_t rop11, rop12, rop13, r2;
  56. fpe_t fpe1;
  57. scalar_t s1, s2, s3;
  58. unsigned long long t[NTESTS+1];
  59. int main(int argc, char* argv[])
  60. {
  61. int i, j;
  62. int choose;
  63. if(argc >= 2) choose = atoi(argv[1]);
  64. else choose = 0;
  65. scalar_setrandom(s1, bn_n);
  66. scalar_setrandom(s2, bn_n);
  67. scalar_setrandom(s3, bn_n);
  68. curvepoint_fp_scalarmult_vartime(p1, bn_curvegen, s1);
  69. curvepoint_fp_makeaffine(p1);
  70. twistpoint_fp2_scalarmult_vartime(p2, bn_twistgen, s2);
  71. twistpoint_fp2_makeaffine(p2);
  72. twistpoint_fp2_scalarmult_vartime(p3, bn_twistgen, s3);
  73. twistpoint_fp2_makeaffine(p3);
  74. fp2e_setone(rop11);
  75. fp2e_setone(rop12);
  76. fp2e_setone(rop13);
  77. fp2e_setone(r2);
  78. fpe_setone(fpe1);
  79. fp12e_setone(e1);
  80. if(!choose || choose == 1)
  81. {
  82. printf("Fp2 multiplication: ");
  83. for(i=0;i<NTESTS+1;i++)
  84. {
  85. mycpucycles(t[i]);
  86. for(j=0;j<REP;j++) fp2e_mul(r2, rop11, rop12);
  87. }
  88. print_bench(t,NTESTS+1);
  89. }
  90. if(!choose || choose == 2)
  91. {
  92. printf("Fp2 squaring: ");
  93. for(i=0;i<NTESTS+1;i++)
  94. {
  95. mycpucycles(t[i]);
  96. for(j=0;j<REP;j++) fp2e_square(r2, rop11);
  97. }
  98. print_bench(t,NTESTS+1);
  99. }
  100. if(!choose || choose == 3)
  101. {
  102. printf("Fp2xFp multiplication: ");
  103. for(i=0;i<NTESTS+1;i++)
  104. {
  105. mycpucycles(t[i]);
  106. for(j=0;j<REP;j++) fp2e_mul_fpe(r2, rop11, fpe1);
  107. }
  108. print_bench(t,NTESTS+1);
  109. }
  110. if(!choose || choose == 4)
  111. {
  112. printf("Fp2 short coeffred: ");
  113. for(i=0;i<NTESTS+1;i++)
  114. {
  115. mycpucycles(t[i]);
  116. for(j=0;j<REP;j++) fp2e_short_coeffred(r2);
  117. }
  118. print_bench(t,NTESTS+1);
  119. }
  120. if(!choose || choose == 5)
  121. {
  122. printf("Linefunction add: ");
  123. for(i=0;i<NTESTS+1;i++)
  124. {
  125. mycpucycles(t[i]);
  126. for(j=0;j<REP;j++) linefunction_add_ate(rop11, rop12, rop13, rq, p2, p3, p1, r2);
  127. }
  128. print_bench(t,NTESTS+1);
  129. }
  130. if(!choose || choose == 6)
  131. {
  132. printf("Linefunction double: ");
  133. for(i=0;i<NTESTS+1;i++)
  134. {
  135. mycpucycles(t[i]);
  136. for(j=0;j<REP;j++) linefunction_double_ate(rop11, rop12, rop13, rq, p2, p1);
  137. }
  138. print_bench(t,NTESTS+1);
  139. }
  140. if(!choose || choose == 7)
  141. {
  142. printf("Fp12 multiplication: ");
  143. for(i=0;i<NTESTS+1;i++)
  144. {
  145. mycpucycles(t[i]);
  146. for(j=0;j<REP;j++) fp12e_mul(e1, e1, e1);
  147. }
  148. print_bench(t,NTESTS+1);
  149. }
  150. if(!choose || choose == 8)
  151. {
  152. printf("Fp12 squaring: ");
  153. for(i=0;i<NTESTS+1;i++)
  154. {
  155. mycpucycles(t[i]);
  156. for(j=0;j<REP;j++) fp12e_square(e1, e1);
  157. }
  158. print_bench(t,NTESTS+1);
  159. }
  160. if(!choose || choose == 9)
  161. {
  162. printf("Fp12 linefunction multiplication: ");
  163. for(i=0;i<NTESTS+1;i++)
  164. {
  165. mycpucycles(t[i]);
  166. for(j=0;j<REP;j++) fp12e_mul_line(e1, e1, r2, r2, r2);
  167. }
  168. print_bench(t,NTESTS+1);
  169. }
  170. if(!choose || choose == 10)
  171. {
  172. printf("Fp12 inversion: ");
  173. for(i=0;i<NTESTS+1;i++)
  174. {
  175. mycpucycles(t[i]);
  176. for(j=0;j<REP;j++) fp12e_invert(e1, e1);
  177. }
  178. print_bench(t,NTESTS+1);
  179. }
  180. if(!choose || choose == 11)
  181. {
  182. printf("Miller loop: ");
  183. for(i=0;i<NTESTS+1;i++)
  184. {
  185. mycpucycles(t[i]);
  186. for(j=0;j<REP;j++) optate_miller(e1, p2, p1);
  187. }
  188. print_bench(t,NTESTS+1);
  189. }
  190. if(!choose || choose == 12)
  191. {
  192. printf("Optimal ate pairing: ");
  193. for(i=0;i<NTESTS+1;i++)
  194. {
  195. mycpucycles(t[i]);
  196. for(j=0;j<REP;j++) optate(e1, p2, p1);
  197. }
  198. print_bench(t,NTESTS+1);
  199. }
  200. }