Skip to content

Commit c83c5a7

Browse files
committed
Deprecate ElligatorSwiftParty in favor of Party
The initial naming of ElligatorSwiftParty wasn't very descriptive, so it will be deprecated in favor of a more descriptive `Party` enum
1 parent 379e128 commit c83c5a7

File tree

1 file changed

+44
-11
lines changed

1 file changed

+44
-11
lines changed

src/ellswift.rs

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl ElligatorSwift {
154154
/// ```
155155
/// # #[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,13 +293,17 @@ 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")]
297+
#[allow(deprecated)]
294298
pub enum ElligatorSwiftParty {
295299
/// We are the initiator of the ECDH
296300
A,
297301
/// We are the responder of the ECDH
298302
B,
299303
}
300304

305+
#[allow(deprecated)]
306+
#[allow(dead_code)] // We aren't using this anymore in this library, but users might be using it
301307
impl ElligatorSwiftParty {
302308
fn to_ffi_int(self) -> c_int {
303309
match self {
@@ -307,6 +313,34 @@ impl ElligatorSwiftParty {
307313
}
308314
}
309315

316+
/// Represents the two parties in ECDH
317+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
318+
pub enum Party {
319+
/// The party that starts the key exchange or communication process
320+
Initiator,
321+
/// The party that responds to the initiator's communications
322+
Responder,
323+
}
324+
325+
#[allow(deprecated)]
326+
impl From<ElligatorSwiftParty> for Party {
327+
fn from(value: ElligatorSwiftParty) -> Self {
328+
match value {
329+
ElligatorSwiftParty::A => Party::Initiator,
330+
ElligatorSwiftParty::B => Party::Responder,
331+
}
332+
}
333+
}
334+
335+
impl Party {
336+
fn to_ffi_int(self) -> c_int {
337+
match self {
338+
Party::Initiator => 0,
339+
Party::Responder => 1,
340+
}
341+
}
342+
}
343+
310344
impl FromStr for ElligatorSwift {
311345
fn from_str(hex: &str) -> Result<Self, Self::Err> {
312346
let mut ser = [0u8; 64];
@@ -345,7 +379,7 @@ mod tests {
345379

346380
use crate::ellswift::ElligatorSwift;
347381
#[cfg(all(not(secp256k1_fuzz), feature = "alloc"))]
348-
use crate::ellswift::{ElligatorSwiftParty, ElligatorSwiftSharedSecret};
382+
use crate::ellswift::{ElligatorSwiftSharedSecret, Party};
349383
#[cfg(all(not(secp256k1_fuzz), feature = "alloc"))]
350384
use crate::SecretKey;
351385
use crate::{from_hex, PublicKey, XOnlyPublicKey};
@@ -391,7 +425,7 @@ mod tests {
391425
ell,
392426
ell,
393427
SecretKey::from_slice(&priv32).unwrap(),
394-
ElligatorSwiftParty::A,
428+
Party::Initiator,
395429
|_, _, _| ElligatorSwiftSharedSecret([0xff; 32]),
396430
);
397431
assert_eq!(pk, ElligatorSwiftSharedSecret([0xff; 32]));
@@ -605,8 +639,7 @@ mod tests {
605639
)
606640
};
607641
let sec_key = SecretKey::from_slice(&my_secret).unwrap();
608-
let initiator =
609-
if initiator == 0 { ElligatorSwiftParty::B } else { ElligatorSwiftParty::A };
642+
let initiator = if initiator == 0 { Party::Responder } else { Party::Initiator };
610643

611644
let shared = ElligatorSwift::shared_secret(el_a, el_b, sec_key, initiator, None);
612645

0 commit comments

Comments
 (0)