Refactor the comparison predicates, check for identity first.

This commit is contained in:
Dag-Erling Smørgrav 2017-03-29 19:56:53 +02:00
parent 47a0bf838f
commit a11c52e896
5 changed files with 48 additions and 16 deletions

View file

@ -43,15 +43,10 @@ int
mpi_cmp(const cryb_mpi *X, const cryb_mpi *Y) mpi_cmp(const cryb_mpi *X, const cryb_mpi *Y)
{ {
if (X->neg) { if (X == Y)
if (Y->neg) return (0);
return (-mpi_cmp_abs(X, Y)); else if (X->neg)
else return (Y->neg ? -mpi_cmp_abs(X, Y) : -1);
return (-1); else
} else { return (Y->neg ? 1 : mpi_cmp_abs(X, Y));
if (Y->neg)
return (1);
else
return (mpi_cmp_abs(X, Y));
}
} }

View file

@ -44,7 +44,9 @@ mpi_cmp_abs(const cryb_mpi *X, const cryb_mpi *Y)
{ {
int i; int i;
/* check width first */ /* check trivial cases first */
if (X == Y)
return (0);
if (X->msb > Y->msb) if (X->msb > Y->msb)
return (1); return (1);
if (X->msb < Y->msb) if (X->msb < Y->msb)

View file

@ -43,6 +43,9 @@ int
mpi_eq(const cryb_mpi *A, const cryb_mpi *B) mpi_eq(const cryb_mpi *A, const cryb_mpi *B)
{ {
return (A->neg == B->neg && A->msb == B->msb && if (A == B)
memcmp(A->words, B->words, (A->msb + 31) / 32) == 0); return (1);
if (A->neg != B->neg || A->msb != B->msb)
return (0);
return (memcmp(A->words, B->words, (A->msb + 31) / 32) == 0);
} }

View file

@ -43,6 +43,9 @@ int
mpi_eq_abs(const cryb_mpi *A, const cryb_mpi *B) mpi_eq_abs(const cryb_mpi *A, const cryb_mpi *B)
{ {
return (A->msb == B->msb && if (A == B)
memcmp(A->words, B->words, (A->msb + 31) / 32) == 0); return (1);
if (A->msb != B->msb)
return (0);
return (memcmp(A->words, B->words, (A->msb + 31) / 32) == 0);
} }

View file

@ -142,6 +142,33 @@ t_mpi_eq(char **desc CRYB_UNUSED, void *arg)
return (ret); return (ret);
} }
/*
* Compare an MPI with itself
*/
static int
t_mpi_cmp_ident(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&a, CRYB_TO);
ret &= t_compare_i(0, mpi_cmp(&a, &a));
mpi_destroy(&a);
return (ret);
}
static int
t_mpi_eq_ident(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&a, CRYB_TO);
ret &= t_compare_i(1, mpi_eq(&a, &a));
mpi_destroy(&a);
return (ret);
}
/* /*
* Compare an MPI with an integer * Compare an MPI with an integer
*/ */
@ -187,9 +214,11 @@ t_prepare(int argc, char *argv[])
t_mpi_prepare(); t_mpi_prepare();
/* comparison */ /* comparison */
t_add_test(t_mpi_cmp_ident, NULL, "mpi cmp mpi (identity)");
for (i = 0; i < sizeof t_cmp_cases / sizeof t_cmp_cases[0]; ++i) for (i = 0; i < sizeof t_cmp_cases / sizeof t_cmp_cases[0]; ++i)
t_add_test(t_mpi_cmp, &t_cmp_cases[i], t_add_test(t_mpi_cmp, &t_cmp_cases[i],
"mpi cmp mpi (%s)", t_cmp_cases[i].desc); "mpi cmp mpi (%s)", t_cmp_cases[i].desc);
t_add_test(t_mpi_eq_ident, NULL, "mpi eq mpi (identity)");
for (i = 0; i < sizeof t_cmp_cases / sizeof t_cmp_cases[0]; ++i) for (i = 0; i < sizeof t_cmp_cases / sizeof t_cmp_cases[0]; ++i)
t_add_test(t_mpi_eq, &t_cmp_cases[i], t_add_test(t_mpi_eq, &t_cmp_cases[i],
"mpi eq mpi (%s)", t_cmp_cases[i].desc); "mpi eq mpi (%s)", t_cmp_cases[i].desc);