randommod.c 325 B

1234567891011121314
  1. #include "randombytes.h"
  2. /* XXX: current implementation is limited to n<2^55 */
  3. long long randommod(long long n)
  4. {
  5. long long result = 0;
  6. long long j;
  7. unsigned char r[32];
  8. if (n <= 1) return 0;
  9. randombytes(r,32);
  10. for (j = 0;j < 32;++j) result = (result * 256 + (unsigned long long) r[j]) % n;
  11. return result;
  12. }