aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2018-12-22 08:45:09 -0500
committerJack Lloyd <[email protected]>2018-12-22 08:49:34 -0500
commit480046433becaa5e52ab85dfedf2a1061c78f65b (patch)
tree66d6c49acc1f42cc32d33ede51bf722b4c0aff4b /src/lib
parent108ccfa35f5d24e41c586c5b1184934f6f538d52 (diff)
Promote ct_is_zero and expand_top_bit to bit_ops.h
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/utils/bit_ops.h18
-rw-r--r--src/lib/utils/ct_utils.h13
2 files changed, 21 insertions, 10 deletions
diff --git a/src/lib/utils/bit_ops.h b/src/lib/utils/bit_ops.h
index f32250b9c..75cefbdfa 100644
--- a/src/lib/utils/bit_ops.h
+++ b/src/lib/utils/bit_ops.h
@@ -17,6 +17,24 @@
namespace Botan {
/**
+* If top bit of arg is set, return ~0. Otherwise return 0.
+*/
+template<typename T>
+inline T expand_top_bit(T a)
+ {
+ return static_cast<T>(0) - (a >> (sizeof(T)*8-1));
+ }
+
+/**
+* If arg is zero, return ~0. Otherwise return 0
+*/
+template<typename T>
+inline T ct_is_zero(T x)
+ {
+ return expand_top_bit<T>(~x & (x - 1));
+ }
+
+/**
* Power of 2 test. T should be an unsigned integer type
* @param arg an integer value
* @return true iff arg is 2^n for some n > 0
diff --git a/src/lib/utils/ct_utils.h b/src/lib/utils/ct_utils.h
index 72be79114..941df839a 100644
--- a/src/lib/utils/ct_utils.h
+++ b/src/lib/utils/ct_utils.h
@@ -15,6 +15,7 @@
#define BOTAN_CT_UTILS_H_
#include <botan/secmem.h>
+#include <botan/internal/bit_ops.h>
#include <type_traits>
#include <vector>
@@ -139,7 +140,7 @@ class Mask
*/
static Mask<T> is_zero(T x)
{
- return Mask<T>(expand_top_bit(~x & (x - 1)));
+ return Mask<T>(ct_is_zero<T>(x));
}
/**
@@ -155,7 +156,7 @@ class Mask
*/
static Mask<T> is_lt(T x, T y)
{
- return Mask<T>(expand_top_bit(x^((x^y) | ((x-y)^x))));
+ return Mask<T>(expand_top_bit<T>(x^((x^y) | ((x-y)^x))));
}
/**
@@ -329,14 +330,6 @@ class Mask
}
private:
- /**
- * If top bit of arg is set, return ~0. Otherwise return 0.
- */
- static T expand_top_bit(T a)
- {
- return static_cast<T>(0) - (a >> (sizeof(T)*8-1));
- }
-
Mask(T m) : m_mask(m) {}
T m_mask;