Skip to content

Commit

Permalink
Add BigInt::_data
Browse files Browse the repository at this point in the history
This allows us to deprecate BigInt::data and still use it in
headers/inlines without triggering a warning in user code.
  • Loading branch information
randombit committed May 12, 2024
1 parent f3df66f commit 79db481
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 81 deletions.
22 changes: 11 additions & 11 deletions src/lib/math/bigint/big_ops2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ BigInt& BigInt::add(const word y[], size_t y_words, Sign y_sign) {
if(sign() == y_sign) {
bigint_add2(mutable_data(), size() - 1, y, y_words);
} else {
const int32_t relative_size = bigint_cmp(data(), x_sw, y, y_words);
const int32_t relative_size = bigint_cmp(_data(), x_sw, y, y_words);

if(relative_size >= 0) {
// *this >= y
Expand Down Expand Up @@ -71,15 +71,15 @@ BigInt& BigInt::mod_add(const BigInt& s, const BigInt& mod, secure_vector<word>&
ws.resize(3 * mod_sw);
}

word borrow = bigint_sub3(&ws[0], mod.data(), mod_sw, s.data(), mod_sw);
word borrow = bigint_sub3(&ws[0], mod._data(), mod_sw, s._data(), mod_sw);
BOTAN_DEBUG_ASSERT(borrow == 0);
BOTAN_UNUSED(borrow);

// Compute t - ws
borrow = bigint_sub3(&ws[mod_sw], this->data(), mod_sw, &ws[0], mod_sw);
borrow = bigint_sub3(&ws[mod_sw], this->_data(), mod_sw, &ws[0], mod_sw);

// Compute t + s
bigint_add3_nc(&ws[mod_sw * 2], this->data(), mod_sw, s.data(), mod_sw);
bigint_add3_nc(&ws[mod_sw * 2], this->_data(), mod_sw, s._data(), mod_sw);

CT::conditional_copy_mem(borrow, &ws[0], &ws[mod_sw * 2], &ws[mod_sw], mod_sw);
set_words(&ws[0], mod_sw);
Expand All @@ -106,11 +106,11 @@ BigInt& BigInt::mod_sub(const BigInt& s, const BigInt& mod, secure_vector<word>&
}

if(mod_sw == 4) {
bigint_mod_sub_n<4>(mutable_data(), s.data(), mod.data(), ws.data());
bigint_mod_sub_n<4>(mutable_data(), s._data(), mod._data(), ws.data());
} else if(mod_sw == 6) {
bigint_mod_sub_n<6>(mutable_data(), s.data(), mod.data(), ws.data());
bigint_mod_sub_n<6>(mutable_data(), s._data(), mod._data(), ws.data());
} else {
bigint_mod_sub(mutable_data(), s.data(), mod.data(), mod_sw, ws.data());
bigint_mod_sub(mutable_data(), s._data(), mod._data(), mod_sw, ws.data());
}

return (*this);
Expand All @@ -137,7 +137,7 @@ BigInt& BigInt::rev_sub(const word y[], size_t y_sw, secure_vector<word>& ws) {
ws.resize(std::max(x_sw, y_sw));
clear_mem(ws.data(), ws.size());

const int32_t relative_size = bigint_sub_abs(ws.data(), data(), x_sw, y, y_sw);
const int32_t relative_size = bigint_sub_abs(ws.data(), _data(), x_sw, y, y_sw);

this->cond_flip_sign(relative_size > 0);
this->swap_reg(ws);
Expand All @@ -163,7 +163,7 @@ BigInt& BigInt::mul(const BigInt& y, secure_vector<word>& ws) {
set_sign(Positive);
} else if(x_sw == 1 && y_sw) {
grow_to(y_sw + 1);
bigint_linmul3(mutable_data(), y.data(), y_sw, word_at(0));
bigint_linmul3(mutable_data(), y._data(), y_sw, word_at(0));
} else if(y_sw == 1 && x_sw) {
word carry = bigint_linmul2(mutable_data(), x_sw, y.word_at(0));
set_word_at(x_sw, carry);
Expand All @@ -172,7 +172,7 @@ BigInt& BigInt::mul(const BigInt& y, secure_vector<word>& ws) {
ws.resize(new_size);
secure_vector<word> z_reg(new_size);

bigint_mul(z_reg.data(), z_reg.size(), data(), size(), x_sw, y.data(), y.size(), y_sw, ws.data(), ws.size());
bigint_mul(z_reg.data(), z_reg.size(), _data(), size(), x_sw, y._data(), y.size(), y_sw, ws.data(), ws.size());

this->swap_reg(z_reg);
}
Expand All @@ -186,7 +186,7 @@ BigInt& BigInt::square(secure_vector<word>& ws) {
secure_vector<word> z(2 * sw);
ws.resize(z.size());

bigint_sqr(z.data(), z.size(), data(), size(), sw, ws.data(), ws.size());
bigint_sqr(z.data(), z.size(), _data(), size(), sw, ws.data(), ws.size());

swap_reg(z);
set_sign(BigInt::Positive);
Expand Down
18 changes: 9 additions & 9 deletions src/lib/math/bigint/big_ops3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ BigInt BigInt::add2(const BigInt& x, const word y[], size_t y_words, BigInt::Sig
BigInt z = BigInt::with_capacity(std::max(x_sw, y_words) + 1);

if(x.sign() == y_sign) {
bigint_add3(z.mutable_data(), x.data(), x_sw, y, y_words);
bigint_add3(z.mutable_data(), x._data(), x_sw, y, y_words);
z.set_sign(x.sign());
} else {
const int32_t relative_size = bigint_sub_abs(z.mutable_data(), x.data(), x_sw, y, y_words);
const int32_t relative_size = bigint_sub_abs(z.mutable_data(), x._data(), x_sw, y, y_words);

//z.sign_fixup(relative_size, y_sign);
if(relative_size < 0) {
Expand All @@ -50,18 +50,18 @@ BigInt operator*(const BigInt& x, const BigInt& y) {
BigInt z = BigInt::with_capacity(x.size() + y.size());

if(x_sw == 1 && y_sw) {
bigint_linmul3(z.mutable_data(), y.data(), y_sw, x.word_at(0));
bigint_linmul3(z.mutable_data(), y._data(), y_sw, x.word_at(0));
} else if(y_sw == 1 && x_sw) {
bigint_linmul3(z.mutable_data(), x.data(), x_sw, y.word_at(0));
bigint_linmul3(z.mutable_data(), x._data(), x_sw, y.word_at(0));
} else if(x_sw && y_sw) {
secure_vector<word> workspace(z.size());

bigint_mul(z.mutable_data(),
z.size(),
x.data(),
x._data(),
x.size(),
x_sw,
y.data(),
y._data(),
y.size(),
y_sw,
workspace.data(),
Expand All @@ -82,7 +82,7 @@ BigInt operator*(const BigInt& x, word y) {
BigInt z = BigInt::with_capacity(x_sw + 1);

if(x_sw && y) {
bigint_linmul3(z.mutable_data(), x.data(), x_sw, y);
bigint_linmul3(z.mutable_data(), x._data(), x_sw, y);
z.set_sign(x.sign());
}

Expand Down Expand Up @@ -176,7 +176,7 @@ BigInt operator<<(const BigInt& x, size_t shift) {

const size_t new_size = x_sw + (shift + BOTAN_MP_WORD_BITS - 1) / BOTAN_MP_WORD_BITS;
BigInt y = BigInt::with_capacity(new_size);
bigint_shl2(y.mutable_data(), x.data(), x_sw, shift);
bigint_shl2(y.mutable_data(), x._data(), x_sw, shift);
y.set_sign(x.sign());
return y;
}
Expand All @@ -193,7 +193,7 @@ BigInt operator>>(const BigInt& x, size_t shift) {
}

BigInt y = BigInt::with_capacity(x_sw - shift_words);
bigint_shr2(y.mutable_data(), x.data(), x_sw, shift);
bigint_shr2(y.mutable_data(), x._data(), x_sw, shift);

if(x.is_negative() && y.is_zero()) {
y.set_sign(BigInt::Positive);
Expand Down
26 changes: 13 additions & 13 deletions src/lib/math/bigint/bigint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ int32_t BigInt::cmp_word(word other) const {
return 1; // must be larger since other is just one word ...
}

return bigint_cmp(this->data(), sw, &other, 1);
return bigint_cmp(this->_data(), sw, &other, 1);
}

/*
Expand All @@ -158,19 +158,19 @@ int32_t BigInt::cmp(const BigInt& other, bool check_signs) const {
}

if(other.is_negative() && this->is_negative()) {
return (-bigint_cmp(this->data(), this->size(), other.data(), other.size()));
return (-bigint_cmp(this->_data(), this->size(), other._data(), other.size()));
}
}

return bigint_cmp(this->data(), this->size(), other.data(), other.size());
return bigint_cmp(this->_data(), this->size(), other._data(), other.size());
}

bool BigInt::is_equal(const BigInt& other) const {
if(this->sign() != other.sign()) {
return false;
}

return bigint_ct_is_eq(this->data(), this->sig_words(), other.data(), other.sig_words()).as_bool();
return bigint_ct_is_eq(this->_data(), this->sig_words(), other._data(), other.sig_words()).as_bool();
}

bool BigInt::is_less_than(const BigInt& other) const {
Expand All @@ -183,10 +183,10 @@ bool BigInt::is_less_than(const BigInt& other) const {
}

if(other.is_negative() && this->is_negative()) {
return bigint_ct_is_lt(other.data(), other.sig_words(), this->data(), this->sig_words()).as_bool();
return bigint_ct_is_lt(other._data(), other.sig_words(), this->_data(), this->sig_words()).as_bool();
}

return bigint_ct_is_lt(this->data(), this->sig_words(), other.data(), other.sig_words()).as_bool();
return bigint_ct_is_lt(this->_data(), this->sig_words(), other._data(), other.sig_words()).as_bool();
}

void BigInt::encode_words(word out[], size_t size) const {
Expand All @@ -197,7 +197,7 @@ void BigInt::encode_words(word out[], size_t size) const {
}

clear_mem(out, size);
copy_mem(out, data(), words);
copy_mem(out, _data(), words);
}

size_t BigInt::Data::calc_sig_words() const {
Expand Down Expand Up @@ -334,7 +334,7 @@ size_t BigInt::reduce_below(const BigInt& p, secure_vector<word>& ws) {
size_t reductions = 0;

for(;;) {
word borrow = bigint_sub3(ws.data(), data(), p_words + 1, p.data(), p_words);
word borrow = bigint_sub3(ws.data(), _data(), p_words + 1, p._data(), p_words);
if(borrow) {
break;
}
Expand Down Expand Up @@ -362,9 +362,9 @@ void BigInt::ct_reduce_below(const BigInt& mod, secure_vector<word>& ws, size_t
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);
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);
CT::Mask<word>::is_zero(borrow).select_n(mutable_data(), ws.data(), _data(), sz);
}
}

Expand Down Expand Up @@ -438,20 +438,20 @@ void BigInt::ct_cond_add(bool predicate, const BigInt& value) {
}
this->grow_to(1 + value.sig_words());

bigint_cnd_add(static_cast<word>(predicate), this->mutable_data(), this->size(), value.data(), value.sig_words());
bigint_cnd_add(static_cast<word>(predicate), this->mutable_data(), this->size(), value._data(), value.sig_words());
}

void BigInt::ct_shift_left(size_t shift) {
auto shl_bit = [](const BigInt& a, BigInt& result) {
BOTAN_DEBUG_ASSERT(a.size() + 1 == result.size());
bigint_shl2(result.mutable_data(), a.data(), a.size(), 1);
bigint_shl2(result.mutable_data(), a._data(), a.size(), 1);
// shl2 may have shifted a bit into the next word, which must be dropped
clear_mem(result.mutable_data() + result.size() - 1, 1);
};

auto shl_word = [](const BigInt& a, BigInt& result) {
// the most significant word is not copied, aka. shifted out
bigint_shl2(result.mutable_data(), a.data(), a.size() - 1 /* ignore msw */, BOTAN_MP_WORD_BITS);
bigint_shl2(result.mutable_data(), a._data(), a.size() - 1 /* ignore msw */, BOTAN_MP_WORD_BITS);
// we left-shifted by a full word, the least significant word must be zero'ed
clear_mem(result.mutable_data(), 1);
};
Expand Down
21 changes: 15 additions & 6 deletions src/lib/math/bigint/bigint.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class BOTAN_PUBLIC_API(2, 0) BigInt final {
* += operator
* @param y the BigInt to add to this
*/
BigInt& operator+=(const BigInt& y) { return add(y.data(), y.sig_words(), y.sign()); }
BigInt& operator+=(const BigInt& y) { return add(y._data(), y.sig_words(), y.sign()); }

/**
* += operator
Expand All @@ -216,7 +216,7 @@ class BOTAN_PUBLIC_API(2, 0) BigInt final {
* -= operator
* @param y the BigInt to subtract from this
*/
BigInt& operator-=(const BigInt& y) { return sub(y.data(), y.sig_words(), y.sign()); }
BigInt& operator-=(const BigInt& y) { return sub(y._data(), y.sig_words(), y.sign()); }

/**
* -= operator
Expand Down Expand Up @@ -643,8 +643,7 @@ class BOTAN_PUBLIC_API(2, 0) BigInt final {
* Return a const pointer to the register
* @result a pointer to the start of the internal register
*/
//BOTAN_DEPRECATED("Deprecated no replacement")
const word* data() const { return m_data.const_data(); }
BOTAN_DEPRECATED("Deprecated no replacement") const word* data() const { return m_data.const_data(); }

/**
* Don't use this function in application code
Expand Down Expand Up @@ -927,6 +926,16 @@ class BOTAN_PUBLIC_API(2, 0) BigInt final {
BOTAN_DEPRECATED("Deprecated no replacement")
static secure_vector<uint8_t> encode_fixed_length_int_pair(const BigInt& n1, const BigInt& n2, size_t bytes);

/**
* Return a const pointer to the register
*
* WARNING this is an implementation detail which is not for
* public use and not covered by SemVer.
*
* @result a pointer to the start of the internal register
*/
const word* _data() const { return m_data.const_data(); }

private:
class Data {
public:
Expand Down Expand Up @@ -1055,7 +1064,7 @@ class BOTAN_PUBLIC_API(2, 0) BigInt final {
* Arithmetic Operators
*/
inline BigInt operator+(const BigInt& x, const BigInt& y) {
return BigInt::add2(x, y.data(), y.sig_words(), y.sign());
return BigInt::add2(x, y._data(), y.sig_words(), y.sign());
}

inline BigInt operator+(const BigInt& x, word y) {
Expand All @@ -1067,7 +1076,7 @@ inline BigInt operator+(word x, const BigInt& y) {
}

inline BigInt operator-(const BigInt& x, const BigInt& y) {
return BigInt::add2(x, y.data(), y.sig_words(), y.reverse_sign());
return BigInt::add2(x, y._data(), y.sig_words(), y.reverse_sign());
}

inline BigInt operator-(const BigInt& x, word y) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/math/bigint/divide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void ct_divide(const BigInt& x, const BigInt& y, BigInt& q_out, BigInt& r_out) {
r *= 2;
r.conditionally_set_bit(0, x_b);

const bool r_gte_y = bigint_sub3(t.mutable_data(), r.data(), r.size(), y.data(), y_words) == 0;
const bool r_gte_y = bigint_sub3(t.mutable_data(), r._data(), r.size(), y._data(), y_words) == 0;

q.conditionally_set_bit(b, r_gte_y);
r.ct_cond_swap(r_gte_y, t);
Expand Down Expand Up @@ -133,7 +133,7 @@ BigInt ct_modulo(const BigInt& x, const BigInt& y) {
r *= 2;
r.conditionally_set_bit(0, x_b);

const bool r_gte_y = bigint_sub3(t.mutable_data(), r.data(), r.size(), y.data(), y_words) == 0;
const bool r_gte_y = bigint_sub3(t.mutable_data(), r._data(), r.size(), y._data(), y_words) == 0;

r.ct_cond_swap(r_gte_y, t);
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib/math/numbertheory/mod_inv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ BigInt inverse_mod_odd_modulus(const BigInt& n, const BigInt& mod) {

CT::poison(tmp_mem.data(), tmp_mem.size());

copy_mem(a_w, n.data(), std::min(n.size(), mod_words));
copy_mem(b_w, mod.data(), std::min(mod.size(), mod_words));
copy_mem(a_w, n._data(), std::min(n.size(), mod_words));
copy_mem(b_w, mod._data(), std::min(mod.size(), mod_words));
u_w[0] = 1;
// v_w = 0

// compute (mod + 1) / 2 which [because mod is odd] is equal to
// (mod / 2) + 1
copy_mem(mp1o2, mod.data(), std::min(mod.size(), mod_words));
copy_mem(mp1o2, mod._data(), std::min(mod.size(), mod_words));
bigint_shr1(mp1o2, mod_words, 1);
word carry = bigint_add2_nc(mp1o2, mod_words, u_w, 1);
BOTAN_ASSERT_NOMSG(carry == 0);
Expand All @@ -87,7 +87,7 @@ BigInt inverse_mod_odd_modulus(const BigInt& n, const BigInt& mod) {
word borrow = bigint_cnd_sub(odd_a, u_w, v_w, mod_words);

// if(borrow) u += p
bigint_cnd_add(borrow, u_w, mod.data(), mod_words);
bigint_cnd_add(borrow, u_w, mod._data(), mod_words);

const word odd_u = u_w[0] & 1;

Expand Down
Loading

0 comments on commit 79db481

Please sign in to comment.