mirror of
https://github.com/cuberite/polarssl.git
synced 2025-10-03 10:34:16 -04:00
Update mbedtls_mpi_lt_mpi_ct to new interface
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
This commit is contained in:
parent
589ccb8aaa
commit
1a7a5626ec
@ -61,9 +61,7 @@ int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi *X,
|
|||||||
const mbedtls_mpi *Y,
|
const mbedtls_mpi *Y,
|
||||||
unsigned *ret)
|
unsigned *ret)
|
||||||
{
|
{
|
||||||
size_t i;
|
mbedtls_ct_condition_t cond, X_is_negative, Y_is_negative, result;
|
||||||
/* The value of any of these variables is either 0 or 1 at all times. */
|
|
||||||
unsigned cond, done, X_is_negative, Y_is_negative;
|
|
||||||
|
|
||||||
MPI_VALIDATE_RET(X != NULL);
|
MPI_VALIDATE_RET(X != NULL);
|
||||||
MPI_VALIDATE_RET(Y != NULL);
|
MPI_VALIDATE_RET(Y != NULL);
|
||||||
@ -77,46 +75,33 @@ int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi *X,
|
|||||||
* Set sign_N to 1 if N >= 0, 0 if N < 0.
|
* Set sign_N to 1 if N >= 0, 0 if N < 0.
|
||||||
* We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
|
* We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
|
||||||
*/
|
*/
|
||||||
X_is_negative = (X->s & 2) >> 1;
|
X_is_negative = mbedtls_ct_bool((X->s & 2) >> 1);
|
||||||
Y_is_negative = (Y->s & 2) >> 1;
|
Y_is_negative = mbedtls_ct_bool((Y->s & 2) >> 1);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If the signs are different, then the positive operand is the bigger.
|
* If the signs are different, then the positive operand is the bigger.
|
||||||
* That is if X is negative (X_is_negative == 1), then X < Y is true and it
|
* That is if X is negative (X_is_negative == 1), then X < Y is true and it
|
||||||
* is false if X is positive (X_is_negative == 0).
|
* is false if X is positive (X_is_negative == 0).
|
||||||
*/
|
*/
|
||||||
cond = (X_is_negative ^ Y_is_negative);
|
cond = mbedtls_ct_bool_xor(X_is_negative, Y_is_negative); // non-zero if different sign
|
||||||
*ret = cond & X_is_negative;
|
result = mbedtls_ct_bool_and(cond, X_is_negative);
|
||||||
|
|
||||||
/*
|
/* Assuming signs are the same, compare X and Y. We switch the comparison
|
||||||
* This is a constant-time function. We might have the result, but we still
|
* order if they are negative so that we get the right result, regardles of
|
||||||
* need to go through the loop. Record if we have the result already.
|
* sign.
|
||||||
|
*
|
||||||
|
* Store in ret iff the signs are the same (i.e., iff cond == 0). If
|
||||||
|
* the signs differ, done has already been set.
|
||||||
*/
|
*/
|
||||||
done = cond;
|
|
||||||
|
|
||||||
for (i = X->n; i > 0; i--) {
|
/* This is used to conditionally swap the pointers in const time */
|
||||||
/*
|
void * const p[2] = { X->p, Y->p };
|
||||||
* If Y->p[i - 1] < X->p[i - 1] then X < Y is true if and only if both
|
mbedtls_ct_condition_t lt = mbedtls_mpi_core_lt_ct(
|
||||||
* X and Y are negative.
|
p[X_is_negative & 1], p[(X_is_negative & 1) ^ 1], X->n);
|
||||||
*
|
|
||||||
* Again even if we can make a decision, we just mark the result and
|
|
||||||
* the fact that we are done and continue looping.
|
|
||||||
*/
|
|
||||||
cond = mbedtls_ct_mpi_uint_lt(Y->p[i - 1], X->p[i - 1]);
|
|
||||||
*ret |= cond & (1 - done) & X_is_negative;
|
|
||||||
done |= cond;
|
|
||||||
|
|
||||||
/*
|
result = mbedtls_ct_bool_or(result, mbedtls_ct_bool_and(mbedtls_ct_bool_not(cond), lt));
|
||||||
* If X->p[i - 1] < Y->p[i - 1] then X < Y is true if and only if both
|
|
||||||
* X and Y are positive.
|
*ret = mbedtls_ct_uint_if0(result, 1);
|
||||||
*
|
|
||||||
* Again even if we can make a decision, we just mark the result and
|
|
||||||
* the fact that we are done and continue looping.
|
|
||||||
*/
|
|
||||||
cond = mbedtls_ct_mpi_uint_lt(X->p[i - 1], Y->p[i - 1]);
|
|
||||||
*ret |= cond & (1 - done) & (1 - X_is_negative);
|
|
||||||
done |= cond;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user