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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 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
   // Reverse and convert to textual digits
73
   for(auto i = digits.rbegin(); i != digits.rend(); ++i) {
470✔
74
      s.push_back(*i + '0');  // assumes ASCII
418✔
75
   }
76

77
   if(s.empty())
52✔
78
      s += "0";
6✔
79

80
   return s;
52✔
81
}
153✔
82

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

87
   if(this_bytes > 0)
74✔
88
      this->binary_encode(bits.data());
72✔
89

90
   std::string hrep;
74✔
91
   if(is_negative())
74✔
92
      hrep += "-";
5✔
93
   hrep += "0x";
74✔
94
   hrep += hex_encode(bits);
148✔
95
   return hrep;
74✔
96
}
74✔
97

98
/*
99
* Encode a BigInt, with leading 0s if needed
100
*/
101
secure_vector<uint8_t> BigInt::encode_1363(const BigInt& n, size_t bytes) {
26,036✔
102
   if(n.bytes() > bytes)
26,036✔
103
      throw Encoding_Error("encode_1363: n is too large to encode properly");
223✔
104

105
   secure_vector<uint8_t> output(bytes);
25,813✔
106
   n.binary_encode(output.data(), output.size());
25,813✔
107
   return output;
25,813✔
108
}
×
109

110
//static
111
void BigInt::encode_1363(uint8_t output[], size_t bytes, const BigInt& n) {
4,898✔
112
   if(n.bytes() > bytes)
4,898✔
113
      throw Encoding_Error("encode_1363: n is too large to encode properly");
×
114

115
   n.binary_encode(output, bytes);
4,898✔
116
}
4,898✔
117

118
/*
119
* Encode two BigInt, with leading 0s if needed, and concatenate
120
*/
121
secure_vector<uint8_t> BigInt::encode_fixed_length_int_pair(const BigInt& n1, const BigInt& n2, size_t bytes) {
634✔
122
   if(n1.is_negative() || n2.is_negative())
634✔
123
      throw Encoding_Error("encode_fixed_length_int_pair: values must be positive");
×
124
   if(n1.bytes() > bytes || n2.bytes() > bytes)
634✔
125
      throw Encoding_Error("encode_fixed_length_int_pair: values too large to encode properly");
×
126
   secure_vector<uint8_t> output(2 * bytes);
634✔
127
   n1.binary_encode(output.data(), bytes);
634✔
128
   n2.binary_encode(output.data() + bytes, bytes);
634✔
129
   return output;
634✔
130
}
×
131

132
/*
133
* Decode a BigInt
134
*/
135
BigInt BigInt::decode(const uint8_t buf[], size_t length, Base base) {
44,418✔
136
   BigInt r;
44,418✔
137
   if(base == Binary) {
44,418✔
138
      r.binary_decode(buf, length);
×
139
   } else if(base == Hexadecimal) {
44,418✔
140
      secure_vector<uint8_t> binary;
39,699✔
141

142
      if(length % 2) {
39,699✔
143
         // Handle lack of leading 0
144
         const char buf0_with_leading_0[2] = {'0', static_cast<char>(buf[0])};
2,851✔
145

146
         binary = hex_decode_locked(buf0_with_leading_0, 2);
5,702✔
147

148
         binary += hex_decode_locked(cast_uint8_ptr_to_char(&buf[1]), length - 1, false);
5,702✔
149
      } else
150
         binary = hex_decode_locked(cast_uint8_ptr_to_char(buf), length, false);
73,696✔
151

152
      r.binary_decode(binary.data(), binary.size());
39,699✔
153
   } else if(base == Decimal) {
44,418✔
154
      // This could be made faster using the same trick as to_dec_string
155
      for(size_t i = 0; i != length; ++i) {
394,798✔
156
         const char c = buf[i];
390,079✔
157

158
         if(c < '0' || c > '9')
390,079✔
159
            throw Invalid_Argument("BigInt::decode: invalid decimal char");
×
160

161
         const uint8_t x = c - '0';
390,079✔
162
         BOTAN_ASSERT_NOMSG(x < 10);
390,079✔
163

164
         r *= 10;
390,079✔
165
         r += x;
390,079✔
166
      }
167
   } else
168
      throw Invalid_Argument("Unknown BigInt decoding method");
×
169
   return r;
44,418✔
170
}
×
171

172
}
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