projects
/
silc.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Integer type name change.
[silc.git]
/
lib
/
silcmath
/
silcprimegen.c
diff --git
a/lib/silcmath/silcprimegen.c
b/lib/silcmath/silcprimegen.c
index beb07f285f87ea09d3cba6d34638a4f66c9682ce..515f2b400fc8a0dc6d5c724f43e2fd9ddd260a25 100644
(file)
--- a/
lib/silcmath/silcprimegen.c
+++ b/
lib/silcmath/silcprimegen.c
@@
-53,7
+53,7
@@
*/
*/
-static
ui
nt32 primetable[] =
+static
SilcUI
nt32 primetable[] =
{
2, 3, 5, 7, 11, 13, 17, 19,
23, 29, 31, 37, 41, 43, 47, 53,
{
2, 3, 5, 7, 11, 13, 17, 19,
23, 29, 31, 37, 41, 43, 47, 53,
@@
-195,28
+195,37
@@
static uint32 primetable[] =
If argument verbose is TRUE this will display some status information
about the progress of generation. */
If argument verbose is TRUE this will display some status information
about the progress of generation. */
-
int silc_math_gen_prime(SilcInt *prime, uint32 bits, int
verbose)
+
bool silc_math_gen_prime(SilcMPInt *prime, SilcUInt32 bits, bool
verbose)
{
{
- unsigned char *numbuf;
-
ui
nt32 i, b, k;
-
ui
nt32 *spmods;
- SilcInt r, base, tmp, tmp2, oprime;
+ unsigned char *numbuf
= NULL
;
+
SilcUI
nt32 i, b, k;
+
SilcUI
nt32 *spmods;
+ Silc
MP
Int r, base, tmp, tmp2, oprime;
silc_mp_init(&r);
silc_mp_init(&r);
- silc_mp_init
_set_ui(&base, 2
);
+ silc_mp_init
(&base
);
silc_mp_init(&tmp);
silc_mp_init(&tmp2);
silc_mp_init(&oprime);
silc_mp_init(&tmp);
silc_mp_init(&tmp2);
silc_mp_init(&oprime);
+ silc_mp_set_ui(&base, 2);
+
SILC_LOG_DEBUG(("Generating new prime"));
SILC_LOG_DEBUG(("Generating new prime"));
- /* Get random number */
- numbuf = silc_rng_global_get_rn_string((bits / 8));
- if (!numbuf)
- return FALSE;
+ /* Get random number and assure that the first digit is not zero since
+ our conversion routines does not like the first digit being zero. */
+ do {
+ if (numbuf) {
+ memset(numbuf, 0, (bits / 8));
+ silc_free(numbuf);
+ }
+ numbuf = silc_rng_global_get_rn_string((bits / 8));
+ if (!numbuf)
+ return FALSE;
+ } while (numbuf[0] == '0');
/* Convert into MP and set the size */
/* Convert into MP and set the size */
- silc_mp_set_str(prime, numbuf, 16);
+ silc_mp_set_str(prime, numbuf, 16);
silc_mp_mod_2exp(prime, prime, bits);
/* Empty buffer */
silc_mp_mod_2exp(prime, prime, bits);
/* Empty buffer */
@@
-225,11
+234,11
@@
int silc_math_gen_prime(SilcInt *prime, uint32 bits, int verbose)
/* Number could be even number, so we'll make it odd. */
silc_mp_set_ui(&tmp, 1);
/* Number could be even number, so we'll make it odd. */
silc_mp_set_ui(&tmp, 1);
- silc_mp_
ior(prime, prime, &tmp);
/* OR operator */
+ silc_mp_
or(prime, prime, &tmp);
/* OR operator */
/* Init modulo table with the prime candidate and the primes
in the primetable. */
/* Init modulo table with the prime candidate and the primes
in the primetable. */
- spmods = silc_calloc(1, sizeof(primetable) * sizeof(
ui
nt32));
+ spmods = silc_calloc(1, sizeof(primetable) * sizeof(
SilcUI
nt32));
for (i = 0; primetable[i] != 0; i++) {
silc_mp_mod_ui(&tmp, prime, primetable[i]);
spmods[i] = silc_mp_get_ui(&tmp);
for (i = 0; primetable[i] != 0; i++) {
silc_mp_mod_ui(&tmp, prime, primetable[i]);
spmods[i] = silc_mp_get_ui(&tmp);
@@
-263,7
+272,7
@@
int silc_math_gen_prime(SilcInt *prime, uint32 bits, int verbose)
/* Does the prime pass the Fermat's prime test.
* r = 2 ^ p mod p, if r == 2, then p is probably a prime.
*/
/* Does the prime pass the Fermat's prime test.
* r = 2 ^ p mod p, if r == 2, then p is probably a prime.
*/
- silc_mp_pow
m
(&r, &base, &oprime, &oprime);
+ silc_mp_pow
_mod
(&r, &base, &oprime, &oprime);
if (silc_mp_cmp_ui(&r, 2) != 0) {
if (verbose) {
printf(".");
if (silc_mp_cmp_ui(&r, 2) != 0) {
if (verbose) {
printf(".");
@@
-289,11
+298,11
@@
int silc_math_gen_prime(SilcInt *prime, uint32 bits, int verbose)
}
silc_free(spmods);
}
silc_free(spmods);
- silc_mp_
clear
(&r);
- silc_mp_
clear
(&base);
- silc_mp_
clear
(&tmp);
- silc_mp_
clear
(&tmp2);
- silc_mp_
clear
(&oprime);
+ silc_mp_
uninit
(&r);
+ silc_mp_
uninit
(&base);
+ silc_mp_
uninit
(&tmp);
+ silc_mp_
uninit
(&tmp2);
+ silc_mp_
uninit
(&oprime);
return TRUE;
}
return TRUE;
}
@@
-301,14
+310,15
@@
int silc_math_gen_prime(SilcInt *prime, uint32 bits, int verbose)
/* Performs primality testings for given number. Returns TRUE if the
number is probably a prime. */
/* Performs primality testings for given number. Returns TRUE if the
number is probably a prime. */
-
int silc_math_prime_test(Silc
Int *p)
+
bool silc_math_prime_test(SilcMP
Int *p)
{
{
- SilcInt r, base, tmp;
+ Silc
MP
Int r, base, tmp;
int i, ret = 0;
silc_mp_init(&r);
silc_mp_init(&tmp);
int i, ret = 0;
silc_mp_init(&r);
silc_mp_init(&tmp);
- silc_mp_init_set_ui(&base, 2);
+ silc_mp_init(&base);
+ silc_mp_set_ui(&base, 2);
SILC_LOG_DEBUG(("Testing probability of prime"));
SILC_LOG_DEBUG(("Testing probability of prime"));
@@
-325,13
+335,13
@@
int silc_math_prime_test(SilcInt *p)
/* Does the prime pass the Fermat's prime test.
* r = 2 ^ p mod p, if r == 2, then p is probably a prime.
*/
/* Does the prime pass the Fermat's prime test.
* r = 2 ^ p mod p, if r == 2, then p is probably a prime.
*/
- silc_mp_pow
m
(&r, &base, p, p);
+ silc_mp_pow
_mod
(&r, &base, p, p);
if (silc_mp_cmp_ui(&r, 2) != 0)
ret = -1;
if (silc_mp_cmp_ui(&r, 2) != 0)
ret = -1;
- silc_mp_
clear
(&r);
- silc_mp_
clear
(&tmp);
- silc_mp_
clear
(&base);
+ silc_mp_
uninit
(&r);
+ silc_mp_
uninit
(&tmp);
+ silc_mp_
uninit
(&base);
if (ret)
return FALSE;
if (ret)
return FALSE;