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

randombit / botan / 12869303872

20 Jan 2025 01:40PM UTC coverage: 91.213% (+0.01%) from 91.202%
12869303872

push

github

web-flow
Merge pull request #4569 from randombit/jack/mod-inv-distinguish-cases

When computing modular inverses distingush which case we are in

93546 of 102558 relevant lines covered (91.21%)

11542300.02 hits per line

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

95.45
/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/reducer.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/mod_inv.h>
17

18
extern "C" {
19

20
using namespace Botan_FFI;
21

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

28
      auto mp = std::make_unique<Botan::BigInt>();
133✔
29
      *mp_out = new botan_mp_struct(std::move(mp));
133✔
30
      return BOTAN_FFI_SUCCESS;
133✔
31
   });
266✔
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) {
29✔
43
   return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn = Botan::BigInt(str); });
58✔
44
}
45

46
int botan_mp_set_from_radix_str(botan_mp_t mp, const char* str, size_t radix) {
4✔
47
   return BOTAN_FFI_VISIT(mp, [=](auto& bn) {
10✔
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
      const uint8_t* bytes = Botan::cast_char_ptr_to_uint8(str);
58
      const size_t len = strlen(str);
59

60
      bn = Botan::BigInt(bytes, len, base);
61
      return BOTAN_FFI_SUCCESS;
62
   });
63
}
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(); });
10✔
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) {
38✔
86
   return BOTAN_FFI_VISIT(mp, [=](const auto& bn) {
76✔
87
      const std::string hex = bn.to_hex_string();
88
      std::memcpy(out, hex.c_str(), 1 + hex.size());
89
   });
90
}
91

92
int botan_mp_to_str(const botan_mp_t mp, uint8_t digit_base, char* out, size_t* out_len) {
14✔
93
   return BOTAN_FFI_VISIT(mp, [=](const auto& bn) -> int {
28✔
94
      if(digit_base == 0 || digit_base == 10) {
95
         return write_str_output(out, out_len, bn.to_dec_string());
96
      } else if(digit_base == 16) {
97
         return write_str_output(out, out_len, bn.to_hex_string());
98
      } else {
99
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
100
      }
101
   });
102
}
103

104
int botan_mp_to_bin(const botan_mp_t mp, uint8_t vec[]) {
3✔
105
   return BOTAN_FFI_VISIT(mp, [=](const auto& bn) { bn.serialize_to(std::span{vec, bn.bytes()}); });
6✔
106
}
107

108
int botan_mp_to_uint32(const botan_mp_t mp, uint32_t* val) {
5✔
109
   if(val == nullptr) {
5✔
110
      return BOTAN_FFI_ERROR_NULL_POINTER;
111
   }
112
   return BOTAN_FFI_VISIT(mp, [=](const auto& bn) { *val = bn.to_u32bit(); });
10✔
113
}
114

115
int botan_mp_destroy(botan_mp_t mp) {
133✔
116
   return BOTAN_FFI_CHECKED_DELETE(mp);
133✔
117
}
118

119
int botan_mp_add(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) {
5✔
120
   return BOTAN_FFI_VISIT(result, [=](auto& res) {
10✔
121
      if(result == x) {
122
         res += safe_get(y);
123
      } else {
124
         res = safe_get(x) + safe_get(y);
125
      }
126
   });
127
}
128

129
int botan_mp_sub(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) {
1✔
130
   return BOTAN_FFI_VISIT(result, [=](auto& res) {
3✔
131
      if(result == x) {
132
         res -= safe_get(y);
133
      } else {
134
         res = safe_get(x) - safe_get(y);
135
      }
136
   });
137
}
138

139
int botan_mp_add_u32(botan_mp_t result, const botan_mp_t x, uint32_t y) {
2✔
140
   return BOTAN_FFI_VISIT(result, [=](auto& res) {
4✔
141
      if(result == x) {
142
         res += static_cast<Botan::word>(y);
143
      } else {
144
         res = safe_get(x) + static_cast<Botan::word>(y);
145
      }
146
   });
147
}
148

149
int botan_mp_sub_u32(botan_mp_t result, const botan_mp_t x, uint32_t y) {
1✔
150
   return BOTAN_FFI_VISIT(result, [=](auto& res) {
2✔
151
      if(result == x) {
152
         res -= static_cast<Botan::word>(y);
153
      } else {
154
         res = safe_get(x) - static_cast<Botan::word>(y);
155
      }
156
   });
157
}
158

159
int botan_mp_mul(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) {
6✔
160
   return BOTAN_FFI_VISIT(result, [=](auto& res) {
13✔
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_div(botan_mp_t quotient, botan_mp_t remainder, const botan_mp_t x, const botan_mp_t y) {
2✔
170
   return BOTAN_FFI_VISIT(quotient, [=](auto& q) {
4✔
171
      Botan::BigInt r;
172
      Botan::vartime_divide(safe_get(x), safe_get(y), q, r);
173
      safe_get(remainder) = r;
174
   });
175
}
176

177
int botan_mp_equal(const botan_mp_t x_w, const botan_mp_t y_w) {
10✔
178
   return BOTAN_FFI_VISIT(x_w, [=](const auto& x) -> int { return x == safe_get(y_w); });
20✔
179
}
180

181
int botan_mp_is_zero(const botan_mp_t mp) {
3✔
182
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_zero(); });
6✔
183
}
184

185
int botan_mp_is_odd(const botan_mp_t mp) {
2✔
186
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_odd(); });
4✔
187
}
188

189
int botan_mp_is_even(const botan_mp_t mp) {
2✔
190
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_even(); });
4✔
191
}
192

193
int botan_mp_cmp(int* result, const botan_mp_t x_w, const botan_mp_t y_w) {
38✔
194
   return BOTAN_FFI_VISIT(x_w, [=](auto& x) { *result = x.cmp(safe_get(y_w)); });
76✔
195
}
196

197
int botan_mp_swap(botan_mp_t x_w, botan_mp_t y_w) {
×
198
   return BOTAN_FFI_VISIT(x_w, [=](auto& x) { x.swap(safe_get(y_w)); });
×
199
}
200

201
// Return (base^exponent) % modulus
202
int botan_mp_powmod(botan_mp_t out, const botan_mp_t base, const botan_mp_t exponent, const botan_mp_t modulus) {
2✔
203
   return BOTAN_FFI_VISIT(
4✔
204
      out, [=](auto& o) { o = Botan::power_mod(safe_get(base), safe_get(exponent), safe_get(modulus)); });
205
}
206

207
int botan_mp_lshift(botan_mp_t out, const botan_mp_t in, size_t shift) {
2✔
208
   return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) << shift; });
4✔
209
}
210

211
int botan_mp_rshift(botan_mp_t out, const botan_mp_t in, size_t shift) {
4✔
212
   return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) >> shift; });
8✔
213
}
214

215
int botan_mp_mod_inverse(botan_mp_t out, const botan_mp_t in, const botan_mp_t modulus) {
2✔
216
   return BOTAN_FFI_VISIT(out, [=](auto& o) {
5✔
217
      o = Botan::inverse_mod_general(safe_get(in), safe_get(modulus)).value_or(Botan::BigInt::zero());
218
   });
219
}
220

221
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✔
222
   return BOTAN_FFI_VISIT(out, [=](auto& o) {
5✔
223
      Botan::Modular_Reducer reducer(safe_get(modulus));
224
      o = reducer.multiply(safe_get(x), safe_get(y));
225
   });
226
}
227

228
int botan_mp_rand_bits(botan_mp_t rand_out, botan_rng_t rng, size_t bits) {
2✔
229
   return BOTAN_FFI_VISIT(rng, [=](auto& r) { safe_get(rand_out).randomize(r, bits); });
4✔
230
}
231

232
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✔
233
   return BOTAN_FFI_VISIT(
30✔
234
      rng, [=](auto& r) { safe_get(rand_out) = Botan::BigInt::random_integer(r, safe_get(lower), safe_get(upper)); });
235
}
236

237
int botan_mp_gcd(botan_mp_t out, const botan_mp_t x, const botan_mp_t y) {
4✔
238
   return BOTAN_FFI_VISIT(out, [=](auto& o) { o = Botan::gcd(safe_get(x), safe_get(y)); });
8✔
239
}
240

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

245
int botan_mp_get_bit(const botan_mp_t mp, size_t bit) {
6✔
246
   return BOTAN_FFI_VISIT(mp, [=](const auto& n) -> int { return n.get_bit(bit); });
12✔
247
}
248

249
int botan_mp_set_bit(botan_mp_t mp, size_t bit) {
2✔
250
   return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.set_bit(bit); });
4✔
251
}
252

253
int botan_mp_clear_bit(botan_mp_t mp, size_t bit) {
2✔
254
   return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.clear_bit(bit); });
4✔
255
}
256

257
int botan_mp_num_bits(const botan_mp_t mp, size_t* bits) {
7✔
258
   return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bits = n.bits(); });
14✔
259
}
260

261
int botan_mp_num_bytes(const botan_mp_t mp, size_t* bytes) {
45✔
262
   return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bytes = n.bytes(); });
90✔
263
}
264
}
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