fe25519_batchinvert.c 795 B

12345678910111213141516171819202122232425262728293031323334
  1. #include "fe25519.h"
  2. // tmp MUST != out or in
  3. // in MAY == out
  4. void fe25519_batchinvert(fe25519 *out, const fe25519 *in, fe25519 *tmp, size_t num, size_t offset)
  5. {
  6. fe25519 acc;
  7. fe25519 tmpacc;
  8. size_t i;
  9. const fe25519 *inp;
  10. fe25519 *outp;
  11. fe25519_setint(&acc,1);
  12. inp = in;
  13. for (i = 0;i < num;++i) {
  14. tmp[i] = acc;
  15. fe25519_mul(&acc,&acc,inp);
  16. inp = (const fe25519 *)((const char *)inp + offset);
  17. }
  18. fe25519_invert(&acc,&acc);
  19. i = num;
  20. inp = (const fe25519 *)((const char *)in + offset * num);
  21. outp = (fe25519 *)((char *)out + offset * num);
  22. while (i--) {
  23. inp = (const fe25519 *)((const char *)inp - offset);
  24. outp = (fe25519 *)((char *)outp - offset);
  25. fe25519_mul(&tmpacc,&acc,inp);
  26. fe25519_mul(outp,&acc,&tmp[i]);
  27. acc = tmpacc;
  28. }
  29. }