Skip to content

Commit 4fe0101

Browse files
committed
ellswift: Remove context
We are removing the context everywhere we can, do so for the `ellswift` module.
1 parent 70e928c commit 4fe0101

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

src/ellswift.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use core::str::FromStr;
4242
use ffi::CPtr;
4343
use secp256k1_sys::types::{c_int, c_uchar, c_void};
4444

45-
use crate::{constants, ffi, from_hex, Error, PublicKey, Secp256k1, SecretKey, Verification};
45+
use crate::{constants, ffi, from_hex, Error, PublicKey, Secp256k1, SecretKey};
4646

4747
unsafe extern "C" fn hash_callback<F>(
4848
output: *mut c_uchar,
@@ -109,25 +109,25 @@ impl ElligatorSwift {
109109
/// # Example
110110
/// ```
111111
/// # #[cfg(feature = "alloc")] {
112-
/// use secp256k1::{ellswift::ElligatorSwift, PublicKey, Secp256k1, SecretKey};
113-
/// let secp = Secp256k1::new();
112+
/// use secp256k1::{ellswift::ElligatorSwift, PublicKey, SecretKey};
114113
/// let sk = SecretKey::from_secret_bytes([1; 32]).unwrap();
115-
/// let es = ElligatorSwift::from_seckey(&secp, sk, None);
114+
/// let es = ElligatorSwift::from_seckey(sk, None);
116115
/// # }
117116
/// ```
118-
pub fn from_seckey<C: Verification>(
119-
secp: &Secp256k1<C>,
120-
sk: SecretKey,
121-
aux_rand: Option<[u8; 32]>,
122-
) -> ElligatorSwift {
117+
pub fn from_seckey(sk: SecretKey, aux_rand: Option<[u8; 32]>) -> ElligatorSwift {
123118
let mut es_out = [0u8; constants::ELLSWIFT_ENCODING_SIZE];
124119
let aux_rand_ptr = aux_rand.as_c_ptr();
125120
unsafe {
126-
let ret = ffi::secp256k1_ellswift_create(
127-
secp.ctx().as_ptr(),
128-
es_out.as_mut_c_ptr(),
129-
sk.as_c_ptr(),
130-
aux_rand_ptr,
121+
let ret = crate::with_global_context(
122+
|secp: &Secp256k1<crate::AllPreallocated>| {
123+
ffi::secp256k1_ellswift_create(
124+
secp.ctx().as_ptr(),
125+
es_out.as_mut_c_ptr(),
126+
sk.as_c_ptr(),
127+
aux_rand_ptr,
128+
)
129+
},
130+
Some(&sk.to_secret_bytes()),
131131
);
132132
debug_assert_eq!(ret, 1);
133133
}
@@ -154,17 +154,15 @@ impl ElligatorSwift {
154154
/// # #[cfg(feature = "alloc")] {
155155
/// use secp256k1::{
156156
/// ellswift::{ElligatorSwift, Party},
157-
/// PublicKey, SecretKey, XOnlyPublicKey, Secp256k1,
157+
/// PublicKey, SecretKey, XOnlyPublicKey,
158158
/// };
159159
/// use core::str::FromStr;
160160
///
161-
/// let secp = Secp256k1::new();
162-
///
163161
/// let alice_sk = SecretKey::from_str("e714e76bdd67ad9f495683c37934148f4efc25ce3f01652c8a906498339e1f3a").unwrap();
164162
/// let bob_sk = SecretKey::from_str("b6c4b0e2f8c4359caf356a618cd1649d18790a1d67f7c2d1e4760e04c785db4f").unwrap();
165163
///
166-
/// let alice_es = ElligatorSwift::from_seckey(&secp, alice_sk, None);
167-
/// let bob_es = ElligatorSwift::from_seckey(&secp, bob_sk, None);
164+
/// let alice_es = ElligatorSwift::from_seckey(alice_sk, None);
165+
/// let bob_es = ElligatorSwift::from_seckey(bob_sk, None);
168166
///
169167
/// let alice_shared_secret = ElligatorSwift::shared_secret(alice_es, bob_es, alice_sk, Party::Initiator);
170168
/// let bob_shared_secret = ElligatorSwift::shared_secret(alice_es, bob_es, bob_sk, Party::Responder);
@@ -385,11 +383,9 @@ mod tests {
385383
#[cfg(all(not(secp256k1_fuzz), feature = "alloc"))]
386384
fn test_create_elligator_swift_create_rtt() {
387385
// Test that we can round trip an ElligatorSwift created from a secret key
388-
let secp = crate::Secp256k1::new();
389386
let rand32 = [1u8; 32];
390387
let priv32 = [1u8; 32];
391-
let ell =
392-
ElligatorSwift::from_seckey(&secp, SecretKey::from_secret_bytes(rand32).unwrap(), None);
388+
let ell = ElligatorSwift::from_seckey(SecretKey::from_secret_bytes(rand32).unwrap(), None);
393389
let pk = PublicKey::from_ellswift(ell);
394390
let expected = PublicKey::from_secret_key(&SecretKey::from_secret_bytes(priv32).unwrap());
395391

@@ -399,11 +395,9 @@ mod tests {
399395
#[cfg(all(not(secp256k1_fuzz), feature = "alloc"))]
400396
fn test_xdh_with_custom_hasher() {
401397
// Test the ECDH with a custom hash function
402-
let secp = crate::Secp256k1::new();
403398
let rand32 = [1u8; 32];
404399
let priv32 = [2u8; 32];
405400
let ell = ElligatorSwift::from_seckey(
406-
&secp,
407401
SecretKey::from_secret_bytes(rand32).unwrap(),
408402
Some(rand32),
409403
);

0 commit comments

Comments
 (0)