@@ -322,27 +322,6 @@ static SECP256K1_INLINE void buffer_append(unsigned char *buf, unsigned int *off
322
322
* offset += len ;
323
323
}
324
324
325
- /* This nonce function is described in BIP-schnorr
326
- * (https://github.com/sipa/bips/blob/bip-schnorr/bip-schnorr.mediawiki) */
327
- static int secp256k1_nonce_function_bipschnorr (const secp256k1_context * ctx , unsigned char * nonce32 , const unsigned char * msg32 , const unsigned char * key32 , const unsigned char * algo16 , void * data , unsigned int counter ) {
328
- secp256k1_sha256 sha ;
329
- (void ) data ;
330
- (void ) counter ;
331
- VERIFY_CHECK (counter == 0 );
332
-
333
- /* Hash x||msg as per the spec */
334
- secp256k1_sha256_initialize (& sha );
335
- secp256k1_sha256_write (& sha , key32 , 32 );
336
- secp256k1_sha256_write (& sha , msg32 , 32 );
337
- /* Hash in algorithm, which is not in the spec, but may be critical to
338
- * users depending on it to avoid nonce reuse across algorithms. */
339
- if (algo16 != NULL ) {
340
- secp256k1_sha256_write (& sha , algo16 , 16 );
341
- }
342
- secp256k1_sha256_finalize (& sha , nonce32 );
343
- return 1 ;
344
- }
345
-
346
325
static int nonce_function_rfc6979 (const secp256k1_context * ctx , unsigned char * nonce32 , const unsigned char * msg32 , const unsigned char * key32 , const unsigned char * algo16 , void * data , unsigned int counter ) {
347
326
unsigned char keydata [112 ];
348
327
unsigned int offset = 0 ;
@@ -707,6 +686,72 @@ int secp256k1_ec_commit_verify(const secp256k1_context* ctx, const secp256k1_pub
707
686
return secp256k1_gej_is_infinity (& pj );
708
687
}
709
688
689
+ int secp256k1_s2c_commit_context_create (secp256k1_context * ctx , secp256k1_s2c_commit_context * s2c_ctx , const unsigned char * data32 ) {
690
+ secp256k1_sha256 sha ;
691
+
692
+ VERIFY_CHECK (ctx != NULL );
693
+ ARG_CHECK (s2c_ctx != NULL );
694
+ ARG_CHECK (data32 != NULL );
695
+
696
+ memcpy (s2c_ctx -> data , data32 , 32 );
697
+ secp256k1_sha256_initialize (& sha );
698
+ secp256k1_sha256_write (& sha , data32 , 32 );
699
+ secp256k1_sha256_finalize (& sha , s2c_ctx -> data_commitment );
700
+ return 1 ;
701
+ }
702
+
703
+ int secp256k1_s2c_commit_get_opening (secp256k1_context * ctx , secp256k1_pubkey * opening , const secp256k1_s2c_commit_context * s2c_ctx ) {
704
+ VERIFY_CHECK (ctx != NULL );
705
+ ARG_CHECK (opening != NULL );
706
+ ARG_CHECK (s2c_ctx != NULL );
707
+
708
+ memcpy (opening , & s2c_ctx -> original_pubnonce , sizeof (secp256k1_pubkey ));
709
+ return 1 ;
710
+ }
711
+
712
+ static int secp256k1_nonce_function_bipschnorr_no_s2c (const secp256k1_context * ctx , unsigned char * nonce32 , const unsigned char * msg32 , const unsigned char * key32 , const unsigned char * algo16 , void * data , unsigned int counter ) {
713
+ secp256k1_sha256 sha ;
714
+
715
+ VERIFY_CHECK (counter == 0 );
716
+ (void ) counter ;
717
+
718
+ /* Hash x||msg as per the spec */
719
+ secp256k1_sha256_initialize (& sha );
720
+ secp256k1_sha256_write (& sha , key32 , 32 );
721
+ secp256k1_sha256_write (& sha , msg32 , 32 );
722
+ /* Hash in algorithm, which is not in the spec, but may be critical to
723
+ * users depending on it to avoid nonce reuse across algorithms. */
724
+ if (algo16 != NULL ) {
725
+ secp256k1_sha256_write (& sha , algo16 , 16 );
726
+ }
727
+ if (data == NULL ) {
728
+ secp256k1_sha256_finalize (& sha , nonce32 );
729
+ } else {
730
+ /* Do a sign-to-contract commitment if data is provided */
731
+ secp256k1_s2c_commit_context * s2c_ctx = (secp256k1_s2c_commit_context * )data ;
732
+ secp256k1_sha256_write (& sha , s2c_ctx -> data_commitment , 32 );
733
+ secp256k1_sha256_finalize (& sha , nonce32 );
734
+
735
+ if (!secp256k1_ec_pubkey_create (ctx , & s2c_ctx -> original_pubnonce , nonce32 )) {
736
+ return 0 ;
737
+ }
738
+ }
739
+ return 1 ;
740
+ }
741
+
742
+ /* This nonce function is described in BIP-schnorr
743
+ * (https://github.com/sipa/bips/blob/bip-schnorr/bip-schnorr.mediawiki) */
744
+ static int secp256k1_nonce_function_bipschnorr (const secp256k1_context * ctx , unsigned char * nonce32 , const unsigned char * msg32 , const unsigned char * key32 , const unsigned char * algo16 , void * data , unsigned int counter ) {
745
+ if (!secp256k1_nonce_function_bipschnorr_no_s2c (ctx , nonce32 , msg32 , key32 , algo16 , data , counter )) {
746
+ return 0 ;
747
+ }
748
+ if (data != NULL ) {
749
+ secp256k1_s2c_commit_context * s2c_ctx = (secp256k1_s2c_commit_context * )data ;
750
+ return secp256k1_ec_commit_seckey (ctx , nonce32 , & s2c_ctx -> original_pubnonce , s2c_ctx -> data , 32 );
751
+ }
752
+ return 1 ;
753
+ }
754
+
710
755
#ifdef ENABLE_MODULE_ECDH
711
756
# include "modules/ecdh/main_impl.h"
712
757
#endif
0 commit comments