From ec7f25200f46519403259544f533a78c7e0b71a9 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Sun, 20 Jul 2014 16:47:29 +0200 Subject: [PATCH] add fp_rand() it's a port of the function in libtommath --- src/headers/tfm.h | 3 +++ src/misc/fp_rand.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/misc/fp_rand.c diff --git a/src/headers/tfm.h b/src/headers/tfm.h index abb309f..0f4be58 100644 --- a/src/headers/tfm.h +++ b/src/headers/tfm.h @@ -317,6 +317,9 @@ const char *fp_ident(void); /* set to a small digit */ void fp_set(fp_int *a, fp_digit b); +/* makes a pseudo-random int of a given size */ +void fp_rand(fp_int *a, int digits); + /* copy from a to b */ #define fp_copy(a, b) (void)(((a) != (b)) && memcpy((b), (a), sizeof(fp_int))) #define fp_init_copy(a, b) fp_copy(b, a) diff --git a/src/misc/fp_rand.c b/src/misc/fp_rand.c new file mode 100644 index 0000000..5691a32 --- /dev/null +++ b/src/misc/fp_rand.c @@ -0,0 +1,41 @@ +/* TomsFastMath, a fast ISO C bignum library. + * + * This project is meant to fill in where LibTomMath + * falls short. That is speed ;-) + * + * This project is public domain and free for all purposes. + * + * Tom St Denis, tomstdenis@gmail.com + */ +#include + +/* makes a pseudo-random int of a given size */ + +void fp_rand(fp_int *a, int digits) +{ + fp_digit d; + + fp_zero(a); + if (digits <= 0) { + return; + } + + /* first place a random non-zero digit */ + do { + d = ((fp_digit) abs (rand ())) & FP_MASK; + } while (d == 0); + + fp_add_d (a, d, a); + + while (--digits > 0) { + fp_lshd (a, 1); + fp_add_d (a, ((fp_digit) abs (rand ())), a); + } + + return; + +} + +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */