Skip to content

Commit 7d35501

Browse files
committed
Remove usage of secp256k1_context_no_precomp
Now that we don't need to pass the context into APIs we can remove the usage of `secp256k1_context_no_precomp` which was specifically used to avoid the context. Close #846
1 parent 155c7c1 commit 7d35501

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

src/ecdh.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use secp256k1_sys::types::{c_int, c_uchar, c_void};
1010

1111
use crate::ffi::{self, CPtr};
1212
use crate::key::{PublicKey, SecretKey};
13-
use crate::{constants, Error};
13+
use crate::{constants, Error, Secp256k1};
1414

1515
// The logic for displaying shared secrets relies on this (see `secret.rs`).
1616
const SHARED_SECRET_SIZE: usize = constants::SECRET_KEY_SIZE;
@@ -39,14 +39,20 @@ impl SharedSecret {
3939
#[inline]
4040
pub fn new(point: &PublicKey, scalar: &SecretKey) -> SharedSecret {
4141
let mut buf = [0u8; SHARED_SECRET_SIZE];
42+
4243
let res = unsafe {
43-
ffi::secp256k1_ecdh(
44-
ffi::secp256k1_context_no_precomp,
45-
buf.as_mut_ptr(),
46-
point.as_c_ptr(),
47-
scalar.as_c_ptr(),
48-
ffi::secp256k1_ecdh_hash_function_default,
49-
ptr::null_mut(),
44+
crate::with_global_context(
45+
|secp: &Secp256k1<crate::AllPreallocated>| {
46+
ffi::secp256k1_ecdh(
47+
secp.ctx.as_ptr(),
48+
buf.as_mut_ptr(),
49+
point.as_c_ptr(),
50+
scalar.as_c_ptr(),
51+
ffi::secp256k1_ecdh_hash_function_default,
52+
ptr::null_mut(),
53+
)
54+
},
55+
None,
5056
)
5157
};
5258
debug_assert_eq!(res, 1);
@@ -125,13 +131,18 @@ pub fn shared_secret_point(point: &PublicKey, scalar: &SecretKey) -> [u8; 64] {
125131
let mut xy = [0u8; 64];
126132

127133
let res = unsafe {
128-
ffi::secp256k1_ecdh(
129-
ffi::secp256k1_context_no_precomp,
130-
xy.as_mut_ptr(),
131-
point.as_c_ptr(),
132-
scalar.as_c_ptr(),
133-
Some(c_callback),
134-
ptr::null_mut(),
134+
crate::with_global_context(
135+
|secp: &Secp256k1<crate::AllPreallocated>| {
136+
ffi::secp256k1_ecdh(
137+
secp.ctx.as_ptr(),
138+
xy.as_mut_ptr(),
139+
point.as_c_ptr(),
140+
scalar.as_c_ptr(),
141+
Some(c_callback),
142+
ptr::null_mut(),
143+
)
144+
},
145+
None,
135146
)
136147
};
137148
// Our callback *always* returns 1.

0 commit comments

Comments
 (0)