From c5b962387ce0bd47ee6abb058c304f8a360e988a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= <des@des.no>
Date: Tue, 30 Dec 2014 11:12:11 +0000
Subject: [PATCH] Add mpi_eq_abs() and mpi_eq() predicates.

---
 include/cryb/mpi.h |  2 ++
 lib/mpi/mpi_cmp.c  | 25 ++++++++++++++++++++++++-
 2 files changed, 26 insertions(+), 1 deletion(-)

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 <stdint.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include <cryb/mpi.h>
 
@@ -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);
+}