Skip to content

Commit d9d378e

Browse files
committed
Deprecate ElligatorSwiftParty in favor of Party
1 parent fef48bc commit d9d378e

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

src/ellswift.rs

+15-22
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ impl ElligatorSwift {
152152
/// the x-only Elliptic Curve Diffie-Hellman (ECDH) shared secret between Alice and Bob.
153153
/// # Example
154154
/// ```
155-
/// # #[cfg(feature = "alloc")] {
155+
/// #[cfg(feature = "alloc")] {
156156
/// use secp256k1::{
157-
/// ellswift::{ElligatorSwift, ElligatorSwiftParty},
157+
/// ellswift::{ElligatorSwift, Party},
158158
/// PublicKey, SecretKey, XOnlyPublicKey, Secp256k1,
159159
/// };
160160
/// use core::str::FromStr;
@@ -167,8 +167,8 @@ impl ElligatorSwift {
167167
/// let alice_es = ElligatorSwift::from_seckey(&secp, alice_sk, None);
168168
/// let bob_es = ElligatorSwift::from_seckey(&secp, bob_sk, None);
169169
///
170-
/// let alice_shared_secret = ElligatorSwift::shared_secret(alice_es, bob_es, alice_sk, ElligatorSwiftParty::A, None);
171-
/// let bob_shared_secret = ElligatorSwift::shared_secret(alice_es, bob_es, bob_sk, ElligatorSwiftParty::B, None);
170+
/// let alice_shared_secret = ElligatorSwift::shared_secret(alice_es, bob_es, alice_sk, Party::Initiator, None);
171+
/// let bob_shared_secret = ElligatorSwift::shared_secret(alice_es, bob_es, bob_sk, Party::Responder, None);
172172
///
173173
/// assert_eq!(alice_shared_secret, bob_shared_secret);
174174
/// # }
@@ -177,18 +177,19 @@ impl ElligatorSwift {
177177
ellswift_a: ElligatorSwift,
178178
ellswift_b: ElligatorSwift,
179179
secret_key: SecretKey,
180-
party: ElligatorSwiftParty,
180+
party: impl Into<Party>,
181181
data: Option<&[u8]>,
182182
) -> ElligatorSwiftSharedSecret {
183183
let mut shared_secret = [0u8; 32];
184+
let p: Party = party.into();
184185
unsafe {
185186
let ret = ffi::secp256k1_ellswift_xdh(
186187
ffi::secp256k1_context_no_precomp,
187188
shared_secret.as_mut_c_ptr(),
188189
ellswift_a.as_c_ptr(),
189190
ellswift_b.as_c_ptr(),
190191
secret_key.as_c_ptr(),
191-
party.to_ffi_int(),
192+
p.to_ffi_int(),
192193
ffi::secp256k1_ellswift_xdh_hash_function_bip324,
193194
data.as_c_ptr() as *mut c_void,
194195
);
@@ -206,22 +207,23 @@ impl ElligatorSwift {
206207
ellswift_a: ElligatorSwift,
207208
ellswift_b: ElligatorSwift,
208209
secret_key: SecretKey,
209-
party: ElligatorSwiftParty,
210+
party: impl Into<Party>,
210211
mut hash_function: F,
211212
) -> ElligatorSwiftSharedSecret
212213
where
213214
F: FnMut([u8; 32], [u8; 64], [u8; 64]) -> ElligatorSwiftSharedSecret,
214215
{
215216
let mut shared_secret = [0u8; 32];
216217
let hashfp = hash_callback::<F>;
218+
let p: Party = party.into();
217219
unsafe {
218220
let ret = ffi::secp256k1_ellswift_xdh(
219221
ffi::secp256k1_context_no_precomp,
220222
shared_secret.as_mut_c_ptr(),
221223
ellswift_a.0.as_c_ptr(),
222224
ellswift_b.0.as_c_ptr(),
223225
secret_key.as_c_ptr(),
224-
party.to_ffi_int(),
226+
p.to_ffi_int(),
225227
Some(hashfp),
226228
&mut hash_function as *mut F as *mut c_void,
227229
);
@@ -291,23 +293,14 @@ impl ElligatorSwiftSharedSecret {
291293
/// This distinction is important because the different parties compute different
292294
/// hashes of the shared secret.
293295
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
296+
#[deprecated(since = "0.30.0", note = "Use `Party` instead.")]
294297
pub enum ElligatorSwiftParty {
295298
/// We are the initiator of the ECDH
296299
A,
297300
/// We are the responder of the ECDH
298301
B,
299302
}
300303

301-
302-
impl ElligatorSwiftParty {
303-
fn to_ffi_int(self) -> c_int {
304-
match self {
305-
ElligatorSwiftParty::A => 0,
306-
ElligatorSwiftParty::B => 1,
307-
}
308-
}
309-
}
310-
311304
/// Represents the two parties in ECDH
312305
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
313306
pub enum Party {
@@ -317,6 +310,7 @@ pub enum Party {
317310
Responder,
318311
}
319312

313+
#[allow(deprecated)]
320314
impl From<ElligatorSwiftParty> for Party {
321315
fn from(value: ElligatorSwiftParty) -> Self {
322316
match value {
@@ -373,7 +367,7 @@ mod tests {
373367

374368
use crate::ellswift::ElligatorSwift;
375369
#[cfg(all(not(secp256k1_fuzz), feature = "alloc"))]
376-
use crate::ellswift::{ElligatorSwiftParty, ElligatorSwiftSharedSecret};
370+
use crate::ellswift::{ElligatorSwiftSharedSecret, Party};
377371
#[cfg(all(not(secp256k1_fuzz), feature = "alloc"))]
378372
use crate::SecretKey;
379373
use crate::{from_hex, PublicKey, XOnlyPublicKey};
@@ -419,7 +413,7 @@ mod tests {
419413
ell,
420414
ell,
421415
SecretKey::from_slice(&priv32).unwrap(),
422-
ElligatorSwiftParty::A,
416+
Party::Initiator,
423417
|_, _, _| ElligatorSwiftSharedSecret([0xff; 32]),
424418
);
425419
assert_eq!(pk, ElligatorSwiftSharedSecret([0xff; 32]));
@@ -633,8 +627,7 @@ mod tests {
633627
)
634628
};
635629
let sec_key = SecretKey::from_slice(&my_secret).unwrap();
636-
let initiator =
637-
if initiator == 0 { ElligatorSwiftParty::B } else { ElligatorSwiftParty::A };
630+
let initiator = if initiator == 0 { Party::Responder } else { Party::Initiator };
638631

639632
let shared = ElligatorSwift::shared_secret(el_a, el_b, sec_key, initiator, None);
640633

0 commit comments

Comments
 (0)