@@ -74,11 +74,11 @@ impl str::FromStr for Signature {
74
74
}
75
75
}
76
76
77
- /// A Schnorr public key, used for verification of Schnorr signatures
77
+ /// A x-only public key, used for verification of Schnorr signatures
78
78
#[ derive( Copy , Clone , PartialEq , Eq , Debug , PartialOrd , Ord , Hash ) ]
79
- pub struct PublicKey ( ffi:: XOnlyPublicKey ) ;
79
+ pub struct XOnlyPublicKey ( ffi:: XOnlyPublicKey ) ;
80
80
81
- impl fmt:: LowerHex for PublicKey {
81
+ impl fmt:: LowerHex for XOnlyPublicKey {
82
82
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
83
83
let ser = self . serialize ( ) ;
84
84
for ch in & ser[ ..] {
@@ -88,19 +88,19 @@ impl fmt::LowerHex for PublicKey {
88
88
}
89
89
}
90
90
91
- impl fmt:: Display for PublicKey {
91
+ impl fmt:: Display for XOnlyPublicKey {
92
92
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
93
93
fmt:: LowerHex :: fmt ( self , f)
94
94
}
95
95
}
96
96
97
- impl str:: FromStr for PublicKey {
97
+ impl str:: FromStr for XOnlyPublicKey {
98
98
type Err = Error ;
99
- fn from_str ( s : & str ) -> Result < PublicKey , Error > {
99
+ fn from_str ( s : & str ) -> Result < XOnlyPublicKey , Error > {
100
100
let mut res = [ 0u8 ; constants:: SCHNORRSIG_PUBLIC_KEY_SIZE ] ;
101
101
match from_hex ( s, & mut res) {
102
102
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 ] )
104
104
}
105
105
_ => Err ( Error :: InvalidPublicKey ) ,
106
106
}
@@ -122,7 +122,7 @@ impl Signature {
122
122
}
123
123
}
124
124
125
- impl PublicKey {
125
+ impl XOnlyPublicKey {
126
126
/// Obtains a raw const pointer suitable for use with FFI functions
127
127
#[ inline]
128
128
pub fn as_ptr ( & self ) -> * const ffi:: XOnlyPublicKey {
@@ -137,7 +137,7 @@ impl PublicKey {
137
137
138
138
/// Creates a new Schnorr public key from a Schnorr key pair.
139
139
#[ 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 {
141
141
let mut pk_parity = 0 ;
142
142
unsafe {
143
143
let mut xonly_pk = ffi:: XOnlyPublicKey :: new ( ) ;
@@ -148,7 +148,7 @@ impl PublicKey {
148
148
keypair. as_ptr ( ) ,
149
149
) ;
150
150
debug_assert_eq ! ( ret, 1 ) ;
151
- PublicKey ( xonly_pk)
151
+ XOnlyPublicKey ( xonly_pk)
152
152
}
153
153
}
154
154
@@ -159,7 +159,7 @@ impl PublicKey {
159
159
/// Returns [`Error::InvalidPublicKey`] if the length of the data slice is not 32 bytes or the
160
160
/// slice does not represent a valid Secp256k1 point x coordinate
161
161
#[ inline]
162
- pub fn from_slice ( data : & [ u8 ] ) -> Result < PublicKey , Error > {
162
+ pub fn from_slice ( data : & [ u8 ] ) -> Result < XOnlyPublicKey , Error > {
163
163
if data. is_empty ( ) || data. len ( ) != constants:: SCHNORRSIG_PUBLIC_KEY_SIZE {
164
164
return Err ( Error :: InvalidPublicKey ) ;
165
165
}
@@ -172,7 +172,7 @@ impl PublicKey {
172
172
data. as_c_ptr ( ) ,
173
173
) == 1
174
174
{
175
- Ok ( PublicKey ( pk) )
175
+ Ok ( XOnlyPublicKey ( pk) )
176
176
} else {
177
177
Err ( Error :: InvalidPublicKey )
178
178
}
@@ -270,7 +270,7 @@ impl PublicKey {
270
270
}
271
271
}
272
272
273
- impl CPtr for PublicKey {
273
+ impl CPtr for XOnlyPublicKey {
274
274
type Target = ffi:: XOnlyPublicKey ;
275
275
fn as_c_ptr ( & self ) -> * const Self :: Target {
276
276
self . as_ptr ( )
@@ -282,15 +282,15 @@ impl CPtr for PublicKey {
282
282
}
283
283
284
284
/// 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 {
286
286
#[ inline]
287
- fn from ( pk : ffi:: XOnlyPublicKey ) -> PublicKey {
288
- PublicKey ( pk)
287
+ fn from ( pk : ffi:: XOnlyPublicKey ) -> XOnlyPublicKey {
288
+ XOnlyPublicKey ( pk)
289
289
}
290
290
}
291
291
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 {
294
294
unsafe {
295
295
let mut pk = ffi:: XOnlyPublicKey :: new ( ) ;
296
296
assert_eq ! (
@@ -302,13 +302,13 @@ impl From<::key::PublicKey> for PublicKey {
302
302
src. as_c_ptr( ) ,
303
303
)
304
304
) ;
305
- PublicKey ( pk)
305
+ XOnlyPublicKey ( pk)
306
306
}
307
307
}
308
308
}
309
309
310
310
#[ cfg( feature = "serde" ) ]
311
- impl :: serde:: Serialize for PublicKey {
311
+ impl :: serde:: Serialize for XOnlyPublicKey {
312
312
fn serialize < S : :: serde:: Serializer > ( & self , s : S ) -> Result < S :: Ok , S :: Error > {
313
313
if s. is_human_readable ( ) {
314
314
s. collect_str ( self )
@@ -319,7 +319,7 @@ impl ::serde::Serialize for PublicKey {
319
319
}
320
320
321
321
#[ cfg( feature = "serde" ) ]
322
- impl < ' de > :: serde:: Deserialize < ' de > for PublicKey {
322
+ impl < ' de > :: serde:: Deserialize < ' de > for XOnlyPublicKey {
323
323
fn deserialize < D : :: serde:: Deserializer < ' de > > ( d : D ) -> Result < Self , D :: Error > {
324
324
if d. is_human_readable ( ) {
325
325
d. deserialize_str ( super :: serde_util:: FromStrVisitor :: new (
@@ -328,7 +328,7 @@ impl<'de> ::serde::Deserialize<'de> for PublicKey {
328
328
} else {
329
329
d. deserialize_bytes ( super :: serde_util:: BytesVisitor :: new (
330
330
"raw 32 bytes schnorr public key" ,
331
- PublicKey :: from_slice
331
+ XOnlyPublicKey :: from_slice
332
332
) )
333
333
}
334
334
}
@@ -411,7 +411,7 @@ impl<C: Signing> Secp256k1<C> {
411
411
& self ,
412
412
sig : & Signature ,
413
413
msg : & Message ,
414
- pubkey : & PublicKey ,
414
+ pubkey : & XOnlyPublicKey ,
415
415
) -> Result < ( ) , Error > {
416
416
unsafe {
417
417
let ret = ffi:: secp256k1_schnorrsig_verify (
@@ -439,9 +439,9 @@ impl<C: Signing> Secp256k1<C> {
439
439
pub fn generate_schnorrsig_keypair < R : Rng + ?Sized > (
440
440
& self ,
441
441
rng : & mut R ,
442
- ) -> ( KeyPair , PublicKey ) {
442
+ ) -> ( KeyPair , XOnlyPublicKey ) {
443
443
let sk = KeyPair :: new ( self , rng) ;
444
- let pubkey = PublicKey :: from_keypair ( self , & sk) ;
444
+ let pubkey = XOnlyPublicKey :: from_keypair ( self , & sk) ;
445
445
( sk, pubkey)
446
446
}
447
447
}
@@ -450,7 +450,7 @@ impl<C: Signing> Secp256k1<C> {
450
450
mod tests {
451
451
use super :: super :: Error :: InvalidPublicKey ;
452
452
use super :: super :: { constants, from_hex, All , Message , Secp256k1 } ;
453
- use super :: { KeyPair , PublicKey , Signature } ;
453
+ use super :: { KeyPair , XOnlyPublicKey , Signature } ;
454
454
use rand:: { rngs:: ThreadRng , thread_rng, Error , ErrorKind , RngCore } ;
455
455
use rand_core:: impls;
456
456
use std:: iter;
@@ -548,17 +548,17 @@ mod tests {
548
548
let msg = Message :: from_slice ( & hex_msg) . unwrap ( ) ;
549
549
let sig = Signature :: from_str ( "6470FD1303DDA4FDA717B9837153C24A6EAB377183FC438F939E0ED2B620E9EE5077C4A8B8DCA28963D772A94F5F0DDF598E1C47C137F91933274C7C3EDADCE8" ) . unwrap ( ) ;
550
550
let pubkey =
551
- PublicKey :: from_str ( "B33CC9EDC096D0A83416964BD3C6247B8FECD256E4EFA7870D2C854BDEB33390" )
551
+ XOnlyPublicKey :: from_str ( "B33CC9EDC096D0A83416964BD3C6247B8FECD256E4EFA7870D2C854BDEB33390" )
552
552
. unwrap ( ) ;
553
553
554
554
assert ! ( secp. schnorrsig_verify( & sig, & msg, & pubkey) . is_ok( ) ) ;
555
555
}
556
556
557
557
#[ test]
558
558
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 ( & [
562
562
0xB3 , 0x3C , 0xC9 , 0xED , 0xC0 , 0x96 , 0xD0 , 0xA8 , 0x34 , 0x16 , 0x96 , 0x4B , 0xD3 , 0xC6 ,
563
563
0x24 , 0x7B , 0x8F , 0xEC , 0xD2 , 0x56 , 0xE4 , 0xEF , 0xA7 , 0x87 , 0x0D , 0x2C , 0x85 , 0x4B ,
564
564
0xDE , 0xB3 , 0x33 , 0x90 ,
@@ -571,7 +571,7 @@ mod tests {
571
571
let secp = Secp256k1 :: new ( ) ;
572
572
let ( _, pubkey) = secp. generate_schnorrsig_keypair ( & mut thread_rng ( ) ) ;
573
573
let ser = pubkey. serialize ( ) ;
574
- let pubkey2 = PublicKey :: from_slice ( & ser) . unwrap ( ) ;
574
+ let pubkey2 = XOnlyPublicKey :: from_slice ( & ser) . unwrap ( ) ;
575
575
assert_eq ! ( pubkey, pubkey2) ;
576
576
}
577
577
@@ -584,35 +584,35 @@ mod tests {
584
584
assert_eq ! ( SecretKey :: from_str( sk_str) . unwrap( ) , sk) ;
585
585
let pk = :: key:: PublicKey :: from_keypair ( & keypair) ;
586
586
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) ;
589
589
}
590
590
591
591
#[ test]
592
592
fn test_pubkey_from_bad_slice ( ) {
593
593
// Bad sizes
594
594
assert_eq ! (
595
- PublicKey :: from_slice( & [ 0 ; constants:: SCHNORRSIG_PUBLIC_KEY_SIZE - 1 ] ) ,
595
+ XOnlyPublicKey :: from_slice( & [ 0 ; constants:: SCHNORRSIG_PUBLIC_KEY_SIZE - 1 ] ) ,
596
596
Err ( InvalidPublicKey )
597
597
) ;
598
598
assert_eq ! (
599
- PublicKey :: from_slice( & [ 0 ; constants:: SCHNORRSIG_PUBLIC_KEY_SIZE + 1 ] ) ,
599
+ XOnlyPublicKey :: from_slice( & [ 0 ; constants:: SCHNORRSIG_PUBLIC_KEY_SIZE + 1 ] ) ,
600
600
Err ( InvalidPublicKey )
601
601
) ;
602
602
603
603
// Bad parse
604
604
assert_eq ! (
605
- PublicKey :: from_slice( & [ 0xff ; constants:: SCHNORRSIG_PUBLIC_KEY_SIZE ] ) ,
605
+ XOnlyPublicKey :: from_slice( & [ 0xff ; constants:: SCHNORRSIG_PUBLIC_KEY_SIZE ] ) ,
606
606
Err ( InvalidPublicKey )
607
607
) ;
608
608
// In fuzzing mode restrictions on public key validity are much more
609
609
// relaxed, thus the invalid check below is expected to fail.
610
610
#[ cfg( not( fuzzing) ) ]
611
611
assert_eq ! (
612
- PublicKey :: from_slice( & [ 0x55 ; constants:: SCHNORRSIG_PUBLIC_KEY_SIZE ] ) ,
612
+ XOnlyPublicKey :: from_slice( & [ 0x55 ; constants:: SCHNORRSIG_PUBLIC_KEY_SIZE ] ) ,
613
613
Err ( InvalidPublicKey )
614
614
) ;
615
- assert_eq ! ( PublicKey :: from_slice( & [ ] ) , Err ( InvalidPublicKey ) ) ;
615
+ assert_eq ! ( XOnlyPublicKey :: from_slice( & [ ] ) , Err ( InvalidPublicKey ) ) ;
616
616
}
617
617
618
618
#[ test]
@@ -630,43 +630,43 @@ mod tests {
630
630
// In fuzzing mode secret->public key derivation is different, so
631
631
// hard-code the epected result.
632
632
#[ cfg( not( fuzzing) ) ]
633
- let pk = PublicKey :: from_keypair ( & s, & sk) ;
633
+ let pk = XOnlyPublicKey :: from_keypair ( & s, & sk) ;
634
634
#[ 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" ) ;
636
636
637
637
assert_eq ! (
638
638
pk. to_string( ) ,
639
639
"18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166"
640
640
) ;
641
641
assert_eq ! (
642
- PublicKey :: from_str( "18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166" )
642
+ XOnlyPublicKey :: from_str( "18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166" )
643
643
. unwrap( ) ,
644
644
pk
645
645
) ;
646
646
647
- assert ! ( PublicKey :: from_str(
647
+ assert ! ( XOnlyPublicKey :: from_str(
648
648
"00000000000000000000000000000000000000000000000000000000000000000"
649
649
)
650
650
. is_err( ) ) ;
651
- assert ! ( PublicKey :: from_str(
651
+ assert ! ( XOnlyPublicKey :: from_str(
652
652
"18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd16601"
653
653
)
654
654
. is_err( ) ) ;
655
- assert ! ( PublicKey :: from_str(
655
+ assert ! ( XOnlyPublicKey :: from_str(
656
656
"18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd16"
657
657
)
658
658
. is_err( ) ) ;
659
- assert ! ( PublicKey :: from_str(
659
+ assert ! ( XOnlyPublicKey :: from_str(
660
660
"18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd1"
661
661
)
662
662
. is_err( ) ) ;
663
- assert ! ( PublicKey :: from_str(
663
+ assert ! ( XOnlyPublicKey :: from_str(
664
664
"xx18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd1"
665
665
)
666
666
. is_err( ) ) ;
667
667
668
668
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( ) ) ;
670
670
}
671
671
672
672
#[ test]
@@ -734,7 +734,7 @@ mod tests {
734
734
static PK_STR : & ' static str = "\
735
735
18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166\
736
736
";
737
- let pk = PublicKey :: from_slice ( & PK_BYTES ) . unwrap ( ) ;
737
+ let pk = XOnlyPublicKey :: from_slice ( & PK_BYTES ) . unwrap ( ) ;
738
738
739
739
assert_tokens ( & sig. compact ( ) , & [ Token :: BorrowedBytes ( & SIG_BYTES [ ..] ) ] ) ;
740
740
assert_tokens ( & sig. compact ( ) , & [ Token :: Bytes ( & SIG_BYTES [ ..] ) ] ) ;
@@ -763,7 +763,7 @@ mod tests {
763
763
let orig_pk = pk;
764
764
kp. tweak_add_assign ( & s, & tweak) . expect ( "Tweak error" ) ;
765
765
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) ;
767
767
assert ! ( orig_pk. tweak_add_check( & s, & pk, parity, tweak) ) ;
768
768
}
769
769
}
@@ -779,8 +779,8 @@ mod tests {
779
779
)
780
780
. unwrap ( ) ;
781
781
782
- let pk1 = PublicKey :: from ( kpk1) ;
783
- let pk2 = PublicKey :: from ( kpk2) ;
782
+ let pk1 = XOnlyPublicKey :: from ( kpk1) ;
783
+ let pk2 = XOnlyPublicKey :: from ( kpk2) ;
784
784
785
785
assert_eq ! ( pk1. serialize( ) [ ..] , kpk1. serialize( ) [ 1 ..] ) ;
786
786
assert_eq ! ( pk2. serialize( ) [ ..] , kpk2. serialize( ) [ 1 ..] ) ;
0 commit comments