aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/math/bigint/bigint.cpp
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2018-12-07 11:54:49 -0500
committerJack Lloyd <[email protected]>2018-12-07 11:54:49 -0500
commitefad37ca79d9749e4492a07d61f0356aad221193 (patch)
tree2c9a4ebf8820f9e95f936d0f72ed9ebc605d81c4 /src/lib/math/bigint/bigint.cpp
parentdbadf2e21b9925b593beab95bead38b94622c4f4 (diff)
parente7038bf0c5a8e083555c2ce4e00a11a74e55cf0a (diff)
Merge GH #1773 Add BigInt::ct_reduce_below
Diffstat (limited to 'src/lib/math/bigint/bigint.cpp')
-rw-r--r--src/lib/math/bigint/bigint.cpp27
1 files changed, 25 insertions, 2 deletions
diff --git a/src/lib/math/bigint/bigint.cpp b/src/lib/math/bigint/bigint.cpp
index dd5ee6ac3..a760be4e8 100644
--- a/src/lib/math/bigint/bigint.cpp
+++ b/src/lib/math/bigint/bigint.cpp
@@ -320,8 +320,8 @@ BigInt BigInt::operator-() const
size_t BigInt::reduce_below(const BigInt& p, secure_vector<word>& ws)
{
- if(p.is_negative())
- throw Invalid_Argument("BigInt::reduce_below mod must be positive");
+ if(p.is_negative() || this->is_negative())
+ throw Invalid_Argument("BigInt::reduce_below both values must be positive");
const size_t p_words = p.sig_words();
@@ -348,6 +348,29 @@ size_t BigInt::reduce_below(const BigInt& p, secure_vector<word>& ws)
return reductions;
}
+void BigInt::ct_reduce_below(const BigInt& mod, secure_vector<word>& ws, size_t bound)
+ {
+ if(mod.is_negative() || this->is_negative())
+ throw Invalid_Argument("BigInt::ct_reduce_below both values must be positive");
+
+ const size_t mod_words = mod.sig_words();
+
+ grow_to(mod_words);
+
+ const size_t sz = size();
+
+ ws.resize(sz);
+
+ clear_mem(ws.data(), sz);
+
+ for(size_t i = 0; i != bound; ++i)
+ {
+ word borrow = bigint_sub3(ws.data(), data(), sz, mod.data(), mod_words);
+
+ CT::Mask<word>::is_zero(borrow).select_n(mutable_data(), ws.data(), data(), sz);
+ }
+ }
+
/*
* Return the absolute value of this number
*/