static unsigned int BN_STACK_pop(BN_STACK *st)
{
    return st->indexes[--(st->depth)];
}
['int BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,\n                     const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m,\n                     BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n    int i, j, bits, b, bits1, bits2, ret =\n        0, wpos1, wpos2, window1, window2, wvalue1, wvalue2;\n    int r_is_one = 1;\n    BIGNUM *d, *r;\n    const BIGNUM *a_mod_m;\n    BIGNUM *val1[TABLE_SIZE], *val2[TABLE_SIZE];\n    BN_MONT_CTX *mont = NULL;\n    bn_check_top(a1);\n    bn_check_top(p1);\n    bn_check_top(a2);\n    bn_check_top(p2);\n    bn_check_top(m);\n    if (!(m->d[0] & 1)) {\n        BNerr(BN_F_BN_MOD_EXP2_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n        return 0;\n    }\n    bits1 = BN_num_bits(p1);\n    bits2 = BN_num_bits(p2);\n    if ((bits1 == 0) && (bits2 == 0)) {\n        ret = BN_one(rr);\n        return ret;\n    }\n    bits = (bits1 > bits2) ? bits1 : bits2;\n    BN_CTX_start(ctx);\n    d = BN_CTX_get(ctx);\n    r = BN_CTX_get(ctx);\n    val1[0] = BN_CTX_get(ctx);\n    val2[0] = BN_CTX_get(ctx);\n    if (val2[0] == NULL)\n        goto err;\n    if (in_mont != NULL)\n        mont = in_mont;\n    else {\n        if ((mont = BN_MONT_CTX_new()) == NULL)\n            goto err;\n        if (!BN_MONT_CTX_set(mont, m, ctx))\n            goto err;\n    }\n    window1 = BN_window_bits_for_exponent_size(bits1);\n    window2 = BN_window_bits_for_exponent_size(bits2);\n    if (a1->neg || BN_ucmp(a1, m) >= 0) {\n        if (!BN_mod(val1[0], a1, m, ctx))\n            goto err;\n        a_mod_m = val1[0];\n    } else\n        a_mod_m = a1;\n    if (BN_is_zero(a_mod_m)) {\n        BN_zero(rr);\n        ret = 1;\n        goto err;\n    }\n    if (!BN_to_montgomery(val1[0], a_mod_m, mont, ctx))\n        goto err;\n    if (window1 > 1) {\n        if (!BN_mod_mul_montgomery(d, val1[0], val1[0], mont, ctx))\n            goto err;\n        j = 1 << (window1 - 1);\n        for (i = 1; i < j; i++) {\n            if (((val1[i] = BN_CTX_get(ctx)) == NULL) ||\n                !BN_mod_mul_montgomery(val1[i], val1[i - 1], d, mont, ctx))\n                goto err;\n        }\n    }\n    if (a2->neg || BN_ucmp(a2, m) >= 0) {\n        if (!BN_mod(val2[0], a2, m, ctx))\n            goto err;\n        a_mod_m = val2[0];\n    } else\n        a_mod_m = a2;\n    if (BN_is_zero(a_mod_m)) {\n        BN_zero(rr);\n        ret = 1;\n        goto err;\n    }\n    if (!BN_to_montgomery(val2[0], a_mod_m, mont, ctx))\n        goto err;\n    if (window2 > 1) {\n        if (!BN_mod_mul_montgomery(d, val2[0], val2[0], mont, ctx))\n            goto err;\n        j = 1 << (window2 - 1);\n        for (i = 1; i < j; i++) {\n            if (((val2[i] = BN_CTX_get(ctx)) == NULL) ||\n                !BN_mod_mul_montgomery(val2[i], val2[i - 1], d, mont, ctx))\n                goto err;\n        }\n    }\n    r_is_one = 1;\n    wvalue1 = 0;\n    wvalue2 = 0;\n    wpos1 = 0;\n    wpos2 = 0;\n    if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))\n        goto err;\n    for (b = bits - 1; b >= 0; b--) {\n        if (!r_is_one) {\n            if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n                goto err;\n        }\n        if (!wvalue1)\n            if (BN_is_bit_set(p1, b)) {\n                i = b - window1 + 1;\n                while (!BN_is_bit_set(p1, i))\n                    i++;\n                wpos1 = i;\n                wvalue1 = 1;\n                for (i = b - 1; i >= wpos1; i--) {\n                    wvalue1 <<= 1;\n                    if (BN_is_bit_set(p1, i))\n                        wvalue1++;\n                }\n            }\n        if (!wvalue2)\n            if (BN_is_bit_set(p2, b)) {\n                i = b - window2 + 1;\n                while (!BN_is_bit_set(p2, i))\n                    i++;\n                wpos2 = i;\n                wvalue2 = 1;\n                for (i = b - 1; i >= wpos2; i--) {\n                    wvalue2 <<= 1;\n                    if (BN_is_bit_set(p2, i))\n                        wvalue2++;\n                }\n            }\n        if (wvalue1 && b == wpos1) {\n            if (!BN_mod_mul_montgomery(r, r, val1[wvalue1 >> 1], mont, ctx))\n                goto err;\n            wvalue1 = 0;\n            r_is_one = 0;\n        }\n        if (wvalue2 && b == wpos2) {\n            if (!BN_mod_mul_montgomery(r, r, val2[wvalue2 >> 1], mont, ctx))\n                goto err;\n            wvalue2 = 0;\n            r_is_one = 0;\n        }\n    }\n    if (!BN_from_montgomery(rr, r, mont, ctx))\n        goto err;\n    ret = 1;\n err:\n    if (in_mont == NULL)\n        BN_MONT_CTX_free(mont);\n    BN_CTX_end(ctx);\n    bn_check_top(rr);\n    return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n    CTXDBG("ENTER BN_CTX_start()", ctx);\n    if (ctx->err_stack || ctx->too_many)\n        ctx->err_stack++;\n    else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n        BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n        ctx->err_stack++;\n    }\n    CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n    BIGNUM *ret;\n    CTXDBG("ENTER BN_CTX_get()", ctx);\n    if (ctx->err_stack || ctx->too_many)\n        return NULL;\n    if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n        ctx->too_many = 1;\n        BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n        return NULL;\n    }\n    BN_zero(ret);\n    ret->flags &= (~BN_FLG_CONSTTIME);\n    ctx->used++;\n    CTXDBG("LEAVE BN_CTX_get()", ctx);\n    return ret;\n}', 'int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n                     BN_CTX *ctx)\n{\n    return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);\n}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n                          BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n    int ret = bn_mul_mont_fixed_top(r, a, b, mont, ctx);\n    bn_correct_top(r);\n    bn_check_top(r);\n    return ret;\n}', 'int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n                          BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n    BIGNUM *tmp;\n    int ret = 0;\n    int num = mont->N.top;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n    if (num > 1 && a->top == num && b->top == num) {\n        if (bn_wexpand(r, num) == NULL)\n            return 0;\n        if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n            r->neg = a->neg ^ b->neg;\n            r->top = num;\n            r->flags |= BN_FLG_FIXED_TOP;\n            return 1;\n        }\n    }\n#endif\n    if ((a->top + b->top) > 2 * num)\n        return 0;\n    BN_CTX_start(ctx);\n    tmp = BN_CTX_get(ctx);\n    if (tmp == NULL)\n        goto err;\n    bn_check_top(tmp);\n    if (a == b) {\n        if (!bn_sqr_fixed_top(tmp, a, ctx))\n            goto err;\n    } else {\n        if (!bn_mul_fixed_top(tmp, a, b, ctx))\n            goto err;\n    }\n#ifdef MONT_WORD\n    if (!bn_from_montgomery_word(r, tmp, mont))\n        goto err;\n#else\n    if (!BN_from_montgomery(r, tmp, mont, ctx))\n        goto err;\n#endif\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    return ret;\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_div_fixed_top(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n                     const BIGNUM *divisor, BN_CTX *ctx)\n{\n    int norm_shift, i, j, loop;\n    BIGNUM *tmp, *snum, *sdiv, *res;\n    BN_ULONG *resp, *wnum, *wnumtop;\n    BN_ULONG d0, d1;\n    int num_n, div_n;\n    assert(divisor->top > 0 && divisor->d[divisor->top - 1] != 0);\n    bn_check_top(num);\n    bn_check_top(divisor);\n    bn_check_top(dv);\n    bn_check_top(rm);\n    BN_CTX_start(ctx);\n    res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n    tmp = BN_CTX_get(ctx);\n    snum = BN_CTX_get(ctx);\n    sdiv = BN_CTX_get(ctx);\n    if (sdiv == NULL)\n        goto err;\n    if (!BN_copy(sdiv, divisor))\n        goto err;\n    norm_shift = bn_left_align(sdiv);\n    sdiv->neg = 0;\n    if (!(bn_lshift_fixed_top(snum, num, norm_shift)))\n        goto err;\n    div_n = sdiv->top;\n    num_n = snum->top;\n    if (num_n <= div_n) {\n        if (bn_wexpand(snum, div_n + 1) == NULL)\n            goto err;\n        memset(&(snum->d[num_n]), 0, (div_n - num_n + 1) * sizeof(BN_ULONG));\n        snum->top = num_n = div_n + 1;\n    }\n    loop = num_n - div_n;\n    wnum = &(snum->d[loop]);\n    wnumtop = &(snum->d[num_n - 1]);\n    d0 = sdiv->d[div_n - 1];\n    d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n    if (!bn_wexpand(res, loop))\n        goto err;\n    res->neg = (num->neg ^ divisor->neg);\n    res->top = loop;\n    res->flags |= BN_FLG_FIXED_TOP;\n    resp = &(res->d[loop]);\n    if (!bn_wexpand(tmp, (div_n + 1)))\n        goto err;\n    for (i = 0; i < loop; i++, wnumtop--) {\n        BN_ULONG q, l0;\n# if defined(BN_DIV3W)\n        q = bn_div_3_words(wnumtop, d1, d0);\n# else\n        BN_ULONG n0, n1, rem = 0;\n        n0 = wnumtop[0];\n        n1 = wnumtop[-1];\n        if (n0 == d0)\n            q = BN_MASK2;\n        else {\n            BN_ULONG n2 = (wnumtop == wnum) ? 0 : wnumtop[-2];\n#  ifdef BN_LLONG\n            BN_ULLONG t2;\n#   if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n            q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n#   else\n            q = bn_div_words(n0, n1, d0);\n#   endif\n#   ifndef REMAINDER_IS_ALREADY_CALCULATED\n            rem = (n1 - q * d0) & BN_MASK2;\n#   endif\n            t2 = (BN_ULLONG) d1 *q;\n            for (;;) {\n                if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | n2))\n                    break;\n                q--;\n                rem += d0;\n                if (rem < d0)\n                    break;\n                t2 -= d1;\n            }\n#  else\n            BN_ULONG t2l, t2h;\n            q = bn_div_words(n0, n1, d0);\n#   ifndef REMAINDER_IS_ALREADY_CALCULATED\n            rem = (n1 - q * d0) & BN_MASK2;\n#   endif\n#   if defined(BN_UMULT_LOHI)\n            BN_UMULT_LOHI(t2l, t2h, d1, q);\n#   elif defined(BN_UMULT_HIGH)\n            t2l = d1 * q;\n            t2h = BN_UMULT_HIGH(d1, q);\n#   else\n            {\n                BN_ULONG ql, qh;\n                t2l = LBITS(d1);\n                t2h = HBITS(d1);\n                ql = LBITS(q);\n                qh = HBITS(q);\n                mul64(t2l, t2h, ql, qh);\n            }\n#   endif\n            for (;;) {\n                if ((t2h < rem) || ((t2h == rem) && (t2l <= n2)))\n                    break;\n                q--;\n                rem += d0;\n                if (rem < d0)\n                    break;\n                if (t2l < d1)\n                    t2h--;\n                t2l -= d1;\n            }\n#  endif\n        }\n# endif\n        l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n        tmp->d[div_n] = l0;\n        wnum--;\n        l0 = bn_sub_words(wnum, wnum, tmp->d, div_n + 1);\n        q -= l0;\n        for (l0 = 0 - l0, j = 0; j < div_n; j++)\n            tmp->d[j] = sdiv->d[j] & l0;\n        l0 = bn_add_words(wnum, wnum, tmp->d, div_n);\n        (*wnumtop) += l0;\n        assert((*wnumtop) == 0);\n        *--resp = q;\n    }\n    snum->neg = num->neg;\n    snum->top = div_n;\n    snum->flags |= BN_FLG_FIXED_TOP;\n    if (rm != NULL)\n        bn_rshift_fixed_top(rm, snum, norm_shift);\n    BN_CTX_end(ctx);\n    return 1;\n err:\n    bn_check_top(rm);\n    BN_CTX_end(ctx);\n    return 0;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n    CTXDBG("ENTER BN_CTX_end()", ctx);\n    if (ctx->err_stack)\n        ctx->err_stack--;\n    else {\n        unsigned int fp = BN_STACK_pop(&ctx->stack);\n        if (fp < ctx->used)\n            BN_POOL_release(&ctx->pool, ctx->used - fp);\n        ctx->used = fp;\n        ctx->too_many = 0;\n    }\n    CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n    return st->indexes[--(st->depth)];\n}']