Commit 149ee9bf authored by Andrew Talbot's avatar Andrew Talbot Committed by Alexandre Julliard

rsaenh: Declare some functions static.

parent cbaa2e5b
...@@ -232,6 +232,28 @@ mp_zero (mp_int * a) ...@@ -232,6 +232,28 @@ mp_zero (mp_int * a)
memset (a->dp, 0, sizeof (mp_digit) * a->alloc); memset (a->dp, 0, sizeof (mp_digit) * a->alloc);
} }
/* b = |a|
*
* Simple function copies the input and fixes the sign to positive
*/
static int
mp_abs (const mp_int * a, mp_int * b)
{
int res;
/* copy a to b */
if (a != b) {
if ((res = mp_copy (a, b)) != MP_OKAY) {
return res;
}
}
/* force the sign of b to positive */
b->sign = MP_ZPOS;
return MP_OKAY;
}
/* computes the modular inverse via binary extended euclidean algorithm, /* computes the modular inverse via binary extended euclidean algorithm,
* that is c = 1/a mod b * that is c = 1/a mod b
* *
...@@ -793,7 +815,7 @@ static int fast_s_mp_sqr (const mp_int * a, mp_int * b) ...@@ -793,7 +815,7 @@ static int fast_s_mp_sqr (const mp_int * a, mp_int * b)
* Simple algorithm which zeroes the int, grows it then just sets one bit * Simple algorithm which zeroes the int, grows it then just sets one bit
* as required. * as required.
*/ */
int static int
mp_2expt (mp_int * a, int b) mp_2expt (mp_int * a, int b)
{ {
int res; int res;
...@@ -815,28 +837,6 @@ mp_2expt (mp_int * a, int b) ...@@ -815,28 +837,6 @@ mp_2expt (mp_int * a, int b)
return MP_OKAY; return MP_OKAY;
} }
/* b = |a|
*
* Simple function copies the input and fixes the sign to positive
*/
int
mp_abs (const mp_int * a, mp_int * b)
{
int res;
/* copy a to b */
if (a != b) {
if ((res = mp_copy (a, b)) != MP_OKAY) {
return res;
}
}
/* force the sign of b to positive */
b->sign = MP_ZPOS;
return MP_OKAY;
}
/* high level addition (handles signs) */ /* high level addition (handles signs) */
int mp_add (mp_int * a, mp_int * b, mp_int * c) int mp_add (mp_int * a, mp_int * b, mp_int * c)
{ {
...@@ -870,7 +870,7 @@ int mp_add (mp_int * a, mp_int * b, mp_int * c) ...@@ -870,7 +870,7 @@ int mp_add (mp_int * a, mp_int * b, mp_int * c)
/* single digit addition */ /* single digit addition */
int static int
mp_add_d (mp_int * a, mp_digit b, mp_int * c) mp_add_d (mp_int * a, mp_digit b, mp_int * c)
{ {
int res, ix, oldused; int res, ix, oldused;
...@@ -1205,6 +1205,57 @@ mp_mod_2d (const mp_int * a, int b, mp_int * c) ...@@ -1205,6 +1205,57 @@ mp_mod_2d (const mp_int * a, int b, mp_int * c)
return MP_OKAY; return MP_OKAY;
} }
/* shift right a certain amount of digits */
static void mp_rshd (mp_int * a, int b)
{
int x;
/* if b <= 0 then ignore it */
if (b <= 0) {
return;
}
/* if b > used then simply zero it and return */
if (a->used <= b) {
mp_zero (a);
return;
}
{
register mp_digit *bottom, *top;
/* shift the digits down */
/* bottom */
bottom = a->dp;
/* top [offset into digits] */
top = a->dp + b;
/* this is implemented as a sliding window where
* the window is b-digits long and digits from
* the top of the window are copied to the bottom
*
* e.g.
b-2 | b-1 | b0 | b1 | b2 | ... | bb | ---->
/\ | ---->
\-------------------/ ---->
*/
for (x = 0; x < (a->used - b); x++) {
*bottom++ = *top++;
}
/* zero the top digits */
for (; x < a->used; x++) {
*bottom++ = 0;
}
}
/* remove excess digits */
a->used -= b;
}
/* shift right by a certain bit count (store quotient in c, optional remainder in d) */ /* shift right by a certain bit count (store quotient in c, optional remainder in d) */
static int mp_div_2d (const mp_int * a, int b, mp_int * c, mp_int * d) static int mp_div_2d (const mp_int * a, int b, mp_int * c, mp_int * d)
{ {
...@@ -3096,7 +3147,7 @@ static const mp_digit __prime_tab[] = { ...@@ -3096,7 +3147,7 @@ static const mp_digit __prime_tab[] = {
* *
* sets result to 0 if not, 1 if yes * sets result to 0 if not, 1 if yes
*/ */
int mp_prime_is_divisible (const mp_int * a, int *result) static int mp_prime_is_divisible (const mp_int * a, int *result)
{ {
int err, ix; int err, ix;
mp_digit res; mp_digit res;
...@@ -3120,68 +3171,6 @@ int mp_prime_is_divisible (const mp_int * a, int *result) ...@@ -3120,68 +3171,6 @@ int mp_prime_is_divisible (const mp_int * a, int *result)
return MP_OKAY; return MP_OKAY;
} }
/* performs a variable number of rounds of Miller-Rabin
*
* Probability of error after t rounds is no more than
*
* Sets result to 1 if probably prime, 0 otherwise
*/
int mp_prime_is_prime (mp_int * a, int t, int *result)
{
mp_int b;
int ix, err, res;
/* default to no */
*result = MP_NO;
/* valid value of t? */
if (t <= 0 || t > PRIME_SIZE) {
return MP_VAL;
}
/* is the input equal to one of the primes in the table? */
for (ix = 0; ix < PRIME_SIZE; ix++) {
if (mp_cmp_d(a, __prime_tab[ix]) == MP_EQ) {
*result = 1;
return MP_OKAY;
}
}
/* first perform trial division */
if ((err = mp_prime_is_divisible (a, &res)) != MP_OKAY) {
return err;
}
/* return if it was trivially divisible */
if (res == MP_YES) {
return MP_OKAY;
}
/* now perform the miller-rabin rounds */
if ((err = mp_init (&b)) != MP_OKAY) {
return err;
}
for (ix = 0; ix < t; ix++) {
/* set the prime */
mp_set (&b, __prime_tab[ix]);
if ((err = mp_prime_miller_rabin (a, &b, &res)) != MP_OKAY) {
goto __B;
}
if (res == MP_NO) {
goto __B;
}
}
/* passed the test */
*result = MP_YES;
__B:mp_clear (&b);
return err;
}
/* Miller-Rabin test of "a" to the base of "b" as described in /* Miller-Rabin test of "a" to the base of "b" as described in
* HAC pp. 139 Algorithm 4.24 * HAC pp. 139 Algorithm 4.24
* *
...@@ -3189,7 +3178,7 @@ __B:mp_clear (&b); ...@@ -3189,7 +3178,7 @@ __B:mp_clear (&b);
* Randomly the chance of error is no more than 1/4 and often * Randomly the chance of error is no more than 1/4 and often
* very much lower. * very much lower.
*/ */
int mp_prime_miller_rabin (mp_int * a, const mp_int * b, int *result) static int mp_prime_miller_rabin (mp_int * a, const mp_int * b, int *result)
{ {
mp_int n1, y, r; mp_int n1, y, r;
int s, j, err; int s, j, err;
...@@ -3264,6 +3253,68 @@ __N1:mp_clear (&n1); ...@@ -3264,6 +3253,68 @@ __N1:mp_clear (&n1);
return err; return err;
} }
/* performs a variable number of rounds of Miller-Rabin
*
* Probability of error after t rounds is no more than
*
* Sets result to 1 if probably prime, 0 otherwise
*/
static int mp_prime_is_prime (mp_int * a, int t, int *result)
{
mp_int b;
int ix, err, res;
/* default to no */
*result = MP_NO;
/* valid value of t? */
if (t <= 0 || t > PRIME_SIZE) {
return MP_VAL;
}
/* is the input equal to one of the primes in the table? */
for (ix = 0; ix < PRIME_SIZE; ix++) {
if (mp_cmp_d(a, __prime_tab[ix]) == MP_EQ) {
*result = 1;
return MP_OKAY;
}
}
/* first perform trial division */
if ((err = mp_prime_is_divisible (a, &res)) != MP_OKAY) {
return err;
}
/* return if it was trivially divisible */
if (res == MP_YES) {
return MP_OKAY;
}
/* now perform the miller-rabin rounds */
if ((err = mp_init (&b)) != MP_OKAY) {
return err;
}
for (ix = 0; ix < t; ix++) {
/* set the prime */
mp_set (&b, __prime_tab[ix]);
if ((err = mp_prime_miller_rabin (a, &b, &res)) != MP_OKAY) {
goto __B;
}
if (res == MP_NO) {
goto __B;
}
}
/* passed the test */
*result = MP_YES;
__B:mp_clear (&b);
return err;
}
static const struct { static const struct {
int k, t; int k, t;
} sizes[] = { } sizes[] = {
...@@ -3574,57 +3625,6 @@ int mp_reduce_setup (mp_int * a, const mp_int * b) ...@@ -3574,57 +3625,6 @@ int mp_reduce_setup (mp_int * a, const mp_int * b)
return mp_div (a, b, a, NULL); return mp_div (a, b, a, NULL);
} }
/* shift right a certain amount of digits */
void mp_rshd (mp_int * a, int b)
{
int x;
/* if b <= 0 then ignore it */
if (b <= 0) {
return;
}
/* if b > used then simply zero it and return */
if (a->used <= b) {
mp_zero (a);
return;
}
{
register mp_digit *bottom, *top;
/* shift the digits down */
/* bottom */
bottom = a->dp;
/* top [offset into digits] */
top = a->dp + b;
/* this is implemented as a sliding window where
* the window is b-digits long and digits from
* the top of the window are copied to the bottom
*
* e.g.
b-2 | b-1 | b0 | b1 | b2 | ... | bb | ---->
/\ | ---->
\-------------------/ ---->
*/
for (x = 0; x < (a->used - b); x++) {
*bottom++ = *top++;
}
/* zero the top digits */
for (; x < a->used; x++) {
*bottom++ = 0;
}
}
/* remove excess digits */
a->used -= b;
}
/* set to a digit */ /* set to a digit */
void mp_set (mp_int * a, mp_digit b) void mp_set (mp_int * a, mp_digit b)
{ {
......
...@@ -275,12 +275,6 @@ void mp_clamp(mp_int *a); ...@@ -275,12 +275,6 @@ void mp_clamp(mp_int *a);
/* ---> digit manipulation <--- */ /* ---> digit manipulation <--- */
/* right shift by "b" digits */
void mp_rshd(mp_int *a, int b);
/* computes a = 2**b */
int mp_2expt(mp_int *a, int b);
/* Counts the number of lsbs which are zero before the first zero bit */ /* Counts the number of lsbs which are zero before the first zero bit */
int mp_cnt_lsb(const mp_int *a); int mp_cnt_lsb(const mp_int *a);
...@@ -304,9 +298,6 @@ int mp_and(mp_int *a, mp_int *b, mp_int *c); ...@@ -304,9 +298,6 @@ int mp_and(mp_int *a, mp_int *b, mp_int *c);
/* b = -a */ /* b = -a */
int mp_neg(mp_int *a, mp_int *b); int mp_neg(mp_int *a, mp_int *b);
/* b = |a| */
int mp_abs(const mp_int *a, mp_int *b);
/* compare a to b */ /* compare a to b */
int mp_cmp(const mp_int *a, const mp_int *b); int mp_cmp(const mp_int *a, const mp_int *b);
...@@ -333,9 +324,6 @@ int mp_mod(const mp_int *a, mp_int *b, mp_int *c); ...@@ -333,9 +324,6 @@ int mp_mod(const mp_int *a, mp_int *b, mp_int *c);
/* compare against a single digit */ /* compare against a single digit */
int mp_cmp_d(const mp_int *a, mp_digit b); int mp_cmp_d(const mp_int *a, mp_digit b);
/* c = a + b */
int mp_add_d(mp_int *a, mp_digit b, mp_int *c);
/* c = a - b */ /* c = a - b */
int mp_sub_d(mp_int *a, mp_digit b, mp_int *c); int mp_sub_d(mp_int *a, mp_digit b, mp_int *c);
...@@ -427,33 +415,16 @@ int mp_exptmod(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d); ...@@ -427,33 +415,16 @@ int mp_exptmod(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d);
/* number of primes */ /* number of primes */
#define PRIME_SIZE 256 #define PRIME_SIZE 256
/* result=1 if a is divisible by one of the first PRIME_SIZE primes */
int mp_prime_is_divisible(const mp_int *a, int *result);
/* performs one Fermat test of "a" using base "b". /* performs one Fermat test of "a" using base "b".
* Sets result to 0 if composite or 1 if probable prime * Sets result to 0 if composite or 1 if probable prime
*/ */
int mp_prime_fermat(mp_int *a, mp_int *b, int *result); int mp_prime_fermat(mp_int *a, mp_int *b, int *result);
/* performs one Miller-Rabin test of "a" using base "b".
* Sets result to 0 if composite or 1 if probable prime
*/
int mp_prime_miller_rabin(mp_int *a, const mp_int *b, int *result);
/* This gives [for a given bit size] the number of trials required /* This gives [for a given bit size] the number of trials required
* such that Miller-Rabin gives a prob of failure lower than 2^-96 * such that Miller-Rabin gives a prob of failure lower than 2^-96
*/ */
int mp_prime_rabin_miller_trials(int size); int mp_prime_rabin_miller_trials(int size);
/* performs t rounds of Miller-Rabin on "a" using the first
* t prime bases. Also performs an initial sieve of trial
* division. Determines if "a" is prime with probability
* of error no more than (1/4)**t.
*
* Sets result to 1 if probably prime, 0 otherwise
*/
int mp_prime_is_prime(mp_int *a, int t, int *result);
/* finds the next prime after the number "a" using "t" trials /* finds the next prime after the number "a" using "t" trials
* of Miller-Rabin. * of Miller-Rabin.
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment