ge25519_scalarmult_base.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "fe25519.h"
  2. #include "sc25519.h"
  3. #include "ge25519.h"
  4. /* Multiples of the base point in Niels' representation */
  5. static const ge25519_niels ge25519_base_multiples_niels[] = {
  6. #include "ge25519_base_niels_smalltables.data"
  7. };
  8. /* d */
  9. static const fe25519 ecd = {{929955233495203, 466365720129213, 1662059464998953, 2033849074728123, 1442794654840575}};
  10. void ge25519_scalarmult_base(ge25519_p3 *r, const sc25519 *s)
  11. {
  12. signed char b[64];
  13. int i;
  14. ge25519_niels t;
  15. fe25519 d;
  16. sc25519_window4(b,s);
  17. ge25519_p1p1 tp1p1;
  18. choose_t((ge25519_niels *)r, 0, (signed long long) b[1], ge25519_base_multiples_niels);
  19. fe25519_sub(&d, &r->y, &r->x);
  20. fe25519_add(&r->y, &r->y, &r->x);
  21. r->x = d;
  22. r->t = r->z;
  23. fe25519_setint(&r->z,2);
  24. for(i=3;i<64;i+=2)
  25. {
  26. choose_t(&t, (unsigned long long) i/2, (signed long long) b[i], ge25519_base_multiples_niels);
  27. ge25519_nielsadd2(r, &t);
  28. }
  29. ge25519_dbl_p1p1(&tp1p1,(ge25519_p2 *)r);
  30. ge25519_p1p1_to_p2((ge25519_p2 *)r, &tp1p1);
  31. ge25519_dbl_p1p1(&tp1p1,(ge25519_p2 *)r);
  32. ge25519_p1p1_to_p2((ge25519_p2 *)r, &tp1p1);
  33. ge25519_dbl_p1p1(&tp1p1,(ge25519_p2 *)r);
  34. ge25519_p1p1_to_p2((ge25519_p2 *)r, &tp1p1);
  35. ge25519_dbl_p1p1(&tp1p1,(ge25519_p2 *)r);
  36. ge25519_p1p1_to_p3(r, &tp1p1);
  37. choose_t(&t, (unsigned long long) 0, (signed long long) b[0], ge25519_base_multiples_niels);
  38. fe25519_mul(&t.t2d, &t.t2d, &ecd);
  39. ge25519_nielsadd2(r, &t);
  40. for(i=2;i<64;i+=2)
  41. {
  42. choose_t(&t, (unsigned long long) i/2, (signed long long) b[i], ge25519_base_multiples_niels);
  43. ge25519_nielsadd2(r, &t);
  44. }
  45. }