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

randombit / botan / 21850311256

10 Feb 2026 03:17AM UTC coverage: 91.635% (+0.001%) from 91.634%
21850311256

Pull #5302

github

web-flow
Merge 26bacb252 into 1d119e57a
Pull Request #5302: Cleanup various header inclusions

104007 of 113502 relevant lines covered (91.63%)

11071649.74 hits per line

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

95.7
/src/lib/ffi/ffi_mp.cpp
1
/*
2
* (C) 2015,2017 Jack Lloyd
3
* (C) 2017 Ribose Inc
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/ffi.h>
9

10
#include <botan/assert.h>
11
#include <botan/numthry.h>
12
#include <botan/internal/barrett.h>
13
#include <botan/internal/divide.h>
14
#include <botan/internal/ffi_mp.h>
15
#include <botan/internal/ffi_rng.h>
16
#include <botan/internal/ffi_util.h>
17
#include <botan/internal/mem_utils.h>
18
#include <botan/internal/mod_inv.h>
19

20
extern "C" {
21

22
using namespace Botan_FFI;
23

24
int botan_mp_init(botan_mp_t* mp_out) {
152✔
25
   return ffi_guard_thunk(__func__, [=]() -> int {
152✔
26
      if(mp_out == nullptr) {
152✔
27
         return BOTAN_FFI_ERROR_NULL_POINTER;
28
      }
29

30
      auto mp = std::make_unique<Botan::BigInt>();
152✔
31
      return ffi_new_object(mp_out, std::move(mp));
152✔
32
   });
152✔
33
}
34

35
int botan_mp_clear(botan_mp_t mp) {
×
36
   return BOTAN_FFI_VISIT(mp, [](auto& bn) { bn.clear(); });
×
37
}
38

39
int botan_mp_set_from_int(botan_mp_t mp, int initial_value) {
2✔
40
   return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn = Botan::BigInt::from_s32(initial_value); });
4✔
41
}
42

43
int botan_mp_set_from_str(botan_mp_t mp, const char* str) {
35✔
44
   return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn = Botan::BigInt(str); });
70✔
45
}
46

47
int botan_mp_set_from_radix_str(botan_mp_t mp, const char* str, size_t radix) {
10✔
48
   return BOTAN_FFI_VISIT(mp, [=](auto& bn) {
20✔
49
      Botan::BigInt::Base base;
50
      if(radix == 10) {
51
         base = Botan::BigInt::Decimal;
52
      } else if(radix == 16) {
53
         base = Botan::BigInt::Hexadecimal;
54
      } else {
55
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
56
      }
57

58
      bn = Botan::BigInt::decode(Botan::cstr_as_span_of_bytes(str), base);
59
      return BOTAN_FFI_SUCCESS;
60
   });
61
}
62

63
// NOLINTBEGIN(misc-misplaced-const)
64

65
int botan_mp_set_from_mp(botan_mp_t dest, const botan_mp_t source) {
4✔
66
   return BOTAN_FFI_VISIT(dest, [=](auto& bn) { bn = safe_get(source); });
8✔
67
}
68

69
int botan_mp_is_negative(const botan_mp_t mp) {
10✔
70
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.is_negative() ? 1 : 0; });
20✔
71
}
72

73
int botan_mp_is_positive(const botan_mp_t mp) {
6✔
74
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.is_positive() ? 1 : 0; });
12✔
75
}
76

77
int botan_mp_flip_sign(botan_mp_t mp) {
5✔
78
   return BOTAN_FFI_VISIT(mp, [](auto& bn) { bn.flip_sign(); });
15✔
79
}
80

81
int botan_mp_from_bin(botan_mp_t mp, const uint8_t bin[], size_t bin_len) {
1✔
82
   return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn._assign_from_bytes({bin, bin_len}); });
2✔
83
}
84

85
int botan_mp_to_hex(const botan_mp_t mp, char* out) {
3✔
86
   return BOTAN_FFI_VISIT(mp, [=](const auto& bn) {
6✔
87
      const std::string hex = bn.to_hex_string();
88

89
      // Check that we are about to write no more than the documented upper bound
90
      const size_t upper_bound = 2 * bn.bytes() + 5;
91
      BOTAN_ASSERT_NOMSG(hex.size() + 1 <= upper_bound);
92
      std::memcpy(out, hex.c_str(), 1 + hex.size());
93
   });
94
}
95

96
int botan_mp_view_hex(const botan_mp_t mp, botan_view_ctx ctx, botan_view_str_fn view) {
36✔
97
   return BOTAN_FFI_VISIT(mp, [=](const auto& bn) -> int {
108✔
98
      const std::string hex = bn.to_hex_string();
99
      return invoke_view_callback(view, ctx, hex);
100
   });
101
}
102

103
int botan_mp_to_str(const botan_mp_t mp, uint8_t radix, char* out, size_t* out_len) {
14✔
104
   return BOTAN_FFI_VISIT(mp, [=](const auto& bn) -> int {
28✔
105
      if(radix == 0 || radix == 10) {
106
         return write_str_output(out, out_len, bn.to_dec_string());
107
      } else if(radix == 16) {
108
         return write_str_output(out, out_len, bn.to_hex_string());
109
      } else {
110
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
111
      }
112
   });
113
}
114

115
int botan_mp_view_str(const botan_mp_t mp, uint8_t radix, botan_view_ctx ctx, botan_view_str_fn view) {
3✔
116
   return BOTAN_FFI_VISIT(mp, [=](const auto& bn) -> int {
9✔
117
      if(radix == 10) {
118
         return invoke_view_callback(view, ctx, bn.to_dec_string());
119
      } else if(radix == 16) {
120
         return invoke_view_callback(view, ctx, bn.to_hex_string());
121
      } else {
122
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
123
      }
124
   });
125
}
126

127
int botan_mp_to_bin(const botan_mp_t mp, uint8_t vec[]) {
3✔
128
   return BOTAN_FFI_VISIT(mp, [=](const auto& bn) { bn.serialize_to(std::span{vec, bn.bytes()}); });
6✔
129
}
130

131
int botan_mp_view_bin(const botan_mp_t mp, botan_view_ctx ctx, botan_view_bin_fn view) {
3✔
132
   return BOTAN_FFI_VISIT(mp, [=](const auto& bn) {
15✔
133
      const auto bytes = bn.serialize();
134
      return invoke_view_callback(view, ctx, bytes);
135
   });
136
}
137

138
int botan_mp_to_uint32(const botan_mp_t mp, uint32_t* val) {
5✔
139
   if(val == nullptr) {
5✔
140
      return BOTAN_FFI_ERROR_NULL_POINTER;
141
   }
142
   return BOTAN_FFI_VISIT(mp, [=](const auto& bn) { *val = bn.to_u32bit(); });
10✔
143
}
144

145
int botan_mp_destroy(botan_mp_t mp) {
174✔
146
   return BOTAN_FFI_CHECKED_DELETE(mp);
174✔
147
}
148

149
int botan_mp_add(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) {
5✔
150
   return BOTAN_FFI_VISIT(result, [=](auto& res) {
10✔
151
      if(result == x) {
152
         res += safe_get(y);
153
      } else {
154
         res = safe_get(x) + safe_get(y);
155
      }
156
   });
157
}
158

159
int botan_mp_sub(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) {
1✔
160
   return BOTAN_FFI_VISIT(result, [=](auto& res) {
2✔
161
      if(result == x) {
162
         res -= safe_get(y);
163
      } else {
164
         res = safe_get(x) - safe_get(y);
165
      }
166
   });
167
}
168

169
int botan_mp_add_u32(botan_mp_t result, const botan_mp_t x, uint32_t y) {
2✔
170
   return BOTAN_FFI_VISIT(result, [=](auto& res) {
4✔
171
      if(result == x) {
172
         res += static_cast<Botan::word>(y);
173
      } else {
174
         res = safe_get(x) + static_cast<Botan::word>(y);
175
      }
176
   });
177
}
178

179
int botan_mp_sub_u32(botan_mp_t result, const botan_mp_t x, uint32_t y) {
1✔
180
   return BOTAN_FFI_VISIT(result, [=](auto& res) {
2✔
181
      if(result == x) {
182
         res -= static_cast<Botan::word>(y);
183
      } else {
184
         res = safe_get(x) - static_cast<Botan::word>(y);
185
      }
186
   });
187
}
188

189
int botan_mp_mul(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) {
6✔
190
   return BOTAN_FFI_VISIT(result, [=](auto& res) {
12✔
191
      if(result == x) {
192
         res *= safe_get(y);
193
      } else {
194
         res = safe_get(x) * safe_get(y);
195
      }
196
   });
197
}
198

199
int botan_mp_div(botan_mp_t quotient, botan_mp_t remainder, const botan_mp_t x, const botan_mp_t y) {
2✔
200
   return BOTAN_FFI_VISIT(quotient, [=](auto& q) {
4✔
201
      Botan::BigInt r;
202
      Botan::vartime_divide(safe_get(x), safe_get(y), q, r);
203
      safe_get(remainder) = r;
204
   });
205
}
206

207
int botan_mp_equal(const botan_mp_t x_w, const botan_mp_t y_w) {
23✔
208
   return BOTAN_FFI_VISIT(x_w, [=](const auto& x) -> int { return x == safe_get(y_w); });
46✔
209
}
210

211
int botan_mp_is_zero(const botan_mp_t mp) {
3✔
212
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_zero(); });
6✔
213
}
214

215
int botan_mp_is_odd(const botan_mp_t mp) {
2✔
216
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_odd(); });
5✔
217
}
218

219
int botan_mp_is_even(const botan_mp_t mp) {
2✔
220
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_even(); });
5✔
221
}
222

223
int botan_mp_cmp(int* result, const botan_mp_t x_w, const botan_mp_t y_w) {
44✔
224
   return BOTAN_FFI_VISIT(x_w, [=](auto& x) { *result = x.cmp(safe_get(y_w)); });
88✔
225
}
226

227
int botan_mp_swap(botan_mp_t x_w, botan_mp_t y_w) {
×
228
   return BOTAN_FFI_VISIT(x_w, [=](auto& x) { x.swap(safe_get(y_w)); });
×
229
}
230

231
// Return (base^exponent) % modulus
232
int botan_mp_powmod(botan_mp_t out, const botan_mp_t base, const botan_mp_t exponent, const botan_mp_t modulus) {
2✔
233
   return BOTAN_FFI_VISIT(
4✔
234
      out, [=](auto& o) { o = Botan::power_mod(safe_get(base), safe_get(exponent), safe_get(modulus)); });
235
}
236

237
int botan_mp_lshift(botan_mp_t out, const botan_mp_t in, size_t shift) {
3✔
238
   return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) << shift; });
6✔
239
}
240

241
int botan_mp_rshift(botan_mp_t out, const botan_mp_t in, size_t shift) {
4✔
242
   return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) >> shift; });
8✔
243
}
244

245
int botan_mp_mod_inverse(botan_mp_t out, const botan_mp_t in, const botan_mp_t modulus) {
2✔
246
   return BOTAN_FFI_VISIT(out, [=](auto& o) {
4✔
247
      o = Botan::inverse_mod_general(safe_get(in), safe_get(modulus)).value_or(Botan::BigInt::zero());
248
   });
249
}
250

251
int botan_mp_mod_mul(botan_mp_t out, const botan_mp_t x, const botan_mp_t y, const botan_mp_t modulus) {
2✔
252
   return BOTAN_FFI_VISIT(out, [=](auto& o) {
6✔
253
      auto reducer = Botan::Barrett_Reduction::for_secret_modulus(safe_get(modulus));
254
      o = reducer.multiply(safe_get(x), safe_get(y));
255
   });
256
}
257

258
int botan_mp_rand_bits(botan_mp_t rand_out, botan_rng_t rng, size_t bits) {
2✔
259
   return BOTAN_FFI_VISIT(rng, [=](auto& r) { safe_get(rand_out).randomize(r, bits); });
4✔
260
}
261

262
int botan_mp_rand_range(botan_mp_t rand_out, botan_rng_t rng, const botan_mp_t lower, const botan_mp_t upper) {
10✔
263
   return BOTAN_FFI_VISIT(
30✔
264
      rng, [=](auto& r) { safe_get(rand_out) = Botan::BigInt::random_integer(r, safe_get(lower), safe_get(upper)); });
265
}
266

267
int botan_mp_gcd(botan_mp_t out, const botan_mp_t x, const botan_mp_t y) {
4✔
268
   return BOTAN_FFI_VISIT(out, [=](auto& o) { o = Botan::gcd(safe_get(x), safe_get(y)); });
8✔
269
}
270

271
int botan_mp_is_prime(const botan_mp_t mp, botan_rng_t rng, size_t test_prob) {
5✔
272
   return BOTAN_FFI_VISIT(mp, [=](const auto& n) { return (Botan::is_prime(n, safe_get(rng), test_prob)) ? 1 : 0; });
10✔
273
}
274

275
int botan_mp_get_bit(const botan_mp_t mp, size_t bit) {
6✔
276
   return BOTAN_FFI_VISIT(mp, [=](const auto& n) -> int { return n.get_bit(bit); });
12✔
277
}
278

279
int botan_mp_set_bit(botan_mp_t mp, size_t bit) {
2✔
280
   return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.set_bit(bit); });
4✔
281
}
282

283
int botan_mp_clear_bit(botan_mp_t mp, size_t bit) {
2✔
284
   return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.clear_bit(bit); });
4✔
285
}
286

287
int botan_mp_num_bits(const botan_mp_t mp, size_t* bits) {
7✔
288
   return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bits = n.bits(); });
14✔
289
}
290

291
int botan_mp_num_bytes(const botan_mp_t mp, size_t* bytes) {
10✔
292
   return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bytes = n.bytes(); });
20✔
293
}
294

295
// NOLINTEND(misc-misplaced-const)
296
}
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

© 2026 Coveralls, Inc