add fp_rand()

it's a port of the function in libtommath
This commit is contained in:
Steffen Jaeckel 2014-07-20 16:47:29 +02:00
parent 56438df4cf
commit ec7f25200f
2 changed files with 44 additions and 0 deletions

View File

@ -317,6 +317,9 @@ const char *fp_ident(void);
/* set to a small digit */ /* set to a small digit */
void fp_set(fp_int *a, fp_digit b); 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 */ /* copy from a to b */
#define fp_copy(a, b) (void)(((a) != (b)) && memcpy((b), (a), sizeof(fp_int))) #define fp_copy(a, b) (void)(((a) != (b)) && memcpy((b), (a), sizeof(fp_int)))
#define fp_init_copy(a, b) fp_copy(b, a) #define fp_init_copy(a, b) fp_copy(b, a)

41
src/misc/fp_rand.c Normal file
View File

@ -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 <tfm.h>
/* 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$ */