@@ -20,20 +20,12 @@ use elements::secp256k1_zkp;
2020
2121use crate :: ToPublicKey ;
2222
23- /// The SHA-256 initial midstate value for the [`TweakHash`].
24- const MIDSTATE_HASH_TO_PRIVATE_HASH : [ u8 ; 32 ] = [
25- 0x2f , 0x85 , 0x61 , 0xec , 0x30 , 0x88 , 0xad , 0xa9 , 0x5a , 0xe7 , 0x43 , 0xcd , 0x3c , 0x5f , 0x59 , 0x7d ,
26- 0xc0 , 0x4b , 0xd0 , 0x7f , 0x06 , 0x5f , 0x1c , 0x06 , 0x47 , 0x89 , 0x36 , 0x63 , 0xf3 , 0x92 , 0x6e , 0x65 ,
27- ] ;
28-
29- sha256t_hash_newtype ! (
30- TweakHash ,
31- TweakTag ,
32- MIDSTATE_HASH_TO_PRIVATE_HASH ,
33- 64 ,
34- doc = "BIP-340 Tagged hash for tweaking blinding keys" ,
35- forward
36- ) ;
23+ sha256t_hash_newtype ! {
24+ pub struct TapTweakTag = hash_str( "CT-Blinding-Key/1.0" ) ;
25+ /// Taproot-tagged hash for elements tapscript Merkle tree leafs
26+ #[ hash_newtype( forward) ]
27+ pub struct TapTweakHash ( _) ;
28+ }
3729
3830/// Tweaks a bare key using the scriptPubKey of a descriptor
3931pub fn tweak_key < ' a , Pk , V > (
@@ -45,12 +37,12 @@ where
4537 Pk : ToPublicKey + ' a ,
4638 V : secp256k1_zkp:: Verification ,
4739{
48- let mut eng = TweakHash :: engine ( ) ;
40+ let mut eng = TapTweakHash :: engine ( ) ;
4941 pk. to_public_key ( )
5042 . write_into ( & mut eng)
5143 . expect ( "engines don't error" ) ;
5244 spk. consensus_encode ( & mut eng) . expect ( "engines don't error" ) ;
53- let hash_bytes = TweakHash :: from_engine ( eng) . to_byte_array ( ) ;
45+ let hash_bytes = TapTweakHash :: from_engine ( eng) . to_byte_array ( ) ;
5446 let hash_scalar = secp256k1_zkp:: Scalar :: from_be_bytes ( hash_bytes) . expect ( "bytes from hash" ) ;
5547 pk. to_public_key ( )
5648 . inner
@@ -67,12 +59,12 @@ pub fn tweak_private_key<V>(
6759where
6860 V : secp256k1_zkp:: Signing ,
6961{
70- let mut eng = TweakHash :: engine ( ) ;
62+ let mut eng = TapTweakHash :: engine ( ) ;
7163 bitcoin:: PublicKey :: new ( sk. public_key ( secp) )
7264 . write_into ( & mut eng)
7365 . expect ( "engines don't error" ) ;
7466 spk. consensus_encode ( & mut eng) . expect ( "engines don't error" ) ;
75- let hash_bytes = TweakHash :: from_engine ( eng) . to_byte_array ( ) ;
67+ let hash_bytes = TapTweakHash :: from_engine ( eng) . to_byte_array ( ) ;
7668 let hash_scalar = secp256k1_zkp:: Scalar :: from_be_bytes ( hash_bytes) . expect ( "bytes from hash" ) ;
7769 sk. add_tweak ( & hash_scalar) . unwrap ( )
7870}
@@ -84,6 +76,12 @@ mod tests {
8476
8577 use super :: * ;
8678
79+ const MIDSTATE_HASH_TO_PRIVATE_HASH : [ u8 ; 32 ] = [
80+ 0x2f , 0x85 , 0x61 , 0xec , 0x30 , 0x88 , 0xad , 0xa9 , 0x5a , 0xe7 , 0x43 , 0xcd , 0x3c , 0x5f , 0x59 ,
81+ 0x7d , 0xc0 , 0x4b , 0xd0 , 0x7f , 0x06 , 0x5f , 0x1c , 0x06 , 0x47 , 0x89 , 0x36 , 0x63 , 0xf3 , 0x92 ,
82+ 0x6e , 0x65 ,
83+ ] ;
84+
8785 #[ test]
8886 fn tagged_hash ( ) {
8987 // Check that cached midstate is computed correctly
@@ -100,18 +98,18 @@ mod tests {
10098
10199 // Test empty hash
102100 assert_eq ! (
103- TweakHash :: from_engine( TweakTag :: engine( ) ) . to_string( ) ,
101+ TapTweakHash :: from_engine( TapTweakTag :: engine( ) ) . to_string( ) ,
104102 "d12a140aca856fbb917b931f263c42f064608985e2ce17ae5157daa17c55e8d9" ,
105103 ) ;
106104 assert_eq ! (
107- TweakHash :: hash( & [ ] ) . to_string( ) ,
105+ TapTweakHash :: hash( & [ ] ) . to_string( ) ,
108106 "d12a140aca856fbb917b931f263c42f064608985e2ce17ae5157daa17c55e8d9" ,
109107 ) ;
110108
111109 // And hash of 100 bytes
112110 let data: Vec < u8 > = ( 0 ..80 ) . collect ( ) ;
113111 assert_eq ! (
114- TweakHash :: hash( & data) . to_string( ) ,
112+ TapTweakHash :: hash( & data) . to_string( ) ,
115113 "e1e52419a2934d278c50e29608969d2f23c1bd1243a09bfc8026d4ed4b085e39" ,
116114 ) ;
117115 }
0 commit comments