Skip to content

Commit c05fc63

Browse files
committed
Add BigInt::signum
1 parent 00c63a4 commit c05fc63

File tree

25 files changed

+124
-100
lines changed

25 files changed

+124
-100
lines changed

src/fuzzer/bn_cmp.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ void fuzz(const std::span<const uint8_t> in) {
5454

5555
if(is_lt) {
5656
FUZZER_ASSERT_TRUE(!is_gt);
57-
FUZZER_ASSERT_TRUE(d1.is_nonzero());
58-
FUZZER_ASSERT_TRUE(d2.is_nonzero());
59-
FUZZER_ASSERT_TRUE(d1.is_negative());
60-
FUZZER_ASSERT_TRUE(d2.is_positive());
57+
FUZZER_ASSERT_TRUE(d1.signum() != 0);
58+
FUZZER_ASSERT_TRUE(d2.signum() != 0);
59+
FUZZER_ASSERT_TRUE(d1.signum() < 0);
60+
FUZZER_ASSERT_TRUE(d2.signum() > 0);
6161
}
6262

6363
if(is_gt) {
6464
FUZZER_ASSERT_TRUE(!is_lt);
65-
FUZZER_ASSERT_TRUE(d1.is_nonzero());
66-
FUZZER_ASSERT_TRUE(d2.is_nonzero());
67-
FUZZER_ASSERT_TRUE(d1.is_positive());
68-
FUZZER_ASSERT_TRUE(d2.is_negative());
65+
FUZZER_ASSERT_TRUE(d1.signum() != 0);
66+
FUZZER_ASSERT_TRUE(d2.signum() != 0);
67+
FUZZER_ASSERT_TRUE(d1.signum() > 0);
68+
FUZZER_ASSERT_TRUE(d2.signum() < 0);
6969
}
7070
}

src/fuzzer/invert.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Botan::BigInt ref_inverse_mod(const Botan::BigInt& n, const Botan::BigInt& mod)
2323
Botan::BigInt C = 0;
2424
Botan::BigInt D = 1;
2525

26-
while(u.is_nonzero()) {
26+
while(!u.is_zero()) {
2727
const size_t u_zero_bits = Botan::low_zero_bits(u);
2828
u >>= u_zero_bits;
2929
for(size_t i = 0; i != u_zero_bits; ++i) {
@@ -61,7 +61,7 @@ Botan::BigInt ref_inverse_mod(const Botan::BigInt& n, const Botan::BigInt& mod)
6161
return 0; // no modular inverse
6262
}
6363

64-
while(D.is_negative()) {
64+
while(D.signum() < 0) {
6565
D += mod;
6666
}
6767
while(D >= mod) {

src/lib/asn1/ber_dec.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ BER_Decoder& BER_Decoder::decode(size_t& out, ASN1_Type type_tag, ASN1_Class cla
427427
BigInt integer;
428428
decode(integer, type_tag, class_tag);
429429

430-
if(integer.is_negative()) {
430+
if(integer.signum() < 0) {
431431
throw BER_Decoding_Error("Decoded small integer value was negative");
432432
}
433433

src/lib/codec/base58/base58.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ std::string base58_encode(BigInt v, size_t leading_zeros) {
100100
BigInt q;
101101
std::vector<uint8_t> digits;
102102

103-
while(v.is_nonzero()) {
103+
while(!v.is_zero()) {
104104
word r = 0;
105105
ct_divide_word(v, radix, q, r);
106106

src/lib/ffi/ffi_mp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ int botan_mp_set_from_mp(botan_mp_t dest, const botan_mp_t source) {
6262
}
6363

6464
int botan_mp_is_negative(const botan_mp_t mp) {
65-
return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.is_negative() ? 1 : 0; });
65+
return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.signum() < 0 ? 1 : 0; });
6666
}
6767

6868
int botan_mp_is_positive(const botan_mp_t mp) {
69-
return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.is_positive() ? 1 : 0; });
69+
return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.signum() >= 0 ? 1 : 0; });
7070
}
7171

7272
int botan_mp_flip_sign(botan_mp_t mp) {

src/lib/math/bigint/big_code.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ std::string BigInt::to_dec_string() const {
8686
std::string s;
8787
s.reserve(1 + digits.size());
8888

89-
if(is_negative()) {
89+
if(signum() < 0) {
9090
s += "-";
9191
}
9292

@@ -113,7 +113,7 @@ std::string BigInt::to_hex_string() const {
113113
}
114114

115115
std::string hrep;
116-
if(is_negative()) {
116+
if(signum() < 0) {
117117
hrep += "-";
118118
}
119119
hrep += "0x";
@@ -184,7 +184,7 @@ BigInt BigInt::from_radix_digits(std::string_view digits, size_t radix) {
184184
* Encode two BigInt, with leading 0s if needed, and concatenate
185185
*/
186186
secure_vector<uint8_t> BigInt::encode_fixed_length_int_pair(const BigInt& n1, const BigInt& n2, size_t bytes) {
187-
if(n1.is_negative() || n2.is_negative()) {
187+
if(n1.signum() < 0 || n2.signum() < 0) {
188188
throw Encoding_Error("encode_fixed_length_int_pair: values must be positive");
189189
}
190190
if(n1.bytes() > bytes || n2.bytes() > bytes) {

src/lib/math/bigint/big_ops2.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ BigInt& BigInt::add(const word y[], size_t y_words, Sign y_sign) {
4343
}
4444

4545
BigInt& BigInt::mod_add(const BigInt& s, const BigInt& mod, secure_vector<word>& ws) {
46-
if(this->is_negative() || s.is_negative() || mod.is_negative()) {
46+
if(this->signum() < 0 || s.signum() < 0 || mod.signum() < 0) {
4747
throw Invalid_Argument("BigInt::mod_add expects all arguments are positive");
4848
}
4949

@@ -92,7 +92,7 @@ BigInt& BigInt::mod_add(const BigInt& s, const BigInt& mod, secure_vector<word>&
9292
}
9393

9494
BigInt& BigInt::mod_sub(const BigInt& s, const BigInt& mod, secure_vector<word>& ws) {
95-
if(this->is_negative() || s.is_negative() || mod.is_negative()) {
95+
if(this->signum() < 0 || s.signum() < 0 || mod.signum() < 0) {
9696
throw Invalid_Argument("BigInt::mod_sub expects all arguments are positive");
9797
}
9898

@@ -120,7 +120,7 @@ BigInt& BigInt::mod_sub(const BigInt& s, const BigInt& mod, secure_vector<word>&
120120
}
121121

122122
BigInt& BigInt::mod_mul(uint8_t y, const BigInt& mod, secure_vector<word>& ws) {
123-
BOTAN_ARG_CHECK(this->is_negative() == false, "*this must be positive");
123+
BOTAN_ARG_CHECK(this->signum() >= 0, "*this must be positive");
124124
BOTAN_ARG_CHECK(y < 16, "y too large");
125125

126126
BOTAN_DEBUG_ASSERT(*this < mod);
@@ -202,7 +202,7 @@ BigInt& BigInt::operator*=(word y) {
202202
* Division Operator
203203
*/
204204
BigInt& BigInt::operator/=(const BigInt& y) {
205-
if(y.sig_words() == 1 && is_positive() && y.is_positive() && is_power_of_2(y.word_at(0))) {
205+
if(y.sig_words() == 1 && signum() >= 0 && y.signum() >= 0 && is_power_of_2(y.word_at(0))) {
206206
(*this) >>= (y.bits() - 1);
207207
} else {
208208
(*this) = (*this) / y;
@@ -267,8 +267,8 @@ BigInt& BigInt::operator<<=(size_t shift) {
267267
BigInt& BigInt::operator>>=(size_t shift) {
268268
bigint_shr1(m_data.mutable_data(), m_data.size(), shift);
269269

270-
if(is_negative() && is_zero()) {
271-
set_sign(Positive);
270+
if(sig_words() == 0 && m_signedness == Negative) {
271+
m_signedness = Positive;
272272
}
273273

274274
return (*this);

src/lib/math/bigint/big_ops3.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ BigInt operator*(const BigInt& x, word y) {
104104
* Division Operator
105105
*/
106106
BigInt operator/(const BigInt& x, const BigInt& y) {
107-
if(y.sig_words() == 1 && y.is_positive()) {
107+
if(y.sig_words() == 1 && y.signum() >= 0) {
108108
return x / y.word_at(0);
109109
}
110110

@@ -135,10 +135,10 @@ BigInt operator%(const BigInt& n, const BigInt& mod) {
135135
if(mod.is_zero()) {
136136
throw Invalid_Argument("BigInt::operator% divide by zero");
137137
}
138-
if(mod.is_negative()) {
138+
if(mod.signum() < 0) {
139139
throw Invalid_Argument("BigInt::operator% modulus must be > 0");
140140
}
141-
if(n.is_positive() && mod.is_positive() && n < mod) {
141+
if(n.signum() >= 0 && mod.signum() >= 0 && n < mod) {
142142
return n;
143143
}
144144

@@ -166,7 +166,7 @@ word operator%(const BigInt& n, word mod) {
166166

167167
word remainder = 0;
168168

169-
if(n.is_positive() && is_power_of_2(mod)) {
169+
if(n.signum() >= 0 && is_power_of_2(mod)) {
170170
remainder = (n.word_at(0) & (mod - 1));
171171
} else {
172172
const divide_precomp redc_mod(mod);
@@ -213,7 +213,7 @@ BigInt operator>>(const BigInt& x, size_t shift) {
213213
BigInt y = BigInt::with_capacity(x_sw - shift_words);
214214
bigint_shr2(y.mutable_data(), x._data(), x_sw, shift);
215215

216-
if(x.is_negative() && y.is_zero()) {
216+
if(x.signum() < 0 && y.is_zero()) {
217217
y.set_sign(BigInt::Positive);
218218
} else {
219219
y.set_sign(x.sign());

src/lib/math/bigint/big_rand.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void BigInt::randomize(RandomNumberGenerator& rng, size_t bitsize, bool set_high
4242
* Generate a random integer within given range
4343
*/
4444
BigInt BigInt::random_integer(RandomNumberGenerator& rng, const BigInt& min, const BigInt& max) {
45-
if(min.is_negative() || max.is_negative() || max <= min) {
45+
if(min.signum() < 0 || max.signum() < 0 || max <= min) {
4646
throw Invalid_Argument("BigInt::random_integer invalid range");
4747
}
4848

src/lib/math/bigint/bigint.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ uint8_t BigInt::byte_at(size_t n) const {
120120
}
121121

122122
int32_t BigInt::cmp_word(word other) const {
123-
if(is_negative()) {
123+
if(signum() < 0) {
124124
return -1; // other is positive ...
125125
}
126126

@@ -137,15 +137,15 @@ int32_t BigInt::cmp_word(word other) const {
137137
*/
138138
int32_t BigInt::cmp(const BigInt& other, bool check_signs) const {
139139
if(check_signs) {
140-
if(other.is_positive() && this->is_negative()) {
140+
if(other.signum() >= 0 && this->signum() < 0) {
141141
return -1;
142142
}
143143

144-
if(other.is_negative() && this->is_positive()) {
144+
if(other.signum() < 0 && this->signum() >= 0) {
145145
return 1;
146146
}
147147

148-
if(other.is_negative() && this->is_negative()) {
148+
if(other.signum() < 0 && this->signum() < 0) {
149149
return (-bigint_cmp(this->_data(), this->size(), other._data(), other.size()));
150150
}
151151
}
@@ -162,15 +162,15 @@ bool BigInt::is_equal(const BigInt& other) const {
162162
}
163163

164164
bool BigInt::is_less_than(const BigInt& other) const {
165-
if(this->is_negative() && other.is_positive()) {
165+
if(this->signum() < 0 && other.signum() >= 0) {
166166
return true;
167167
}
168168

169-
if(this->is_positive() && other.is_negative()) {
169+
if(this->signum() >= 0 && other.signum() < 0) {
170170
return false;
171171
}
172172

173-
if(other.is_negative() && this->is_negative()) {
173+
if(other.signum() < 0 && this->signum() < 0) {
174174
return bigint_ct_is_lt(other._data(), other.size(), this->_data(), this->size()).as_bool();
175175
}
176176

@@ -265,7 +265,7 @@ uint32_t BigInt::get_substring(size_t offset, size_t length) const {
265265
* Convert this number to a uint32_t, if possible
266266
*/
267267
uint32_t BigInt::to_u32bit() const {
268-
if(is_negative()) {
268+
if(signum() < 0) {
269269
throw Encoding_Error("BigInt::to_u32bit: Number is negative");
270270
}
271271
if(bits() > 32) {
@@ -327,7 +327,7 @@ BigInt BigInt::operator-() const {
327327
}
328328

329329
size_t BigInt::reduce_below(const BigInt& p, secure_vector<word>& ws) {
330-
if(p.is_negative() || this->is_negative()) {
330+
if(p.signum() < 0 || this->signum() < 0) {
331331
throw Invalid_Argument("BigInt::reduce_below both values must be positive");
332332
}
333333

@@ -359,7 +359,7 @@ size_t BigInt::reduce_below(const BigInt& p, secure_vector<word>& ws) {
359359
}
360360

361361
void BigInt::ct_reduce_below(const BigInt& mod, secure_vector<word>& ws, size_t bound) {
362-
if(mod.is_negative() || this->is_negative()) {
362+
if(mod.signum() < 0 || this->signum() < 0) {
363363
throw Invalid_Argument("BigInt::ct_reduce_below both values must be positive");
364364
}
365365

@@ -447,7 +447,7 @@ void BigInt::assign_from_bytes(std::span<const uint8_t> bytes) {
447447
}
448448

449449
void BigInt::ct_cond_add(bool predicate, const BigInt& value) {
450-
if(this->is_negative() || value.is_negative()) {
450+
if(this->signum() < 0 || value.signum() < 0) {
451451
throw Invalid_Argument("BigInt::ct_cond_add requires both values to be positive");
452452
}
453453
const size_t v_words = value.sig_words();

0 commit comments

Comments
 (0)