@@ -2475,6 +2475,85 @@ void run_ec_combine(void) {
2475
2475
}
2476
2476
}
2477
2477
2478
+ int test_ec_commit_seckey (unsigned char * seckey , secp256k1_pubkey * commitment ) {
2479
+ /* Return if seckey is the discrete log of commitment */
2480
+ secp256k1_pubkey pubkey_tmp ;
2481
+ return secp256k1_ec_pubkey_create (ctx , & pubkey_tmp , seckey ) == 1
2482
+ && memcmp (& pubkey_tmp , commitment , sizeof (pubkey_tmp )) == 0 ;
2483
+ }
2484
+
2485
+ void test_ec_commit (void ) {
2486
+ unsigned char seckey [32 ];
2487
+ secp256k1_pubkey pubkey ;
2488
+ secp256k1_pubkey commitment ;
2489
+ unsigned char data [32 ];
2490
+
2491
+ /* Create random keypair and data */
2492
+ secp256k1_rand256 (seckey );
2493
+ CHECK (secp256k1_ec_pubkey_create (ctx , & pubkey , seckey ));
2494
+ secp256k1_rand256_test (data );
2495
+
2496
+ /* Commit to data and verify */
2497
+ CHECK (secp256k1_ec_commit (ctx , & commitment , & pubkey , data , 32 ));
2498
+ CHECK (secp256k1_ec_commit_verify (ctx , & commitment , & pubkey , data , 32 ));
2499
+ CHECK (secp256k1_ec_commit_seckey (ctx , seckey , & pubkey , data , 32 ));
2500
+ CHECK (test_ec_commit_seckey (seckey , & commitment ) == 1 );
2501
+
2502
+ /* Check that verification fails with different data */
2503
+ CHECK (secp256k1_ec_commit_verify (ctx , & commitment , & pubkey , data , 31 ) == 0 );
2504
+ }
2505
+
2506
+ void test_ec_commit_api (void ) {
2507
+ unsigned char seckey [32 ];
2508
+ secp256k1_pubkey pubkey ;
2509
+ secp256k1_pubkey commitment ;
2510
+ unsigned char data [32 ];
2511
+
2512
+ memset (data , 23 , sizeof (data ));
2513
+
2514
+ /* Create random keypair */
2515
+ secp256k1_rand256 (seckey );
2516
+ CHECK (secp256k1_ec_pubkey_create (ctx , & pubkey , seckey ));
2517
+
2518
+ CHECK (secp256k1_ec_commit (ctx , & commitment , & pubkey , data , 1 ) == 1 );
2519
+ /* The same pubkey can be both input and output of the function */
2520
+ {
2521
+ secp256k1_pubkey pubkey_tmp = pubkey ;
2522
+ CHECK (secp256k1_ec_commit (ctx , & pubkey_tmp , & pubkey_tmp , data , 1 ) == 1 );
2523
+ CHECK (memcmp (commitment .data , pubkey_tmp .data , sizeof (commitment .data )) == 0 );
2524
+ }
2525
+
2526
+ /* If the pubkey is not provided it will be computed from seckey */
2527
+ CHECK (secp256k1_ec_commit_seckey (ctx , seckey , NULL , data , 1 ) == 1 );
2528
+ CHECK (test_ec_commit_seckey (seckey , & commitment ) == 1 );
2529
+ /* pubkey is not provided but seckey overflows */
2530
+ {
2531
+ unsigned char overflowed_seckey [32 ];
2532
+ memset (overflowed_seckey , 0xFF , sizeof (overflowed_seckey ));
2533
+ CHECK (secp256k1_ec_commit_seckey (ctx , overflowed_seckey , NULL , data , 1 ) == 0 );
2534
+ }
2535
+
2536
+ CHECK (secp256k1_ec_commit_verify (ctx , & commitment , & pubkey , data , 1 ) == 1 );
2537
+
2538
+ /* Commitment to 0-len data should fail */
2539
+ CHECK (secp256k1_ec_commit (ctx , & commitment , & pubkey , data , 0 ) == 0 );
2540
+ CHECK (secp256k1_ec_commit_verify (ctx , & commitment , & pubkey , data , 0 ) == 0 );
2541
+ CHECK (memcmp (& pubkey .data , & commitment .data , sizeof (pubkey .data )) == 0 );
2542
+ {
2543
+ unsigned char seckey_tmp [32 ];
2544
+ memcpy (seckey_tmp , seckey , 32 );
2545
+ CHECK (secp256k1_ec_commit_seckey (ctx , seckey_tmp , & pubkey , data , 0 ) == 0 );
2546
+ }
2547
+ }
2548
+
2549
+ void run_ec_commit (void ) {
2550
+ int i ;
2551
+ for (i = 0 ; i < count * 8 ; i ++ ) {
2552
+ test_ec_commit ();
2553
+ }
2554
+ test_ec_commit_api ();
2555
+ }
2556
+
2478
2557
void test_group_decompress (const secp256k1_fe * x ) {
2479
2558
/* The input itself, normalized. */
2480
2559
secp256k1_fe fex = * x ;
@@ -5372,6 +5451,7 @@ int main(int argc, char **argv) {
5372
5451
run_ecmult_const_tests ();
5373
5452
run_ecmult_multi_tests ();
5374
5453
run_ec_combine ();
5454
+ run_ec_commit ();
5375
5455
5376
5456
/* endomorphism tests */
5377
5457
#ifdef USE_ENDOMORPHISM
0 commit comments