Skip to content

Commit 87d936a

Browse files
Rename schnorr::PublicKey to schnorr::XOnlyPublicKey
The public key is unrelated to the signature algorithm. It will be moved out of the module in another commit. For ease of review, the renamed is kept separate.
1 parent 2e0e731 commit 87d936a

File tree

1 file changed

+51
-51
lines changed

1 file changed

+51
-51
lines changed

src/schnorrsig.rs

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ impl str::FromStr for Signature {
7474
}
7575
}
7676

77-
/// A Schnorr public key, used for verification of Schnorr signatures
77+
/// A x-only public key, used for verification of Schnorr signatures
7878
#[derive(Copy, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)]
79-
pub struct PublicKey(ffi::XOnlyPublicKey);
79+
pub struct XOnlyPublicKey(ffi::XOnlyPublicKey);
8080

81-
impl fmt::LowerHex for PublicKey {
81+
impl fmt::LowerHex for XOnlyPublicKey {
8282
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8383
let ser = self.serialize();
8484
for ch in &ser[..] {
@@ -88,19 +88,19 @@ impl fmt::LowerHex for PublicKey {
8888
}
8989
}
9090

91-
impl fmt::Display for PublicKey {
91+
impl fmt::Display for XOnlyPublicKey {
9292
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9393
fmt::LowerHex::fmt(self, f)
9494
}
9595
}
9696

97-
impl str::FromStr for PublicKey {
97+
impl str::FromStr for XOnlyPublicKey {
9898
type Err = Error;
99-
fn from_str(s: &str) -> Result<PublicKey, Error> {
99+
fn from_str(s: &str) -> Result<XOnlyPublicKey, Error> {
100100
let mut res = [0u8; constants::SCHNORRSIG_PUBLIC_KEY_SIZE];
101101
match from_hex(s, &mut res) {
102102
Ok(constants::SCHNORRSIG_PUBLIC_KEY_SIZE) => {
103-
PublicKey::from_slice(&res[0..constants::SCHNORRSIG_PUBLIC_KEY_SIZE])
103+
XOnlyPublicKey::from_slice(&res[0..constants::SCHNORRSIG_PUBLIC_KEY_SIZE])
104104
}
105105
_ => Err(Error::InvalidPublicKey),
106106
}
@@ -122,7 +122,7 @@ impl Signature {
122122
}
123123
}
124124

125-
impl PublicKey {
125+
impl XOnlyPublicKey {
126126
/// Obtains a raw const pointer suitable for use with FFI functions
127127
#[inline]
128128
pub fn as_ptr(&self) -> *const ffi::XOnlyPublicKey {
@@ -137,7 +137,7 @@ impl PublicKey {
137137

138138
/// Creates a new Schnorr public key from a Schnorr key pair.
139139
#[inline]
140-
pub fn from_keypair<C: Signing>(secp: &Secp256k1<C>, keypair: &KeyPair) -> PublicKey {
140+
pub fn from_keypair<C: Signing>(secp: &Secp256k1<C>, keypair: &KeyPair) -> XOnlyPublicKey {
141141
let mut pk_parity = 0;
142142
unsafe {
143143
let mut xonly_pk = ffi::XOnlyPublicKey::new();
@@ -148,7 +148,7 @@ impl PublicKey {
148148
keypair.as_ptr(),
149149
);
150150
debug_assert_eq!(ret, 1);
151-
PublicKey(xonly_pk)
151+
XOnlyPublicKey(xonly_pk)
152152
}
153153
}
154154

@@ -159,7 +159,7 @@ impl PublicKey {
159159
/// Returns [`Error::InvalidPublicKey`] if the length of the data slice is not 32 bytes or the
160160
/// slice does not represent a valid Secp256k1 point x coordinate
161161
#[inline]
162-
pub fn from_slice(data: &[u8]) -> Result<PublicKey, Error> {
162+
pub fn from_slice(data: &[u8]) -> Result<XOnlyPublicKey, Error> {
163163
if data.is_empty() || data.len() != constants::SCHNORRSIG_PUBLIC_KEY_SIZE {
164164
return Err(Error::InvalidPublicKey);
165165
}
@@ -172,7 +172,7 @@ impl PublicKey {
172172
data.as_c_ptr(),
173173
) == 1
174174
{
175-
Ok(PublicKey(pk))
175+
Ok(XOnlyPublicKey(pk))
176176
} else {
177177
Err(Error::InvalidPublicKey)
178178
}
@@ -270,7 +270,7 @@ impl PublicKey {
270270
}
271271
}
272272

273-
impl CPtr for PublicKey {
273+
impl CPtr for XOnlyPublicKey {
274274
type Target = ffi::XOnlyPublicKey;
275275
fn as_c_ptr(&self) -> *const Self::Target {
276276
self.as_ptr()
@@ -282,15 +282,15 @@ impl CPtr for PublicKey {
282282
}
283283

284284
/// Creates a new Schnorr public key from a FFI x-only public key
285-
impl From<ffi::XOnlyPublicKey> for PublicKey {
285+
impl From<ffi::XOnlyPublicKey> for XOnlyPublicKey {
286286
#[inline]
287-
fn from(pk: ffi::XOnlyPublicKey) -> PublicKey {
288-
PublicKey(pk)
287+
fn from(pk: ffi::XOnlyPublicKey) -> XOnlyPublicKey {
288+
XOnlyPublicKey(pk)
289289
}
290290
}
291291

292-
impl From<::key::PublicKey> for PublicKey {
293-
fn from(src: ::key::PublicKey) -> PublicKey {
292+
impl From<::key::PublicKey> for XOnlyPublicKey {
293+
fn from(src: ::key::PublicKey) -> XOnlyPublicKey {
294294
unsafe {
295295
let mut pk = ffi::XOnlyPublicKey::new();
296296
assert_eq!(
@@ -302,13 +302,13 @@ impl From<::key::PublicKey> for PublicKey {
302302
src.as_c_ptr(),
303303
)
304304
);
305-
PublicKey(pk)
305+
XOnlyPublicKey(pk)
306306
}
307307
}
308308
}
309309

310310
#[cfg(feature = "serde")]
311-
impl ::serde::Serialize for PublicKey {
311+
impl ::serde::Serialize for XOnlyPublicKey {
312312
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
313313
if s.is_human_readable() {
314314
s.collect_str(self)
@@ -319,7 +319,7 @@ impl ::serde::Serialize for PublicKey {
319319
}
320320

321321
#[cfg(feature = "serde")]
322-
impl<'de> ::serde::Deserialize<'de> for PublicKey {
322+
impl<'de> ::serde::Deserialize<'de> for XOnlyPublicKey {
323323
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
324324
if d.is_human_readable() {
325325
d.deserialize_str(super::serde_util::FromStrVisitor::new(
@@ -328,7 +328,7 @@ impl<'de> ::serde::Deserialize<'de> for PublicKey {
328328
} else {
329329
d.deserialize_bytes(super::serde_util::BytesVisitor::new(
330330
"raw 32 bytes schnorr public key",
331-
PublicKey::from_slice
331+
XOnlyPublicKey::from_slice
332332
))
333333
}
334334
}
@@ -411,7 +411,7 @@ impl<C: Signing> Secp256k1<C> {
411411
&self,
412412
sig: &Signature,
413413
msg: &Message,
414-
pubkey: &PublicKey,
414+
pubkey: &XOnlyPublicKey,
415415
) -> Result<(), Error> {
416416
unsafe {
417417
let ret = ffi::secp256k1_schnorrsig_verify(
@@ -439,9 +439,9 @@ impl<C: Signing> Secp256k1<C> {
439439
pub fn generate_schnorrsig_keypair<R: Rng + ?Sized>(
440440
&self,
441441
rng: &mut R,
442-
) -> (KeyPair, PublicKey) {
442+
) -> (KeyPair, XOnlyPublicKey) {
443443
let sk = KeyPair::new(self, rng);
444-
let pubkey = PublicKey::from_keypair(self, &sk);
444+
let pubkey = XOnlyPublicKey::from_keypair(self, &sk);
445445
(sk, pubkey)
446446
}
447447
}
@@ -450,7 +450,7 @@ impl<C: Signing> Secp256k1<C> {
450450
mod tests {
451451
use super::super::Error::InvalidPublicKey;
452452
use super::super::{constants, from_hex, All, Message, Secp256k1};
453-
use super::{KeyPair, PublicKey, Signature};
453+
use super::{KeyPair, XOnlyPublicKey, Signature};
454454
use rand::{rngs::ThreadRng, thread_rng, Error, ErrorKind, RngCore};
455455
use rand_core::impls;
456456
use std::iter;
@@ -548,17 +548,17 @@ mod tests {
548548
let msg = Message::from_slice(&hex_msg).unwrap();
549549
let sig = Signature::from_str("6470FD1303DDA4FDA717B9837153C24A6EAB377183FC438F939E0ED2B620E9EE5077C4A8B8DCA28963D772A94F5F0DDF598E1C47C137F91933274C7C3EDADCE8").unwrap();
550550
let pubkey =
551-
PublicKey::from_str("B33CC9EDC096D0A83416964BD3C6247B8FECD256E4EFA7870D2C854BDEB33390")
551+
XOnlyPublicKey::from_str("B33CC9EDC096D0A83416964BD3C6247B8FECD256E4EFA7870D2C854BDEB33390")
552552
.unwrap();
553553

554554
assert!(secp.schnorrsig_verify(&sig, &msg, &pubkey).is_ok());
555555
}
556556

557557
#[test]
558558
fn test_pubkey_from_slice() {
559-
assert_eq!(PublicKey::from_slice(&[]), Err(InvalidPublicKey));
560-
assert_eq!(PublicKey::from_slice(&[1, 2, 3]), Err(InvalidPublicKey));
561-
let pk = PublicKey::from_slice(&[
559+
assert_eq!(XOnlyPublicKey::from_slice(&[]), Err(InvalidPublicKey));
560+
assert_eq!(XOnlyPublicKey::from_slice(&[1, 2, 3]), Err(InvalidPublicKey));
561+
let pk = XOnlyPublicKey::from_slice(&[
562562
0xB3, 0x3C, 0xC9, 0xED, 0xC0, 0x96, 0xD0, 0xA8, 0x34, 0x16, 0x96, 0x4B, 0xD3, 0xC6,
563563
0x24, 0x7B, 0x8F, 0xEC, 0xD2, 0x56, 0xE4, 0xEF, 0xA7, 0x87, 0x0D, 0x2C, 0x85, 0x4B,
564564
0xDE, 0xB3, 0x33, 0x90,
@@ -571,7 +571,7 @@ mod tests {
571571
let secp = Secp256k1::new();
572572
let (_, pubkey) = secp.generate_schnorrsig_keypair(&mut thread_rng());
573573
let ser = pubkey.serialize();
574-
let pubkey2 = PublicKey::from_slice(&ser).unwrap();
574+
let pubkey2 = XOnlyPublicKey::from_slice(&ser).unwrap();
575575
assert_eq!(pubkey, pubkey2);
576576
}
577577

@@ -584,35 +584,35 @@ mod tests {
584584
assert_eq!(SecretKey::from_str(sk_str).unwrap(), sk);
585585
let pk = ::key::PublicKey::from_keypair(&keypair);
586586
assert_eq!(::key::PublicKey::from_secret_key(&secp, &sk), pk);
587-
let xpk = PublicKey::from_keypair(&secp, &keypair);
588-
assert_eq!(PublicKey::from(pk), xpk);
587+
let xpk = XOnlyPublicKey::from_keypair(&secp, &keypair);
588+
assert_eq!(XOnlyPublicKey::from(pk), xpk);
589589
}
590590

591591
#[test]
592592
fn test_pubkey_from_bad_slice() {
593593
// Bad sizes
594594
assert_eq!(
595-
PublicKey::from_slice(&[0; constants::SCHNORRSIG_PUBLIC_KEY_SIZE - 1]),
595+
XOnlyPublicKey::from_slice(&[0; constants::SCHNORRSIG_PUBLIC_KEY_SIZE - 1]),
596596
Err(InvalidPublicKey)
597597
);
598598
assert_eq!(
599-
PublicKey::from_slice(&[0; constants::SCHNORRSIG_PUBLIC_KEY_SIZE + 1]),
599+
XOnlyPublicKey::from_slice(&[0; constants::SCHNORRSIG_PUBLIC_KEY_SIZE + 1]),
600600
Err(InvalidPublicKey)
601601
);
602602

603603
// Bad parse
604604
assert_eq!(
605-
PublicKey::from_slice(&[0xff; constants::SCHNORRSIG_PUBLIC_KEY_SIZE]),
605+
XOnlyPublicKey::from_slice(&[0xff; constants::SCHNORRSIG_PUBLIC_KEY_SIZE]),
606606
Err(InvalidPublicKey)
607607
);
608608
// In fuzzing mode restrictions on public key validity are much more
609609
// relaxed, thus the invalid check below is expected to fail.
610610
#[cfg(not(fuzzing))]
611611
assert_eq!(
612-
PublicKey::from_slice(&[0x55; constants::SCHNORRSIG_PUBLIC_KEY_SIZE]),
612+
XOnlyPublicKey::from_slice(&[0x55; constants::SCHNORRSIG_PUBLIC_KEY_SIZE]),
613613
Err(InvalidPublicKey)
614614
);
615-
assert_eq!(PublicKey::from_slice(&[]), Err(InvalidPublicKey));
615+
assert_eq!(XOnlyPublicKey::from_slice(&[]), Err(InvalidPublicKey));
616616
}
617617

618618
#[test]
@@ -630,43 +630,43 @@ mod tests {
630630
// In fuzzing mode secret->public key derivation is different, so
631631
// hard-code the epected result.
632632
#[cfg(not(fuzzing))]
633-
let pk = PublicKey::from_keypair(&s, &sk);
633+
let pk = XOnlyPublicKey::from_keypair(&s, &sk);
634634
#[cfg(fuzzing)]
635-
let pk = PublicKey::from_slice(&[0x18, 0x84, 0x57, 0x81, 0xf6, 0x31, 0xc4, 0x8f, 0x1c, 0x97, 0x09, 0xe2, 0x30, 0x92, 0x06, 0x7d, 0x06, 0x83, 0x7f, 0x30, 0xaa, 0x0c, 0xd0, 0x54, 0x4a, 0xc8, 0x87, 0xfe, 0x91, 0xdd, 0xd1, 0x66]).expect("pk");
635+
let pk = XOnlyPublicKey::from_slice(&[0x18, 0x84, 0x57, 0x81, 0xf6, 0x31, 0xc4, 0x8f, 0x1c, 0x97, 0x09, 0xe2, 0x30, 0x92, 0x06, 0x7d, 0x06, 0x83, 0x7f, 0x30, 0xaa, 0x0c, 0xd0, 0x54, 0x4a, 0xc8, 0x87, 0xfe, 0x91, 0xdd, 0xd1, 0x66]).expect("pk");
636636

637637
assert_eq!(
638638
pk.to_string(),
639639
"18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166"
640640
);
641641
assert_eq!(
642-
PublicKey::from_str("18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166")
642+
XOnlyPublicKey::from_str("18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166")
643643
.unwrap(),
644644
pk
645645
);
646646

647-
assert!(PublicKey::from_str(
647+
assert!(XOnlyPublicKey::from_str(
648648
"00000000000000000000000000000000000000000000000000000000000000000"
649649
)
650650
.is_err());
651-
assert!(PublicKey::from_str(
651+
assert!(XOnlyPublicKey::from_str(
652652
"18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd16601"
653653
)
654654
.is_err());
655-
assert!(PublicKey::from_str(
655+
assert!(XOnlyPublicKey::from_str(
656656
"18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd16"
657657
)
658658
.is_err());
659-
assert!(PublicKey::from_str(
659+
assert!(XOnlyPublicKey::from_str(
660660
"18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd1"
661661
)
662662
.is_err());
663-
assert!(PublicKey::from_str(
663+
assert!(XOnlyPublicKey::from_str(
664664
"xx18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd1"
665665
)
666666
.is_err());
667667

668668
let long_str: String = iter::repeat('a').take(1024 * 1024).collect();
669-
assert!(PublicKey::from_str(&long_str).is_err());
669+
assert!(XOnlyPublicKey::from_str(&long_str).is_err());
670670
}
671671

672672
#[test]
@@ -734,7 +734,7 @@ mod tests {
734734
static PK_STR: &'static str = "\
735735
18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166\
736736
";
737-
let pk = PublicKey::from_slice(&PK_BYTES).unwrap();
737+
let pk = XOnlyPublicKey::from_slice(&PK_BYTES).unwrap();
738738

739739
assert_tokens(&sig.compact(), &[Token::BorrowedBytes(&SIG_BYTES[..])]);
740740
assert_tokens(&sig.compact(), &[Token::Bytes(&SIG_BYTES[..])]);
@@ -763,7 +763,7 @@ mod tests {
763763
let orig_pk = pk;
764764
kp.tweak_add_assign(&s, &tweak).expect("Tweak error");
765765
let parity = pk.tweak_add_assign(&s, &tweak).expect("Tweak error");
766-
assert_eq!(PublicKey::from_keypair(&s, &kp), pk);
766+
assert_eq!(XOnlyPublicKey::from_keypair(&s, &kp), pk);
767767
assert!(orig_pk.tweak_add_check(&s, &pk, parity, tweak));
768768
}
769769
}
@@ -779,8 +779,8 @@ mod tests {
779779
)
780780
.unwrap();
781781

782-
let pk1 = PublicKey::from(kpk1);
783-
let pk2 = PublicKey::from(kpk2);
782+
let pk1 = XOnlyPublicKey::from(kpk1);
783+
let pk2 = XOnlyPublicKey::from(kpk2);
784784

785785
assert_eq!(pk1.serialize()[..], kpk1.serialize()[1..]);
786786
assert_eq!(pk2.serialize()[..], kpk2.serialize()[1..]);

0 commit comments

Comments
 (0)