diff options
author | Jack Lloyd <[email protected]> | 2018-12-05 11:15:54 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-12-05 11:15:54 -0500 |
commit | 29ca218961972caa8dd2d9f48a1fe4bc9e429109 (patch) | |
tree | 6d8124a747f945d6d57b8c858eada69aa6be4d70 /src/lib/math | |
parent | 5609fbc43c2277947732377c598942ce72aab64a (diff) |
Make BigInt::cond_flip_sign constant time
Diffstat (limited to 'src/lib/math')
-rw-r--r-- | src/lib/math/bigint/bigint.cpp | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/src/lib/math/bigint/bigint.cpp b/src/lib/math/bigint/bigint.cpp index 1a09a92f1..9fd019d87 100644 --- a/src/lib/math/bigint/bigint.cpp +++ b/src/lib/math/bigint/bigint.cpp @@ -402,9 +402,15 @@ void BigInt::ct_cond_swap(bool predicate, BigInt& other) void BigInt::cond_flip_sign(bool predicate) { - // FIXME! - if(predicate) - flip_sign(); + // This code is assuming Negative == 0, Positive == 1 + + const auto mask = CT::Mask<uint8_t>::expand(predicate); + + const uint8_t current_sign = static_cast<uint8_t>(sign()); + + const uint8_t new_sign = mask.select(current_sign ^ 1, current_sign); + + set_sign(static_cast<Sign>(new_sign)); } void BigInt::ct_cond_assign(bool predicate, const BigInt& other) |