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

randombit / botan / 18905193317

29 Oct 2025 10:44AM UTC coverage: 90.668% (+0.003%) from 90.665%
18905193317

push

github

web-flow
Merge pull request #5131 from randombit/jack/mp-view

Add FFI view functions for mp type

100422 of 110758 relevant lines covered (90.67%)

12084810.22 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/numthry.h>
11
#include <botan/internal/barrett.h>
12
#include <botan/internal/divide.h>
13
#include <botan/internal/ffi_mp.h>
14
#include <botan/internal/ffi_rng.h>
15
#include <botan/internal/ffi_util.h>
16
#include <botan/internal/mem_utils.h>
17
#include <botan/internal/mod_inv.h>
18

19
extern "C" {
20

21
using namespace Botan_FFI;
22

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

144
int botan_mp_destroy(botan_mp_t mp) {
172✔
145
   return BOTAN_FFI_CHECKED_DELETE(mp);
172✔
146
}
147

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

250
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✔
251
   return BOTAN_FFI_VISIT(out, [=](auto& o) {
6✔
252
      auto reducer = Botan::Barrett_Reduction::for_secret_modulus(safe_get(modulus));
253
      o = reducer.multiply(safe_get(x), safe_get(y));
254
   });
255
}
256

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

261
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✔
262
   return BOTAN_FFI_VISIT(
30✔
263
      rng, [=](auto& r) { safe_get(rand_out) = Botan::BigInt::random_integer(r, safe_get(lower), safe_get(upper)); });
264
}
265

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

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

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

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

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

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

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

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