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$ */