extrakeys: replace xonly_sort with pubkey_sort
This commit is contained in:
		
							parent
							
								
									98242fcdd9
								
							
						
					
					
						commit
						ae89051547
					
				| @ -155,20 +155,6 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_tweak_add_ | ||||
|     const unsigned char *tweak32 | ||||
| ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5); | ||||
| 
 | ||||
| /** Sorts xonly public keys according to secp256k1_xonly_pubkey_cmp
 | ||||
|  * | ||||
|  *  Returns: 0 if the arguments are invalid. 1 otherwise. | ||||
|  * | ||||
|  *  Args:     ctx: pointer to a context object | ||||
|  *  In:   pubkeys: array of pointers to pubkeys to sort | ||||
|  *      n_pubkeys: number of elements in the pubkeys array | ||||
|  */ | ||||
| SECP256K1_API int secp256k1_xonly_sort( | ||||
|     const secp256k1_context* ctx, | ||||
|     const secp256k1_xonly_pubkey **pubkeys, | ||||
|     size_t n_pubkeys | ||||
| ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2); | ||||
| 
 | ||||
| /** Compute the keypair for a secret key.
 | ||||
|  * | ||||
|  *  Returns: 1: secret was valid, keypair is ready to use | ||||
| @ -271,6 +257,20 @@ SECP256K1_API int secp256k1_pubkey_cmp( | ||||
|     const secp256k1_pubkey* pk2 | ||||
| ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); | ||||
| 
 | ||||
| /** Sorts public keys using lexicographic order
 | ||||
|  * | ||||
|  *  Returns: 0 if the arguments are invalid. 1 otherwise. | ||||
|  * | ||||
|  *  Args:     ctx: pointer to a context object | ||||
|  *  In:   pubkeys: array of pointers to pubkeys to sort | ||||
|  *      n_pubkeys: number of elements in the pubkeys array | ||||
|  */ | ||||
| SECP256K1_API int secp256k1_pubkey_sort( | ||||
|     const secp256k1_context* ctx, | ||||
|     const secp256k1_pubkey **pubkeys, | ||||
|     size_t n_pubkeys | ||||
| ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2); | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  | ||||
| @ -153,28 +153,6 @@ int secp256k1_xonly_pubkey_tweak_add_check(const secp256k1_context* ctx, const u | ||||
|             && secp256k1_fe_is_odd(&pk.y) == tweaked_pk_parity; | ||||
| } | ||||
| 
 | ||||
| /* This struct wraps a const context pointer to satisfy the secp256k1_hsort api
 | ||||
|  * which expects a non-const cmp_data pointer. */ | ||||
| typedef struct { | ||||
|     const secp256k1_context *ctx; | ||||
| } secp256k1_xonly_sort_cmp_data; | ||||
| 
 | ||||
| static int secp256k1_xonly_sort_cmp(const void* pk1, const void* pk2, void *cmp_data) { | ||||
|     return secp256k1_xonly_pubkey_cmp(((secp256k1_xonly_sort_cmp_data*)cmp_data)->ctx, | ||||
|                                       *(secp256k1_xonly_pubkey **)pk1, | ||||
|                                       *(secp256k1_xonly_pubkey **)pk2); | ||||
| } | ||||
| 
 | ||||
| int secp256k1_xonly_sort(const secp256k1_context* ctx, const secp256k1_xonly_pubkey **pubkeys, size_t n_pubkeys) { | ||||
|     secp256k1_xonly_sort_cmp_data cmp_data; | ||||
|     VERIFY_CHECK(ctx != NULL); | ||||
|     ARG_CHECK(pubkeys != NULL); | ||||
| 
 | ||||
|     cmp_data.ctx = ctx; | ||||
|     secp256k1_hsort(pubkeys, n_pubkeys, sizeof(*pubkeys), secp256k1_xonly_sort_cmp, &cmp_data); | ||||
|     return 1; | ||||
| } | ||||
| 
 | ||||
| static void secp256k1_keypair_save(secp256k1_keypair *keypair, const secp256k1_scalar *sk, secp256k1_ge *pk) { | ||||
|     secp256k1_scalar_get_b32(&keypair->data[0], sk); | ||||
|     secp256k1_pubkey_save((secp256k1_pubkey *)&keypair->data[32], pk); | ||||
| @ -331,4 +309,26 @@ int secp256k1_pubkey_cmp(const secp256k1_context* ctx, const secp256k1_pubkey* p | ||||
|     return secp256k1_memcmp_var(out[0], out[1], sizeof(out[1])); | ||||
| } | ||||
| 
 | ||||
| /* This struct wraps a const context pointer to satisfy the secp256k1_hsort api
 | ||||
|  * which expects a non-const cmp_data pointer. */ | ||||
| typedef struct { | ||||
|     const secp256k1_context *ctx; | ||||
| } secp256k1_pubkey_sort_cmp_data; | ||||
| 
 | ||||
| static int secp256k1_pubkey_sort_cmp(const void* pk1, const void* pk2, void *cmp_data) { | ||||
|     return secp256k1_pubkey_cmp(((secp256k1_pubkey_sort_cmp_data*)cmp_data)->ctx, | ||||
|                                   *(secp256k1_pubkey **)pk1, | ||||
|                                   *(secp256k1_pubkey **)pk2); | ||||
| } | ||||
| 
 | ||||
| int secp256k1_pubkey_sort(const secp256k1_context* ctx, const secp256k1_pubkey **pubkeys, size_t n_pubkeys) { | ||||
|     secp256k1_pubkey_sort_cmp_data cmp_data; | ||||
|     VERIFY_CHECK(ctx != NULL); | ||||
|     ARG_CHECK(pubkeys != NULL); | ||||
| 
 | ||||
|     cmp_data.ctx = ctx; | ||||
|     secp256k1_hsort(pubkeys, n_pubkeys, sizeof(*pubkeys), secp256k1_pubkey_sort_cmp, &cmp_data); | ||||
|     return 1; | ||||
| } | ||||
| 
 | ||||
| #endif | ||||
|  | ||||
| @ -658,14 +658,14 @@ void test_pubkey_comparison(void) { | ||||
|     secp256k1_context_destroy(none); | ||||
| } | ||||
| 
 | ||||
| void test_xonly_sort_helper(secp256k1_xonly_pubkey *pk, size_t *pk_order, size_t n_pk) { | ||||
| void test_sort_helper(secp256k1_pubkey *pk, size_t *pk_order, size_t n_pk) { | ||||
|     size_t i; | ||||
|     const secp256k1_xonly_pubkey *pk_test[5]; | ||||
|     const secp256k1_pubkey *pk_test[5]; | ||||
| 
 | ||||
|     for (i = 0; i < n_pk; i++) { | ||||
|         pk_test[i] = &pk[pk_order[i]]; | ||||
|     } | ||||
|     secp256k1_xonly_sort(ctx, pk_test, n_pk); | ||||
|     secp256k1_pubkey_sort(ctx, pk_test, n_pk); | ||||
|     for (i = 0; i < n_pk; i++) { | ||||
|         CHECK(secp256k1_memcmp_var(pk_test[i], &pk[i], sizeof(*pk_test[i])) == 0); | ||||
|     } | ||||
| @ -682,84 +682,82 @@ void permute(size_t *arr, size_t n) { | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| void rand_xonly_pk(secp256k1_xonly_pubkey *pk) { | ||||
| void rand_pk(secp256k1_pubkey *pk) { | ||||
|     unsigned char seckey[32]; | ||||
|     secp256k1_keypair keypair; | ||||
|     secp256k1_testrand256(seckey); | ||||
|     CHECK(secp256k1_keypair_create(ctx, &keypair, seckey) == 1); | ||||
|     CHECK(secp256k1_keypair_xonly_pub(ctx, pk, NULL, &keypair) == 1); | ||||
|     CHECK(secp256k1_keypair_pub(ctx, pk, &keypair) == 1); | ||||
| } | ||||
| 
 | ||||
| void test_xonly_sort_api(void) { | ||||
| void test_sort_api(void) { | ||||
|     int ecount = 0; | ||||
|     secp256k1_xonly_pubkey pks[2]; | ||||
|     const secp256k1_xonly_pubkey *pks_ptr[2]; | ||||
|     secp256k1_pubkey pks[2]; | ||||
|     const secp256k1_pubkey *pks_ptr[2]; | ||||
|     secp256k1_context *none = api_test_context(SECP256K1_CONTEXT_NONE, &ecount); | ||||
| 
 | ||||
|     pks_ptr[0] = &pks[0]; | ||||
|     pks_ptr[1] = &pks[1]; | ||||
| 
 | ||||
|     rand_xonly_pk(&pks[0]); | ||||
|     rand_xonly_pk(&pks[1]); | ||||
|     rand_pk(&pks[0]); | ||||
|     rand_pk(&pks[1]); | ||||
| 
 | ||||
|     CHECK(secp256k1_xonly_sort(none, pks_ptr, 2) == 1); | ||||
|     CHECK(secp256k1_xonly_sort(none, NULL, 2) == 0); | ||||
|     CHECK(secp256k1_pubkey_sort(none, pks_ptr, 2) == 1); | ||||
|     CHECK(secp256k1_pubkey_sort(none, NULL, 2) == 0); | ||||
|     CHECK(ecount == 1); | ||||
|     CHECK(secp256k1_xonly_sort(none, pks_ptr, 0) == 1); | ||||
|     CHECK(secp256k1_pubkey_sort(none, pks_ptr, 0) == 1); | ||||
|     /* Test illegal public keys */ | ||||
|     memset(&pks[0], 0, sizeof(pks[0])); | ||||
|     CHECK(secp256k1_xonly_sort(none, pks_ptr, 2) == 1); | ||||
|     CHECK(secp256k1_pubkey_sort(none, pks_ptr, 2) == 1); | ||||
|     CHECK(ecount == 2); | ||||
|     memset(&pks[1], 0, sizeof(pks[1])); | ||||
|     CHECK(secp256k1_xonly_sort(none, pks_ptr, 2) == 1); | ||||
|     CHECK(secp256k1_pubkey_sort(none, pks_ptr, 2) == 1); | ||||
|     CHECK(ecount > 2); | ||||
| 
 | ||||
|     secp256k1_context_destroy(none); | ||||
| } | ||||
| 
 | ||||
| void test_xonly_sort(void) { | ||||
|     secp256k1_xonly_pubkey pk[5]; | ||||
|     unsigned char pk_ser[5][32]; | ||||
| void test_sort(void) { | ||||
|     secp256k1_pubkey pk[5]; | ||||
|     unsigned char pk_ser[5][33] = { | ||||
|         { 0x02, 0x08 }, | ||||
|         { 0x02, 0x0b }, | ||||
|         { 0x02, 0x0c }, | ||||
|         { 0x03, 0x05 }, | ||||
|         { 0x03, 0x0a }, | ||||
|     }; | ||||
|     int i; | ||||
|     size_t pk_order[5] = { 0, 1, 2, 3, 4 }; | ||||
| 
 | ||||
|     for (i = 0; i < 5; i++) { | ||||
|         memset(pk_ser[i], 0, sizeof(pk_ser[i])); | ||||
|     } | ||||
|     pk_ser[0][0] = 5; | ||||
|     pk_ser[1][0] = 8; | ||||
|     pk_ser[2][0] = 0x0a; | ||||
|     pk_ser[3][0] = 0x0b; | ||||
|     pk_ser[4][0] = 0x0c; | ||||
|     for (i = 0; i < 5; i++) { | ||||
|         CHECK(secp256k1_xonly_pubkey_parse(ctx, &pk[i], pk_ser[i])); | ||||
|         CHECK(secp256k1_ec_pubkey_parse(ctx, &pk[i], pk_ser[i], sizeof(pk_ser[i]))); | ||||
|     } | ||||
| 
 | ||||
|     permute(pk_order, 1); | ||||
|     test_xonly_sort_helper(pk, pk_order, 1); | ||||
|     test_sort_helper(pk, pk_order, 1); | ||||
|     permute(pk_order, 2); | ||||
|     test_xonly_sort_helper(pk, pk_order, 2); | ||||
|     test_sort_helper(pk, pk_order, 2); | ||||
|     permute(pk_order, 3); | ||||
|     test_xonly_sort_helper(pk, pk_order, 3); | ||||
|     test_sort_helper(pk, pk_order, 3); | ||||
|     for (i = 0; i < count; i++) { | ||||
|         permute(pk_order, 4); | ||||
|         test_xonly_sort_helper(pk, pk_order, 4); | ||||
|         test_sort_helper(pk, pk_order, 4); | ||||
|     } | ||||
|     for (i = 0; i < count; i++) { | ||||
|         permute(pk_order, 5); | ||||
|         test_xonly_sort_helper(pk, pk_order, 5); | ||||
|         test_sort_helper(pk, pk_order, 5); | ||||
|     } | ||||
|     /* Check that sorting also works for random pubkeys */ | ||||
|     for (i = 0; i < count; i++) { | ||||
|         int j; | ||||
|         const secp256k1_xonly_pubkey *pk_ptr[5]; | ||||
|         const secp256k1_pubkey *pk_ptr[5]; | ||||
|         for (j = 0; j < 5; j++) { | ||||
|             rand_xonly_pk(&pk[j]); | ||||
|             rand_pk(&pk[j]); | ||||
|             pk_ptr[j] = &pk[j]; | ||||
|         } | ||||
|         secp256k1_xonly_sort(ctx, pk_ptr, 5); | ||||
|         secp256k1_pubkey_sort(ctx, pk_ptr, 5); | ||||
|         for (j = 1; j < 5; j++) { | ||||
|             CHECK(secp256k1_xonly_sort_cmp(&pk_ptr[j - 1], &pk_ptr[j], ctx) <= 0); | ||||
|             CHECK(secp256k1_pubkey_sort_cmp(&pk_ptr[j - 1], &pk_ptr[j], ctx) <= 0); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -777,9 +775,9 @@ void run_extrakeys_tests(void) { | ||||
|     test_keypair_add(); | ||||
| 
 | ||||
|     test_hsort(); | ||||
|     test_xonly_sort_api(); | ||||
|     test_xonly_sort(); | ||||
|     test_pubkey_comparison(); | ||||
|     test_sort_api(); | ||||
|     test_sort(); | ||||
| } | ||||
| 
 | ||||
| #endif | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user