Skip to content

Commit 3c95e1d

Browse files
committed
Merge #753: Backport #752 to 0.29.x
f12bde6 Deprecate `ElligatorSwiftParty` in favor of `Party` (Shing Him Ng) 33fda15 Create `Party` enum (Shing Him Ng) Pull request description: Also, we should consider backporting this to the version used by rust-bitcoin 0.32. _Originally posted by apoelstra in #752 (comment) Backport #752 to the [version used by rust-bitcoin 0.32](https://github.com/rust-bitcoin/rust-bitcoin/blob/7af9e33f2b9033cf2701725eba280e14ebda0cf5/bitcoin/Cargo.toml#L35) ACKs for top commit: apoelstra: ACK f12bde6; successfully ran local tests Tree-SHA512: e8184c0df1f19a6512b1168bb1cf49e906de6d7f51ef1f9a4e3977422c36e603c3325fedb1485efa49ea8cb0361b54a293cdfefef10f3370541c8086b2b28bff
2 parents ba04d92 + f12bde6 commit 3c95e1d

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
@@ -153,7 +153,7 @@ impl ElligatorSwift {
153153
/// ```
154154
/// # #[cfg(feature = "alloc")] {
155155
/// use secp256k1::{
156-
/// ellswift::{ElligatorSwift, ElligatorSwiftParty},
156+
/// ellswift::{ElligatorSwift, Party},
157157
/// PublicKey, SecretKey, XOnlyPublicKey, Secp256k1,
158158
/// };
159159
/// use core::str::FromStr;
@@ -166,8 +166,8 @@ impl ElligatorSwift {
166166
/// let alice_es = ElligatorSwift::from_seckey(&secp, alice_sk, None);
167167
/// let bob_es = ElligatorSwift::from_seckey(&secp, bob_sk, None);
168168
///
169-
/// let alice_shared_secret = ElligatorSwift::shared_secret(alice_es, bob_es, alice_sk, ElligatorSwiftParty::A, None);
170-
/// let bob_shared_secret = ElligatorSwift::shared_secret(alice_es, bob_es, bob_sk, ElligatorSwiftParty::B, None);
169+
/// let alice_shared_secret = ElligatorSwift::shared_secret(alice_es, bob_es, alice_sk, Party::Initiator, None);
170+
/// let bob_shared_secret = ElligatorSwift::shared_secret(alice_es, bob_es, bob_sk, Party::Responder, None);
171171
///
172172
/// assert_eq!(alice_shared_secret, bob_shared_secret);
173173
/// # }
@@ -176,18 +176,19 @@ impl ElligatorSwift {
176176
ellswift_a: ElligatorSwift,
177177
ellswift_b: ElligatorSwift,
178178
secret_key: SecretKey,
179-
party: ElligatorSwiftParty,
179+
party: impl Into<Party>,
180180
data: Option<&[u8]>,
181181
) -> ElligatorSwiftSharedSecret {
182182
let mut shared_secret = [0u8; 32];
183+
let p: Party = party.into();
183184
unsafe {
184185
let ret = ffi::secp256k1_ellswift_xdh(
185186
ffi::secp256k1_context_no_precomp,
186187
shared_secret.as_mut_c_ptr(),
187188
ellswift_a.as_c_ptr(),
188189
ellswift_b.as_c_ptr(),
189190
secret_key.as_c_ptr(),
190-
party.to_ffi_int(),
191+
p.to_ffi_int(),
191192
ffi::secp256k1_ellswift_xdh_hash_function_bip324,
192193
data.as_c_ptr() as *mut c_void,
193194
);
@@ -205,22 +206,23 @@ impl ElligatorSwift {
205206
ellswift_a: ElligatorSwift,
206207
ellswift_b: ElligatorSwift,
207208
secret_key: SecretKey,
208-
party: ElligatorSwiftParty,
209+
party: impl Into<Party>,
209210
mut hash_function: F,
210211
) -> ElligatorSwiftSharedSecret
211212
where
212213
F: FnMut([u8; 32], [u8; 64], [u8; 64]) -> ElligatorSwiftSharedSecret,
213214
{
214215
let mut shared_secret = [0u8; 32];
215216
let hashfp = hash_callback::<F>;
217+
let p: Party = party.into();
216218
unsafe {
217219
let ret = ffi::secp256k1_ellswift_xdh(
218220
ffi::secp256k1_context_no_precomp,
219221
shared_secret.as_mut_c_ptr(),
220222
ellswift_a.0.as_c_ptr(),
221223
ellswift_b.0.as_c_ptr(),
222224
secret_key.as_c_ptr(),
223-
party.to_ffi_int(),
225+
p.to_ffi_int(),
224226
Some(hashfp),
225227
&mut hash_function as *mut F as *mut c_void,
226228
);
@@ -285,18 +287,38 @@ impl ElligatorSwiftSharedSecret {
285287
/// we are. In this context, "we" means the party that is using this library, and possesses the
286288
/// secret key passed to `ElligatorSwift::shared_secret`.
287289
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
290+
#[deprecated(since = "0.29.2", note = "Use `Party` instead.")]
288291
pub enum ElligatorSwiftParty {
289292
/// We are the initiator of the ECDH
290293
A,
291294
/// We are the responder of the ECDH
292295
B,
293296
}
294297

295-
impl ElligatorSwiftParty {
298+
/// Represents the two parties in ECDH
299+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
300+
pub enum Party {
301+
/// The party that starts the key exchange or communication process
302+
Initiator,
303+
/// The party that responds to the initiator's communications
304+
Responder,
305+
}
306+
307+
#[allow(deprecated)]
308+
impl From<ElligatorSwiftParty> for Party {
309+
fn from(value: ElligatorSwiftParty) -> Self {
310+
match value {
311+
ElligatorSwiftParty::A => Party::Initiator,
312+
ElligatorSwiftParty::B => Party::Responder,
313+
}
314+
}
315+
}
316+
317+
impl Party {
296318
fn to_ffi_int(self) -> c_int {
297319
match self {
298-
ElligatorSwiftParty::A => 0,
299-
ElligatorSwiftParty::B => 1,
320+
Party::Initiator => 0,
321+
Party::Responder => 1,
300322
}
301323
}
302324
}
@@ -339,7 +361,7 @@ mod tests {
339361

340362
use crate::ellswift::ElligatorSwift;
341363
#[cfg(all(not(secp256k1_fuzz), feature = "alloc"))]
342-
use crate::ellswift::{ElligatorSwiftParty, ElligatorSwiftSharedSecret};
364+
use crate::ellswift::{ElligatorSwiftSharedSecret, Party};
343365
#[cfg(all(not(secp256k1_fuzz), feature = "alloc"))]
344366
use crate::SecretKey;
345367
use crate::{from_hex, PublicKey, XOnlyPublicKey};
@@ -385,7 +407,7 @@ mod tests {
385407
ell,
386408
ell,
387409
SecretKey::from_slice(&priv32).unwrap(),
388-
ElligatorSwiftParty::A,
410+
Party::Initiator,
389411
|_, _, _| ElligatorSwiftSharedSecret([0xff; 32]),
390412
);
391413
assert_eq!(pk, ElligatorSwiftSharedSecret([0xff; 32]));
@@ -599,8 +621,7 @@ mod tests {
599621
)
600622
};
601623
let sec_key = SecretKey::from_slice(&my_secret).unwrap();
602-
let initiator =
603-
if initiator == 0 { ElligatorSwiftParty::B } else { ElligatorSwiftParty::A };
624+
let initiator = if initiator == 0 { Party::Responder } else { Party::Initiator };
604625

605626
let shared = ElligatorSwift::shared_secret(el_a, el_b, sec_key, initiator, None);
606627

0 commit comments

Comments
 (0)