scalar1024.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include "scalar1024.hpp"
  5. #include "zout.hpp"
  6. void fp12e_pow_vartime1024(fp12e_t rop, const fp12e_t op, const scalar1024 exp)
  7. {
  8. fp12e_t dummy;
  9. unsigned int startbit;
  10. startbit = scalar1024_scanb(exp);
  11. //zout(startbit);
  12. fp12e_set(dummy, op);
  13. fp12e_set(rop,op);
  14. int i;
  15. for(i = startbit; i > 0; i--)
  16. {
  17. fp12e_square(rop, rop);
  18. if(scalar1024_getbit(exp, i - 1))
  19. fp12e_mul(rop, rop, dummy);
  20. }
  21. }
  22. int scalar1024_getbit(const scalar1024 s, unsigned int pos)
  23. {
  24. //zout(s[0]);
  25. //printf("s[0] en mode d = %d\n", s[0]);
  26. //printf("s[0] en mode llx = %llx\n", s[0]);
  27. assert(pos < 1024);
  28. //zout(pos >> 28,s[pos >> 28],(pos & 0x3f),s[pos >> 28] >> (pos & 0x3f));
  29. return (s[pos >> 6] >> (pos & 0x3f)) & 1;
  30. }
  31. // Returns the position of the most significant set bit
  32. int scalar1024_scanb(const scalar1024 s)
  33. {
  34. int i;
  35. unsigned int pos = 0;
  36. //for(i=1023;i>0;i--)
  37. //if(scalar1024_getbit(s,i) && pos == 0) pos = i;
  38. i=1023;
  39. while(scalar1024_getbit(s,i)!=1)
  40. {i--;}
  41. pos=i;
  42. return pos;
  43. }
  44. void scalar1024_print(FILE *fh, const scalar1024 t)
  45. {
  46. int i;
  47. for(i=15;i>=0;i--)
  48. fprintf(fh, "%d\t", t[i]);
  49. //fprintf(fh, "%llx\t", t[i]);
  50. }