diff --git a/lib/mpi/cryb_mpi_add_abs.c b/lib/mpi/cryb_mpi_add_abs.c index 258ae30..29b8396 100644 --- a/lib/mpi/cryb_mpi_add_abs.c +++ b/lib/mpi/cryb_mpi_add_abs.c @@ -92,7 +92,7 @@ mpi_add_abs(cryb_mpi *X, const cryb_mpi *A, const cryb_mpi *B) */ /* add B into X word by word until we run out of B */ - for (c = i = 0; i < (B->msb + 31) / 32; ++i) { + for (c = i = 0; i < MPI_MSW(B); ++i) { X->words[i] += c; c = (X->words[i] < c); X->words[i] += B->words[i]; diff --git a/lib/mpi/cryb_mpi_eq.c b/lib/mpi/cryb_mpi_eq.c index 2ea3a54..50eed59 100644 --- a/lib/mpi/cryb_mpi_eq.c +++ b/lib/mpi/cryb_mpi_eq.c @@ -47,5 +47,5 @@ mpi_eq(const cryb_mpi *A, const cryb_mpi *B) return (1); if (A->neg != B->neg || A->msb != B->msb) return (0); - return (memcmp(A->words, B->words, (A->msb + 31) / 32) == 0); + return (memcmp(A->words, B->words, MPI_MSW(A) * 4) == 0); } diff --git a/lib/mpi/cryb_mpi_eq_abs.c b/lib/mpi/cryb_mpi_eq_abs.c index 7e0e21d..9070830 100644 --- a/lib/mpi/cryb_mpi_eq_abs.c +++ b/lib/mpi/cryb_mpi_eq_abs.c @@ -47,5 +47,5 @@ mpi_eq_abs(const cryb_mpi *A, const cryb_mpi *B) return (1); if (A->msb != B->msb) return (0); - return (memcmp(A->words, B->words, (A->msb + 31) / 32) == 0); + return (memcmp(A->words, B->words, MPI_MSW(A) * 4) == 0); } diff --git a/lib/mpi/cryb_mpi_impl.h b/lib/mpi/cryb_mpi_impl.h index 4c780b5..7a73588 100644 --- a/lib/mpi/cryb_mpi_impl.h +++ b/lib/mpi/cryb_mpi_impl.h @@ -30,6 +30,9 @@ /* n rounded up to nearest multiple of p */ #define RUP(n, p) ((((n) + (p) - 1) / (p)) * (p)) +/* one-based index of the most significant word of an mpi */ +#define MPI_MSW(n) (((n)->msb + 31) / 32) + /* * Initialize an all-zeroes mpi. */ diff --git a/lib/mpi/cryb_mpi_mul_abs.c b/lib/mpi/cryb_mpi_mul_abs.c index 37f5ad6..8dc651d 100644 --- a/lib/mpi/cryb_mpi_mul_abs.c +++ b/lib/mpi/cryb_mpi_mul_abs.c @@ -72,8 +72,8 @@ mpi_mul_abs(cryb_mpi *X, const cryb_mpi *A, const cryb_mpi *B) */ P = (X == A || X == B) ? &T : X; mpi_zero(P); - a = (A->msb + 31) / 32; - b = (B->msb + 31) / 32; + a = MPI_MSW(A); + b = MPI_MSW(B); if (mpi_grow(P, (a + b) * 32) != 0) return (-1); diff --git a/lib/mpi/cryb_mpi_sub_abs.c b/lib/mpi/cryb_mpi_sub_abs.c index da6f46a..3a1c9db 100644 --- a/lib/mpi/cryb_mpi_sub_abs.c +++ b/lib/mpi/cryb_mpi_sub_abs.c @@ -81,7 +81,7 @@ mpi_sub_abs(cryb_mpi *X, const cryb_mpi *A, const cryb_mpi *B) return (-1); /* subtract L from G word by word until we run out of L */ - for (c = i = 0; i < (L->msb + 31) / 32; ++i) { + for (c = i = 0; i < MPI_MSW(L); ++i) { cn = G->words[i] < c || G->words[i] - c < L->words[i]; X->words[i] = G->words[i] - L->words[i] - c; diff --git a/t/t_mpi_compar.c b/t/t_mpi_compar.c index 03f30c2..a66b2d8 100644 --- a/t/t_mpi_compar.c +++ b/t/t_mpi_compar.c @@ -107,6 +107,10 @@ static struct t_cmp_case { { "3 > 1", 3, 1, 1 }, { "3 > 2", 3, 2, 1 }, { "3 == 3", 3, 3, 0 }, + + /* 0x200 and 0x300: same msb, identical lowest byte */ + { "512 < 768", 512, 768, -1 }, + { "768 > 512", 768, 512, 1 }, }; /*