forked from ibphoenix/tomsfastmath
Merge branch 'feature/rand'
This commit is contained in:
commit
b0509acce9
12
makefile
12
makefile
@ -42,12 +42,12 @@ src/bin/fp_to_signed_bin.o src/bin/fp_to_unsigned_bin.o src/bin/fp_unsigned_bin_
|
||||
src/bit/fp_cnt_lsb.o src/bit/fp_count_bits.o src/bit/fp_div_2.o src/bit/fp_div_2d.o src/bit/fp_lshd.o \
|
||||
src/bit/fp_mod_2d.o src/bit/fp_rshd.o src/divide/fp_div.o src/divide/fp_div_d.o src/divide/fp_mod.o \
|
||||
src/divide/fp_mod_d.o src/exptmod/fp_2expt.o src/exptmod/fp_exptmod.o src/misc/fp_ident.o \
|
||||
src/misc/fp_set.o src/mont/fp_montgomery_calc_normalization.o src/mont/fp_montgomery_reduce.o \
|
||||
src/mont/fp_montgomery_setup.o src/mul/fp_mul_2.o src/mul/fp_mul_2d.o src/mul/fp_mul.o \
|
||||
src/mul/fp_mul_comba_12.o src/mul/fp_mul_comba_17.o src/mul/fp_mul_comba_20.o src/mul/fp_mul_comba_24.o \
|
||||
src/mul/fp_mul_comba_28.o src/mul/fp_mul_comba_32.o src/mul/fp_mul_comba_3.o src/mul/fp_mul_comba_48.o \
|
||||
src/mul/fp_mul_comba_4.o src/mul/fp_mul_comba_64.o src/mul/fp_mul_comba_6.o src/mul/fp_mul_comba_7.o \
|
||||
src/mul/fp_mul_comba_8.o src/mul/fp_mul_comba_9.o src/mul/fp_mul_comba.o \
|
||||
src/misc/fp_rand.o src/misc/fp_set.o src/mont/fp_montgomery_calc_normalization.o \
|
||||
src/mont/fp_montgomery_reduce.o src/mont/fp_montgomery_setup.o src/mul/fp_mul_2.o src/mul/fp_mul_2d.o \
|
||||
src/mul/fp_mul.o src/mul/fp_mul_comba_12.o src/mul/fp_mul_comba_17.o src/mul/fp_mul_comba_20.o \
|
||||
src/mul/fp_mul_comba_24.o src/mul/fp_mul_comba_28.o src/mul/fp_mul_comba_32.o src/mul/fp_mul_comba_3.o \
|
||||
src/mul/fp_mul_comba_48.o src/mul/fp_mul_comba_4.o src/mul/fp_mul_comba_64.o src/mul/fp_mul_comba_6.o \
|
||||
src/mul/fp_mul_comba_7.o src/mul/fp_mul_comba_8.o src/mul/fp_mul_comba_9.o src/mul/fp_mul_comba.o \
|
||||
src/mul/fp_mul_comba_small_set.o src/mul/fp_mul_d.o src/mul/fp_mulmod.o src/numtheory/fp_gcd.o \
|
||||
src/numtheory/fp_invmod.o src/numtheory/fp_isprime.o src/numtheory/fp_lcm.o \
|
||||
src/numtheory/fp_prime_miller_rabin.o src/numtheory/fp_prime_random_ex.o src/sqr/fp_sqr.o \
|
||||
|
@ -30,12 +30,12 @@ src/bin/fp_to_signed_bin.o src/bin/fp_to_unsigned_bin.o src/bin/fp_unsigned_bin_
|
||||
src/bit/fp_cnt_lsb.o src/bit/fp_count_bits.o src/bit/fp_div_2.o src/bit/fp_div_2d.o src/bit/fp_lshd.o \
|
||||
src/bit/fp_mod_2d.o src/bit/fp_rshd.o src/divide/fp_div.o src/divide/fp_div_d.o src/divide/fp_mod.o \
|
||||
src/divide/fp_mod_d.o src/exptmod/fp_2expt.o src/exptmod/fp_exptmod.o src/misc/fp_ident.o \
|
||||
src/misc/fp_set.o src/mont/fp_montgomery_calc_normalization.o src/mont/fp_montgomery_reduce.o \
|
||||
src/mont/fp_montgomery_setup.o src/mul/fp_mul_2.o src/mul/fp_mul_2d.o src/mul/fp_mul.o \
|
||||
src/mul/fp_mul_comba_12.o src/mul/fp_mul_comba_17.o src/mul/fp_mul_comba_20.o src/mul/fp_mul_comba_24.o \
|
||||
src/mul/fp_mul_comba_28.o src/mul/fp_mul_comba_32.o src/mul/fp_mul_comba_3.o src/mul/fp_mul_comba_48.o \
|
||||
src/mul/fp_mul_comba_4.o src/mul/fp_mul_comba_64.o src/mul/fp_mul_comba_6.o src/mul/fp_mul_comba_7.o \
|
||||
src/mul/fp_mul_comba_8.o src/mul/fp_mul_comba_9.o src/mul/fp_mul_comba.o \
|
||||
src/misc/fp_rand.o src/misc/fp_set.o src/mont/fp_montgomery_calc_normalization.o \
|
||||
src/mont/fp_montgomery_reduce.o src/mont/fp_montgomery_setup.o src/mul/fp_mul_2.o src/mul/fp_mul_2d.o \
|
||||
src/mul/fp_mul.o src/mul/fp_mul_comba_12.o src/mul/fp_mul_comba_17.o src/mul/fp_mul_comba_20.o \
|
||||
src/mul/fp_mul_comba_24.o src/mul/fp_mul_comba_28.o src/mul/fp_mul_comba_32.o src/mul/fp_mul_comba_3.o \
|
||||
src/mul/fp_mul_comba_48.o src/mul/fp_mul_comba_4.o src/mul/fp_mul_comba_64.o src/mul/fp_mul_comba_6.o \
|
||||
src/mul/fp_mul_comba_7.o src/mul/fp_mul_comba_8.o src/mul/fp_mul_comba_9.o src/mul/fp_mul_comba.o \
|
||||
src/mul/fp_mul_comba_small_set.o src/mul/fp_mul_d.o src/mul/fp_mulmod.o src/numtheory/fp_gcd.o \
|
||||
src/numtheory/fp_invmod.o src/numtheory/fp_isprime.o src/numtheory/fp_lcm.o \
|
||||
src/numtheory/fp_prime_miller_rabin.o src/numtheory/fp_prime_random_ex.o src/sqr/fp_sqr.o \
|
||||
|
@ -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)
|
||||
|
41
src/misc/fp_rand.c
Normal file
41
src/misc/fp_rand.c
Normal 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$ */
|
Loading…
Reference in New Issue
Block a user