csecp256k1

Haskell FFI bindings to bitcoin-core/secp256k1 (docs.ppad.tech/csecp256k1).
git clone git://git.ppad.tech/csecp256k1.git
Log | Files | Refs | README | LICENSE

tests_exhaustive_impl.h (3557B)


      1 /***********************************************************************
      2  * Copyright (c) 2020 Pieter Wuille                                    *
      3  * Distributed under the MIT software license, see the accompanying    *
      4  * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
      5  ***********************************************************************/
      6 
      7 #ifndef SECP256K1_MODULE_EXTRAKEYS_TESTS_EXHAUSTIVE_H
      8 #define SECP256K1_MODULE_EXTRAKEYS_TESTS_EXHAUSTIVE_H
      9 
     10 #include "../../../include/secp256k1_extrakeys.h"
     11 #include "main_impl.h"
     12 
     13 static void test_exhaustive_extrakeys(const haskellsecp256k1_v0_1_0_context *ctx, const haskellsecp256k1_v0_1_0_ge* group) {
     14     haskellsecp256k1_v0_1_0_keypair keypair[EXHAUSTIVE_TEST_ORDER - 1];
     15     haskellsecp256k1_v0_1_0_pubkey pubkey[EXHAUSTIVE_TEST_ORDER - 1];
     16     haskellsecp256k1_v0_1_0_xonly_pubkey xonly_pubkey[EXHAUSTIVE_TEST_ORDER - 1];
     17     int parities[EXHAUSTIVE_TEST_ORDER - 1];
     18     unsigned char xonly_pubkey_bytes[EXHAUSTIVE_TEST_ORDER - 1][32];
     19     int i;
     20 
     21     for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) {
     22         haskellsecp256k1_v0_1_0_fe fe;
     23         haskellsecp256k1_v0_1_0_scalar scalar_i;
     24         unsigned char buf[33];
     25         int parity;
     26 
     27         haskellsecp256k1_v0_1_0_scalar_set_int(&scalar_i, i);
     28         haskellsecp256k1_v0_1_0_scalar_get_b32(buf, &scalar_i);
     29 
     30         /* Construct pubkey and keypair. */
     31         CHECK(haskellsecp256k1_v0_1_0_keypair_create(ctx, &keypair[i - 1], buf));
     32         CHECK(haskellsecp256k1_v0_1_0_ec_pubkey_create(ctx, &pubkey[i - 1], buf));
     33 
     34         /* Construct serialized xonly_pubkey from keypair. */
     35         CHECK(haskellsecp256k1_v0_1_0_keypair_xonly_pub(ctx, &xonly_pubkey[i - 1], &parities[i - 1], &keypair[i - 1]));
     36         CHECK(haskellsecp256k1_v0_1_0_xonly_pubkey_serialize(ctx, xonly_pubkey_bytes[i - 1], &xonly_pubkey[i - 1]));
     37 
     38         /* Parse the xonly_pubkey back and verify it matches the previously serialized value. */
     39         CHECK(haskellsecp256k1_v0_1_0_xonly_pubkey_parse(ctx, &xonly_pubkey[i - 1], xonly_pubkey_bytes[i - 1]));
     40         CHECK(haskellsecp256k1_v0_1_0_xonly_pubkey_serialize(ctx, buf, &xonly_pubkey[i - 1]));
     41         CHECK(haskellsecp256k1_v0_1_0_memcmp_var(xonly_pubkey_bytes[i - 1], buf, 32) == 0);
     42 
     43         /* Construct the xonly_pubkey from the pubkey, and verify it matches the same. */
     44         CHECK(haskellsecp256k1_v0_1_0_xonly_pubkey_from_pubkey(ctx, &xonly_pubkey[i - 1], &parity, &pubkey[i - 1]));
     45         CHECK(parity == parities[i - 1]);
     46         CHECK(haskellsecp256k1_v0_1_0_xonly_pubkey_serialize(ctx, buf, &xonly_pubkey[i - 1]));
     47         CHECK(haskellsecp256k1_v0_1_0_memcmp_var(xonly_pubkey_bytes[i - 1], buf, 32) == 0);
     48 
     49         /* Compare the xonly_pubkey bytes against the precomputed group. */
     50         haskellsecp256k1_v0_1_0_fe_set_b32_mod(&fe, xonly_pubkey_bytes[i - 1]);
     51         CHECK(haskellsecp256k1_v0_1_0_fe_equal(&fe, &group[i].x));
     52 
     53         /* Check the parity against the precomputed group. */
     54         fe = group[i].y;
     55         haskellsecp256k1_v0_1_0_fe_normalize_var(&fe);
     56         CHECK(haskellsecp256k1_v0_1_0_fe_is_odd(&fe) == parities[i - 1]);
     57 
     58         /* Verify that the higher half is identical to the lower half mirrored. */
     59         if (i > EXHAUSTIVE_TEST_ORDER / 2) {
     60             CHECK(haskellsecp256k1_v0_1_0_memcmp_var(xonly_pubkey_bytes[i - 1], xonly_pubkey_bytes[EXHAUSTIVE_TEST_ORDER - i - 1], 32) == 0);
     61             CHECK(parities[i - 1] == 1 - parities[EXHAUSTIVE_TEST_ORDER - i - 1]);
     62         }
     63     }
     64 
     65     /* TODO: keypair/xonly_pubkey tweak tests */
     66 }
     67 
     68 #endif