Skip to content

Commit

Permalink
add randommod.[ch]
Browse files Browse the repository at this point in the history
  • Loading branch information
janmojzis committed Dec 28, 2021
1 parent 1696201 commit 1645cd3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
17 changes: 17 additions & 0 deletions randommod.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* taken from public-domain nacl-20110221, from curvecp/randommod.c */
#include "randombytes.h"
#include "randommod.h"

/* XXX: current implementation is limited to n<2^55 */

long long randommod(long long n) {
long long result = 0;
long long j;
unsigned char r[32];
if (n <= 1) return 0;
randombytes(r, 32);
for (j = 0; j < 32; ++j) {
result = (result * 256 + (unsigned long long) r[j]) % n;
}
return result;
}
6 changes: 6 additions & 0 deletions randommod.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef _RANDOMMOD_H____
#define _RANDOMMOD_H____

extern long long randommod(long long);

#endif

0 comments on commit 1645cd3

Please sign in to comment.