diff --git a/include/cryb/mpi.h b/include/cryb/mpi.h index ca7d515..ba657b2 100644 --- a/include/cryb/mpi.h +++ b/include/cryb/mpi.h @@ -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_cmp_abs(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 diff --git a/lib/mpi/mpi_cmp.c b/lib/mpi/mpi_cmp.c index 55c547a..8d7a348 100644 --- a/lib/mpi/mpi_cmp.c +++ b/lib/mpi/mpi_cmp.c @@ -31,6 +31,7 @@ #include #include +#include #include @@ -51,7 +52,7 @@ mpi_cmp_abs(cryb_mpi *X, cryb_mpi *Y) if (X->msb < Y->msb) return (-1); /* 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]) return (1); 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)); } } + +/* + * 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); +}