From 1645cd3a4a5a6522192d4c17bddbdb3a88b593b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Moj=C5=BE=C3=AD=C5=A1?= Date: Tue, 28 Dec 2021 10:09:06 +0100 Subject: [PATCH] add randommod.[ch] --- randommod.c | 17 +++++++++++++++++ randommod.h | 6 ++++++ 2 files changed, 23 insertions(+) create mode 100644 randommod.c create mode 100644 randommod.h diff --git a/randommod.c b/randommod.c new file mode 100644 index 0000000..4ca2fed --- /dev/null +++ b/randommod.c @@ -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; +} diff --git a/randommod.h b/randommod.h new file mode 100644 index 0000000..a185e15 --- /dev/null +++ b/randommod.h @@ -0,0 +1,6 @@ +#ifndef _RANDOMMOD_H____ +#define _RANDOMMOD_H____ + +extern long long randommod(long long); + +#endif