@@ -203,4 +203,39 @@ int secp256k1_silentpayments_receive_create_shared_secret(const secp256k1_contex
203203 return 1 ;
204204}
205205
206+ /** Set hash state to the BIP340 tagged hash midstate for "BIP0352/Label". */
207+ static void secp256k1_silentpayments_sha256_init_label (secp256k1_sha256 * hash ) {
208+ secp256k1_sha256_initialize (hash );
209+ hash -> s [0 ] = 0x26b95d63ul ;
210+ hash -> s [1 ] = 0x8bf1b740ul ;
211+ hash -> s [2 ] = 0x10a5986ful ;
212+ hash -> s [3 ] = 0x06a387a5ul ;
213+ hash -> s [4 ] = 0x2d1c1c30ul ;
214+ hash -> s [5 ] = 0xd035951aul ;
215+ hash -> s [6 ] = 0x2d7f0f96ul ;
216+ hash -> s [7 ] = 0x29e3e0dbul ;
217+
218+ hash -> bytes = 64 ;
219+ }
220+
221+ int secp256k1_silentpayments_create_label_tweak (const secp256k1_context * ctx , unsigned char * label_tweak32 , const unsigned char * receiver_scan_seckey , unsigned int m ) {
222+ secp256k1_sha256 hash ;
223+ unsigned char m_serialized [4 ];
224+
225+ /* Sanity check inputs. */
226+ VERIFY_CHECK (ctx != NULL );
227+ (void )ctx ;
228+ VERIFY_CHECK (label_tweak32 != NULL );
229+ VERIFY_CHECK (receiver_scan_seckey != NULL );
230+
231+ /* Compute label_tweak = hash(ser_256(b_scan) || ser_32(m)) [sha256 with tag "BIP0352/Label"] */
232+ secp256k1_silentpayments_sha256_init_label (& hash );
233+ secp256k1_sha256_write (& hash , receiver_scan_seckey , 32 );
234+ secp256k1_write_be32 (m_serialized , m );
235+ secp256k1_sha256_write (& hash , m_serialized , sizeof (m_serialized ));
236+ secp256k1_sha256_finalize (& hash , label_tweak32 );
237+
238+ return 1 ;
239+ }
240+
206241#endif
0 commit comments