Fix bugs in cryb_mpi_{add,sub}_abs() caused by assuming that the target is initally positive zero.

If its operands were identical, cryb_mpi_add_abs() would leave the target untouched.  Explicitly call mpi_zero() before returning.  While there, extend the “identical operands” shortcut to also cover equality.

Both cryb_mpi_add_abs() and cryb_mpi_sub_abs() would leave the target's negative flag untouched.  Explicitly clear it before returning.
This commit is contained in:
Dag-Erling Smørgrav 2017-02-19 17:20:40 +01:00
parent 894cc207b9
commit f6905c8edb
2 changed files with 12 additions and 7 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014 Dag-Erling Smørgrav
* Copyright (c) 2014-2017 Dag-Erling Smørgrav
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -46,15 +46,18 @@ mpi_add_abs(cryb_mpi *X, cryb_mpi *A, cryb_mpi *B)
uint32_t c;
/*
* Trivial cases: A and B are identical and / or both zero.
* Trivial cases: A and B are the same or equal or at least one of
* them is zero.
*/
if (A->msb == 0 && B->msb == 0)
return (0);
if (A == B) {
if (X != A && mpi_copy(X, A) != 0)
if (A == B || mpi_eq_abs(A, B)) {
if (X != A && X != B && mpi_copy(X, A) != 0)
return (-1);
return (mpi_lshift(X, 1));
}
if (A->msb == 0)
return (X == B ? 0 : mpi_copy(X, B));
if (B->msb == 0)
return (X == A ? 0 : mpi_copy(X, A));
/*
* Normalize our operands: if X is identical to either A or B, we
@ -97,5 +100,6 @@ mpi_add_abs(cryb_mpi *X, cryb_mpi *A, cryb_mpi *B)
break;
/* add msw offset */
X->msb += i * 32 + 1;
X->neg = 0;
return (0);
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014 Dag-Erling Smørgrav
* Copyright (c) 2014-2017 Dag-Erling Smørgrav
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -92,5 +92,6 @@ mpi_sub_abs(cryb_mpi *X, cryb_mpi *A, cryb_mpi *B)
break;
/* add msw offset */
X->msb += i * 32 + 1;
X->neg = 0;
return (0);
}