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

ecmult_const_impl.h (20658B)


      1 /***********************************************************************
      2  * Copyright (c) 2015, 2022 Pieter Wuille, Andrew Poelstra             *
      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_ECMULT_CONST_IMPL_H
      8 #define SECP256K1_ECMULT_CONST_IMPL_H
      9 
     10 #include "scalar.h"
     11 #include "group.h"
     12 #include "ecmult_const.h"
     13 #include "ecmult_impl.h"
     14 
     15 #if defined(EXHAUSTIVE_TEST_ORDER)
     16 /* We need 2^ECMULT_CONST_GROUP_SIZE - 1 to be less than EXHAUSTIVE_TEST_ORDER, because
     17  * the tables cannot have infinities in them (this breaks the effective-affine technique's
     18  * z-ratio tracking) */
     19 #  if EXHAUSTIVE_TEST_ORDER == 199
     20 #    define ECMULT_CONST_GROUP_SIZE 4
     21 #  elif EXHAUSTIVE_TEST_ORDER == 13
     22 #    define ECMULT_CONST_GROUP_SIZE 3
     23 #  elif EXHAUSTIVE_TEST_ORDER == 7
     24 #    define ECMULT_CONST_GROUP_SIZE 2
     25 #  else
     26 #    error "Unknown EXHAUSTIVE_TEST_ORDER"
     27 #  endif
     28 #else
     29 /* Group size 4 or 5 appears optimal. */
     30 #  define ECMULT_CONST_GROUP_SIZE 5
     31 #endif
     32 
     33 #define ECMULT_CONST_TABLE_SIZE (1L << (ECMULT_CONST_GROUP_SIZE - 1))
     34 #define ECMULT_CONST_GROUPS ((129 + ECMULT_CONST_GROUP_SIZE - 1) / ECMULT_CONST_GROUP_SIZE)
     35 #define ECMULT_CONST_BITS (ECMULT_CONST_GROUPS * ECMULT_CONST_GROUP_SIZE)
     36 
     37 /** Fill a table 'pre' with precomputed odd multiples of a.
     38  *
     39  *  The resulting point set is brought to a single constant Z denominator, stores the X and Y
     40  *  coordinates as ge points in pre, and stores the global Z in globalz.
     41  *
     42  *  'pre' must be an array of size ECMULT_CONST_TABLE_SIZE.
     43  */
     44 static void haskellsecp256k1_v0_1_0_ecmult_const_odd_multiples_table_globalz(haskellsecp256k1_v0_1_0_ge *pre, haskellsecp256k1_v0_1_0_fe *globalz, const haskellsecp256k1_v0_1_0_gej *a) {
     45     haskellsecp256k1_v0_1_0_fe zr[ECMULT_CONST_TABLE_SIZE];
     46 
     47     haskellsecp256k1_v0_1_0_ecmult_odd_multiples_table(ECMULT_CONST_TABLE_SIZE, pre, zr, globalz, a);
     48     haskellsecp256k1_v0_1_0_ge_table_set_globalz(ECMULT_CONST_TABLE_SIZE, pre, zr);
     49 }
     50 
     51 /* Given a table 'pre' with odd multiples of a point, put in r the signed-bit multiplication of n with that point.
     52  *
     53  * For example, if ECMULT_CONST_GROUP_SIZE is 4, then pre is expected to contain 8 entries:
     54  * [1*P, 3*P, 5*P, 7*P, 9*P, 11*P, 13*P, 15*P]. n is then expected to be a 4-bit integer (range 0-15), and its
     55  * bits are interpreted as signs of powers of two to look up.
     56  *
     57  * For example, if n=4, which is 0100 in binary, which is interpreted as [- + - -], so the looked up value is
     58  * [ -(2^3) + (2^2) - (2^1) - (2^0) ]*P = -7*P. Every valid n translates to an odd number in range [-15,15],
     59  * which means we just need to look up one of the precomputed values, and optionally negate it.
     60  */
     61 #define ECMULT_CONST_TABLE_GET_GE(r,pre,n) do { \
     62     unsigned int m = 0; \
     63     /* If the top bit of n is 0, we want the negation. */ \
     64     volatile unsigned int negative = ((n) >> (ECMULT_CONST_GROUP_SIZE - 1)) ^ 1; \
     65     /* Let n[i] be the i-th bit of n, then the index is
     66      *     sum(cnot(n[i]) * 2^i, i=0..l-2)
     67      * where cnot(b) = b if n[l-1] = 1 and 1 - b otherwise.
     68      * For example, if n = 4, in binary 0100, the index is 3, in binary 011.
     69      *
     70      * Proof:
     71      *     Let
     72      *         x = sum((2*n[i] - 1)*2^i, i=0..l-1)
     73      *           = 2*sum(n[i] * 2^i, i=0..l-1) - 2^l + 1
     74      *     be the value represented by n.
     75      *     The index is (x - 1)/2 if x > 0 and -(x + 1)/2 otherwise.
     76      *     Case x > 0:
     77      *         n[l-1] = 1
     78      *         index = sum(n[i] * 2^i, i=0..l-1) - 2^(l-1)
     79      *               = sum(n[i] * 2^i, i=0..l-2)
     80      *     Case x <= 0:
     81      *         n[l-1] = 0
     82      *          index = -(2*sum(n[i] * 2^i, i=0..l-1) - 2^l + 2)/2
     83      *                = 2^(l-1) - 1 - sum(n[i] * 2^i, i=0..l-1)
     84      *                = sum((1 - n[i]) * 2^i, i=0..l-2)
     85      */ \
     86     unsigned int index = ((unsigned int)(-negative) ^ n) & ((1U << (ECMULT_CONST_GROUP_SIZE - 1)) - 1U); \
     87     haskellsecp256k1_v0_1_0_fe neg_y; \
     88     VERIFY_CHECK((n) < (1U << ECMULT_CONST_GROUP_SIZE)); \
     89     VERIFY_CHECK(index < (1U << (ECMULT_CONST_GROUP_SIZE - 1))); \
     90     /* Unconditionally set r->x = (pre)[m].x. r->y = (pre)[m].y. because it's either the correct one
     91      * or will get replaced in the later iterations, this is needed to make sure `r` is initialized. */ \
     92     (r)->x = (pre)[m].x; \
     93     (r)->y = (pre)[m].y; \
     94     for (m = 1; m < ECMULT_CONST_TABLE_SIZE; m++) { \
     95         /* This loop is used to avoid secret data in array indices. See
     96          * the comment in ecmult_gen_impl.h for rationale. */ \
     97         haskellsecp256k1_v0_1_0_fe_cmov(&(r)->x, &(pre)[m].x, m == index); \
     98         haskellsecp256k1_v0_1_0_fe_cmov(&(r)->y, &(pre)[m].y, m == index); \
     99     } \
    100     (r)->infinity = 0; \
    101     haskellsecp256k1_v0_1_0_fe_negate(&neg_y, &(r)->y, 1); \
    102     haskellsecp256k1_v0_1_0_fe_cmov(&(r)->y, &neg_y, negative); \
    103 } while(0)
    104 
    105 /* For K as defined in the comment of haskellsecp256k1_v0_1_0_ecmult_const, we have several precomputed
    106  * formulas/constants.
    107  * - in exhaustive test mode, we give an explicit expression to compute it at compile time: */
    108 #ifdef EXHAUSTIVE_TEST_ORDER
    109 static const haskellsecp256k1_v0_1_0_scalar haskellsecp256k1_v0_1_0_ecmult_const_K = ((SECP256K1_SCALAR_CONST(0, 0, 0, (1U << (ECMULT_CONST_BITS - 128)) - 2U, 0, 0, 0, 0) + EXHAUSTIVE_TEST_ORDER - 1U) * (1U + EXHAUSTIVE_TEST_LAMBDA)) % EXHAUSTIVE_TEST_ORDER;
    110 /* - for the real secp256k1 group we have constants for various ECMULT_CONST_BITS values. */
    111 #elif ECMULT_CONST_BITS == 129
    112 /* For GROUP_SIZE = 1,3. */
    113 static const haskellsecp256k1_v0_1_0_scalar haskellsecp256k1_v0_1_0_ecmult_const_K = SECP256K1_SCALAR_CONST(0xac9c52b3ul, 0x3fa3cf1ful, 0x5ad9e3fdul, 0x77ed9ba4ul, 0xa880b9fcul, 0x8ec739c2ul, 0xe0cfc810ul, 0xb51283ceul);
    114 #elif ECMULT_CONST_BITS == 130
    115 /* For GROUP_SIZE = 2,5. */
    116 static const haskellsecp256k1_v0_1_0_scalar haskellsecp256k1_v0_1_0_ecmult_const_K = SECP256K1_SCALAR_CONST(0xa4e88a7dul, 0xcb13034eul, 0xc2bdd6bful, 0x7c118d6bul, 0x589ae848ul, 0x26ba29e4ul, 0xb5c2c1dcul, 0xde9798d9ul);
    117 #elif ECMULT_CONST_BITS == 132
    118 /* For GROUP_SIZE = 4,6 */
    119 static const haskellsecp256k1_v0_1_0_scalar haskellsecp256k1_v0_1_0_ecmult_const_K = SECP256K1_SCALAR_CONST(0x76b1d93dul, 0x0fae3c6bul, 0x3215874bul, 0x94e93813ul, 0x7937fe0dul, 0xb66bcaaful, 0xb3749ca5ul, 0xd7b6171bul);
    120 #else
    121 #  error "Unknown ECMULT_CONST_BITS"
    122 #endif
    123 
    124 static void haskellsecp256k1_v0_1_0_ecmult_const(haskellsecp256k1_v0_1_0_gej *r, const haskellsecp256k1_v0_1_0_ge *a, const haskellsecp256k1_v0_1_0_scalar *q) {
    125     /* The approach below combines the signed-digit logic from Mike Hamburg's
    126      * "Fast and compact elliptic-curve cryptography" (https://eprint.iacr.org/2012/309)
    127      * Section 3.3, with the GLV endomorphism.
    128      *
    129      * The idea there is to interpret the bits of a scalar as signs (1 = +, 0 = -), and compute a
    130      * point multiplication in that fashion. Let v be an n-bit non-negative integer (0 <= v < 2^n),
    131      * and v[i] its i'th bit (so v = sum(v[i] * 2^i, i=0..n-1)). Then define:
    132      *
    133      *   C_l(v, A) = sum((2*v[i] - 1) * 2^i*A, i=0..l-1)
    134      *
    135      * Then it holds that C_l(v, A) = sum((2*v[i] - 1) * 2^i*A, i=0..l-1)
    136      *                              = (2*sum(v[i] * 2^i, i=0..l-1) + 1 - 2^l) * A
    137      *                              = (2*v + 1 - 2^l) * A
    138      *
    139      * Thus, one can compute q*A as C_256((q + 2^256 - 1) / 2, A). This is the basis for the
    140      * paper's signed-digit multi-comb algorithm for multiplication using a precomputed table.
    141      *
    142      * It is appealing to try to combine this with the GLV optimization: the idea that a scalar
    143      * s can be written as s1 + lambda*s2, where lambda is a curve-specific constant such that
    144      * lambda*A is easy to compute, and where s1 and s2 are small. In particular we have the
    145      * haskellsecp256k1_v0_1_0_scalar_split_lambda function which performs such a split with the resulting s1
    146      * and s2 in range (-2^128, 2^128) mod n. This does work, but is uninteresting:
    147      *
    148      *   To compute q*A:
    149      *   - Let s1, s2 = split_lambda(q)
    150      *   - Let R1 = C_256((s1 + 2^256 - 1) / 2, A)
    151      *   - Let R2 = C_256((s2 + 2^256 - 1) / 2, lambda*A)
    152      *   - Return R1 + R2
    153      *
    154      * The issue is that while s1 and s2 are small-range numbers, (s1 + 2^256 - 1) / 2 (mod n)
    155      * and (s2 + 2^256 - 1) / 2 (mod n) are not, undoing the benefit of the splitting.
    156      *
    157      * To make it work, we want to modify the input scalar q first, before splitting, and then only
    158      * add a 2^128 offset of the split results (so that they end up in the single 129-bit range
    159      * [0,2^129]). A slightly smaller offset would work due to the bounds on the split, but we pick
    160      * 2^128 for simplicity. Let s be the scalar fed to split_lambda, and f(q) the function to
    161      * compute it from q:
    162      *
    163      *   To compute q*A:
    164      *   - Compute s = f(q)
    165      *   - Let s1, s2 = split_lambda(s)
    166      *   - Let v1 = s1 + 2^128 (mod n)
    167      *   - Let v2 = s2 + 2^128 (mod n)
    168      *   - Let R1 = C_l(v1, A)
    169      *   - Let R2 = C_l(v2, lambda*A)
    170      *   - Return R1 + R2
    171      *
    172      * l will thus need to be at least 129, but we may overshoot by a few bits (see
    173      * further), so keep it as a variable.
    174      *
    175      * To solve for s, we reason:
    176      *     q*A  = R1 + R2
    177      * <=> q*A  = C_l(s1 + 2^128, A) + C_l(s2 + 2^128, lambda*A)
    178      * <=> q*A  = (2*(s1 + 2^128) + 1 - 2^l) * A + (2*(s2 + 2^128) + 1 - 2^l) * lambda*A
    179      * <=> q*A  = (2*(s1 + s2*lambda) + (2^129 + 1 - 2^l) * (1 + lambda)) * A
    180      * <=> q    = 2*(s1 + s2*lambda) + (2^129 + 1 - 2^l) * (1 + lambda) (mod n)
    181      * <=> q    = 2*s + (2^129 + 1 - 2^l) * (1 + lambda) (mod n)
    182      * <=> s    = (q + (2^l - 2^129 - 1) * (1 + lambda)) / 2 (mod n)
    183      * <=> f(q) = (q + K) / 2 (mod n)
    184      *            where K = (2^l - 2^129 - 1)*(1 + lambda) (mod n)
    185      *
    186      * We will process the computation of C_l(v1, A) and C_l(v2, lambda*A) in groups of
    187      * ECMULT_CONST_GROUP_SIZE, so we set l to the smallest multiple of ECMULT_CONST_GROUP_SIZE
    188      * that is not less than 129; this equals ECMULT_CONST_BITS.
    189      */
    190 
    191     /* The offset to add to s1 and s2 to make them non-negative. Equal to 2^128. */
    192     static const haskellsecp256k1_v0_1_0_scalar S_OFFSET = SECP256K1_SCALAR_CONST(0, 0, 0, 1, 0, 0, 0, 0);
    193     haskellsecp256k1_v0_1_0_scalar s, v1, v2;
    194     haskellsecp256k1_v0_1_0_ge pre_a[ECMULT_CONST_TABLE_SIZE];
    195     haskellsecp256k1_v0_1_0_ge pre_a_lam[ECMULT_CONST_TABLE_SIZE];
    196     haskellsecp256k1_v0_1_0_fe global_z;
    197     int group, i;
    198 
    199     /* We're allowed to be non-constant time in the point, and the code below (in particular,
    200      * haskellsecp256k1_v0_1_0_ecmult_const_odd_multiples_table_globalz) cannot deal with infinity in a
    201      * constant-time manner anyway. */
    202     if (haskellsecp256k1_v0_1_0_ge_is_infinity(a)) {
    203         haskellsecp256k1_v0_1_0_gej_set_infinity(r);
    204         return;
    205     }
    206 
    207     /* Compute v1 and v2. */
    208     haskellsecp256k1_v0_1_0_scalar_add(&s, q, &haskellsecp256k1_v0_1_0_ecmult_const_K);
    209     haskellsecp256k1_v0_1_0_scalar_half(&s, &s);
    210     haskellsecp256k1_v0_1_0_scalar_split_lambda(&v1, &v2, &s);
    211     haskellsecp256k1_v0_1_0_scalar_add(&v1, &v1, &S_OFFSET);
    212     haskellsecp256k1_v0_1_0_scalar_add(&v2, &v2, &S_OFFSET);
    213 
    214 #ifdef VERIFY
    215     /* Verify that v1 and v2 are in range [0, 2^129-1]. */
    216     for (i = 129; i < 256; ++i) {
    217         VERIFY_CHECK(haskellsecp256k1_v0_1_0_scalar_get_bits(&v1, i, 1) == 0);
    218         VERIFY_CHECK(haskellsecp256k1_v0_1_0_scalar_get_bits(&v2, i, 1) == 0);
    219     }
    220 #endif
    221 
    222     /* Calculate odd multiples of A and A*lambda.
    223      * All multiples are brought to the same Z 'denominator', which is stored
    224      * in global_z. Due to secp256k1' isomorphism we can do all operations pretending
    225      * that the Z coordinate was 1, use affine addition formulae, and correct
    226      * the Z coordinate of the result once at the end.
    227      */
    228     haskellsecp256k1_v0_1_0_gej_set_ge(r, a);
    229     haskellsecp256k1_v0_1_0_ecmult_const_odd_multiples_table_globalz(pre_a, &global_z, r);
    230     for (i = 0; i < ECMULT_CONST_TABLE_SIZE; i++) {
    231         haskellsecp256k1_v0_1_0_ge_mul_lambda(&pre_a_lam[i], &pre_a[i]);
    232     }
    233 
    234     /* Next, we compute r = C_l(v1, A) + C_l(v2, lambda*A).
    235      *
    236      * We proceed in groups of ECMULT_CONST_GROUP_SIZE bits, operating on that many bits
    237      * at a time, from high in v1, v2 to low. Call these bits1 (from v1) and bits2 (from v2).
    238      *
    239      * Now note that ECMULT_CONST_TABLE_GET_GE(&t, pre_a, bits1) loads into t a point equal
    240      * to C_{ECMULT_CONST_GROUP_SIZE}(bits1, A), and analogously for pre_lam_a / bits2.
    241      * This means that all we need to do is add these looked up values together, multiplied
    242      * by 2^(ECMULT_GROUP_SIZE * group).
    243      */
    244     for (group = ECMULT_CONST_GROUPS - 1; group >= 0; --group) {
    245         /* Using the _var get_bits function is ok here, since it's only variable in offset and count, not in the scalar. */
    246         unsigned int bits1 = haskellsecp256k1_v0_1_0_scalar_get_bits_var(&v1, group * ECMULT_CONST_GROUP_SIZE, ECMULT_CONST_GROUP_SIZE);
    247         unsigned int bits2 = haskellsecp256k1_v0_1_0_scalar_get_bits_var(&v2, group * ECMULT_CONST_GROUP_SIZE, ECMULT_CONST_GROUP_SIZE);
    248         haskellsecp256k1_v0_1_0_ge t;
    249         int j;
    250 
    251         ECMULT_CONST_TABLE_GET_GE(&t, pre_a, bits1);
    252         if (group == ECMULT_CONST_GROUPS - 1) {
    253             /* Directly set r in the first iteration. */
    254             haskellsecp256k1_v0_1_0_gej_set_ge(r, &t);
    255         } else {
    256             /* Shift the result so far up. */
    257             for (j = 0; j < ECMULT_CONST_GROUP_SIZE; ++j) {
    258                 haskellsecp256k1_v0_1_0_gej_double(r, r);
    259             }
    260             haskellsecp256k1_v0_1_0_gej_add_ge(r, r, &t);
    261         }
    262         ECMULT_CONST_TABLE_GET_GE(&t, pre_a_lam, bits2);
    263         haskellsecp256k1_v0_1_0_gej_add_ge(r, r, &t);
    264     }
    265 
    266     /* Map the result back to the secp256k1 curve from the isomorphic curve. */
    267     haskellsecp256k1_v0_1_0_fe_mul(&r->z, &r->z, &global_z);
    268 }
    269 
    270 static int haskellsecp256k1_v0_1_0_ecmult_const_xonly(haskellsecp256k1_v0_1_0_fe* r, const haskellsecp256k1_v0_1_0_fe *n, const haskellsecp256k1_v0_1_0_fe *d, const haskellsecp256k1_v0_1_0_scalar *q, int known_on_curve) {
    271 
    272     /* This algorithm is a generalization of Peter Dettman's technique for
    273      * avoiding the square root in a random-basepoint x-only multiplication
    274      * on a Weierstrass curve:
    275      * https://mailarchive.ietf.org/arch/msg/cfrg/7DyYY6gg32wDgHAhgSb6XxMDlJA/
    276      *
    277      *
    278      * === Background: the effective affine technique ===
    279      *
    280      * Let phi_u be the isomorphism that maps (x, y) on secp256k1 curve y^2 = x^3 + 7 to
    281      * x' = u^2*x, y' = u^3*y on curve y'^2 = x'^3 + u^6*7. This new curve has the same order as
    282      * the original (it is isomorphic), but moreover, has the same addition/doubling formulas, as
    283      * the curve b=7 coefficient does not appear in those formulas (or at least does not appear in
    284      * the formulas implemented in this codebase, both affine and Jacobian). See also Example 9.5.2
    285      * in https://www.math.auckland.ac.nz/~sgal018/crypto-book/ch9.pdf.
    286      *
    287      * This means any linear combination of secp256k1 points can be computed by applying phi_u
    288      * (with non-zero u) on all input points (including the generator, if used), computing the
    289      * linear combination on the isomorphic curve (using the same group laws), and then applying
    290      * phi_u^{-1} to get back to secp256k1.
    291      *
    292      * Switching to Jacobian coordinates, note that phi_u applied to (X, Y, Z) is simply
    293      * (X, Y, Z/u). Thus, if we want to compute (X1, Y1, Z) + (X2, Y2, Z), with identical Z
    294      * coordinates, we can use phi_Z to transform it to (X1, Y1, 1) + (X2, Y2, 1) on an isomorphic
    295      * curve where the affine addition formula can be used instead.
    296      * If (X3, Y3, Z3) = (X1, Y1) + (X2, Y2) on that curve, then our answer on secp256k1 is
    297      * (X3, Y3, Z3*Z).
    298      *
    299      * This is the effective affine technique: if we have a linear combination of group elements
    300      * to compute, and all those group elements have the same Z coordinate, we can simply pretend
    301      * that all those Z coordinates are 1, perform the computation that way, and then multiply the
    302      * original Z coordinate back in.
    303      *
    304      * The technique works on any a=0 short Weierstrass curve. It is possible to generalize it to
    305      * other curves too, but there the isomorphic curves will have different 'a' coefficients,
    306      * which typically does affect the group laws.
    307      *
    308      *
    309      * === Avoiding the square root for x-only point multiplication ===
    310      *
    311      * In this function, we want to compute the X coordinate of q*(n/d, y), for
    312      * y = sqrt((n/d)^3 + 7). Its negation would also be a valid Y coordinate, but by convention
    313      * we pick whatever sqrt returns (which we assume to be a deterministic function).
    314      *
    315      * Let g = y^2*d^3 = n^3 + 7*d^3. This also means y = sqrt(g/d^3).
    316      * Further let v = sqrt(d*g), which must exist as d*g = y^2*d^4 = (y*d^2)^2.
    317      *
    318      * The input point (n/d, y) also has Jacobian coordinates:
    319      *
    320      *     (n/d, y, 1)
    321      *   = (n/d * v^2, y * v^3, v)
    322      *   = (n/d * d*g, y * sqrt(d^3*g^3), v)
    323      *   = (n/d * d*g, sqrt(y^2 * d^3*g^3), v)
    324      *   = (n*g, sqrt(g/d^3 * d^3*g^3), v)
    325      *   = (n*g, sqrt(g^4), v)
    326      *   = (n*g, g^2, v)
    327      *
    328      * It is easy to verify that both (n*g, g^2, v) and its negation (n*g, -g^2, v) have affine X
    329      * coordinate n/d, and this holds even when the square root function doesn't have a
    330      * deterministic sign. We choose the (n*g, g^2, v) version.
    331      *
    332      * Now switch to the effective affine curve using phi_v, where the input point has coordinates
    333      * (n*g, g^2). Compute (X, Y, Z) = q * (n*g, g^2) there.
    334      *
    335      * Back on secp256k1, that means q * (n*g, g^2, v) = (X, Y, v*Z). This last point has affine X
    336      * coordinate X / (v^2*Z^2) = X / (d*g*Z^2). Determining the affine Y coordinate would involve
    337      * a square root, but as long as we only care about the resulting X coordinate, no square root
    338      * is needed anywhere in this computation.
    339      */
    340 
    341     haskellsecp256k1_v0_1_0_fe g, i;
    342     haskellsecp256k1_v0_1_0_ge p;
    343     haskellsecp256k1_v0_1_0_gej rj;
    344 
    345     /* Compute g = (n^3 + B*d^3). */
    346     haskellsecp256k1_v0_1_0_fe_sqr(&g, n);
    347     haskellsecp256k1_v0_1_0_fe_mul(&g, &g, n);
    348     if (d) {
    349         haskellsecp256k1_v0_1_0_fe b;
    350         VERIFY_CHECK(!haskellsecp256k1_v0_1_0_fe_normalizes_to_zero(d));
    351         haskellsecp256k1_v0_1_0_fe_sqr(&b, d);
    352         VERIFY_CHECK(SECP256K1_B <= 8); /* magnitude of b will be <= 8 after the next call */
    353         haskellsecp256k1_v0_1_0_fe_mul_int(&b, SECP256K1_B);
    354         haskellsecp256k1_v0_1_0_fe_mul(&b, &b, d);
    355         haskellsecp256k1_v0_1_0_fe_add(&g, &b);
    356         if (!known_on_curve) {
    357             /* We need to determine whether (n/d)^3 + 7 is square.
    358              *
    359              *     is_square((n/d)^3 + 7)
    360              * <=> is_square(((n/d)^3 + 7) * d^4)
    361              * <=> is_square((n^3 + 7*d^3) * d)
    362              * <=> is_square(g * d)
    363              */
    364             haskellsecp256k1_v0_1_0_fe c;
    365             haskellsecp256k1_v0_1_0_fe_mul(&c, &g, d);
    366             if (!haskellsecp256k1_v0_1_0_fe_is_square_var(&c)) return 0;
    367         }
    368     } else {
    369         haskellsecp256k1_v0_1_0_fe_add_int(&g, SECP256K1_B);
    370         if (!known_on_curve) {
    371             /* g at this point equals x^3 + 7. Test if it is square. */
    372             if (!haskellsecp256k1_v0_1_0_fe_is_square_var(&g)) return 0;
    373         }
    374     }
    375 
    376     /* Compute base point P = (n*g, g^2), the effective affine version of (n*g, g^2, v), which has
    377      * corresponding affine X coordinate n/d. */
    378     haskellsecp256k1_v0_1_0_fe_mul(&p.x, &g, n);
    379     haskellsecp256k1_v0_1_0_fe_sqr(&p.y, &g);
    380     p.infinity = 0;
    381 
    382     /* Perform x-only EC multiplication of P with q. */
    383     VERIFY_CHECK(!haskellsecp256k1_v0_1_0_scalar_is_zero(q));
    384     haskellsecp256k1_v0_1_0_ecmult_const(&rj, &p, q);
    385     VERIFY_CHECK(!haskellsecp256k1_v0_1_0_gej_is_infinity(&rj));
    386 
    387     /* The resulting (X, Y, Z) point on the effective-affine isomorphic curve corresponds to
    388      * (X, Y, Z*v) on the secp256k1 curve. The affine version of that has X coordinate
    389      * (X / (Z^2*d*g)). */
    390     haskellsecp256k1_v0_1_0_fe_sqr(&i, &rj.z);
    391     haskellsecp256k1_v0_1_0_fe_mul(&i, &i, &g);
    392     if (d) haskellsecp256k1_v0_1_0_fe_mul(&i, &i, d);
    393     haskellsecp256k1_v0_1_0_fe_inv(&i, &i);
    394     haskellsecp256k1_v0_1_0_fe_mul(r, &rj.x, &i);
    395 
    396     return 1;
    397 }
    398 
    399 #endif /* SECP256K1_ECMULT_CONST_IMPL_H */