Add mpi_eq_abs() and mpi_eq() predicates.

This commit is contained in:
Dag-Erling Smørgrav 2014-12-30 11:12:11 +00:00 committed by des
parent b6368af502
commit c5b962387c
2 changed files with 26 additions and 1 deletions

View file

@ -58,5 +58,7 @@ int mpi_add_abs(cryb_mpi *, cryb_mpi *, cryb_mpi *);
int mpi_sub_abs(cryb_mpi *, cryb_mpi *, cryb_mpi *); int mpi_sub_abs(cryb_mpi *, cryb_mpi *, cryb_mpi *);
int mpi_cmp_abs(cryb_mpi *, cryb_mpi *); int mpi_cmp_abs(cryb_mpi *, cryb_mpi *);
int mpi_cmp(cryb_mpi *, cryb_mpi *); int mpi_cmp(cryb_mpi *, cryb_mpi *);
int mpi_eq_abs(cryb_mpi *, cryb_mpi *);
int mpi_eq(cryb_mpi *, cryb_mpi *);
#endif #endif

View file

@ -31,6 +31,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <cryb/mpi.h> #include <cryb/mpi.h>
@ -51,7 +52,7 @@ mpi_cmp_abs(cryb_mpi *X, cryb_mpi *Y)
if (X->msb < Y->msb) if (X->msb < Y->msb)
return (-1); return (-1);
/* no luck, compare word by word */ /* no luck, compare word by word */
for (i = X->msb / 32; i >= 0; --i) { for (i = (X->msb + 31) / 32; i >= 0; --i) {
if (X->words[i] > Y->words[i]) if (X->words[i] > Y->words[i])
return (1); return (1);
if (X->words[i] < Y->words[i]) if (X->words[i] < Y->words[i])
@ -79,3 +80,25 @@ mpi_cmp(cryb_mpi *X, cryb_mpi *Y)
return (mpi_cmp_abs(X, Y)); return (mpi_cmp_abs(X, Y));
} }
} }
/*
* Compare for equality
*/
int
mpi_eq_abs(cryb_mpi *A, cryb_mpi *B)
{
return (A->msb == B->msb &&
memcmp(A->words, B->words, (A->msb + 31) / 32) == 0);
}
/*
* Compare for equality
*/
int
mpi_eq(cryb_mpi *A, cryb_mpi *B)
{
return (A->neg == B->neg && A->msb != B->msb &&
memcmp(A->words, B->words, (A->msb + 31) / 32) == 0);
}