Skip to contentSkip to conversation

MPILIB: Fix comparison of negative MPIs

PATCH In review Rasmus Villemoes 3 participants 8 messages Dec 6, 2014 – Jan 12, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 MPILIB: Fix comparison of negative MPIs

If u and v both represent negative integers and their limb counts happen to differ, mpi_cmp will always return a positive value - this is obviously bogus. u is smaller than v if and only if it is larger in absolute value.

---
 lib/mpi/mpi-cmp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/mpi/mpi-cmp.c +1 -1
index 1871e7b61ca0..3801694240d8 100644
@@ -57,7 +57,7 @@ int mpi_cmp(MPI u, MPI v)
 	if (usize != vsize && !u->sign && !v->sign)
 		return usize - vsize;
 	if (usize != vsize && u->sign && v->sign)
-		return vsize + usize;
+		return vsize - usize;
 	if (!usize)
 		return 0;
 	cmp = mpihelp_cmp(u->d, v->d, usize);
Signature · 1 line
2.1.3
#1 lore raw

PATCH 2/2 MPILIB: Deobfuscate mpi_cmp

The condition preceding 'return 1;' makes my head hurt. At this point, we know that u and v have the same sign; if they are negative, they compare opposite to how their absolute values compare (which mpihelp_cmp found for us), otherwise cmp itself is the answer. Negating cmp is ok since mpihelp_cmp returns {-1,0,1}; -INT_MIN==INT_MIN won't bite us.

---
 lib/mpi/mpi-cmp.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
lib/mpi/mpi-cmp.c +3 -5
index 3801694240d8..d25e9e96c310 100644
@@ -61,10 +61,8 @@ int mpi_cmp(MPI u, MPI v)
 	if (!usize)
 		return 0;
 	cmp = mpihelp_cmp(u->d, v->d, usize);
-	if (!cmp)
-		return 0;
-	if ((cmp < 0 ? 1 : 0) == (u->sign ? 1 : 0))
-		return 1;
-	return -1;
+	if (u->sign)
+		return -cmp;
+	return cmp;
 }
 EXPORT_SYMBOL_GPL(mpi_cmp);
Signature · 1 line
2.1.3
#2 lore raw

Re: [PATCH 2/2] MPILIB: Deobfuscate mpi_cmp

This looks very reasonable. cc'ing Dmitry for his check.

David
---

Quoted 32 lines · Rasmus Villemoes

The condition preceding 'return 1;' makes my head hurt. At this point, we know that u and v have the same sign; if they are negative, they compare opposite to how their absolute values compare (which mpihelp_cmp found for us), otherwise cmp itself is the answer. Negating cmp is ok since mpihelp_cmp returns {-1,0,1}; -INT_MIN==INT_MIN won't bite us.

---
 lib/mpi/mpi-cmp.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
lib/mpi/mpi-cmp.c +3 -5
index 3801694240d8..d25e9e96c310 100644
@@ -61,10 +61,8 @@ int mpi_cmp(MPI u, MPI v)
 	if (!usize)
 		return 0;
 	cmp = mpihelp_cmp(u->d, v->d, usize);
-	if (!cmp)
-		return 0;
-	if ((cmp < 0 ? 1 : 0) == (u->sign ? 1 : 0))
-		return 1;
-	return -1;
+	if (u->sign)
+		return -cmp;
+	return cmp;
 }
 EXPORT_SYMBOL_GPL(mpi_cmp);
Signature · 1 line
2.1.3
#3 lore raw

Re: [Keyrings] [PATCH 2/2] MPILIB: Deobfuscate mpi_cmp

Hi,

Thank you. Indeed '-cmp' is much more clear.

Ack.

- Dmitry

Quoted 42 lines · David Howells

This looks very reasonable. cc'ing Dmitry for his check.

David
---

Quoted 31 lines · Rasmus Villemoes

The condition preceding 'return 1;' makes my head hurt. At this point, we know that u and v have the same sign; if they are negative, they compare opposite to how their absolute values compare (which mpihelp_cmp found for us), otherwise cmp itself is the answer. Negating cmp is ok since mpihelp_cmp returns {-1,0,1}; -INT_MIN==INT_MIN won't bite us.

---
 lib/mpi/mpi-cmp.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
lib/mpi/mpi-cmp.c +3 -6
index 3801694240d8..d25e9e96c310 100644
@@ -61,10 +61,8 @@ int mpi_cmp(MPI u, MPI v)
      if (!usize)
              return 0;
      cmp = mpihelp_cmp(u->d, v->d, usize);
-     if (!cmp)
-             return 0;
-     if ((cmp < 0 ? 1 : 0) == (u->sign ? 1 : 0))
-             return 1;
-     return -1;
+     if (u->sign)
+             return -cmp;
+     return cmp;
 }
 EXPORT_SYMBOL_GPL(mpi_cmp);
--
Quoted 1 line

2.1.3

_______________________________________________
Keyrings mailing list
[email protected]
To change your subscription to this list, please see http://linux-nfs.org/cgi-bin/mailman/listinfo/keyrings

Signature · 2 lines
Thanks,
Dmitry
#6 lore raw
In reply to Rasmus Villemoes

I think you're right - *adding* the two sizes makes no sense. cc'ing Dmitry also for his check.

David

Quoted 26 lines · Rasmus Villemoes

If u and v both represent negative integers and their limb counts happen to differ, mpi_cmp will always return a positive value - this is obviously bogus. u is smaller than v if and only if it is larger in absolute value.

---
 lib/mpi/mpi-cmp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/mpi/mpi-cmp.c +1 -1
index 1871e7b61ca0..3801694240d8 100644
@@ -57,7 +57,7 @@ int mpi_cmp(MPI u, MPI v)
 	if (usize != vsize && !u->sign && !v->sign)
 		return usize - vsize;
 	if (usize != vsize && u->sign && v->sign)
-		return vsize + usize;
+		return vsize - usize;
 	if (!usize)
 		return 0;
 	cmp = mpihelp_cmp(u->d, v->d, usize);
Signature · 1 line
2.1.3
#7 lore raw

Hi,

Thank you. It looks correct.

Ack.

- Dmitry

Quoted 38 lines · David Howells

I think you're right - *adding* the two sizes makes no sense. cc'ing Dmitry also for his check.

David

Quoted 24 lines · Rasmus Villemoes

If u and v both represent negative integers and their limb counts happen to differ, mpi_cmp will always return a positive value - this is obviously bogus. u is smaller than v if and only if it is larger in absolute value.

---
 lib/mpi/mpi-cmp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/mpi/mpi-cmp.c +1 -2
index 1871e7b61ca0..3801694240d8 100644
@@ -57,7 +57,7 @@ int mpi_cmp(MPI u, MPI v)
      if (usize != vsize && !u->sign && !v->sign)
              return usize - vsize;
      if (usize != vsize && u->sign && v->sign)
-             return vsize + usize;
+             return vsize - usize;
      if (!usize)
              return 0;
      cmp = mpihelp_cmp(u->d, v->d, usize);
--
Quoted 2 lines

2.1.3

_______________________________________________
Keyrings mailing list
[email protected]
To change your subscription to this list, please see http://linux-nfs.org/cgi-bin/mailman/listinfo/keyrings

Signature · 2 lines
Thanks,
Dmitry