int BN_set_word(BIGNUM *a, BN_ULONG w)
{
    bn_check_top(a);
    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
        return 0;
    a->neg = 0;
    a->d[0] = w;
    a->top = (w ? 1 : 0);
    a->flags &= ~BN_FLG_FIXED_TOP;
    bn_check_top(a);
    return 1;
}
['static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)\n{\n    BIGNUM *r1, *m1, *vrfy, *r2, *m[RSA_MAX_PRIME_NUM - 2];\n    int ret = 0, i, ex_primes = 0, smooth = 0;\n    RSA_PRIME_INFO *pinfo;\n    BN_CTX_start(ctx);\n    r1 = BN_CTX_get(ctx);\n    r2 = BN_CTX_get(ctx);\n    m1 = BN_CTX_get(ctx);\n    vrfy = BN_CTX_get(ctx);\n    if (vrfy == NULL)\n        goto err;\n    if (rsa->version == RSA_ASN1_VERSION_MULTI\n        && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0\n             || ex_primes > RSA_MAX_PRIME_NUM - 2))\n        goto err;\n    if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {\n        BIGNUM *factor = BN_new();\n        if (factor == NULL)\n            goto err;\n        if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),\n              BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,\n                                     factor, ctx))\n            || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),\n                 BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,\n                                        factor, ctx))) {\n            BN_free(factor);\n            goto err;\n        }\n        for (i = 0; i < ex_primes; i++) {\n            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n            BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME);\n            if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) {\n                BN_free(factor);\n                goto err;\n            }\n        }\n        BN_free(factor);\n        smooth = (ex_primes == 0)\n                 && (rsa->meth->bn_mod_exp == BN_mod_exp_mont)\n                 && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));\n    }\n    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)\n        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,\n                                    rsa->n, ctx))\n            goto err;\n    if (smooth) {\n        if (\n            !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)\n            || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)\n            || !BN_mod_exp_mont_consttime(m1, m1, rsa->dmq1, rsa->q, ctx,\n                                          rsa->_method_mod_q)\n            || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)\n            || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)\n            || !BN_mod_exp_mont_consttime(r1, r1, rsa->dmp1, rsa->p, ctx,\n                                          rsa->_method_mod_p)\n            || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)\n            || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)\n            || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,\n                                      ctx)\n            || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)\n            || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))\n            goto err;\n        goto tail;\n    }\n    {\n        BIGNUM *c = BN_new();\n        if (c == NULL)\n            goto err;\n        BN_with_flags(c, I, BN_FLG_CONSTTIME);\n        if (!BN_mod(r1, c, rsa->q, ctx)) {\n            BN_free(c);\n            goto err;\n        }\n        {\n            BIGNUM *dmq1 = BN_new();\n            if (dmq1 == NULL) {\n                BN_free(c);\n                goto err;\n            }\n            BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);\n            if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,\n                                       rsa->_method_mod_q)) {\n                BN_free(c);\n                BN_free(dmq1);\n                goto err;\n            }\n            BN_free(dmq1);\n        }\n        if (!BN_mod(r1, c, rsa->p, ctx)) {\n            BN_free(c);\n            goto err;\n        }\n        BN_free(c);\n    }\n    {\n        BIGNUM *dmp1 = BN_new();\n        if (dmp1 == NULL)\n            goto err;\n        BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);\n        if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,\n                                   rsa->_method_mod_p)) {\n            BN_free(dmp1);\n            goto err;\n        }\n        BN_free(dmp1);\n    }\n    if (ex_primes > 0) {\n        BIGNUM *di = BN_new(), *cc = BN_new();\n        if (cc == NULL || di == NULL) {\n            BN_free(cc);\n            BN_free(di);\n            goto err;\n        }\n        for (i = 0; i < ex_primes; i++) {\n            if ((m[i] = BN_CTX_get(ctx)) == NULL) {\n                BN_free(cc);\n                BN_free(di);\n                goto err;\n            }\n            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n            BN_with_flags(cc, I, BN_FLG_CONSTTIME);\n            BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);\n            if (!BN_mod(r1, cc, pinfo->r, ctx)) {\n                BN_free(cc);\n                BN_free(di);\n                goto err;\n            }\n            if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {\n                BN_free(cc);\n                BN_free(di);\n                goto err;\n            }\n        }\n        BN_free(cc);\n        BN_free(di);\n    }\n    if (!BN_sub(r0, r0, m1))\n        goto err;\n    if (BN_is_negative(r0))\n        if (!BN_add(r0, r0, rsa->p))\n            goto err;\n    if (!BN_mul(r1, r0, rsa->iqmp, ctx))\n        goto err;\n    {\n        BIGNUM *pr1 = BN_new();\n        if (pr1 == NULL)\n            goto err;\n        BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);\n        if (!BN_mod(r0, pr1, rsa->p, ctx)) {\n            BN_free(pr1);\n            goto err;\n        }\n        BN_free(pr1);\n    }\n    if (BN_is_negative(r0))\n        if (!BN_add(r0, r0, rsa->p))\n            goto err;\n    if (!BN_mul(r1, r0, rsa->q, ctx))\n        goto err;\n    if (!BN_add(r0, r1, m1))\n        goto err;\n    if (ex_primes > 0) {\n        BIGNUM *pr2 = BN_new();\n        if (pr2 == NULL)\n            goto err;\n        for (i = 0; i < ex_primes; i++) {\n            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n            if (!BN_sub(r1, m[i], r0)) {\n                BN_free(pr2);\n                goto err;\n            }\n            if (!BN_mul(r2, r1, pinfo->t, ctx)) {\n                BN_free(pr2);\n                goto err;\n            }\n            BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);\n            if (!BN_mod(r1, pr2, pinfo->r, ctx)) {\n                BN_free(pr2);\n                goto err;\n            }\n            if (BN_is_negative(r1))\n                if (!BN_add(r1, r1, pinfo->r)) {\n                    BN_free(pr2);\n                    goto err;\n                }\n            if (!BN_mul(r1, r1, pinfo->pp, ctx)) {\n                BN_free(pr2);\n                goto err;\n            }\n            if (!BN_add(r0, r0, r1)) {\n                BN_free(pr2);\n                goto err;\n            }\n        }\n        BN_free(pr2);\n    }\n tail:\n    if (rsa->e && rsa->n) {\n        if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {\n            if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,\n                                 rsa->_method_mod_n))\n                goto err;\n        } else {\n            bn_correct_top(r0);\n            if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,\n                                       rsa->_method_mod_n))\n                goto err;\n        }\n        if (!BN_sub(vrfy, vrfy, I))\n            goto err;\n        if (BN_is_zero(vrfy)) {\n            bn_correct_top(r0);\n            ret = 1;\n            goto err;\n        }\n        if (!BN_mod(vrfy, vrfy, rsa->n, ctx))\n            goto err;\n        if (BN_is_negative(vrfy))\n            if (!BN_add(vrfy, vrfy, rsa->n))\n                goto err;\n        if (!BN_is_zero(vrfy)) {\n            BIGNUM *d = BN_new();\n            if (d == NULL)\n                goto err;\n            BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);\n            if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,\n                                       rsa->_method_mod_n)) {\n                BN_free(d);\n                goto err;\n            }\n            BN_free(d);\n        }\n    }\n    bn_correct_top(r0);\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    return ret;\n}', 'int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n    int ret, r_neg, cmp_res;\n    bn_check_top(a);\n    bn_check_top(b);\n    if (a->neg != b->neg) {\n        r_neg = a->neg;\n        ret = BN_uadd(r, a, b);\n    } else {\n        cmp_res = BN_ucmp(a, b);\n        if (cmp_res > 0) {\n            r_neg = a->neg;\n            ret = BN_usub(r, a, b);\n        } else if (cmp_res < 0) {\n            r_neg = !b->neg;\n            ret = BN_usub(r, b, a);\n        } else {\n            r_neg = 0;\n            BN_zero(r);\n            ret = 1;\n        }\n    }\n    r->neg = r_neg;\n    bn_check_top(r);\n    return ret;\n}', 'int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n    int max, min, dif;\n    BN_ULONG t1, t2, borrow, *rp;\n    const BN_ULONG *ap, *bp;\n    bn_check_top(a);\n    bn_check_top(b);\n    max = a->top;\n    min = b->top;\n    dif = max - min;\n    if (dif < 0) {\n        BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3);\n        return 0;\n    }\n    if (bn_wexpand(r, max) == NULL)\n        return 0;\n    ap = a->d;\n    bp = b->d;\n    rp = r->d;\n    borrow = bn_sub_words(rp, ap, bp, min);\n    ap += min;\n    rp += min;\n    while (dif) {\n        dif--;\n        t1 = *(ap++);\n        t2 = (t1 - borrow) & BN_MASK2;\n        *(rp++) = t2;\n        borrow &= (t1 == 0);\n    }\n    while (max && *--rp == 0)\n        max--;\n    r->top = max;\n    r->neg = 0;\n    bn_pollute(r);\n    return 1;\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n           BN_CTX *ctx)\n{\n    int ret;\n    if (BN_is_zero(divisor)) {\n        BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n        return 0;\n    }\n    if (divisor->d[divisor->top - 1] == 0) {\n        BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n        return 0;\n    }\n    ret = bn_div_fixed_top(dv, rm, num, divisor, ctx);\n    if (ret) {\n        if (dv != NULL)\n            bn_correct_top(dv);\n        if (rm != NULL)\n            bn_correct_top(rm);\n    }\n    return ret;\n}', 'int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n    int ret, r_neg, cmp_res;\n    bn_check_top(a);\n    bn_check_top(b);\n    if (a->neg == b->neg) {\n        r_neg = a->neg;\n        ret = BN_uadd(r, a, b);\n    } else {\n        cmp_res = BN_ucmp(a, b);\n        if (cmp_res > 0) {\n            r_neg = a->neg;\n            ret = BN_usub(r, a, b);\n        } else if (cmp_res < 0) {\n            r_neg = b->neg;\n            ret = BN_usub(r, b, a);\n        } else {\n            r_neg = 0;\n            BN_zero(r);\n            ret = 1;\n        }\n    }\n    r->neg = r_neg;\n    bn_check_top(r);\n    return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n    bn_check_top(a);\n    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n        return 0;\n    a->neg = 0;\n    a->d[0] = w;\n    a->top = (w ? 1 : 0);\n    a->flags &= ~BN_FLG_FIXED_TOP;\n    bn_check_top(a);\n    return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n    if (bits > (INT_MAX - BN_BITS2 + 1))\n        return NULL;\n    if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n        return a;\n    return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}']