99#include "../../../include/secp256k1.h"
1010#include "../../../include/secp256k1_extrakeys.h"
1111#include "../../../include/secp256k1_silentpayments.h"
12+ #include "dleq_impl.h"
1213
1314#include "../../eckey.h"
1415#include "../../ecmult.h"
@@ -85,14 +86,43 @@ static int secp256k1_silentpayments_calculate_input_hash_scalar(secp256k1_scalar
8586 return !!ret & !overflow ;
8687}
8788
88- static void secp256k1_silentpayments_create_shared_secret (const secp256k1_context * ctx , unsigned char * shared_secret33 , const secp256k1_ge * public_component , const secp256k1_scalar * secret_component ) {
89+ static int secp256k1_silentpayments_create_shared_secret_with_proof (const secp256k1_context * ctx , unsigned char * proof64 , secp256k1_ge * shared_secret , secp256k1_ge * public_component , const secp256k1_scalar * secret_component ) {
8990 secp256k1_gej ss_j ;
91+ int ret = 1 ;
92+
93+ secp256k1_ecmult_const (& ss_j , public_component , secret_component );
94+ secp256k1_ge_set_gej (shared_secret , & ss_j );
95+ secp256k1_declassify (ctx , shared_secret , sizeof (* shared_secret ));
96+
97+ if (proof64 != NULL ) {
98+ secp256k1_scalar s ;
99+ secp256k1_scalar e ;
100+ secp256k1_ge ge_secret_component ;
101+ secp256k1_gej gej_secret_component ;
102+
103+ secp256k1_ecmult_gen (& ctx -> ecmult_gen_ctx , & gej_secret_component , secret_component );
104+ secp256k1_ge_set_gej (& ge_secret_component , & gej_secret_component );
105+ secp256k1_declassify (ctx , & ge_secret_component , sizeof (ge_secret_component ));
106+
107+ ret &= secp256k1_dleq_prove (ctx , & s , & e , secret_component , public_component , & ge_secret_component , shared_secret , NULL , NULL ); /*todo: how to pass auxrand*/
108+ secp256k1_declassify (ctx , & s , sizeof (s ));
109+ secp256k1_declassify (ctx , & e , sizeof (e ));
110+ /* sanity check */
111+ ret &= secp256k1_dleq_verify (& s , & e , & ge_secret_component , public_component , shared_secret , NULL );
112+ secp256k1_scalar_get_b32 (proof64 , & s );
113+ secp256k1_scalar_get_b32 (proof64 + 32 , & e );
114+ }
115+ /* Leaking these values would break indistinguishability of the transaction, so clear them. */
116+ secp256k1_gej_clear (& ss_j );
117+ return ret ;
118+ }
119+ static void secp256k1_silentpayments_create_shared_secret (const secp256k1_context * ctx , unsigned char * shared_secret33 , secp256k1_ge * public_component , const secp256k1_scalar * secret_component ) {
90120 secp256k1_ge ss ;
91121 size_t len ;
92122 int ret ;
93123
94- secp256k1_ecmult_const ( & ss_j , public_component , secret_component );
95- secp256k1_ge_set_gej ( & ss , & ss_j );
124+ ret = secp256k1_silentpayments_create_shared_secret_with_proof ( ctx , NULL , & ss , public_component , secret_component );
125+ VERIFY_CHECK ( ret );
96126 secp256k1_declassify (ctx , & ss , sizeof (ss ));
97127 /* This can only fail if the shared secret is the point at infinity, which should be
98128 * impossible at this point considering we have already validated the public key and
@@ -107,7 +137,6 @@ static void secp256k1_silentpayments_create_shared_secret(const secp256k1_contex
107137
108138 /* Leaking these values would break indistinguishability of the transaction, so clear them. */
109139 secp256k1_ge_clear (& ss );
110- secp256k1_gej_clear (& ss_j );
111140}
112141
113142/** Set hash state to the BIP340 tagged hash midstate for "BIP0352/SharedSecret". */
0 commit comments