sc25519_slide.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "sc25519.h"
  2. void sc25519_slide(signed char r[256], const sc25519 *s, int swindowsize)
  3. {
  4. int i,j,k,b,m=(1<<(swindowsize-1))-1, soplen=256;
  5. unsigned long long sv0 = s->v[0];
  6. unsigned long long sv1 = s->v[1];
  7. unsigned long long sv2 = s->v[2];
  8. unsigned long long sv3 = s->v[3];
  9. /* first put the binary expansion into r */
  10. for(i=0;i<64;i++) {
  11. r[i] = sv0 & 1;
  12. r[i+64] = sv1 & 1;
  13. r[i+128] = sv2 & 1;
  14. r[i+192] = sv3 & 1;
  15. sv0 >>= 1;
  16. sv1 >>= 1;
  17. sv2 >>= 1;
  18. sv3 >>= 1;
  19. }
  20. /* Making it sliding window */
  21. for (j = 0;j < soplen;++j)
  22. {
  23. if (r[j]) {
  24. for (b = 1;b < soplen - j && b <= 6;++b) {
  25. if (r[j] + (r[j + b] << b) <= m)
  26. {
  27. r[j] += r[j + b] << b; r[j + b] = 0;
  28. }
  29. else if (r[j] - (r[j + b] << b) >= -m)
  30. {
  31. r[j] -= r[j + b] << b;
  32. for (k = j + b;k < soplen;++k)
  33. {
  34. if (!r[k]) {
  35. r[k] = 1;
  36. break;
  37. }
  38. r[k] = 0;
  39. }
  40. }
  41. else if (r[j + b])
  42. break;
  43. }
  44. }
  45. }
  46. }