static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
    BN_ULONG *A, *a = NULL;
    const BN_ULONG *B;
    int i;
    bn_check_top(b);
    if (words > (INT_MAX / (4 * BN_BITS2))) {
        BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
        return NULL;
    }
    if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
        BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
        return (NULL);
    }
    if (BN_get_flags(b, BN_FLG_SECURE))
        a = A = OPENSSL_secure_zalloc(words * sizeof(*a));
    else
        a = A = OPENSSL_zalloc(words * sizeof(*a));
    if (A == NULL) {
        BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
        return (NULL);
    }
#if 1
    B = b->d;
    if (B != NULL) {
        for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
            BN_ULONG a0, a1, a2, a3;
            a0 = B[0];
            a1 = B[1];
            a2 = B[2];
            a3 = B[3];
            A[0] = a0;
            A[1] = a1;
            A[2] = a2;
            A[3] = a3;
        }
        switch (b->top & 3) {
        case 3:
            A[2] = B[2];
        case 2:
            A[1] = B[1];
        case 1:
            A[0] = B[0];
        case 0:
            ;
        }
    }
#else
    memset(A, 0, sizeof(*A) * words);
    memcpy(A, b->d, sizeof(b->d[0]) * b->top);
#endif
    return (a);
}
['static int ec_GF2m_montgomery_point_multiply(const EC_GROUP *group,\n                                             EC_POINT *r,\n                                             const BIGNUM *scalar,\n                                             const EC_POINT *point,\n                                             BN_CTX *ctx)\n{\n    BIGNUM *x1, *x2, *z1, *z2;\n    int ret = 0, i, group_top;\n    BN_ULONG mask, word;\n    if (r == point) {\n        ECerr(EC_F_EC_GF2M_MONTGOMERY_POINT_MULTIPLY, EC_R_INVALID_ARGUMENT);\n        return 0;\n    }\n    if ((scalar == NULL) || BN_is_zero(scalar) || (point == NULL) ||\n        EC_POINT_is_at_infinity(group, point)) {\n        return EC_POINT_set_to_infinity(group, r);\n    }\n    if (!point->Z_is_one)\n        return 0;\n    BN_CTX_start(ctx);\n    x1 = BN_CTX_get(ctx);\n    z1 = BN_CTX_get(ctx);\n    if (z1 == NULL)\n        goto err;\n    x2 = r->X;\n    z2 = r->Y;\n    group_top = bn_get_top(group->field);\n    if (bn_wexpand(x1, group_top) == NULL\n        || bn_wexpand(z1, group_top) == NULL\n        || bn_wexpand(x2, group_top) == NULL\n        || bn_wexpand(z2, group_top) == NULL)\n        goto err;\n    if (!BN_GF2m_mod_arr(x1, point->X, group->poly))\n        goto err;\n    if (!BN_one(z1))\n        goto err;\n    if (!group->meth->field_sqr(group, z2, x1, ctx))\n        goto err;\n    if (!group->meth->field_sqr(group, x2, z2, ctx))\n        goto err;\n    if (!BN_GF2m_add(x2, x2, group->b))\n        goto err;\n    i = bn_get_top(scalar) - 1;\n    mask = BN_TBIT;\n    word = bn_get_words(scalar)[i];\n    while (!(word & mask))\n        mask >>= 1;\n    mask >>= 1;\n    if (!mask) {\n        i--;\n        mask = BN_TBIT;\n    }\n    for (; i >= 0; i--) {\n        word = bn_get_words(scalar)[i];\n        while (mask) {\n            BN_consttime_swap(word & mask, x1, x2, group_top);\n            BN_consttime_swap(word & mask, z1, z2, group_top);\n            if (!gf2m_Madd(group, point->X, x2, z2, x1, z1, ctx))\n                goto err;\n            if (!gf2m_Mdouble(group, x1, z1, ctx))\n                goto err;\n            BN_consttime_swap(word & mask, x1, x2, group_top);\n            BN_consttime_swap(word & mask, z1, z2, group_top);\n            mask >>= 1;\n        }\n        mask = BN_TBIT;\n    }\n    i = gf2m_Mxy(group, point->X, point->Y, x1, z1, x2, z2, ctx);\n    if (i == 0)\n        goto err;\n    else if (i == 1) {\n        if (!EC_POINT_set_to_infinity(group, r))\n            goto err;\n    } else {\n        if (!BN_one(r->Z))\n            goto err;\n        r->Z_is_one = 1;\n    }\n    BN_set_negative(r->X, 0);\n    BN_set_negative(r->Y, 0);\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    return ret;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n    return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'int BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n    int i;\n    const BIGNUM *at, *bt;\n    bn_check_top(a);\n    bn_check_top(b);\n    if (a->top < b->top) {\n        at = b;\n        bt = a;\n    } else {\n        at = a;\n        bt = b;\n    }\n    if (bn_wexpand(r, at->top) == NULL)\n        return 0;\n    for (i = 0; i < bt->top; i++) {\n        r->d[i] = at->d[i] ^ bt->d[i];\n    }\n    for (; i < at->top; i++) {\n        r->d[i] = at->d[i];\n    }\n    r->top = at->top;\n    bn_correct_top(r);\n    return 1;\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n    bn_check_top(b);\n    if (words > b->dmax) {\n        BN_ULONG *a = bn_expand_internal(b, words);\n        if (!a)\n            return NULL;\n        if (b->d) {\n            OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n            bn_free_d(b);\n        }\n        b->d = a;\n        b->dmax = words;\n    }\n    bn_check_top(b);\n    return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n    BN_ULONG *A, *a = NULL;\n    const BN_ULONG *B;\n    int i;\n    bn_check_top(b);\n    if (words > (INT_MAX / (4 * BN_BITS2))) {\n        BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n        return NULL;\n    }\n    if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n        BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n        return (NULL);\n    }\n    if (BN_get_flags(b, BN_FLG_SECURE))\n        a = A = OPENSSL_secure_zalloc(words * sizeof(*a));\n    else\n        a = A = OPENSSL_zalloc(words * sizeof(*a));\n    if (A == NULL) {\n        BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n        return (NULL);\n    }\n#if 1\n    B = b->d;\n    if (B != NULL) {\n        for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n            BN_ULONG a0, a1, a2, a3;\n            a0 = B[0];\n            a1 = B[1];\n            a2 = B[2];\n            a3 = B[3];\n            A[0] = a0;\n            A[1] = a1;\n            A[2] = a2;\n            A[3] = a3;\n        }\n        switch (b->top & 3) {\n        case 3:\n            A[2] = B[2];\n        case 2:\n            A[1] = B[1];\n        case 1:\n            A[0] = B[0];\n        case 0:\n            ;\n        }\n    }\n#else\n    memset(A, 0, sizeof(*A) * words);\n    memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n    return (a);\n}']