smult.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "crypto_scalarmult.h"
  2. #define mult crypto_scalarmult_curve25519_athlon_mult
  3. #define square crypto_scalarmult_curve25519_athlon_square
  4. void crypto_scalarmult_curve25519_athlon_recip(double out[10],const double z[10])
  5. {
  6. double z2[10];
  7. double z9[10];
  8. double z11[10];
  9. double z2_5_0[10];
  10. double z2_10_0[10];
  11. double z2_20_0[10];
  12. double z2_50_0[10];
  13. double z2_100_0[10];
  14. double t0[10];
  15. double t1[10];
  16. int i;
  17. /* 2 */ square(z2,z);
  18. /* 4 */ square(t1,z2);
  19. /* 8 */ square(t0,t1);
  20. /* 9 */ mult(z9,t0,z);
  21. /* 11 */ mult(z11,z9,z2);
  22. /* 22 */ square(t0,z11);
  23. /* 2^5 - 2^0 = 31 */ mult(z2_5_0,t0,z9);
  24. /* 2^6 - 2^1 */ square(t0,z2_5_0);
  25. /* 2^7 - 2^2 */ square(t1,t0);
  26. /* 2^8 - 2^3 */ square(t0,t1);
  27. /* 2^9 - 2^4 */ square(t1,t0);
  28. /* 2^10 - 2^5 */ square(t0,t1);
  29. /* 2^10 - 2^0 */ mult(z2_10_0,t0,z2_5_0);
  30. /* 2^11 - 2^1 */ square(t0,z2_10_0);
  31. /* 2^12 - 2^2 */ square(t1,t0);
  32. /* 2^20 - 2^10 */ for (i = 2;i < 10;i += 2) { square(t0,t1); square(t1,t0); }
  33. /* 2^20 - 2^0 */ mult(z2_20_0,t1,z2_10_0);
  34. /* 2^21 - 2^1 */ square(t0,z2_20_0);
  35. /* 2^22 - 2^2 */ square(t1,t0);
  36. /* 2^40 - 2^20 */ for (i = 2;i < 20;i += 2) { square(t0,t1); square(t1,t0); }
  37. /* 2^40 - 2^0 */ mult(t0,t1,z2_20_0);
  38. /* 2^41 - 2^1 */ square(t1,t0);
  39. /* 2^42 - 2^2 */ square(t0,t1);
  40. /* 2^50 - 2^10 */ for (i = 2;i < 10;i += 2) { square(t1,t0); square(t0,t1); }
  41. /* 2^50 - 2^0 */ mult(z2_50_0,t0,z2_10_0);
  42. /* 2^51 - 2^1 */ square(t0,z2_50_0);
  43. /* 2^52 - 2^2 */ square(t1,t0);
  44. /* 2^100 - 2^50 */ for (i = 2;i < 50;i += 2) { square(t0,t1); square(t1,t0); }
  45. /* 2^100 - 2^0 */ mult(z2_100_0,t1,z2_50_0);
  46. /* 2^101 - 2^1 */ square(t1,z2_100_0);
  47. /* 2^102 - 2^2 */ square(t0,t1);
  48. /* 2^200 - 2^100 */ for (i = 2;i < 100;i += 2) { square(t1,t0); square(t0,t1); }
  49. /* 2^200 - 2^0 */ mult(t1,t0,z2_100_0);
  50. /* 2^201 - 2^1 */ square(t0,t1);
  51. /* 2^202 - 2^2 */ square(t1,t0);
  52. /* 2^250 - 2^50 */ for (i = 2;i < 50;i += 2) { square(t0,t1); square(t1,t0); }
  53. /* 2^250 - 2^0 */ mult(t0,t1,z2_50_0);
  54. /* 2^251 - 2^1 */ square(t1,t0);
  55. /* 2^252 - 2^2 */ square(t0,t1);
  56. /* 2^253 - 2^3 */ square(t1,t0);
  57. /* 2^254 - 2^4 */ square(t0,t1);
  58. /* 2^255 - 2^5 */ square(t1,t0);
  59. /* 2^255 - 21 */ mult(out,t1,z11);
  60. }
  61. int crypto_scalarmult(unsigned char *q,
  62. const unsigned char *n,
  63. const unsigned char *p)
  64. {
  65. double work[30];
  66. unsigned char e[32];
  67. int i;
  68. for (i = 0;i < 32;++i) e[i] = n[i];
  69. e[0] &= 248;
  70. e[31] &= 127;
  71. e[31] |= 64;
  72. crypto_scalarmult_curve25519_athlon_init();
  73. crypto_scalarmult_curve25519_athlon_todouble(work,p);
  74. crypto_scalarmult_curve25519_athlon_mainloop(work,e);
  75. crypto_scalarmult_curve25519_athlon_recip(work + 10,work + 10);
  76. mult(work + 20,work,work + 10);
  77. crypto_scalarmult_curve25519_athlon_fromdouble(q,work + 20);
  78. return 0;
  79. }