scalarmult.c 786 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "crypto_scalarmult.h"
  2. #include "fe.h"
  3. int crypto_scalarmult(unsigned char *q,
  4. const unsigned char *n,
  5. const unsigned char *p)
  6. {
  7. unsigned char e[32];
  8. unsigned int i;
  9. fe x1;
  10. fe x2;
  11. fe z2;
  12. fe x3;
  13. fe z3;
  14. fe tmp0;
  15. fe tmp1;
  16. int pos;
  17. unsigned int swap;
  18. unsigned int b;
  19. for (i = 0;i < 32;++i) e[i] = n[i];
  20. e[0] &= 248;
  21. e[31] &= 127;
  22. e[31] |= 64;
  23. fe_frombytes(x1,p);
  24. fe_1(x2);
  25. fe_0(z2);
  26. fe_copy(x3,x1);
  27. fe_1(z3);
  28. swap = 0;
  29. for (pos = 254;pos >= 0;--pos) {
  30. b = e[pos / 8] >> (pos & 7);
  31. b &= 1;
  32. swap ^= b;
  33. fe_cswap(x2,x3,swap);
  34. fe_cswap(z2,z3,swap);
  35. swap = b;
  36. #include "montgomery.h"
  37. }
  38. fe_cswap(x2,x3,swap);
  39. fe_cswap(z2,z3,swap);
  40. fe_invert(z2,z2);
  41. fe_mul(x2,x2,z2);
  42. fe_tobytes(q,x2);
  43. return 0;
  44. }