@@ -3596,6 +3596,84 @@ void run_ec_combine(void) {
3596
3596
}
3597
3597
}
3598
3598
3599
+ void test_ec_commit (void ) {
3600
+ secp256k1_scalar seckey_s ;
3601
+ secp256k1_ge pubkey ;
3602
+ secp256k1_gej pubkeyj ;
3603
+ secp256k1_ge commitment ;
3604
+ unsigned char data [32 ];
3605
+ secp256k1_sha256 sha ;
3606
+
3607
+ /* Create random keypair and data */
3608
+ random_scalar_order_test (& seckey_s );
3609
+ secp256k1_ecmult_gen (& ctx -> ecmult_gen_ctx , & pubkeyj , & seckey_s );
3610
+ secp256k1_ge_set_gej (& pubkey , & pubkeyj );
3611
+ secp256k1_testrand256_test (data );
3612
+
3613
+ /* Commit to data and verify */
3614
+ secp256k1_sha256_initialize (& sha );
3615
+ CHECK (secp256k1_ec_commit (& commitment , & pubkey , & sha , data , 32 ) == 1 );
3616
+ secp256k1_sha256_initialize (& sha );
3617
+ CHECK (secp256k1_ec_commit_verify (& commitment , & pubkey , & sha , data , 32 ) == 1 );
3618
+ secp256k1_sha256_initialize (& sha );
3619
+ CHECK (secp256k1_ec_commit_seckey (& seckey_s , & pubkey , & sha , data , 32 ) == 1 );
3620
+ secp256k1_ecmult_gen (& ctx -> ecmult_gen_ctx , & pubkeyj , & seckey_s );
3621
+ ge_equals_gej (& commitment , & pubkeyj );
3622
+
3623
+ /* Check that verification fails with different data */
3624
+ secp256k1_sha256_initialize (& sha );
3625
+ CHECK (secp256k1_ec_commit_verify (& commitment , & pubkey , & sha , data , 31 ) == 0 );
3626
+
3627
+ /* Check that commmitting fails when the inner pubkey is the point at
3628
+ * infinity */
3629
+ secp256k1_sha256_initialize (& sha );
3630
+ secp256k1_ge_set_infinity (& pubkey );
3631
+ CHECK (secp256k1_ec_commit (& commitment , & pubkey , & sha , data , 32 ) == 0 );
3632
+ secp256k1_scalar_set_int (& seckey_s , 0 );
3633
+ CHECK (secp256k1_ec_commit_seckey (& seckey_s , & pubkey , & sha , data , 32 ) == 0 );
3634
+ CHECK (secp256k1_ec_commit_verify (& commitment , & pubkey , & sha , data , 32 ) == 0 );
3635
+ }
3636
+
3637
+
3638
+ void test_ec_commit_api (void ) {
3639
+ unsigned char seckey [32 ];
3640
+ secp256k1_scalar seckey_s ;
3641
+ secp256k1_ge pubkey ;
3642
+ secp256k1_gej pubkeyj ;
3643
+ secp256k1_ge commitment ;
3644
+ unsigned char data [32 ];
3645
+ secp256k1_sha256 sha ;
3646
+
3647
+ memset (data , 23 , sizeof (data ));
3648
+
3649
+ /* Create random keypair */
3650
+ random_scalar_order_test (& seckey_s );
3651
+ secp256k1_scalar_get_b32 (seckey , & seckey_s );
3652
+ secp256k1_ecmult_gen (& ctx -> ecmult_gen_ctx , & pubkeyj , & seckey_s );
3653
+ secp256k1_ge_set_gej (& pubkey , & pubkeyj );
3654
+
3655
+ secp256k1_sha256_initialize (& sha );
3656
+ CHECK (secp256k1_ec_commit (& commitment , & pubkey , & sha , data , 1 ) == 1 );
3657
+ /* The same pubkey can be both input and output of the function */
3658
+ {
3659
+ secp256k1_ge pubkey_tmp = pubkey ;
3660
+ secp256k1_sha256_initialize (& sha );
3661
+ CHECK (secp256k1_ec_commit (& pubkey_tmp , & pubkey_tmp , & sha , data , 1 ) == 1 );
3662
+ ge_equals_ge (& commitment , & pubkey_tmp );
3663
+ }
3664
+
3665
+ secp256k1_sha256_initialize (& sha );
3666
+ CHECK (secp256k1_ec_commit_verify (& commitment , & pubkey , & sha , data , 1 ) == 1 );
3667
+ }
3668
+
3669
+ void run_ec_commit (void ) {
3670
+ int i ;
3671
+ for (i = 0 ; i < count * 8 ; i ++ ) {
3672
+ test_ec_commit ();
3673
+ }
3674
+ test_ec_commit_api ();
3675
+ }
3676
+
3599
3677
void test_group_decompress (const secp256k1_fe * x ) {
3600
3678
/* The input itself, normalized. */
3601
3679
secp256k1_fe fex = * x ;
@@ -7137,6 +7215,7 @@ int main(int argc, char **argv) {
7137
7215
run_ecmult_const_tests ();
7138
7216
run_ecmult_multi_tests ();
7139
7217
run_ec_combine ();
7218
+ run_ec_commit ();
7140
7219
7141
7220
/* endomorphism tests */
7142
7221
run_endomorphism_tests ();
0 commit comments