Skip to content

Commit 36be55f

Browse files
committed
Merge #752: Create Party enum and deprecate ElligatorSwiftParty in favor of it
bf2c35c Deprecate `ElligatorSwiftParty` in favor of `Party` (Shing Him Ng) 3b7d393 Create `Party` enum (Shing Him Ng) Pull request description: The initial naming of ElligatorSwiftParty wasn't very descriptive, so it will be deprecated in favor of a more descriptive `Party` enum. I updated `shared_secret` and `shared_secret_with_hasher` to accept the new `Party` enum as well - I'm not sure if there's a better way to do it, but changing it to an `impl Into<Party>` should preserve backwards compatibility Fixes #741 ACKs for top commit: tcharding: ACK bf2c35c apoelstra: ACK bf2c35c; successfully ran local tests Tree-SHA512: c516b8797b53e8e4167666ee6c93be61f67f2e71d33ba7354d6432199bd1f80680eea030c0c00ee5c0ba23204439d8c63c0efb8fc753f13e4cec189f7eee9a36
2 parents 379e128 + bf2c35c commit 36be55f

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

src/ellswift.rs

+35-14
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,18 +293,38 @@ 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-
impl ElligatorSwiftParty {
304+
/// Represents the two parties in ECDH
305+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
306+
pub enum Party {
307+
/// The party that starts the key exchange or communication process
308+
Initiator,
309+
/// The party that responds to the initiator's communications
310+
Responder,
311+
}
312+
313+
#[allow(deprecated)]
314+
impl From<ElligatorSwiftParty> for Party {
315+
fn from(value: ElligatorSwiftParty) -> Self {
316+
match value {
317+
ElligatorSwiftParty::A => Party::Initiator,
318+
ElligatorSwiftParty::B => Party::Responder,
319+
}
320+
}
321+
}
322+
323+
impl Party {
302324
fn to_ffi_int(self) -> c_int {
303325
match self {
304-
ElligatorSwiftParty::A => 0,
305-
ElligatorSwiftParty::B => 1,
326+
Party::Initiator => 0,
327+
Party::Responder => 1,
306328
}
307329
}
308330
}
@@ -345,7 +367,7 @@ mod tests {
345367

346368
use crate::ellswift::ElligatorSwift;
347369
#[cfg(all(not(secp256k1_fuzz), feature = "alloc"))]
348-
use crate::ellswift::{ElligatorSwiftParty, ElligatorSwiftSharedSecret};
370+
use crate::ellswift::{ElligatorSwiftSharedSecret, Party};
349371
#[cfg(all(not(secp256k1_fuzz), feature = "alloc"))]
350372
use crate::SecretKey;
351373
use crate::{from_hex, PublicKey, XOnlyPublicKey};
@@ -391,7 +413,7 @@ mod tests {
391413
ell,
392414
ell,
393415
SecretKey::from_slice(&priv32).unwrap(),
394-
ElligatorSwiftParty::A,
416+
Party::Initiator,
395417
|_, _, _| ElligatorSwiftSharedSecret([0xff; 32]),
396418
);
397419
assert_eq!(pk, ElligatorSwiftSharedSecret([0xff; 32]));
@@ -605,8 +627,7 @@ mod tests {
605627
)
606628
};
607629
let sec_key = SecretKey::from_slice(&my_secret).unwrap();
608-
let initiator =
609-
if initiator == 0 { ElligatorSwiftParty::B } else { ElligatorSwiftParty::A };
630+
let initiator = if initiator == 0 { Party::Responder } else { Party::Initiator };
610631

611632
let shared = ElligatorSwift::shared_secret(el_a, el_b, sec_key, initiator, None);
612633

0 commit comments

Comments
 (0)