@@ -29,6 +29,7 @@ use crate::ffi::types::c_uint;
29
29
use crate :: { Message , ecdsa, SECP256K1 } ;
30
30
#[ cfg( all( feature = "global-context" , feature = "rand-std" ) ) ]
31
31
use crate :: schnorr;
32
+ use crate :: Scalar ;
32
33
33
34
/// Secret 256-bit key used as `x` in an ECDSA signature.
34
35
///
@@ -232,15 +233,11 @@ impl SecretKey {
232
233
///
233
234
/// # Errors
234
235
///
235
- /// Returns an error if the resulting key would be invalid or if the tweak was not a 32-byte
236
- /// length slice.
236
+ /// Returns an error if the resulting key would be invalid.
237
237
pub fn add_assign (
238
238
& mut self ,
239
- other : & [ u8 ] ,
239
+ other : & Scalar ,
240
240
) -> Result < ( ) , Error > {
241
- if other. len ( ) != 32 {
242
- return Err ( Error :: InvalidTweak ) ;
243
- }
244
241
unsafe {
245
242
if ffi:: secp256k1_ec_seckey_tweak_add (
246
243
ffi:: secp256k1_context_no_precomp,
@@ -257,15 +254,11 @@ impl SecretKey {
257
254
258
255
#[ inline]
259
256
/// Multiplies one secret key by another, modulo the curve order. Will
260
- /// return an error if the resulting key would be invalid or if
261
- /// the tweak was not a 32-byte length slice.
257
+ /// return an error if the resulting key would be invalid.
262
258
pub fn mul_assign (
263
259
& mut self ,
264
- other : & [ u8 ] ,
260
+ other : & Scalar ,
265
261
) -> Result < ( ) , Error > {
266
- if other. len ( ) != 32 {
267
- return Err ( Error :: InvalidTweak ) ;
268
- }
269
262
unsafe {
270
263
if ffi:: secp256k1_ec_seckey_tweak_mul (
271
264
ffi:: secp256k1_context_no_precomp,
@@ -498,20 +491,16 @@ impl PublicKey {
498
491
}
499
492
500
493
#[ inline]
501
- /// Adds the `other` public key to `self` in place.
494
+ /// Adds `other * G` to `self` in place.
502
495
///
503
496
/// # Errors
504
497
///
505
- /// Returns an error if the resulting key would be invalid or if the tweak was not a 32-byte
506
- /// length slice.
498
+ /// Returns an error if the resulting key would be invalid.
507
499
pub fn add_exp_assign < C : Verification > (
508
500
& mut self ,
509
501
secp : & Secp256k1 < C > ,
510
- other : & [ u8 ]
502
+ other : & Scalar
511
503
) -> Result < ( ) , Error > {
512
- if other. len ( ) != 32 {
513
- return Err ( Error :: InvalidTweak ) ;
514
- }
515
504
unsafe {
516
505
if ffi:: secp256k1_ec_pubkey_tweak_add ( secp. ctx , & mut self . 0 , other. as_c_ptr ( ) ) == 1 {
517
506
Ok ( ( ) )
@@ -526,16 +515,12 @@ impl PublicKey {
526
515
///
527
516
/// # Errors
528
517
///
529
- /// Returns an error if the resulting key would be invalid or if the tweak was not a 32-byte
530
- /// length slice.
518
+ /// Returns an error if the resulting key would be invalid.
531
519
pub fn mul_assign < C : Verification > (
532
520
& mut self ,
533
521
secp : & Secp256k1 < C > ,
534
- other : & [ u8 ] ,
522
+ other : & Scalar ,
535
523
) -> Result < ( ) , Error > {
536
- if other. len ( ) != 32 {
537
- return Err ( Error :: InvalidTweak ) ;
538
- }
539
524
unsafe {
540
525
if ffi:: secp256k1_ec_pubkey_tweak_mul ( secp. ctx , & mut self . 0 , other. as_c_ptr ( ) ) == 1 {
541
526
Ok ( ( ) )
@@ -860,8 +845,7 @@ impl KeyPair {
860
845
///
861
846
/// # Errors
862
847
///
863
- /// Returns an error if the resulting key would be invalid or if the tweak was not a 32-byte
864
- /// length slice.
848
+ /// Returns an error if the resulting key would be invalid.
865
849
///
866
850
/// NB: Will not error if the tweaked public key has an odd value and can't be used for
867
851
/// BIP 340-342 purposes.
@@ -870,12 +854,11 @@ impl KeyPair {
870
854
///
871
855
/// ```
872
856
/// # #[cfg(all(feature = "std", feature = "rand-std"))] {
873
- /// use secp256k1::{Secp256k1, KeyPair};
857
+ /// use secp256k1::{Secp256k1, KeyPair, Scalar };
874
858
/// use secp256k1::rand::{RngCore, thread_rng};
875
859
///
876
860
/// let secp = Secp256k1::new();
877
- /// let mut tweak = [0u8; 32];
878
- /// thread_rng().fill_bytes(&mut tweak);
861
+ /// let tweak = Scalar::random();
879
862
///
880
863
/// let mut key_pair = KeyPair::new(&secp, &mut thread_rng());
881
864
/// key_pair.tweak_add_assign(&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
@@ -886,12 +869,8 @@ impl KeyPair {
886
869
pub fn tweak_add_assign < C : Verification > (
887
870
& mut self ,
888
871
secp : & Secp256k1 < C > ,
889
- tweak : & [ u8 ] ,
872
+ tweak : & Scalar ,
890
873
) -> Result < ( ) , Error > {
891
- if tweak. len ( ) != 32 {
892
- return Err ( Error :: InvalidTweak ) ;
893
- }
894
-
895
874
unsafe {
896
875
let err = ffi:: secp256k1_keypair_xonly_tweak_add (
897
876
secp. ctx ,
@@ -1150,12 +1129,11 @@ impl XOnlyPublicKey {
1150
1129
///
1151
1130
/// ```
1152
1131
/// # #[cfg(all(feature = "std", feature = "rand-std"))] {
1153
- /// use secp256k1::{Secp256k1, KeyPair};
1132
+ /// use secp256k1::{Secp256k1, KeyPair, Scalar };
1154
1133
/// use secp256k1::rand::{RngCore, thread_rng};
1155
1134
///
1156
1135
/// let secp = Secp256k1::new();
1157
- /// let mut tweak = [0u8; 32];
1158
- /// thread_rng().fill_bytes(&mut tweak);
1136
+ /// let tweak = Scalar::random();
1159
1137
///
1160
1138
/// let mut key_pair = KeyPair::new(&secp, &mut thread_rng());
1161
1139
/// let (mut public_key, _parity) = key_pair.x_only_public_key();
@@ -1165,12 +1143,8 @@ impl XOnlyPublicKey {
1165
1143
pub fn tweak_add_assign < V : Verification > (
1166
1144
& mut self ,
1167
1145
secp : & Secp256k1 < V > ,
1168
- tweak : & [ u8 ] ,
1146
+ tweak : & Scalar ,
1169
1147
) -> Result < Parity , Error > {
1170
- if tweak. len ( ) != 32 {
1171
- return Err ( Error :: InvalidTweak ) ;
1172
- }
1173
-
1174
1148
let mut pk_parity = 0 ;
1175
1149
unsafe {
1176
1150
let mut pubkey = ffi:: PublicKey :: new ( ) ;
@@ -1215,12 +1189,11 @@ impl XOnlyPublicKey {
1215
1189
///
1216
1190
/// ```
1217
1191
/// # #[cfg(all(feature = "std", feature = "rand-std"))] {
1218
- /// use secp256k1::{Secp256k1, KeyPair};
1192
+ /// use secp256k1::{Secp256k1, KeyPair, Scalar };
1219
1193
/// use secp256k1::rand::{thread_rng, RngCore};
1220
1194
///
1221
1195
/// let secp = Secp256k1::new();
1222
- /// let mut tweak = [0u8; 32];
1223
- /// thread_rng().fill_bytes(&mut tweak);
1196
+ /// let tweak = Scalar::random();
1224
1197
///
1225
1198
/// let mut key_pair = KeyPair::new(&secp, &mut thread_rng());
1226
1199
/// let (mut public_key, _) = key_pair.x_only_public_key();
@@ -1234,7 +1207,7 @@ impl XOnlyPublicKey {
1234
1207
secp : & Secp256k1 < V > ,
1235
1208
tweaked_key : & Self ,
1236
1209
tweaked_parity : Parity ,
1237
- tweak : [ u8 ; 32 ] ,
1210
+ tweak : Scalar ,
1238
1211
) -> bool {
1239
1212
let tweaked_ser = tweaked_key. serialize ( ) ;
1240
1213
unsafe {
@@ -1512,6 +1485,7 @@ mod test {
1512
1485
use super :: { XOnlyPublicKey , PublicKey , Secp256k1 , SecretKey , KeyPair , Parity } ;
1513
1486
use crate :: { constants, from_hex, to_hex} ;
1514
1487
use crate :: Error :: { InvalidPublicKey , InvalidSecretKey } ;
1488
+ use crate :: Scalar ;
1515
1489
1516
1490
macro_rules! hex {
1517
1491
( $hex: expr) => ( {
@@ -1770,15 +1744,17 @@ mod test {
1770
1744
1771
1745
let ( mut sk1, mut pk1) = s. generate_keypair ( & mut thread_rng ( ) ) ;
1772
1746
let ( mut sk2, mut pk2) = s. generate_keypair ( & mut thread_rng ( ) ) ;
1747
+ let scalar1 = Scalar :: from ( sk1) ;
1748
+ let scalar2 = Scalar :: from ( sk1) ;
1773
1749
1774
1750
assert_eq ! ( PublicKey :: from_secret_key( & s, & sk1) , pk1) ;
1775
- assert ! ( sk1. add_assign( & sk2 [ .. ] ) . is_ok( ) ) ;
1776
- assert ! ( pk1. add_exp_assign( & s, & sk2 [ .. ] ) . is_ok( ) ) ;
1751
+ assert ! ( sk1. add_assign( & scalar2 ) . is_ok( ) ) ;
1752
+ assert ! ( pk1. add_exp_assign( & s, & scalar2 ) . is_ok( ) ) ;
1777
1753
assert_eq ! ( PublicKey :: from_secret_key( & s, & sk1) , pk1) ;
1778
1754
1779
1755
assert_eq ! ( PublicKey :: from_secret_key( & s, & sk2) , pk2) ;
1780
- assert ! ( sk2. add_assign( & sk1 [ .. ] ) . is_ok( ) ) ;
1781
- assert ! ( pk2. add_exp_assign( & s, & sk1 [ .. ] ) . is_ok( ) ) ;
1756
+ assert ! ( sk2. add_assign( & scalar1 ) . is_ok( ) ) ;
1757
+ assert ! ( pk2. add_exp_assign( & s, & scalar1 ) . is_ok( ) ) ;
1782
1758
assert_eq ! ( PublicKey :: from_secret_key( & s, & sk2) , pk2) ;
1783
1759
}
1784
1760
@@ -1789,15 +1765,17 @@ mod test {
1789
1765
1790
1766
let ( mut sk1, mut pk1) = s. generate_keypair ( & mut thread_rng ( ) ) ;
1791
1767
let ( mut sk2, mut pk2) = s. generate_keypair ( & mut thread_rng ( ) ) ;
1768
+ let scalar1 = Scalar :: from ( sk1) ;
1769
+ let scalar2 = Scalar :: from ( sk1) ;
1792
1770
1793
1771
assert_eq ! ( PublicKey :: from_secret_key( & s, & sk1) , pk1) ;
1794
- assert ! ( sk1. mul_assign( & sk2 [ .. ] ) . is_ok( ) ) ;
1795
- assert ! ( pk1. mul_assign( & s, & sk2 [ .. ] ) . is_ok( ) ) ;
1772
+ assert ! ( sk1. mul_assign( & scalar2 ) . is_ok( ) ) ;
1773
+ assert ! ( pk1. mul_assign( & s, & scalar2 ) . is_ok( ) ) ;
1796
1774
assert_eq ! ( PublicKey :: from_secret_key( & s, & sk1) , pk1) ;
1797
1775
1798
1776
assert_eq ! ( PublicKey :: from_secret_key( & s, & sk2) , pk2) ;
1799
- assert ! ( sk2. mul_assign( & sk1 [ .. ] ) . is_ok( ) ) ;
1800
- assert ! ( pk2. mul_assign( & s, & sk1 [ .. ] ) . is_ok( ) ) ;
1777
+ assert ! ( sk2. mul_assign( & scalar1 ) . is_ok( ) ) ;
1778
+ assert ! ( pk2. mul_assign( & s, & scalar1 ) . is_ok( ) ) ;
1801
1779
assert_eq ! ( PublicKey :: from_secret_key( & s, & sk2) , pk2) ;
1802
1780
}
1803
1781
@@ -1913,7 +1891,7 @@ mod test {
1913
1891
assert ! ( sum2. is_ok( ) ) ;
1914
1892
assert_eq ! ( sum1, sum2) ;
1915
1893
1916
- assert ! ( sk1. add_assign( & sk2 . as_ref ( ) [ .. ] ) . is_ok( ) ) ;
1894
+ assert ! ( sk1. add_assign( & Scalar :: from ( sk2 ) ) . is_ok( ) ) ;
1917
1895
let sksum = PublicKey :: from_secret_key ( & s, & sk1) ;
1918
1896
assert_eq ! ( Ok ( sksum) , sum1) ;
1919
1897
}
@@ -1999,8 +1977,7 @@ mod test {
1999
1977
let s = Secp256k1 :: new ( ) ;
2000
1978
2001
1979
for _ in 0 ..10 {
2002
- let mut tweak = [ 0u8 ; 32 ] ;
2003
- thread_rng ( ) . fill_bytes ( & mut tweak) ;
1980
+ let tweak = Scalar :: random ( ) ;
2004
1981
2005
1982
let mut kp = KeyPair :: new ( & s, & mut thread_rng ( ) ) ;
2006
1983
let ( mut pk, _parity) = kp. x_only_public_key ( ) ;
0 commit comments