mirror of
https://github.com/cryb-to/cryb-to.git
synced 2024-12-22 12:31:07 +00:00
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:
parent
894cc207b9
commit
f6905c8edb
2 changed files with 12 additions and 7 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2014 Dag-Erling Smørgrav
|
* Copyright (c) 2014-2017 Dag-Erling Smørgrav
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* 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;
|
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)
|
if (A == B || mpi_eq_abs(A, B)) {
|
||||||
return (0);
|
if (X != A && X != B && mpi_copy(X, A) != 0)
|
||||||
if (A == B) {
|
|
||||||
if (X != A && mpi_copy(X, A) != 0)
|
|
||||||
return (-1);
|
return (-1);
|
||||||
return (mpi_lshift(X, 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
|
* 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;
|
break;
|
||||||
/* add msw offset */
|
/* add msw offset */
|
||||||
X->msb += i * 32 + 1;
|
X->msb += i * 32 + 1;
|
||||||
|
X->neg = 0;
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2014 Dag-Erling Smørgrav
|
* Copyright (c) 2014-2017 Dag-Erling Smørgrav
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* 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;
|
break;
|
||||||
/* add msw offset */
|
/* add msw offset */
|
||||||
X->msb += i * 32 + 1;
|
X->msb += i * 32 + 1;
|
||||||
|
X->neg = 0;
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue