@@ -2339,6 +2339,106 @@ void run_ec_combine(void) {
2339
2339
}
2340
2340
}
2341
2341
2342
+ int test_ec_commit_seckey (unsigned char * seckey , secp256k1_pubkey * commitment ) {
2343
+ /* Return if seckey is the discrete log of commitment */
2344
+ secp256k1_pubkey pubkey_tmp ;
2345
+ return secp256k1_ec_pubkey_create (ctx , & pubkey_tmp , seckey ) == 1
2346
+ && memcmp (& pubkey_tmp , commitment , sizeof (pubkey_tmp )) == 0 ;
2347
+ }
2348
+
2349
+ void test_ec_commit (void ) {
2350
+ unsigned char seckey [32 ];
2351
+ secp256k1_pubkey pubkey ;
2352
+ secp256k1_pubkey commitment ;
2353
+ unsigned char data [32 ];
2354
+
2355
+ /* Create random keypair and data */
2356
+ secp256k1_rand256 (seckey );
2357
+ CHECK (secp256k1_ec_pubkey_create (ctx , & pubkey , seckey ));
2358
+ secp256k1_rand256_test (data );
2359
+
2360
+ /* Commit to data and verify */
2361
+ CHECK (secp256k1_ec_commit (ctx , & commitment , & pubkey , data , 32 ));
2362
+ CHECK (secp256k1_ec_commit_verify (ctx , & commitment , & pubkey , data , 32 ));
2363
+ CHECK (secp256k1_ec_commit_seckey (ctx , seckey , & pubkey , data , 32 ));
2364
+ CHECK (test_ec_commit_seckey (seckey , & commitment ) == 1 );
2365
+
2366
+ /* Check that verification fails with different data */
2367
+ CHECK (secp256k1_ec_commit_verify (ctx , & commitment , & pubkey , data , 31 ) == 0 );
2368
+ }
2369
+
2370
+ void test_ec_commit_api (void ) {
2371
+ unsigned char seckey [32 ];
2372
+ secp256k1_pubkey pubkey ;
2373
+ secp256k1_pubkey commitment ;
2374
+ unsigned char data [32 ];
2375
+ int32_t ecount ;
2376
+
2377
+ memset (data , 23 , sizeof (data ));
2378
+ secp256k1_context_set_illegal_callback (ctx , counting_illegal_callback_fn , & ecount );
2379
+
2380
+ /* Create random keypair */
2381
+ secp256k1_rand256 (seckey );
2382
+ CHECK (secp256k1_ec_pubkey_create (ctx , & pubkey , seckey ));
2383
+
2384
+ ecount = 0 ;
2385
+ CHECK (secp256k1_ec_commit (ctx , NULL , & pubkey , data , 1 ) == 0 );
2386
+ CHECK (ecount == 1 );
2387
+ CHECK (secp256k1_ec_commit (ctx , & commitment , NULL , data , 1 ) == 0 );
2388
+ CHECK (ecount == 2 );
2389
+ CHECK (secp256k1_ec_commit (ctx , & commitment , & pubkey , NULL , 1 ) == 0 );
2390
+ CHECK (ecount == 3 );
2391
+ CHECK (secp256k1_ec_commit (ctx , & commitment , & pubkey , data , 1 ) == 1 );
2392
+ /* The same pubkey can be both input and output of the function */
2393
+ {
2394
+ secp256k1_pubkey pubkey_tmp = pubkey ;
2395
+ CHECK (secp256k1_ec_commit (ctx , & pubkey_tmp , & pubkey_tmp , data , 1 ) == 1 );
2396
+ CHECK (memcmp (commitment .data , pubkey_tmp .data , sizeof (commitment .data )) == 0 );
2397
+ }
2398
+
2399
+ ecount = 0 ;
2400
+ CHECK (secp256k1_ec_commit_seckey (ctx , NULL , & pubkey , data , 1 ) == 0 );
2401
+ CHECK (ecount == 1 );
2402
+ /* If the pubkey is not provided it will be computed from seckey */
2403
+ CHECK (secp256k1_ec_commit_seckey (ctx , seckey , NULL , data , 1 ) == 1 );
2404
+ CHECK (test_ec_commit_seckey (seckey , & commitment ) == 1 );
2405
+ /* pubkey is not provided but seckey overflows */
2406
+ {
2407
+ unsigned char overflowed_seckey [32 ];
2408
+ memset (overflowed_seckey , 0xFF , sizeof (overflowed_seckey ));
2409
+ CHECK (secp256k1_ec_commit_seckey (ctx , overflowed_seckey , NULL , data , 1 ) == 0 );
2410
+ }
2411
+ CHECK (secp256k1_ec_commit_seckey (ctx , seckey , & pubkey , NULL , 1 ) == 0 );
2412
+ CHECK (ecount == 2 );
2413
+
2414
+ ecount = 0 ;
2415
+ CHECK (secp256k1_ec_commit_verify (ctx , & commitment , & pubkey , data , 1 ) == 1 );
2416
+ CHECK (secp256k1_ec_commit_verify (ctx , NULL , & pubkey , data , 1 ) == 0 );
2417
+ CHECK (ecount == 1 );
2418
+ CHECK (secp256k1_ec_commit_verify (ctx , & commitment , NULL , data , 1 ) == 0 );
2419
+ CHECK (ecount == 2 );
2420
+ CHECK (secp256k1_ec_commit_verify (ctx , & commitment , & pubkey , NULL , 1 ) == 0 );
2421
+ CHECK (ecount == 3 );
2422
+
2423
+ /* Commitment to 0-len data should fail */
2424
+ CHECK (secp256k1_ec_commit (ctx , & commitment , & pubkey , data , 0 ) == 0 );
2425
+ CHECK (secp256k1_ec_commit_verify (ctx , & commitment , & pubkey , data , 0 ) == 0 );
2426
+ CHECK (memcmp (& pubkey .data , & commitment .data , sizeof (pubkey .data )) == 0 );
2427
+ {
2428
+ unsigned char seckey_tmp [32 ];
2429
+ memcpy (seckey_tmp , seckey , 32 );
2430
+ CHECK (secp256k1_ec_commit_seckey (ctx , seckey_tmp , & pubkey , data , 0 ) == 0 );
2431
+ }
2432
+ }
2433
+
2434
+ void run_ec_commit (void ) {
2435
+ int i ;
2436
+ for (i = 0 ; i < count * 8 ; i ++ ) {
2437
+ test_ec_commit ();
2438
+ }
2439
+ test_ec_commit_api ();
2440
+ }
2441
+
2342
2442
void test_group_decompress (const secp256k1_fe * x ) {
2343
2443
/* The input itself, normalized. */
2344
2444
secp256k1_fe fex = * x ;
@@ -5157,6 +5257,7 @@ int main(int argc, char **argv) {
5157
5257
run_ecmult_const_tests ();
5158
5258
run_ecmult_multi_tests ();
5159
5259
run_ec_combine ();
5260
+ run_ec_commit ();
5160
5261
5161
5262
/* endomorphism tests */
5162
5263
#ifdef USE_ENDOMORPHISM
0 commit comments