static unsigned int BN_STACK_pop(BN_STACK *st)
{
    return st->indexes[--(st->depth)];
}
['static int test_check_private_exponent(void)\n{\n    int ret = 0;\n    RSA *key = NULL;\n    BN_CTX *ctx = NULL;\n    BIGNUM *p = NULL, *q = NULL, *e = NULL, *d = NULL, *n = NULL;\n    ret = TEST_ptr(key = RSA_new())\n          && TEST_ptr(ctx = BN_CTX_new())\n          && TEST_ptr(p = BN_new())\n          && TEST_ptr(q = BN_new())\n          && TEST_ptr(e = BN_new())\n          && TEST_ptr(d = BN_new())\n          && TEST_ptr(n = BN_new())\n          && TEST_true(BN_set_word(p, 15))\n          && TEST_true(BN_set_word(q, 17))\n          && TEST_true(BN_set_word(e, 5))\n          && TEST_true(BN_set_word(d, 157))\n          && TEST_true(BN_set_word(n, 15*17))\n          && TEST_true(RSA_set0_factors(key, p, q))\n          && TEST_true(RSA_set0_key(key, n, e, d))\n          && TEST_false(rsa_check_private_exponent(key, 8, ctx))\n          && TEST_true(BN_set_word(d, 45))\n          && TEST_true(rsa_check_private_exponent(key, 8, ctx))\n          && TEST_false(rsa_check_private_exponent(key, 16, ctx))\n          && TEST_true(BN_set_word(d, 16))\n          && TEST_false(rsa_check_private_exponent(key, 8, ctx))\n          && TEST_true(BN_set_word(d, 46))\n          && TEST_false(rsa_check_private_exponent(key, 8, ctx));\n    RSA_free(key);\n    BN_CTX_free(ctx);\n    return ret;\n}', 'int rsa_check_private_exponent(const RSA *rsa, int nbits, BN_CTX *ctx)\n{\n    int ret;\n    BIGNUM *r, *p1, *q1, *lcm, *p1q1, *gcd;\n    if (BN_num_bits(rsa->d) <= (nbits >> 1))\n        return 0;\n    BN_CTX_start(ctx);\n    r = BN_CTX_get(ctx);\n    p1 = BN_CTX_get(ctx);\n    q1 = BN_CTX_get(ctx);\n    lcm = BN_CTX_get(ctx);\n    p1q1 = BN_CTX_get(ctx);\n    gcd = BN_CTX_get(ctx);\n    ret = (gcd != NULL\n          && (rsa_get_lcm(ctx, rsa->p, rsa->q, lcm, gcd, p1, q1, p1q1) == 1)\n          && (BN_cmp(rsa->d, lcm) < 0)\n          && BN_mod_mul(r, rsa->e, rsa->d, lcm, ctx)\n          && BN_is_one(r));\n    BN_clear(p1);\n    BN_clear(q1);\n    BN_clear(lcm);\n    BN_clear(gcd);\n    BN_CTX_end(ctx);\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 rsa_get_lcm(BN_CTX *ctx, const BIGNUM *p, const BIGNUM *q,\n                BIGNUM *lcm, BIGNUM *gcd, BIGNUM *p1, BIGNUM *q1,\n                BIGNUM *p1q1)\n{\n    return BN_sub(p1, p, BN_value_one())\n           && BN_sub(q1, q, BN_value_one())\n           && BN_mul(p1q1, p1, q1, ctx)\n           && BN_gcd(gcd, p1, q1, ctx)\n           && BN_div(lcm, NULL, p1q1, gcd, ctx);\n}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n               BN_CTX *ctx)\n{\n    BIGNUM *t;\n    int ret = 0;\n    bn_check_top(a);\n    bn_check_top(b);\n    bn_check_top(m);\n    BN_CTX_start(ctx);\n    if ((t = BN_CTX_get(ctx)) == NULL)\n        goto err;\n    if (a == b) {\n        if (!BN_sqr(t, a, ctx))\n            goto err;\n    } else {\n        if (!BN_mul(t, a, b, ctx))\n            goto err;\n    }\n    if (!BN_nnmod(r, t, m, ctx))\n        goto err;\n    bn_check_top(r);\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    return ret;\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}']