• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.11
/src/lib/math/bigint/big_code.cpp
1
/*
2
* BigInt Encoding/Decoding
3
* (C) 1999-2010,2012,2019,2021 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/bigint.h>
9

10
#include <botan/hex.h>
11
#include <botan/internal/divide.h>
12

13
namespace Botan {
14

15
std::string BigInt::to_dec_string() const {
52✔
16
   // Use the largest power of 10 that fits in a word
17
#if(BOTAN_MP_WORD_BITS == 64)
18
   const word conversion_radix = 10000000000000000000U;
52✔
19
   const word radix_digits = 19;
52✔
20
#else
21
   const word conversion_radix = 1000000000U;
22
   const word radix_digits = 9;
23
#endif
24

25
   // (over-)estimate of the number of digits needed; log2(10) ~ 3.3219
26
   const size_t digit_estimate = static_cast<size_t>(1 + (this->bits() / 3.32));
52✔
27

28
   // (over-)estimate of db such that conversion_radix^db > *this
29
   const size_t digit_blocks = (digit_estimate + radix_digits - 1) / radix_digits;
52✔
30

31
   BigInt value = *this;
52✔
32
   value.set_sign(Positive);
52✔
33

34
   // Extract groups of digits into words
35
   std::vector<word> digit_groups(digit_blocks);
52✔
36

37
   for(size_t i = 0; i != digit_blocks; ++i) {
115✔
38
      word remainder = 0;
63✔
39
      ct_divide_word(value, conversion_radix, value, remainder);
63✔
40
      digit_groups[i] = remainder;
63✔
41
   }
42

43
   BOTAN_ASSERT_NOMSG(value.is_zero());
104✔
44

45
   // Extract digits from the groups
46
   std::vector<uint8_t> digits(digit_blocks * radix_digits);
52✔
47

48
   for(size_t i = 0; i != digit_blocks; ++i) {
115✔
49
      word remainder = digit_groups[i];
63✔
50
      for(size_t j = 0; j != radix_digits; ++j) {
1,260✔
51
         // Compiler should convert div/mod by 10 into mul by magic constant
52
         const word digit = remainder % 10;
1,197✔
53
         remainder /= 10;
1,197✔
54
         digits[radix_digits * i + j] = static_cast<uint8_t>(digit);
1,197✔
55
      }
56
   }
57

58
   // remove leading zeros
59
   while(!digits.empty() && digits.back() == 0) {
825✔
60
      digits.pop_back();
831✔
61
   }
62

63
   BOTAN_ASSERT_NOMSG(digit_estimate >= digits.size());
52✔
64

65
   // Reverse the digits to big-endian and format to text
66
   std::string s;
52✔
67
   s.reserve(1 + digits.size());
52✔
68

69
   if(is_negative()) {
52✔
70
      s += "-";
5✔
71
   }
72

73
   // Reverse and convert to textual digits
74
   for(auto i = digits.rbegin(); i != digits.rend(); ++i) {
470✔
75
      s.push_back(*i + '0');  // assumes ASCII
418✔
76
   }
77

78
   if(s.empty()) {
52✔
79
      s += "0";
6✔
80
   }
81

82
   return s;
52✔
83
}
153✔
84

85
std::string BigInt::to_hex_string() const {
74✔
86
   const size_t this_bytes = this->bytes();
74✔
87
   std::vector<uint8_t> bits(std::max<size_t>(1, this_bytes));
121✔
88

89
   if(this_bytes > 0) {
74✔
90
      this->binary_encode(bits.data());
72✔
91
   }
92

93
   std::string hrep;
74✔
94
   if(is_negative()) {
74✔
95
      hrep += "-";
5✔
96
   }
97
   hrep += "0x";
74✔
98
   hrep += hex_encode(bits);
148✔
99
   return hrep;
74✔
100
}
74✔
101

102
/*
103
* Encode a BigInt, with leading 0s if needed
104
*/
105
secure_vector<uint8_t> BigInt::encode_1363(const BigInt& n, size_t bytes) {
26,055✔
106
   if(n.bytes() > bytes) {
26,055✔
107
      throw Encoding_Error("encode_1363: n is too large to encode properly");
223✔
108
   }
109

110
   secure_vector<uint8_t> output(bytes);
25,832✔
111
   n.binary_encode(output.data(), output.size());
25,832✔
112
   return output;
25,832✔
113
}
×
114

115
//static
116
void BigInt::encode_1363(uint8_t output[], size_t bytes, const BigInt& n) {
4,902✔
117
   if(n.bytes() > bytes) {
4,902✔
118
      throw Encoding_Error("encode_1363: n is too large to encode properly");
×
119
   }
120

121
   n.binary_encode(output, bytes);
4,902✔
122
}
4,902✔
123

124
/*
125
* Encode two BigInt, with leading 0s if needed, and concatenate
126
*/
127
secure_vector<uint8_t> BigInt::encode_fixed_length_int_pair(const BigInt& n1, const BigInt& n2, size_t bytes) {
635✔
128
   if(n1.is_negative() || n2.is_negative()) {
635✔
129
      throw Encoding_Error("encode_fixed_length_int_pair: values must be positive");
×
130
   }
131
   if(n1.bytes() > bytes || n2.bytes() > bytes) {
635✔
132
      throw Encoding_Error("encode_fixed_length_int_pair: values too large to encode properly");
×
133
   }
134
   secure_vector<uint8_t> output(2 * bytes);
635✔
135
   n1.binary_encode(output.data(), bytes);
635✔
136
   n2.binary_encode(output.data() + bytes, bytes);
635✔
137
   return output;
635✔
138
}
×
139

140
/*
141
* Decode a BigInt
142
*/
143
BigInt BigInt::decode(const uint8_t buf[], size_t length, Base base) {
44,414✔
144
   BigInt r;
44,414✔
145
   if(base == Binary) {
44,414✔
146
      r.binary_decode(buf, length);
×
147
   } else if(base == Hexadecimal) {
44,414✔
148
      secure_vector<uint8_t> binary;
39,695✔
149

150
      if(length % 2) {
39,695✔
151
         // Handle lack of leading 0
152
         const char buf0_with_leading_0[2] = {'0', static_cast<char>(buf[0])};
2,849✔
153

154
         binary = hex_decode_locked(buf0_with_leading_0, 2);
5,698✔
155

156
         binary += hex_decode_locked(cast_uint8_ptr_to_char(&buf[1]), length - 1, false);
5,698✔
157
      } else {
158
         binary = hex_decode_locked(cast_uint8_ptr_to_char(buf), length, false);
73,692✔
159
      }
160

161
      r.binary_decode(binary.data(), binary.size());
39,695✔
162
   } else if(base == Decimal) {
44,414✔
163
      // This could be made faster using the same trick as to_dec_string
164
      for(size_t i = 0; i != length; ++i) {
394,796✔
165
         const char c = buf[i];
390,077✔
166

167
         if(c < '0' || c > '9') {
390,077✔
168
            throw Invalid_Argument("BigInt::decode: invalid decimal char");
×
169
         }
170

171
         const uint8_t x = c - '0';
390,077✔
172
         BOTAN_ASSERT_NOMSG(x < 10);
390,077✔
173

174
         r *= 10;
390,077✔
175
         r += x;
390,077✔
176
      }
177
   } else {
178
      throw Invalid_Argument("Unknown BigInt decoding method");
×
179
   }
180
   return r;
44,414✔
181
}
×
182

183
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc