From 5a6c6c8d0a1f3abac1b33e29a37967c36db9becb Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Sat, 17 Jan 2015 10:38:16 -0600 Subject: [PATCH] Fix for secp256k1 ffi changes All tests pass, compile now --- src/ffi.rs | 47 +++++++++++++++++++++++++---------------------- src/key.rs | 16 ++++++++-------- src/secp256k1.rs | 3 ++- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/src/ffi.rs b/src/ffi.rs index 3042685..408aa06 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -14,11 +14,14 @@ // //! FFI bindings -use libc::{c_int, c_uchar}; +use libc::{c_int, c_uchar, c_uint}; + +pub const SECP256K1_START_VERIFY: c_uint = 0x1; +pub const SECP256K1_START_SIGN: c_uint = 0x2; #[link(name = "secp256k1")] extern "C" { - pub fn secp256k1_start(); + pub fn secp256k1_start(flags: c_uint); pub fn secp256k1_stop(); @@ -27,9 +30,9 @@ extern "C" { pk: *const c_uchar, pk_len: c_int) -> c_int; - pub fn secp256k1_ecdsa_pubkey_create(pk: *mut c_uchar, pk_len : *mut c_int, - sk: *const c_uchar, compressed: c_int) - -> c_int; + pub fn secp256k1_ec_pubkey_create(pk: *mut c_uchar, pk_len: *mut c_int, + sk: *const c_uchar, compressed: c_int) + -> c_int; pub fn secp256k1_ecdsa_sign(msg: *const c_uchar, msg_len: c_int, sig: *mut c_uchar, sig_len: *mut c_int, @@ -46,27 +49,27 @@ extern "C" { pk_len: *mut c_int, compressed: c_int, recid: c_int) -> c_int; - pub fn secp256k1_ecdsa_seckey_verify(sk: *const c_uchar) -> c_int; + pub fn secp256k1_ec_seckey_verify(sk: *const c_uchar) -> c_int; - pub fn secp256k1_ecdsa_pubkey_verify(pk: *const c_uchar, - pk_len: c_int) -> c_int; + pub fn secp256k1_ec_pubkey_verify(pk: *const c_uchar, + pk_len: c_int) -> c_int; - pub fn secp256k1_ecdsa_privkey_tweak_add(sk: *mut c_uchar, - tweak: *const c_uchar) - -> c_int; + pub fn secp256k1_ec_privkey_tweak_add(sk: *mut c_uchar, + tweak: *const c_uchar) + -> c_int; - pub fn secp256k1_ecdsa_pubkey_tweak_add(pk: *mut c_uchar, - pk_len: c_int, - tweak: *const c_uchar) - -> c_int; + pub fn secp256k1_ec_pubkey_tweak_add(pk: *mut c_uchar, + pk_len: c_int, + tweak: *const c_uchar) + -> c_int; - pub fn secp256k1_ecdsa_privkey_tweak_mul(sk: *mut c_uchar, - tweak: *const c_uchar) - -> c_int; + pub fn secp256k1_ec_privkey_tweak_mul(sk: *mut c_uchar, + tweak: *const c_uchar) + -> c_int; - pub fn secp256k1_ecdsa_pubkey_tweak_mul(pk: *mut c_uchar, - pk_len: c_int, - tweak: *const c_uchar) - -> c_int; + pub fn secp256k1_ec_pubkey_tweak_mul(pk: *mut c_uchar, + pk_len: c_int, + tweak: *const c_uchar) + -> c_int; } diff --git a/src/key.rs b/src/key.rs index 1c0255d..d2c3dc5 100644 --- a/src/key.rs +++ b/src/key.rs @@ -164,7 +164,7 @@ impl SecretKey { init(); let mut data = random_32_bytes(rng); unsafe { - while ffi::secp256k1_ecdsa_seckey_verify(data.as_ptr()) == 0 { + while ffi::secp256k1_ec_seckey_verify(data.as_ptr()) == 0 { data = random_32_bytes(rng); } } @@ -179,7 +179,7 @@ impl SecretKey { constants::SECRET_KEY_SIZE => { let mut ret = [0; constants::SECRET_KEY_SIZE]; unsafe { - if ffi::secp256k1_ecdsa_seckey_verify(data.as_ptr()) == 0 { + if ffi::secp256k1_ec_seckey_verify(data.as_ptr()) == 0 { return Err(InvalidSecretKey); } copy_nonoverlapping_memory(ret.as_mut_ptr(), @@ -200,7 +200,7 @@ impl SecretKey { pub fn add_assign(&mut self, other: &SecretKey) -> Result<()> { init(); unsafe { - if ffi::secp256k1_ecdsa_privkey_tweak_add(self.as_mut_ptr(), other.as_ptr()) != 1 { + if ffi::secp256k1_ec_privkey_tweak_add(self.as_mut_ptr(), other.as_ptr()) != 1 { Err(Unknown) } else { Ok(()) @@ -257,7 +257,7 @@ impl PublicKey { unsafe { // We can assume the return value because it's not possible to construct // an invalid `SecretKey` without transmute trickery or something - assert_eq!(ffi::secp256k1_ecdsa_pubkey_create( + assert_eq!(ffi::secp256k1_ec_pubkey_create( pk.as_mut_ptr(), &mut len, sk.as_ptr(), compressed), 1); } @@ -272,7 +272,7 @@ impl PublicKey { constants::COMPRESSED_PUBLIC_KEY_SIZE => { let mut ret = [0; constants::COMPRESSED_PUBLIC_KEY_SIZE]; unsafe { - if ffi::secp256k1_ecdsa_pubkey_verify(data.as_ptr(), + if ffi::secp256k1_ec_pubkey_verify(data.as_ptr(), data.len() as ::libc::c_int) == 0 { return Err(InvalidPublicKey); } @@ -349,9 +349,9 @@ impl PublicKey { pub fn add_exp_assign(&mut self, other: &SecretKey) -> Result<()> { init(); unsafe { - if ffi::secp256k1_ecdsa_pubkey_tweak_add(self.as_mut_ptr(), - self.len() as ::libc::c_int, - other.as_ptr()) != 1 { + if ffi::secp256k1_ec_pubkey_tweak_add(self.as_mut_ptr(), + self.len() as ::libc::c_int, + other.as_ptr()) != 1 { Err(Unknown) } else { Ok(()) diff --git a/src/secp256k1.rs b/src/secp256k1.rs index 5444e55..5a31dfe 100644 --- a/src/secp256k1.rs +++ b/src/secp256k1.rs @@ -150,7 +150,8 @@ pub struct Secp256k1 { pub fn init() { unsafe { Secp256k1_init.call_once(|| { - ffi::secp256k1_start(); + ffi::secp256k1_start(ffi::SECP256K1_START_VERIFY | + ffi::SECP256K1_START_SIGN); }); } }