@@ -25,69 +25,69 @@ use bitcoin::secp256k1::PublicKey;
25
25
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
26
26
27
27
macro_rules! doc_comment {
28
- ( $x: expr, $( $tt: tt) * ) => {
29
- #[ doc = $x]
30
- $( $tt) *
31
- } ;
28
+ ( $x: expr, $( $tt: tt) * ) => {
29
+ #[ doc = $x]
30
+ $( $tt) *
31
+ } ;
32
32
}
33
33
macro_rules! basepoint_impl {
34
- ( $BasepointT: ty) => {
35
- impl $BasepointT {
36
- /// Get inner Public Key
37
- pub fn to_public_key( & self ) -> PublicKey {
38
- self . 0
39
- }
40
- }
41
-
42
- impl From <PublicKey > for $BasepointT {
43
- fn from( value: PublicKey ) -> Self {
44
- Self ( value)
45
- }
46
- }
47
-
48
- }
34
+ ( $BasepointT: ty) => {
35
+ impl $BasepointT {
36
+ /// Get inner Public Key
37
+ pub fn to_public_key( & self ) -> PublicKey {
38
+ self . 0
39
+ }
40
+ }
41
+
42
+ impl From <PublicKey > for $BasepointT {
43
+ fn from( value: PublicKey ) -> Self {
44
+ Self ( value)
45
+ }
46
+ }
47
+
48
+ }
49
49
}
50
50
macro_rules! key_impl {
51
- ( $BasepointT: ty, $KeyName: expr) => {
52
- doc_comment! {
53
- concat!( "Generate " , $KeyName, " using per_commitment_point" ) ,
54
- pub fn from_basepoint<T : secp256k1:: Signing >(
55
- secp_ctx: & Secp256k1 <T >,
56
- basepoint: & $BasepointT,
57
- per_commitment_point: & PublicKey ,
58
- ) -> Self {
59
- Self ( derive_public_key( secp_ctx, per_commitment_point, & basepoint. 0 ) )
60
- }
61
- }
62
-
63
- doc_comment! {
64
- concat!( "Generate " , $KeyName, " from privkey" ) ,
65
- pub fn from_secret_key<T : secp256k1:: Signing >( secp_ctx: & Secp256k1 <T >, sk: & SecretKey ) -> Self {
66
- Self ( PublicKey :: from_secret_key( & secp_ctx, & sk) )
67
- }
68
- }
69
-
70
- /// Get inner Public Key
71
- pub fn to_public_key( & self ) -> PublicKey {
72
- self . 0
73
- }
74
- }
51
+ ( $BasepointT: ty, $KeyName: expr) => {
52
+ doc_comment! {
53
+ concat!( "Generate " , $KeyName, " using per_commitment_point" ) ,
54
+ pub fn from_basepoint<T : secp256k1:: Signing >(
55
+ secp_ctx: & Secp256k1 <T >,
56
+ basepoint: & $BasepointT,
57
+ per_commitment_point: & PublicKey ,
58
+ ) -> Self {
59
+ Self ( derive_public_key( secp_ctx, per_commitment_point, & basepoint. 0 ) )
60
+ }
61
+ }
62
+
63
+ doc_comment! {
64
+ concat!( "Generate " , $KeyName, " from privkey" ) ,
65
+ pub fn from_secret_key<T : secp256k1:: Signing >( secp_ctx: & Secp256k1 <T >, sk: & SecretKey ) -> Self {
66
+ Self ( PublicKey :: from_secret_key( & secp_ctx, & sk) )
67
+ }
68
+ }
69
+
70
+ /// Get inner Public Key
71
+ pub fn to_public_key( & self ) -> PublicKey {
72
+ self . 0
73
+ }
74
+ }
75
75
}
76
76
macro_rules! key_read_write {
77
- ( $SelfT: ty) => {
78
- impl Writeable for $SelfT {
79
- fn write<W : Writer >( & self , w: & mut W ) -> Result <( ) , io:: Error > {
80
- self . 0 . serialize( ) . write( w)
81
- }
82
- }
83
-
84
- impl Readable for $SelfT {
85
- fn read<R : io:: Read >( r: & mut R ) -> Result <Self , DecodeError > {
86
- let key: PublicKey = Readable :: read( r) ?;
87
- Ok ( Self ( key) )
88
- }
89
- }
90
- }
77
+ ( $SelfT: ty) => {
78
+ impl Writeable for $SelfT {
79
+ fn write<W : Writer >( & self , w: & mut W ) -> Result <( ) , io:: Error > {
80
+ self . 0 . serialize( ) . write( w)
81
+ }
82
+ }
83
+
84
+ impl Readable for $SelfT {
85
+ fn read<R : io:: Read >( r: & mut R ) -> Result <Self , DecodeError > {
86
+ let key: PublicKey = Readable :: read( r) ?;
87
+ Ok ( Self ( key) )
88
+ }
89
+ }
90
+ }
91
91
}
92
92
93
93
@@ -107,7 +107,7 @@ key_read_write!(DelayedPaymentBasepoint);
107
107
pub struct DelayedPaymentKey ( pub PublicKey ) ;
108
108
109
109
impl DelayedPaymentKey {
110
- key_impl ! ( DelayedPaymentBasepoint , "delayedpubkey" ) ;
110
+ key_impl ! ( DelayedPaymentBasepoint , "delayedpubkey" ) ;
111
111
}
112
112
key_read_write ! ( DelayedPaymentKey ) ;
113
113
@@ -127,7 +127,7 @@ key_read_write!(PaymentBasepoint);
127
127
pub struct PaymentKey ( pub PublicKey ) ;
128
128
129
129
impl PaymentKey {
130
- key_impl ! ( PaymentBasepoint , "localpubkey" ) ;
130
+ key_impl ! ( PaymentBasepoint , "localpubkey" ) ;
131
131
}
132
132
key_read_write ! ( PaymentKey ) ;
133
133
@@ -144,7 +144,7 @@ key_read_write!(HtlcBasepoint);
144
144
pub struct HtlcKey ( pub PublicKey ) ;
145
145
146
146
impl HtlcKey {
147
- key_impl ! ( HtlcBasepoint , "htlcpubkey" ) ;
147
+ key_impl ! ( HtlcBasepoint , "htlcpubkey" ) ;
148
148
}
149
149
key_read_write ! ( HtlcKey ) ;
150
150
@@ -156,7 +156,6 @@ fn derive_public_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitm
156
156
sha. input ( & per_commitment_point. serialize ( ) ) ;
157
157
sha. input ( & base_point. serialize ( ) ) ;
158
158
let res = Sha256 :: from_engine ( sha) . to_byte_array ( ) ;
159
-
160
159
161
160
let hashkey = PublicKey :: from_secret_key ( & secp_ctx,
162
161
& SecretKey :: from_slice ( & res) . expect ( "Hashes should always be valid keys unless SHA-256 is broken" ) ) ;
@@ -178,62 +177,62 @@ key_read_write!(RevocationBasepoint);
178
177
pub struct RevocationKey ( pub PublicKey ) ;
179
178
180
179
impl RevocationKey {
181
- /// Derives a per-commitment-transaction revocation public key from its constituent parts. This is
182
- /// the public equivalend of derive_private_revocation_key - using only public keys to derive a
183
- /// public key instead of private keys.
184
- ///
185
- /// Only the cheating participant owns a valid witness to propagate a revoked
186
- /// commitment transaction, thus per_commitment_point always come from cheater
187
- /// and revocation_base_point always come from punisher, which is the broadcaster
188
- /// of the transaction spending with this key knowledge.
189
- ///
190
- /// Note that this is infallible iff we trust that at least one of the two input keys are randomly
191
- /// generated (ie our own).
192
- pub fn from_basepoint < T : secp256k1:: Verification > (
193
- secp_ctx : & Secp256k1 < T > ,
194
- basepoint : & RevocationBasepoint ,
195
- per_commitment_point : & PublicKey ,
196
- ) -> Self {
197
- let rev_append_commit_hash_key = {
198
- let mut sha = Sha256 :: engine ( ) ;
199
- sha. input ( & basepoint. to_public_key ( ) . serialize ( ) ) ;
200
- sha. input ( & per_commitment_point. serialize ( ) ) ;
201
-
202
- Sha256 :: from_engine ( sha) . to_byte_array ( )
203
- } ;
204
- let commit_append_rev_hash_key = {
205
- let mut sha = Sha256 :: engine ( ) ;
206
- sha. input ( & per_commitment_point. serialize ( ) ) ;
207
- sha. input ( & basepoint. to_public_key ( ) . serialize ( ) ) ;
208
-
209
- Sha256 :: from_engine ( sha) . to_byte_array ( )
210
- } ;
211
-
212
- let countersignatory_contrib = basepoint. to_public_key ( ) . mul_tweak ( & secp_ctx, & Scalar :: from_be_bytes ( rev_append_commit_hash_key) . unwrap ( ) )
213
- . expect ( "Multiplying a valid public key by a hash is expected to never fail per secp256k1 docs" ) ;
214
- let broadcaster_contrib = ( & per_commitment_point) . mul_tweak ( & secp_ctx, & Scalar :: from_be_bytes ( commit_append_rev_hash_key) . unwrap ( ) )
215
- . expect ( "Multiplying a valid public key by a hash is expected to never fail per secp256k1 docs" ) ;
216
- let pk = countersignatory_contrib. combine ( & broadcaster_contrib)
217
- . expect ( "Addition only fails if the tweak is the inverse of the key. This is not possible when the tweak commits to the key." ) ;
218
- Self ( pk)
219
- }
220
-
221
- /// Get inner Public Key
222
- pub fn to_public_key ( & self ) -> PublicKey {
223
- self . 0
224
- }
180
+ /// Derives a per-commitment-transaction revocation public key from its constituent parts. This is
181
+ /// the public equivalend of derive_private_revocation_key - using only public keys to derive a
182
+ /// public key instead of private keys.
183
+ ///
184
+ /// Only the cheating participant owns a valid witness to propagate a revoked
185
+ /// commitment transaction, thus per_commitment_point always come from cheater
186
+ /// and revocation_base_point always come from punisher, which is the broadcaster
187
+ /// of the transaction spending with this key knowledge.
188
+ ///
189
+ /// Note that this is infallible iff we trust that at least one of the two input keys are randomly
190
+ /// generated (ie our own).
191
+ pub fn from_basepoint < T : secp256k1:: Verification > (
192
+ secp_ctx : & Secp256k1 < T > ,
193
+ basepoint : & RevocationBasepoint ,
194
+ per_commitment_point : & PublicKey ,
195
+ ) -> Self {
196
+ let rev_append_commit_hash_key = {
197
+ let mut sha = Sha256 :: engine ( ) ;
198
+ sha. input ( & basepoint. to_public_key ( ) . serialize ( ) ) ;
199
+ sha. input ( & per_commitment_point. serialize ( ) ) ;
200
+
201
+ Sha256 :: from_engine ( sha) . to_byte_array ( )
202
+ } ;
203
+ let commit_append_rev_hash_key = {
204
+ let mut sha = Sha256 :: engine ( ) ;
205
+ sha. input ( & per_commitment_point. serialize ( ) ) ;
206
+ sha. input ( & basepoint. to_public_key ( ) . serialize ( ) ) ;
207
+
208
+ Sha256 :: from_engine ( sha) . to_byte_array ( )
209
+ } ;
210
+
211
+ let countersignatory_contrib = basepoint. to_public_key ( ) . mul_tweak ( & secp_ctx, & Scalar :: from_be_bytes ( rev_append_commit_hash_key) . unwrap ( ) )
212
+ . expect ( "Multiplying a valid public key by a hash is expected to never fail per secp256k1 docs" ) ;
213
+ let broadcaster_contrib = ( & per_commitment_point) . mul_tweak ( & secp_ctx, & Scalar :: from_be_bytes ( commit_append_rev_hash_key) . unwrap ( ) )
214
+ . expect ( "Multiplying a valid public key by a hash is expected to never fail per secp256k1 docs" ) ;
215
+ let pk = countersignatory_contrib. combine ( & broadcaster_contrib)
216
+ . expect ( "Addition only fails if the tweak is the inverse of the key. This is not possible when the tweak commits to the key." ) ;
217
+ Self ( pk)
218
+ }
219
+
220
+ /// Get inner Public Key
221
+ pub fn to_public_key ( & self ) -> PublicKey {
222
+ self . 0
223
+ }
225
224
}
226
225
key_read_write ! ( RevocationKey ) ;
227
226
228
227
229
228
230
229
#[ cfg( test) ]
231
230
mod test {
232
- use bitcoin:: secp256k1:: { Secp256k1 , SecretKey , PublicKey } ;
233
- use bitcoin:: hashes:: hex:: FromHex ;
234
- use super :: derive_public_key;
231
+ use bitcoin:: secp256k1:: { Secp256k1 , SecretKey , PublicKey } ;
232
+ use bitcoin:: hashes:: hex:: FromHex ;
233
+ use super :: derive_public_key;
235
234
236
- #[ test]
235
+ #[ test]
237
236
fn test_key_derivation ( ) {
238
237
// Test vectors from BOLT 3 Appendix E:
239
238
let secp_ctx = Secp256k1 :: new ( ) ;
@@ -248,6 +247,6 @@ mod test {
248
247
assert_eq ! ( per_commitment_point. serialize( ) [ ..] , <Vec <u8 >>:: from_hex( "025f7117a78150fe2ef97db7cfc83bd57b2e2c0d0dd25eaf467a4a1c2a45ce1486" ) . unwrap( ) [ ..] ) ;
249
248
250
249
assert_eq ! ( derive_public_key( & secp_ctx, & per_commitment_point, & base_point) . serialize( ) [ ..] ,
251
- <Vec <u8 >>:: from_hex( "0235f2dbfaa89b57ec7b055afe29849ef7ddfeb1cefdb9ebdc43f5494984db29e5" ) . unwrap( ) [ ..] ) ;
250
+ <Vec <u8 >>:: from_hex( "0235f2dbfaa89b57ec7b055afe29849ef7ddfeb1cefdb9ebdc43f5494984db29e5" ) . unwrap( ) [ ..] ) ;
252
251
}
253
252
}
0 commit comments