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

randombit / botan / 24032704228

06 Apr 2026 12:58PM UTC coverage: 89.451% (-0.003%) from 89.454%
24032704228

Pull #5521

github

web-flow
Merge 17f437d7f into 417709dd7
Pull Request #5521: Rollup of small fixes

105882 of 118369 relevant lines covered (89.45%)

11473459.55 hits per line

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

95.96
/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) {
148✔
25
   return ffi_guard_thunk(__func__, [=]() -> int {
148✔
26
      if(mp_out == nullptr) {
148✔
27
         return BOTAN_FFI_ERROR_NULL_POINTER;
28
      }
29

30
      auto mp = std::make_unique<Botan::BigInt>();
148✔
31
      return ffi_new_object(mp_out, std::move(mp));
148✔
32
   });
148✔
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) {
37✔
44
   return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn = Botan::BigInt(str); });
74✔
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
      if(radix != 10 && radix != 16) {
50
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
51
      }
52

53
      bn = Botan::BigInt::from_radix_digits(std::string_view(str), radix);
54
      return BOTAN_FFI_SUCCESS;
55
   });
56
}
57

58
// NOLINTBEGIN(misc-misplaced-const)
59

60
int botan_mp_set_from_mp(botan_mp_t dest, const botan_mp_t source) {
4✔
61
   return BOTAN_FFI_VISIT(dest, [=](auto& bn) { bn = safe_get(source); });
8✔
62
}
63

64
int botan_mp_is_negative(const botan_mp_t mp) {
10✔
65
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.signum() < 0 ? 1 : 0; });
20✔
66
}
67

68
int botan_mp_is_positive(const botan_mp_t mp) {
6✔
69
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.signum() >= 0 ? 1 : 0; });
12✔
70
}
71

72
int botan_mp_flip_sign(botan_mp_t mp) {
5✔
73
   return BOTAN_FFI_VISIT(mp, [](auto& bn) { bn.flip_sign(); });
15✔
74
}
75

76
int botan_mp_from_bin(botan_mp_t mp, const uint8_t bin[], size_t bin_len) {
1✔
77
   if(bin_len > 0 && bin == nullptr) {
1✔
78
      return BOTAN_FFI_ERROR_NULL_POINTER;
79
   }
80
   return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn._assign_from_bytes({bin, bin_len}); });
2✔
81
}
82

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

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

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

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

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

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

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

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

149
int botan_mp_destroy(botan_mp_t mp) {
178✔
150
   return BOTAN_FFI_CHECKED_DELETE(mp);
178✔
151
}
152

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

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

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

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

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

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

211
int botan_mp_equal(const botan_mp_t x_w, const botan_mp_t y_w) {
24✔
212
   return BOTAN_FFI_VISIT(x_w, [=](const auto& x) -> int { return x == safe_get(y_w); });
48✔
213
}
214

215
int botan_mp_is_zero(const botan_mp_t mp) {
3✔
216
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_zero(); });
6✔
217
}
218

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

223
int botan_mp_is_even(const botan_mp_t mp) {
2✔
224
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_even(); });
5✔
225
}
226

227
int botan_mp_cmp(int* result, const botan_mp_t x_w, const botan_mp_t y_w) {
45✔
228
   if(result == nullptr) {
45✔
229
      return BOTAN_FFI_ERROR_NULL_POINTER;
230
   }
231
   return BOTAN_FFI_VISIT(x_w, [=](auto& x) { *result = x.cmp(safe_get(y_w)); });
90✔
232
}
233

234
int botan_mp_swap(botan_mp_t x_w, botan_mp_t y_w) {
×
235
   return BOTAN_FFI_VISIT(x_w, [=](auto& x) { x.swap(safe_get(y_w)); });
×
236
}
237

238
// Return (base^exponent) % modulus
239
int botan_mp_powmod(botan_mp_t out, const botan_mp_t base, const botan_mp_t exponent, const botan_mp_t modulus) {
2✔
240
   return BOTAN_FFI_VISIT(
4✔
241
      out, [=](auto& o) { o = Botan::power_mod(safe_get(base), safe_get(exponent), safe_get(modulus)); });
242
}
243

244
int botan_mp_lshift(botan_mp_t out, const botan_mp_t in, size_t shift) {
3✔
245
   return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) << shift; });
6✔
246
}
247

248
int botan_mp_rshift(botan_mp_t out, const botan_mp_t in, size_t shift) {
4✔
249
   return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) >> shift; });
8✔
250
}
251

252
int botan_mp_mod_inverse(botan_mp_t out, const botan_mp_t in, const botan_mp_t modulus) {
2✔
253
   return BOTAN_FFI_VISIT(out, [=](auto& o) {
4✔
254
      o = Botan::inverse_mod_general(safe_get(in), safe_get(modulus)).value_or(Botan::BigInt::zero());
255
   });
256
}
257

258
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✔
259
   return BOTAN_FFI_VISIT(out, [=](auto& o) {
6✔
260
      auto reducer = Botan::Barrett_Reduction::for_secret_modulus(safe_get(modulus));
261
      o = reducer.multiply(safe_get(x), safe_get(y));
262
   });
263
}
264

265
int botan_mp_rand_bits(botan_mp_t rand_out, botan_rng_t rng, size_t bits) {
2✔
266
   return BOTAN_FFI_VISIT(rng, [=](auto& r) { safe_get(rand_out).randomize(r, bits); });
4✔
267
}
268

269
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✔
270
   return BOTAN_FFI_VISIT(
30✔
271
      rng, [=](auto& r) { safe_get(rand_out) = Botan::BigInt::random_integer(r, safe_get(lower), safe_get(upper)); });
272
}
273

274
int botan_mp_gcd(botan_mp_t out, const botan_mp_t x, const botan_mp_t y) {
4✔
275
   return BOTAN_FFI_VISIT(out, [=](auto& o) { o = Botan::gcd(safe_get(x), safe_get(y)); });
8✔
276
}
277

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

282
int botan_mp_get_bit(const botan_mp_t mp, size_t bit) {
6✔
283
   return BOTAN_FFI_VISIT(mp, [=](const auto& n) -> int { return n.get_bit(bit); });
12✔
284
}
285

286
int botan_mp_set_bit(botan_mp_t mp, size_t bit) {
2✔
287
   return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.set_bit(bit); });
4✔
288
}
289

290
int botan_mp_clear_bit(botan_mp_t mp, size_t bit) {
2✔
291
   return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.clear_bit(bit); });
4✔
292
}
293

294
int botan_mp_num_bits(const botan_mp_t mp, size_t* bits) {
7✔
295
   if(bits == nullptr) {
7✔
296
      return BOTAN_FFI_ERROR_NULL_POINTER;
297
   }
298
   return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bits = n.bits(); });
14✔
299
}
300

301
int botan_mp_num_bytes(const botan_mp_t mp, size_t* bytes) {
10✔
302
   if(bytes == nullptr) {
10✔
303
      return BOTAN_FFI_ERROR_NULL_POINTER;
304
   }
305
   return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bytes = n.bytes(); });
20✔
306
}
307

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