MPILIB: Fix comparison of negative MPIs
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
@@ -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
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
@@ -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
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_cmpfound for us), otherwise cmp itself is the answer. Negating cmp is ok sincempihelp_cmpreturns {-1,0,1}; -INT_MIN==INT_MIN won't bite us.Signed-off-by Rasmus Villemoes <[email protected]>--- lib/mpi/mpi-cmp.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)lib/mpi/mpi-cmp.c +3
-5@@ -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
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_cmpfound for us), otherwise cmp itself is the answer. Negating cmp is ok sincempihelp_cmpreturns {-1,0,1}; -INT_MIN==INT_MIN won't bite us.Signed-off-by Rasmus Villemoes <[email protected]>--- lib/mpi/mpi-cmp.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)lib/mpi/mpi-cmp.c +3
-6@@ -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
David Howells
Acked-by Dmitry KasatkinRe: [Keyrings] [PATCH 2/2] MPILIB: Deobfuscate mpi_cmp
Quoted 1 line · Dmitry Kasatkin
Ack.
To what email address do I translate that now?
perchance?
David
Dmitry Kasatkin
Acked-byRe: [Keyrings] [PATCH 2/2] MPILIB: Deobfuscate mpi_cmp
correct.
Dmitry
Quoted 11 lines · David Howells
Quoted 1 line · Dmitry Kasatkin
Ack.
To what email address do I translate that now?
Acked-by Dmitry Kasatkin <[email protected]>perchance?
David
Signature · 2 lines
Thanks, Dmitry
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_cmpwill always return a positive value - this is obviously bogus. u is smaller than v if and only if it is larger in absolute value.Signed-off-by Rasmus Villemoes <[email protected]>--- lib/mpi/mpi-cmp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)lib/mpi/mpi-cmp.c +1
-1@@ -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
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_cmpwill always return a positive value - this is obviously bogus. u is smaller than v if and only if it is larger in absolute value.Signed-off-by Rasmus Villemoes <[email protected]>--- lib/mpi/mpi-cmp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)lib/mpi/mpi-cmp.c +1
-2@@ -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