Skip to content

Commit bf3fba7

Browse files
committed
Add external-symbols feature to support external libsecp
This feature disables using the bundles sources and will link into existing libsecp256k1 symbols.
1 parent 55fab77 commit bf3fba7

File tree

5 files changed

+56
-47
lines changed

5 files changed

+56
-47
lines changed

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ recovery = ["secp256k1-sys/recovery"]
3030
endomorphism = ["secp256k1-sys/endomorphism"]
3131
lowmemory = ["secp256k1-sys/lowmemory"]
3232

33-
# Do not use this feature! HAZMAT. (meant for Bitcoin Core only)
34-
dont_replace_c_symbols = ["secp256k1-sys/dont_replace_c_symbols"]
33+
# Use this feature to not compile the bundled libsecp256k1 C symbols,
34+
# but use external ones. Use this only if you know what you are doing!
35+
external-symbols = ["secp256k1-sys/external-symbols"]
36+
3537
# Do not use this feature! HAZMAT. (meant for Fuzzing only. this is *BROKEN CRYPTOGRAPHY*)
3638
fuzztarget = ["secp256k1-sys/fuzztarget"]
3739

secp256k1-sys/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ endomorphism = []
3232
lowmemory = []
3333
std = []
3434

35-
# Do not use this feature! HAZMAT. (meant for Bitcoin Core only)
36-
dont_replace_c_symbols = []
35+
# Use this feature to not compile the bundled libsecp256k1 C symbols,
36+
# but use external ones. Use this only if you know what you are doing!
37+
external-symbols = []
38+
3739
# Do not use this feature! HAZMAT. (meant for Fuzzing only. this is *BROKEN CRYPTOGRAPHY*)
3840
fuzztarget = []

secp256k1-sys/build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ extern crate cc;
2626
use std::env;
2727

2828
fn main() {
29+
if cfg!(feature = "external-symbols") {
30+
println!("cargo:rustc-link-lib=static=secp256k1");
31+
return;
32+
}
33+
2934
// Check whether we can use 64-bit compilation
3035
let use_64bit_compilation = if env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap() == "64" {
3136
let check = cc::Build::new().file("depend/check_uint128_t.c")

secp256k1-sys/src/lib.rs

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -157,91 +157,91 @@ impl Default for SharedSecret {
157157
#[cfg(not(feature = "fuzztarget"))]
158158
extern "C" {
159159
/// Default ECDH hash function
160-
#[link_name = "rustsecp256k1_v0_1_0_ecdh_hash_function_default"]
160+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdh_hash_function_default")]
161161
pub static secp256k1_ecdh_hash_function_default: EcdhHashFn;
162162

163-
#[link_name = "rustsecp256k1_v0_1_0_nonce_function_rfc6979"]
163+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_nonce_function_rfc6979")]
164164
pub static secp256k1_nonce_function_rfc6979: NonceFn;
165165

166-
#[link_name = "rustsecp256k1_v0_1_0_nonce_function_default"]
166+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_nonce_function_default")]
167167
pub static secp256k1_nonce_function_default: NonceFn;
168168

169-
#[link_name = "rustsecp256k1_v0_1_0_context_no_precomp"]
169+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_context_no_precomp")]
170170
pub static secp256k1_context_no_precomp: *const Context;
171171

172172
// Contexts
173-
#[link_name = "rustsecp256k1_v0_1_0_context_preallocated_size"]
173+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_context_preallocated_size")]
174174
pub fn secp256k1_context_preallocated_size(flags: c_uint) -> usize;
175175

176-
#[link_name = "rustsecp256k1_v0_1_0_context_preallocated_create"]
176+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_context_preallocated_create")]
177177
pub fn secp256k1_context_preallocated_create(prealloc: *mut c_void, flags: c_uint) -> *mut Context;
178178

179-
#[link_name = "rustsecp256k1_v0_1_0_context_preallocated_destroy"]
179+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_context_preallocated_destroy")]
180180
pub fn secp256k1_context_preallocated_destroy(cx: *mut Context);
181181

182-
#[link_name = "rustsecp256k1_v0_1_0_context_preallocated_clone_size"]
182+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_context_preallocated_clone_size")]
183183
pub fn secp256k1_context_preallocated_clone_size(cx: *const Context) -> usize;
184184

185-
#[link_name = "rustsecp256k1_v0_1_0_context_preallocated_clone"]
185+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_context_preallocated_clone")]
186186
pub fn secp256k1_context_preallocated_clone(cx: *const Context, prealloc: *mut c_void) -> *mut Context;
187187

188-
#[link_name = "rustsecp256k1_v0_1_0_context_randomize"]
188+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_context_randomize")]
189189
pub fn secp256k1_context_randomize(cx: *mut Context,
190190
seed32: *const c_uchar)
191191
-> c_int;
192192

193193
// Pubkeys
194-
#[link_name = "rustsecp256k1_v0_1_0_ec_pubkey_parse"]
194+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ec_pubkey_parse")]
195195
pub fn secp256k1_ec_pubkey_parse(cx: *const Context, pk: *mut PublicKey,
196196
input: *const c_uchar, in_len: usize)
197197
-> c_int;
198198

199-
#[link_name = "rustsecp256k1_v0_1_0_ec_pubkey_serialize"]
199+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ec_pubkey_serialize")]
200200
pub fn secp256k1_ec_pubkey_serialize(cx: *const Context, output: *mut c_uchar,
201201
out_len: *mut usize, pk: *const PublicKey,
202202
compressed: c_uint)
203203
-> c_int;
204204

205205
// Signatures
206-
#[link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_parse_der"]
206+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_parse_der")]
207207
pub fn secp256k1_ecdsa_signature_parse_der(cx: *const Context, sig: *mut Signature,
208208
input: *const c_uchar, in_len: usize)
209209
-> c_int;
210210

211-
#[link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_parse_compact"]
211+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_parse_compact")]
212212
pub fn secp256k1_ecdsa_signature_parse_compact(cx: *const Context, sig: *mut Signature,
213213
input64: *const c_uchar)
214214
-> c_int;
215215

216-
#[link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_parse_der_lax"]
216+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_parse_der_lax")]
217217
pub fn ecdsa_signature_parse_der_lax(cx: *const Context, sig: *mut Signature,
218218
input: *const c_uchar, in_len: usize)
219219
-> c_int;
220220

221-
#[link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_serialize_der"]
221+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_serialize_der")]
222222
pub fn secp256k1_ecdsa_signature_serialize_der(cx: *const Context, output: *mut c_uchar,
223223
out_len: *mut usize, sig: *const Signature)
224224
-> c_int;
225225

226-
#[link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_serialize_compact"]
226+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_serialize_compact")]
227227
pub fn secp256k1_ecdsa_signature_serialize_compact(cx: *const Context, output64: *mut c_uchar,
228228
sig: *const Signature)
229229
-> c_int;
230230

231-
#[link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_normalize"]
231+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_normalize")]
232232
pub fn secp256k1_ecdsa_signature_normalize(cx: *const Context, out_sig: *mut Signature,
233233
in_sig: *const Signature)
234234
-> c_int;
235235

236236
// ECDSA
237-
#[link_name = "rustsecp256k1_v0_1_0_ecdsa_verify"]
237+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_verify")]
238238
pub fn secp256k1_ecdsa_verify(cx: *const Context,
239239
sig: *const Signature,
240240
msg32: *const c_uchar,
241241
pk: *const PublicKey)
242242
-> c_int;
243243

244-
#[link_name = "rustsecp256k1_v0_1_0_ecdsa_sign"]
244+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_sign")]
245245
pub fn secp256k1_ecdsa_sign(cx: *const Context,
246246
sig: *mut Signature,
247247
msg32: *const c_uchar,
@@ -251,49 +251,49 @@ extern "C" {
251251
-> c_int;
252252

253253
// EC
254-
#[link_name = "rustsecp256k1_v0_1_0_ec_seckey_verify"]
254+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ec_seckey_verify")]
255255
pub fn secp256k1_ec_seckey_verify(cx: *const Context,
256256
sk: *const c_uchar) -> c_int;
257257

258-
#[link_name = "rustsecp256k1_v0_1_0_ec_pubkey_create"]
258+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ec_pubkey_create")]
259259
pub fn secp256k1_ec_pubkey_create(cx: *const Context, pk: *mut PublicKey,
260260
sk: *const c_uchar) -> c_int;
261261

262262
//TODO secp256k1_ec_privkey_export
263263
//TODO secp256k1_ec_privkey_import
264264

265-
#[link_name = "rustsecp256k1_v0_1_0_ec_privkey_tweak_add"]
265+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ec_privkey_tweak_add")]
266266
pub fn secp256k1_ec_privkey_tweak_add(cx: *const Context,
267267
sk: *mut c_uchar,
268268
tweak: *const c_uchar)
269269
-> c_int;
270270

271-
#[link_name = "rustsecp256k1_v0_1_0_ec_pubkey_tweak_add"]
271+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ec_pubkey_tweak_add")]
272272
pub fn secp256k1_ec_pubkey_tweak_add(cx: *const Context,
273273
pk: *mut PublicKey,
274274
tweak: *const c_uchar)
275275
-> c_int;
276276

277-
#[link_name = "rustsecp256k1_v0_1_0_ec_privkey_tweak_mul"]
277+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ec_privkey_tweak_mul")]
278278
pub fn secp256k1_ec_privkey_tweak_mul(cx: *const Context,
279279
sk: *mut c_uchar,
280280
tweak: *const c_uchar)
281281
-> c_int;
282282

283-
#[link_name = "rustsecp256k1_v0_1_0_ec_pubkey_tweak_mul"]
283+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ec_pubkey_tweak_mul")]
284284
pub fn secp256k1_ec_pubkey_tweak_mul(cx: *const Context,
285285
pk: *mut PublicKey,
286286
tweak: *const c_uchar)
287287
-> c_int;
288288

289-
#[link_name = "rustsecp256k1_v0_1_0_ec_pubkey_combine"]
289+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ec_pubkey_combine")]
290290
pub fn secp256k1_ec_pubkey_combine(cx: *const Context,
291291
out: *mut PublicKey,
292292
ins: *const *const PublicKey,
293293
n: c_int)
294294
-> c_int;
295295

296-
#[link_name = "rustsecp256k1_v0_1_0_ecdh"]
296+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdh")]
297297
pub fn secp256k1_ecdh(
298298
cx: *const Context,
299299
output: *mut SharedSecret,
@@ -305,8 +305,6 @@ extern "C" {
305305
}
306306

307307

308-
#[cfg(all(feature = "std", not(feature = "dont_replace_c_symbols")))]
309-
#[no_mangle]
310308
/// A reimplementation of the C function `secp256k1_context_create` in rust.
311309
///
312310
/// This function allocates memory, the pointer should be deallocated using `secp256k1_context_destroy`
@@ -315,6 +313,8 @@ extern "C" {
315313
/// This will create a secp256k1 raw context.
316314
// Returns: a newly created context object.
317315
// In: flags: which parts of the context to initialize.
316+
#[no_mangle]
317+
#[cfg(all(feature = "std", not(feature = "external_symbols")))]
318318
pub unsafe extern "C" fn rustsecp256k1_v0_1_0_context_create(flags: c_uint) -> *mut Context {
319319
use std::mem;
320320
assert!(mem::align_of::<usize>() >= mem::align_of::<u8>());
@@ -331,19 +331,19 @@ pub unsafe extern "C" fn rustsecp256k1_v0_1_0_context_create(flags: c_uint) -> *
331331
secp256k1_context_preallocated_create(ptr as *mut c_void, flags)
332332
}
333333

334-
#[cfg(all(feature = "std", not(feature = "dont_replace_c_symbols")))]
334+
#[cfg(all(feature = "std", not(feature = "external_symbols")))]
335335
pub unsafe fn secp256k1_context_create(flags: c_uint) -> *mut Context {
336336
rustsecp256k1_v0_1_0_context_create(flags)
337337
}
338338

339-
#[cfg(all(feature = "std", not(feature = "dont_replace_c_symbols")))]
340-
#[no_mangle]
341339
/// A reimplementation of the C function `secp256k1_context_destroy` in rust.
342340
///
343341
/// This function destroys and deallcates the context created by `secp256k1_context_create`.
344342
///
345343
/// The pointer shouldn't be used after passing to this function, consider it as passing it to `free()`.
346344
///
345+
#[no_mangle]
346+
#[cfg(all(feature = "std", not(feature = "external_symbols")))]
347347
pub unsafe extern "C" fn rustsecp256k1_v0_1_0_context_destroy(ctx: *mut Context) {
348348
secp256k1_context_preallocated_destroy(ctx);
349349
let ctx: *mut usize = ctx as *mut usize;
@@ -354,14 +354,12 @@ pub unsafe extern "C" fn rustsecp256k1_v0_1_0_context_destroy(ctx: *mut Context)
354354
let _ = Box::from_raw(slice as *mut [usize]);
355355
}
356356

357-
#[cfg(all(feature = "std", not(feature = "dont_replace_c_symbols")))]
357+
#[cfg(all(feature = "std", not(feature = "external_symbols")))]
358358
pub unsafe fn secp256k1_context_destroy(ctx: *mut Context) {
359359
rustsecp256k1_v0_1_0_context_destroy(ctx)
360360
}
361361

362362

363-
#[cfg(not(feature = "dont_replace_c_symbols"))]
364-
#[no_mangle]
365363
/// **This function is an override for the C function, this is the an edited version of the original description:**
366364
///
367365
/// A callback function to be called when an illegal argument is passed to
@@ -380,15 +378,15 @@ pub unsafe fn secp256k1_context_destroy(ctx: *mut Context) {
380378
///
381379
/// See also secp256k1_default_error_callback_fn.
382380
///
381+
#[no_mangle]
382+
#[cfg(not(feature = "external_symbols"))]
383383
pub unsafe extern "C" fn rustsecp256k1_v0_1_0_default_illegal_callback_fn(message: *const c_char, _data: *mut c_void) {
384384
use core::str;
385385
let msg_slice = slice::from_raw_parts(message as *const u8, strlen(message));
386386
let msg = str::from_utf8_unchecked(msg_slice);
387387
panic!("[libsecp256k1] illegal argument. {}", msg);
388388
}
389389

390-
#[cfg(not(feature = "dont_replace_c_symbols"))]
391-
#[no_mangle]
392390
/// **This function is an override for the C function, this is the an edited version of the original description:**
393391
///
394392
/// A callback function to be called when an internal consistency check
@@ -403,6 +401,8 @@ pub unsafe extern "C" fn rustsecp256k1_v0_1_0_default_illegal_callback_fn(messag
403401
///
404402
/// See also secp256k1_default_illegal_callback_fn.
405403
///
404+
#[no_mangle]
405+
#[cfg(not(feature = "external_symbols"))]
406406
pub unsafe extern "C" fn rustsecp256k1_v0_1_0_default_error_callback_fn(message: *const c_char, _data: *mut c_void) {
407407
use core::str;
408408
let msg_slice = slice::from_raw_parts(message as *const u8, strlen(message));

secp256k1-sys/src/recovery.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,21 @@ impl Default for RecoverableSignature {
4141

4242
#[cfg(not(feature = "fuzztarget"))]
4343
extern "C" {
44-
#[link_name = "rustsecp256k1_v0_1_0_ecdsa_recoverable_signature_parse_compact"]
44+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_recoverable_signature_parse_compact")]
4545
pub fn secp256k1_ecdsa_recoverable_signature_parse_compact(cx: *const Context, sig: *mut RecoverableSignature,
4646
input64: *const c_uchar, recid: c_int)
4747
-> c_int;
4848

49-
#[link_name = "rustsecp256k1_v0_1_0_ecdsa_recoverable_signature_serialize_compact"]
49+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_recoverable_signature_serialize_compact")]
5050
pub fn secp256k1_ecdsa_recoverable_signature_serialize_compact(cx: *const Context, output64: *mut c_uchar,
5151
recid: *mut c_int, sig: *const RecoverableSignature)
5252
-> c_int;
5353

54-
#[link_name = "rustsecp256k1_v0_1_0_ecdsa_recoverable_signature_convert"]
54+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_recoverable_signature_convert")]
5555
pub fn secp256k1_ecdsa_recoverable_signature_convert(cx: *const Context, sig: *mut Signature,
5656
input: *const RecoverableSignature)
5757
-> c_int;
58-
#[link_name = "rustsecp256k1_v0_1_0_ecdsa_sign_recoverable"]
58+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_sign_recoverable")]
5959
pub fn secp256k1_ecdsa_sign_recoverable(cx: *const Context,
6060
sig: *mut RecoverableSignature,
6161
msg32: *const c_uchar,
@@ -64,7 +64,7 @@ extern "C" {
6464
noncedata: *const c_void)
6565
-> c_int;
6666

67-
#[link_name = "rustsecp256k1_v0_1_0_ecdsa_recover"]
67+
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_recover")]
6868
pub fn secp256k1_ecdsa_recover(cx: *const Context,
6969
pk: *mut PublicKey,
7070
sig: *const RecoverableSignature,

0 commit comments

Comments
 (0)