int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
                size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
                BN_CTX *ctx)
{
    BN_CTX *new_ctx = NULL;
    const EC_POINT *generator = NULL;
    EC_POINT *tmp = NULL;
    size_t totalnum;
    size_t blocksize = 0, numblocks = 0;
    size_t pre_points_per_block = 0;
    size_t i, j;
    int k;
    int r_is_inverted = 0;
    int r_is_at_infinity = 1;
    size_t *wsize = NULL;
    signed char **wNAF = NULL;
    size_t *wNAF_len = NULL;
    size_t max_len = 0;
    size_t num_val;
    EC_POINT **val = NULL;
    EC_POINT **v;
    EC_POINT ***val_sub = NULL;
    const EC_PRE_COMP *pre_comp = NULL;
    int num_scalar = 0;
    int ret = 0;
    if (!ec_point_is_compat(r, group)) {
        ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
        return 0;
    }
    if ((scalar == NULL) && (num == 0)) {
        return EC_POINT_set_to_infinity(group, r);
    }
    if (!BN_is_zero(group->order) && !BN_is_zero(group->cofactor)) {
        if ((scalar != NULL) && (num == 0)) {
            return ec_scalar_mul_ladder(group, r, scalar, NULL, ctx);
        }
        if ((scalar == NULL) && (num == 1)) {
            return ec_scalar_mul_ladder(group, r, scalars[0], points[0], ctx);
        }
    }
    for (i = 0; i < num; i++) {
        if (!ec_point_is_compat(points[i], group)) {
            ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
            return 0;
        }
    }
    if (ctx == NULL) {
        ctx = new_ctx = BN_CTX_new();
        if (ctx == NULL)
            goto err;
    }
    if (scalar != NULL) {
        generator = EC_GROUP_get0_generator(group);
        if (generator == NULL) {
            ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);
            goto err;
        }
        pre_comp = group->pre_comp.ec;
        if (pre_comp && pre_comp->numblocks
            && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) ==
                0)) {
            blocksize = pre_comp->blocksize;
            numblocks = (BN_num_bits(scalar) / blocksize) + 1;
            if (numblocks > pre_comp->numblocks)
                numblocks = pre_comp->numblocks;
            pre_points_per_block = (size_t)1 << (pre_comp->w - 1);
            if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {
                ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
                goto err;
            }
        } else {
            pre_comp = NULL;
            numblocks = 1;
            num_scalar = 1;
        }
    }
    totalnum = num + numblocks;
    wsize = OPENSSL_malloc(totalnum * sizeof(wsize[0]));
    wNAF_len = OPENSSL_malloc(totalnum * sizeof(wNAF_len[0]));
    wNAF = OPENSSL_malloc((totalnum + 1) * sizeof(wNAF[0]));
    val_sub = OPENSSL_malloc(totalnum * sizeof(val_sub[0]));
    if (wNAF != NULL)
        wNAF[0] = NULL;
    if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) {
        ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
        goto err;
    }
    num_val = 0;
    for (i = 0; i < num + num_scalar; i++) {
        size_t bits;
        bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);
        wsize[i] = EC_window_bits_for_scalar_size(bits);
        num_val += (size_t)1 << (wsize[i] - 1);
        wNAF[i + 1] = NULL;
        wNAF[i] =
            bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i],
                            &wNAF_len[i]);
        if (wNAF[i] == NULL)
            goto err;
        if (wNAF_len[i] > max_len)
            max_len = wNAF_len[i];
    }
    if (numblocks) {
        if (pre_comp == NULL) {
            if (num_scalar != 1) {
                ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
                goto err;
            }
        } else {
            signed char *tmp_wNAF = NULL;
            size_t tmp_len = 0;
            if (num_scalar != 0) {
                ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
                goto err;
            }
            wsize[num] = pre_comp->w;
            tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len);
            if (!tmp_wNAF)
                goto err;
            if (tmp_len <= max_len) {
                numblocks = 1;
                totalnum = num + 1;
                wNAF[num] = tmp_wNAF;
                wNAF[num + 1] = NULL;
                wNAF_len[num] = tmp_len;
                val_sub[num] = pre_comp->points;
            } else {
                signed char *pp;
                EC_POINT **tmp_points;
                if (tmp_len < numblocks * blocksize) {
                    numblocks = (tmp_len + blocksize - 1) / blocksize;
                    if (numblocks > pre_comp->numblocks) {
                        ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
                        OPENSSL_free(tmp_wNAF);
                        goto err;
                    }
                    totalnum = num + numblocks;
                }
                pp = tmp_wNAF;
                tmp_points = pre_comp->points;
                for (i = num; i < totalnum; i++) {
                    if (i < totalnum - 1) {
                        wNAF_len[i] = blocksize;
                        if (tmp_len < blocksize) {
                            ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
                            OPENSSL_free(tmp_wNAF);
                            goto err;
                        }
                        tmp_len -= blocksize;
                    } else
                        wNAF_len[i] = tmp_len;
                    wNAF[i + 1] = NULL;
                    wNAF[i] = OPENSSL_malloc(wNAF_len[i]);
                    if (wNAF[i] == NULL) {
                        ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
                        OPENSSL_free(tmp_wNAF);
                        goto err;
                    }
                    memcpy(wNAF[i], pp, wNAF_len[i]);
                    if (wNAF_len[i] > max_len)
                        max_len = wNAF_len[i];
                    if (*tmp_points == NULL) {
                        ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
                        OPENSSL_free(tmp_wNAF);
                        goto err;
                    }
                    val_sub[i] = tmp_points;
                    tmp_points += pre_points_per_block;
                    pp += blocksize;
                }
                OPENSSL_free(tmp_wNAF);
            }
        }
    }
    val = OPENSSL_malloc((num_val + 1) * sizeof(val[0]));
    if (val == NULL) {
        ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
        goto err;
    }
    val[num_val] = NULL;
    v = val;
    for (i = 0; i < num + num_scalar; i++) {
        val_sub[i] = v;
        for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {
            *v = EC_POINT_new(group);
            if (*v == NULL)
                goto err;
            v++;
        }
    }
    if (!(v == val + num_val)) {
        ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
        goto err;
    }
    if ((tmp = EC_POINT_new(group)) == NULL)
        goto err;
    for (i = 0; i < num + num_scalar; i++) {
        if (i < num) {
            if (!EC_POINT_copy(val_sub[i][0], points[i]))
                goto err;
        } else {
            if (!EC_POINT_copy(val_sub[i][0], generator))
                goto err;
        }
        if (wsize[i] > 1) {
            if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))
                goto err;
            for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {
                if (!EC_POINT_add
                    (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))
                    goto err;
            }
        }
    }
    if (!EC_POINTs_make_affine(group, num_val, val, ctx))
        goto err;
    r_is_at_infinity = 1;
    for (k = max_len - 1; k >= 0; k--) {
        if (!r_is_at_infinity) {
            if (!EC_POINT_dbl(group, r, r, ctx))
                goto err;
        }
        for (i = 0; i < totalnum; i++) {
            if (wNAF_len[i] > (size_t)k) {
                int digit = wNAF[i][k];
                int is_neg;
                if (digit) {
                    is_neg = digit < 0;
                    if (is_neg)
                        digit = -digit;
                    if (is_neg != r_is_inverted) {
                        if (!r_is_at_infinity) {
                            if (!EC_POINT_invert(group, r, ctx))
                                goto err;
                        }
                        r_is_inverted = !r_is_inverted;
                    }
                    if (r_is_at_infinity) {
                        if (!EC_POINT_copy(r, val_sub[i][digit >> 1]))
                            goto err;
                        r_is_at_infinity = 0;
                    } else {
                        if (!EC_POINT_add
                            (group, r, r, val_sub[i][digit >> 1], ctx))
                            goto err;
                    }
                }
            }
        }
    }
    if (r_is_at_infinity) {
        if (!EC_POINT_set_to_infinity(group, r))
            goto err;
    } else {
        if (r_is_inverted)
            if (!EC_POINT_invert(group, r, ctx))
                goto err;
    }
    ret = 1;
 err:
    BN_CTX_free(new_ctx);
    EC_POINT_free(tmp);
    OPENSSL_free(wsize);
    OPENSSL_free(wNAF_len);
    if (wNAF != NULL) {
        signed char **w;
        for (w = wNAF; *w != NULL; w++)
            OPENSSL_free(*w);
        OPENSSL_free(wNAF);
    }
    if (val != NULL) {
        for (v = val; *v != NULL; v++)
            EC_POINT_clear_free(*v);
        OPENSSL_free(val);
    }
    OPENSSL_free(val_sub);
    return ret;
}
['static int char2_curve_test(int n)\n{\n    int r = 0;\n    BN_CTX *ctx = NULL;\n    BIGNUM *p = NULL, *a = NULL, *b = NULL;\n    BIGNUM *x = NULL, *y = NULL, *z = NULL, *cof = NULL, *yplusone = NULL;\n    EC_GROUP *group = NULL, *variable = NULL;\n    EC_POINT *P = NULL, *Q = NULL, *R = NULL;\n    const EC_POINT *points[3];\n    const BIGNUM *scalars[3];\n    struct c2_curve_test *const test = char2_curve_tests + n;\n    if (!TEST_ptr(ctx = BN_CTX_new())\n        || !TEST_ptr(p = BN_new())\n        || !TEST_ptr(a = BN_new())\n        || !TEST_ptr(b = BN_new())\n        || !TEST_ptr(x = BN_new())\n        || !TEST_ptr(y = BN_new())\n        || !TEST_ptr(z = BN_new())\n        || !TEST_ptr(yplusone = BN_new())\n        || !TEST_true(BN_hex2bn(&p, test->p))\n        || !TEST_true(BN_hex2bn(&a, test->a))\n        || !TEST_true(BN_hex2bn(&b, test->b))\n        || !TEST_true(group = EC_GROUP_new(EC_GF2m_simple_method()))\n        || !TEST_true(EC_GROUP_set_curve_GF2m(group, p, a, b, ctx))\n        || !TEST_ptr(P = EC_POINT_new(group))\n        || !TEST_ptr(Q = EC_POINT_new(group))\n        || !TEST_ptr(R = EC_POINT_new(group))\n        || !TEST_true(BN_hex2bn(&x, test->x))\n        || !TEST_true(BN_hex2bn(&y, test->y))\n        || !TEST_true(BN_add(yplusone, y, BN_value_one())))\n        goto err;\n# ifdef OPENSSL_EC_BIN_PT_COMP\n    if (!TEST_false(EC_POINT_set_affine_coordinates_GF2m(group, P, x, yplusone,\n                                                         ctx))\n        || !TEST_true(EC_POINT_set_compressed_coordinates_GF2m(group, P, x,\n                                                               test->y_bit,\n                                                               ctx))\n        || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)\n        || !TEST_true(BN_hex2bn(&z, test->order))\n        || !TEST_true(BN_hex2bn(&cof, test->cof))\n        || !TEST_true(EC_GROUP_set_generator(group, P, z, cof))\n        || !TEST_true(EC_POINT_get_affine_coordinates_GF2m(group, P, x, y,\n                                                           ctx)))\n        goto err;\n    TEST_info("%s -- Generator", test->name);\n    test_output_bignum("x", x);\n    test_output_bignum("y", y);\n    if (!TEST_true(BN_hex2bn(&z, test->y))\n        || !TEST_BN_eq(y, z))\n        goto err;\n# else\n    if (!TEST_false(EC_POINT_set_affine_coordinates_GF2m(group, P, x, yplusone,\n                    ctx))\n        || !TEST_true(EC_POINT_set_affine_coordinates_GF2m(group, P, x, y, ctx))\n        || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)\n        || !TEST_true(BN_hex2bn(&z, test->order))\n        || !TEST_true(BN_hex2bn(&cof, test->cof))\n        || !TEST_true(EC_GROUP_set_generator(group, P, z, cof)))\n        goto err;\n    TEST_info("%s -- Generator:", test->name);\n    test_output_bignum("x", x);\n    test_output_bignum("y", y);\n# endif\n    if (!TEST_int_eq(EC_GROUP_get_degree(group), test->degree)\n        || !group_order_tests(group)\n        || !TEST_ptr(variable = EC_GROUP_new(EC_GROUP_method_of(group)))\n        || !TEST_true(EC_GROUP_copy(variable, group)))\n        goto err;\n    if (n == OSSL_NELEM(char2_curve_tests) - 1) {\n        if (!TEST_true(EC_POINT_set_affine_coordinates_GF2m(group, P, x, y,\n                                                                ctx))\n            || !TEST_true(EC_POINT_copy(Q, P))\n            || !TEST_false(EC_POINT_is_at_infinity(group, Q))\n            || !TEST_true(EC_POINT_dbl(group, P, P, ctx))\n            || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)\n            || !TEST_true(EC_POINT_invert(group, Q, ctx))\n            || !TEST_true(EC_POINT_add(group, R, P, Q, ctx))\n            || !TEST_true(EC_POINT_add(group, R, R, Q, ctx))\n            || !TEST_true(EC_POINT_is_at_infinity(group, R))\n            || !TEST_false(EC_POINT_is_at_infinity(group, Q)))\n            goto err;\n        points[0] = Q;\n        points[1] = Q;\n        points[2] = Q;\n        if (!TEST_true(BN_add(y, z, BN_value_one()))\n            || !TEST_BN_even(y)\n            || !TEST_true(BN_rshift1(y, y)))\n            goto err;\n        scalars[0] = y;\n        scalars[1] = y;\n        TEST_note("combined multiplication ...");\n        if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))\n            || !TEST_true(EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))\n            || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))\n            || !TEST_int_eq(0, EC_POINT_cmp(group, R, Q, ctx)))\n            goto err;\n        if (!TEST_true(BN_rand(y, BN_num_bits(y), 0, 0))\n            || !TEST_true(BN_add(z, z, y)))\n            goto err;\n        BN_set_negative(z, 1);\n        scalars[0] = y;\n        scalars[1] = z;\n        if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))\n            || !TEST_true(EC_POINT_is_at_infinity(group, P)))\n            goto err;\n        if (!TEST_true(BN_rand(x, BN_num_bits(y) - 1, 0, 0))\n            || !TEST_true(BN_add(z, x, y)))\n            goto err;\n        BN_set_negative(z, 1);\n        scalars[0] = x;\n        scalars[1] = y;\n        scalars[2] = z;\n        if (!TEST_true(EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx))\n            || !TEST_true(EC_POINT_is_at_infinity(group, P)))\n            goto err;;\n    }\n    r = 1;\nerr:\n    BN_CTX_free(ctx);\n    BN_free(p);\n    BN_free(a);\n    BN_free(b);\n    BN_free(x);\n    BN_free(y);\n    BN_free(z);\n    BN_free(yplusone);\n    BN_free(cof);\n    EC_POINT_free(P);\n    EC_POINT_free(Q);\n    EC_POINT_free(R);\n    EC_GROUP_free(group);\n    EC_GROUP_free(variable);\n    return r;\n}', 'static int group_order_tests(EC_GROUP *group)\n{\n    BIGNUM *n1 = NULL, *n2 = NULL, *order = NULL;\n    EC_POINT *P = NULL, *Q = NULL, *R = NULL, *S = NULL;\n    const EC_POINT *G = NULL;\n    BN_CTX *ctx = NULL;\n    int i = 0, r = 0;\n    if (!TEST_ptr(n1 = BN_new())\n        || !TEST_ptr(n2 = BN_new())\n        || !TEST_ptr(order = BN_new())\n        || !TEST_ptr(ctx = BN_CTX_new())\n        || !TEST_ptr(G = EC_GROUP_get0_generator(group))\n        || !TEST_ptr(P = EC_POINT_new(group))\n        || !TEST_ptr(Q = EC_POINT_new(group))\n        || !TEST_ptr(R = EC_POINT_new(group))\n        || !TEST_ptr(S = EC_POINT_new(group)))\n        goto err;\n    if (!TEST_true(EC_GROUP_get_order(group, order, ctx))\n        || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx))\n        || !TEST_true(EC_POINT_is_at_infinity(group, Q))\n        || !TEST_true(EC_GROUP_precompute_mult(group, ctx))\n        || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx))\n        || !TEST_true(EC_POINT_is_at_infinity(group, Q))\n        || !TEST_true(EC_POINT_copy(P, G))\n        || !TEST_true(BN_one(n1))\n        || !TEST_true(EC_POINT_mul(group, Q, n1, NULL, NULL, ctx))\n        || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))\n        || !TEST_true(BN_sub(n1, order, n1))\n        || !TEST_true(EC_POINT_mul(group, Q, n1, NULL, NULL, ctx))\n        || !TEST_true(EC_POINT_invert(group, Q, ctx))\n        || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)))\n        goto err;\n    for (i = 1; i <= 2; i++) {\n        const BIGNUM *scalars[6];\n        const EC_POINT *points[6];\n        if (!TEST_true(BN_set_word(n1, i))\n            || !TEST_true(EC_POINT_mul(group, P, n1, NULL, NULL, ctx))\n            || (i == 1 && !TEST_int_eq(0, EC_POINT_cmp(group, P, G, ctx)))\n            || !TEST_true(BN_one(n1))\n            || !TEST_true(BN_sub(n1, n1, order))\n            || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n1, ctx))\n            || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))\n            || !TEST_true(BN_add(n2, order, BN_value_one()))\n            || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))\n            || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))\n            || !TEST_true(BN_mul(n2, n1, n2, ctx))\n            || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))\n            || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)))\n            goto err;\n        BN_set_negative(n2, 0);\n        if (!TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))\n            || !TEST_true(EC_POINT_add(group, Q, Q, P, ctx))\n            || !TEST_true(EC_POINT_is_at_infinity(group, Q))\n            || !TEST_false(EC_POINT_is_at_infinity(group, P)))\n            goto err;\n        scalars[0] = scalars[1] = BN_value_one();\n        points[0]  = points[1]  = P;\n        if (!TEST_true(EC_POINTs_mul(group, R, NULL, 2, points, scalars, ctx))\n            || !TEST_true(EC_POINT_dbl(group, S, points[0], ctx))\n            || !TEST_int_eq(0, EC_POINT_cmp(group, R, S, ctx)))\n            goto err;\n        scalars[0] = n1;\n        points[0] = Q;\n        scalars[1] = n2;\n        points[1] = P;\n        scalars[2] = n1;\n        points[2] = Q;\n        scalars[3] = n2;\n        points[3] = Q;\n        scalars[4] = n1;\n        points[4] = P;\n        scalars[5] = n2;\n        points[5] = Q;\n        if (!TEST_true(EC_POINTs_mul(group, P, NULL, 6, points, scalars, ctx))\n            || !TEST_true(EC_POINT_is_at_infinity(group, P)))\n            goto err;\n    }\n    r = 1;\nerr:\n    if (r == 0 && i != 0)\n        TEST_info(i == 1 ? "allowing precomputation" :\n                           "without precomputation");\n    EC_POINT_free(P);\n    EC_POINT_free(Q);\n    EC_POINT_free(R);\n    EC_POINT_free(S);\n    BN_free(n1);\n    BN_free(n2);\n    BN_free(order);\n    BN_CTX_free(ctx);\n    return r;\n}', 'int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)\n{\n    if (group->meth->mul == 0)\n        return ec_wNAF_precompute_mult(group, ctx);\n    if (group->meth->precompute_mult != 0)\n        return group->meth->precompute_mult(group, ctx);\n    else\n        return 1;\n}', 'int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx)\n{\n    const EC_POINT *generator;\n    EC_POINT *tmp_point = NULL, *base = NULL, **var;\n    BN_CTX *new_ctx = NULL;\n    const BIGNUM *order;\n    size_t i, bits, w, pre_points_per_block, blocksize, numblocks, num;\n    EC_POINT **points = NULL;\n    EC_PRE_COMP *pre_comp;\n    int ret = 0;\n    EC_pre_comp_free(group);\n    if ((pre_comp = ec_pre_comp_new(group)) == NULL)\n        return 0;\n    generator = EC_GROUP_get0_generator(group);\n    if (generator == NULL) {\n        ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNDEFINED_GENERATOR);\n        goto err;\n    }\n    if (ctx == NULL) {\n        ctx = new_ctx = BN_CTX_new();\n        if (ctx == NULL)\n            goto err;\n    }\n    BN_CTX_start(ctx);\n    order = EC_GROUP_get0_order(group);\n    if (order == NULL)\n        goto err;\n    if (BN_is_zero(order)) {\n        ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNKNOWN_ORDER);\n        goto err;\n    }\n    bits = BN_num_bits(order);\n    blocksize = 8;\n    w = 4;\n    if (EC_window_bits_for_scalar_size(bits) > w) {\n        w = EC_window_bits_for_scalar_size(bits);\n    }\n    numblocks = (bits + blocksize - 1) / blocksize;\n    pre_points_per_block = (size_t)1 << (w - 1);\n    num = pre_points_per_block * numblocks;\n    points = OPENSSL_malloc(sizeof(*points) * (num + 1));\n    if (points == NULL) {\n        ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    var = points;\n    var[num] = NULL;\n    for (i = 0; i < num; i++) {\n        if ((var[i] = EC_POINT_new(group)) == NULL) {\n            ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);\n            goto err;\n        }\n    }\n    if ((tmp_point = EC_POINT_new(group)) == NULL\n        || (base = EC_POINT_new(group)) == NULL) {\n        ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    if (!EC_POINT_copy(base, generator))\n        goto err;\n    for (i = 0; i < numblocks; i++) {\n        size_t j;\n        if (!EC_POINT_dbl(group, tmp_point, base, ctx))\n            goto err;\n        if (!EC_POINT_copy(*var++, base))\n            goto err;\n        for (j = 1; j < pre_points_per_block; j++, var++) {\n            if (!EC_POINT_add(group, *var, tmp_point, *(var - 1), ctx))\n                goto err;\n        }\n        if (i < numblocks - 1) {\n            size_t k;\n            if (blocksize <= 2) {\n                ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n            if (!EC_POINT_dbl(group, base, tmp_point, ctx))\n                goto err;\n            for (k = 2; k < blocksize; k++) {\n                if (!EC_POINT_dbl(group, base, base, ctx))\n                    goto err;\n            }\n        }\n    }\n    if (!EC_POINTs_make_affine(group, num, points, ctx))\n        goto err;\n    pre_comp->group = group;\n    pre_comp->blocksize = blocksize;\n    pre_comp->numblocks = numblocks;\n    pre_comp->w = w;\n    pre_comp->points = points;\n    points = NULL;\n    pre_comp->num = num;\n    SETPRECOMP(group, ec, pre_comp);\n    pre_comp = NULL;\n    ret = 1;\n err:\n    if (ctx != NULL)\n        BN_CTX_end(ctx);\n    BN_CTX_free(new_ctx);\n    EC_ec_pre_comp_free(pre_comp);\n    if (points) {\n        EC_POINT **p;\n        for (p = points; *p != NULL; p++)\n            EC_POINT_free(*p);\n        OPENSSL_free(points);\n    }\n    EC_POINT_free(tmp_point);\n    EC_POINT_free(base);\n    return ret;\n}', 'int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,\n                  size_t num, const EC_POINT *points[],\n                  const BIGNUM *scalars[], BN_CTX *ctx)\n{\n    if (group->meth->mul == 0)\n        return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);\n    return group->meth->mul(group, r, scalar, num, points, scalars, ctx);\n}', 'int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,\n                size_t num, const EC_POINT *points[], const BIGNUM *scalars[],\n                BN_CTX *ctx)\n{\n    BN_CTX *new_ctx = NULL;\n    const EC_POINT *generator = NULL;\n    EC_POINT *tmp = NULL;\n    size_t totalnum;\n    size_t blocksize = 0, numblocks = 0;\n    size_t pre_points_per_block = 0;\n    size_t i, j;\n    int k;\n    int r_is_inverted = 0;\n    int r_is_at_infinity = 1;\n    size_t *wsize = NULL;\n    signed char **wNAF = NULL;\n    size_t *wNAF_len = NULL;\n    size_t max_len = 0;\n    size_t num_val;\n    EC_POINT **val = NULL;\n    EC_POINT **v;\n    EC_POINT ***val_sub = NULL;\n    const EC_PRE_COMP *pre_comp = NULL;\n    int num_scalar = 0;\n    int ret = 0;\n    if (!ec_point_is_compat(r, group)) {\n        ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n        return 0;\n    }\n    if ((scalar == NULL) && (num == 0)) {\n        return EC_POINT_set_to_infinity(group, r);\n    }\n    if (!BN_is_zero(group->order) && !BN_is_zero(group->cofactor)) {\n        if ((scalar != NULL) && (num == 0)) {\n            return ec_scalar_mul_ladder(group, r, scalar, NULL, ctx);\n        }\n        if ((scalar == NULL) && (num == 1)) {\n            return ec_scalar_mul_ladder(group, r, scalars[0], points[0], ctx);\n        }\n    }\n    for (i = 0; i < num; i++) {\n        if (!ec_point_is_compat(points[i], group)) {\n            ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n            return 0;\n        }\n    }\n    if (ctx == NULL) {\n        ctx = new_ctx = BN_CTX_new();\n        if (ctx == NULL)\n            goto err;\n    }\n    if (scalar != NULL) {\n        generator = EC_GROUP_get0_generator(group);\n        if (generator == NULL) {\n            ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);\n            goto err;\n        }\n        pre_comp = group->pre_comp.ec;\n        if (pre_comp && pre_comp->numblocks\n            && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) ==\n                0)) {\n            blocksize = pre_comp->blocksize;\n            numblocks = (BN_num_bits(scalar) / blocksize) + 1;\n            if (numblocks > pre_comp->numblocks)\n                numblocks = pre_comp->numblocks;\n            pre_points_per_block = (size_t)1 << (pre_comp->w - 1);\n            if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {\n                ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n        } else {\n            pre_comp = NULL;\n            numblocks = 1;\n            num_scalar = 1;\n        }\n    }\n    totalnum = num + numblocks;\n    wsize = OPENSSL_malloc(totalnum * sizeof(wsize[0]));\n    wNAF_len = OPENSSL_malloc(totalnum * sizeof(wNAF_len[0]));\n    wNAF = OPENSSL_malloc((totalnum + 1) * sizeof(wNAF[0]));\n    val_sub = OPENSSL_malloc(totalnum * sizeof(val_sub[0]));\n    if (wNAF != NULL)\n        wNAF[0] = NULL;\n    if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) {\n        ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    num_val = 0;\n    for (i = 0; i < num + num_scalar; i++) {\n        size_t bits;\n        bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);\n        wsize[i] = EC_window_bits_for_scalar_size(bits);\n        num_val += (size_t)1 << (wsize[i] - 1);\n        wNAF[i + 1] = NULL;\n        wNAF[i] =\n            bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i],\n                            &wNAF_len[i]);\n        if (wNAF[i] == NULL)\n            goto err;\n        if (wNAF_len[i] > max_len)\n            max_len = wNAF_len[i];\n    }\n    if (numblocks) {\n        if (pre_comp == NULL) {\n            if (num_scalar != 1) {\n                ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n        } else {\n            signed char *tmp_wNAF = NULL;\n            size_t tmp_len = 0;\n            if (num_scalar != 0) {\n                ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n            wsize[num] = pre_comp->w;\n            tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len);\n            if (!tmp_wNAF)\n                goto err;\n            if (tmp_len <= max_len) {\n                numblocks = 1;\n                totalnum = num + 1;\n                wNAF[num] = tmp_wNAF;\n                wNAF[num + 1] = NULL;\n                wNAF_len[num] = tmp_len;\n                val_sub[num] = pre_comp->points;\n            } else {\n                signed char *pp;\n                EC_POINT **tmp_points;\n                if (tmp_len < numblocks * blocksize) {\n                    numblocks = (tmp_len + blocksize - 1) / blocksize;\n                    if (numblocks > pre_comp->numblocks) {\n                        ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n                        OPENSSL_free(tmp_wNAF);\n                        goto err;\n                    }\n                    totalnum = num + numblocks;\n                }\n                pp = tmp_wNAF;\n                tmp_points = pre_comp->points;\n                for (i = num; i < totalnum; i++) {\n                    if (i < totalnum - 1) {\n                        wNAF_len[i] = blocksize;\n                        if (tmp_len < blocksize) {\n                            ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n                            OPENSSL_free(tmp_wNAF);\n                            goto err;\n                        }\n                        tmp_len -= blocksize;\n                    } else\n                        wNAF_len[i] = tmp_len;\n                    wNAF[i + 1] = NULL;\n                    wNAF[i] = OPENSSL_malloc(wNAF_len[i]);\n                    if (wNAF[i] == NULL) {\n                        ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n                        OPENSSL_free(tmp_wNAF);\n                        goto err;\n                    }\n                    memcpy(wNAF[i], pp, wNAF_len[i]);\n                    if (wNAF_len[i] > max_len)\n                        max_len = wNAF_len[i];\n                    if (*tmp_points == NULL) {\n                        ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n                        OPENSSL_free(tmp_wNAF);\n                        goto err;\n                    }\n                    val_sub[i] = tmp_points;\n                    tmp_points += pre_points_per_block;\n                    pp += blocksize;\n                }\n                OPENSSL_free(tmp_wNAF);\n            }\n        }\n    }\n    val = OPENSSL_malloc((num_val + 1) * sizeof(val[0]));\n    if (val == NULL) {\n        ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    val[num_val] = NULL;\n    v = val;\n    for (i = 0; i < num + num_scalar; i++) {\n        val_sub[i] = v;\n        for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {\n            *v = EC_POINT_new(group);\n            if (*v == NULL)\n                goto err;\n            v++;\n        }\n    }\n    if (!(v == val + num_val)) {\n        ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n        goto err;\n    }\n    if ((tmp = EC_POINT_new(group)) == NULL)\n        goto err;\n    for (i = 0; i < num + num_scalar; i++) {\n        if (i < num) {\n            if (!EC_POINT_copy(val_sub[i][0], points[i]))\n                goto err;\n        } else {\n            if (!EC_POINT_copy(val_sub[i][0], generator))\n                goto err;\n        }\n        if (wsize[i] > 1) {\n            if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))\n                goto err;\n            for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {\n                if (!EC_POINT_add\n                    (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))\n                    goto err;\n            }\n        }\n    }\n    if (!EC_POINTs_make_affine(group, num_val, val, ctx))\n        goto err;\n    r_is_at_infinity = 1;\n    for (k = max_len - 1; k >= 0; k--) {\n        if (!r_is_at_infinity) {\n            if (!EC_POINT_dbl(group, r, r, ctx))\n                goto err;\n        }\n        for (i = 0; i < totalnum; i++) {\n            if (wNAF_len[i] > (size_t)k) {\n                int digit = wNAF[i][k];\n                int is_neg;\n                if (digit) {\n                    is_neg = digit < 0;\n                    if (is_neg)\n                        digit = -digit;\n                    if (is_neg != r_is_inverted) {\n                        if (!r_is_at_infinity) {\n                            if (!EC_POINT_invert(group, r, ctx))\n                                goto err;\n                        }\n                        r_is_inverted = !r_is_inverted;\n                    }\n                    if (r_is_at_infinity) {\n                        if (!EC_POINT_copy(r, val_sub[i][digit >> 1]))\n                            goto err;\n                        r_is_at_infinity = 0;\n                    } else {\n                        if (!EC_POINT_add\n                            (group, r, r, val_sub[i][digit >> 1], ctx))\n                            goto err;\n                    }\n                }\n            }\n        }\n    }\n    if (r_is_at_infinity) {\n        if (!EC_POINT_set_to_infinity(group, r))\n            goto err;\n    } else {\n        if (r_is_inverted)\n            if (!EC_POINT_invert(group, r, ctx))\n                goto err;\n    }\n    ret = 1;\n err:\n    BN_CTX_free(new_ctx);\n    EC_POINT_free(tmp);\n    OPENSSL_free(wsize);\n    OPENSSL_free(wNAF_len);\n    if (wNAF != NULL) {\n        signed char **w;\n        for (w = wNAF; *w != NULL; w++)\n            OPENSSL_free(*w);\n        OPENSSL_free(wNAF);\n    }\n    if (val != NULL) {\n        for (v = val; *v != NULL; v++)\n            EC_POINT_clear_free(*v);\n        OPENSSL_free(val);\n    }\n    OPENSSL_free(val_sub);\n    return ret;\n}']