static int test_modexp_mont5()
{
    BIGNUM *a, *p, *m, *d, *e, *b, *n, *c;
    BN_MONT_CTX *mont;
    char *bigstring;
    a = BN_new();
    p = BN_new();
    m = BN_new();
    d = BN_new();
    e = BN_new();
    b = BN_new();
    n = BN_new();
    c = BN_new();
    mont = BN_MONT_CTX_new();
    BN_bntest_rand(m, 1024, 0, 1);
    BN_bntest_rand(a, 1024, 0, 0);
    BN_zero(p);
    if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
        return 0;
    if (!BN_is_one(d)) {
        printf("Modular exponentiation test failed!\n");
        return 0;
    }
    BN_hex2bn(&a,
        "7878787878787878787878787878787878787878787878787878787878787878"
        "7878787878787878787878787878787878787878787878787878787878787878"
        "7878787878787878787878787878787878787878787878787878787878787878"
        "7878787878787878787878787878787878787878787878787878787878787878");
    BN_hex2bn(&b,
        "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"
        "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"
        "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"
        "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81");
    BN_hex2bn(&n,
        "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"
        "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"
        "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"
        "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF");
    BN_MONT_CTX_set(mont, n, ctx);
    BN_mod_mul_montgomery(c, a, b, mont, ctx);
    BN_mod_mul_montgomery(d, b, a, mont, ctx);
    if (BN_cmp(c, d)) {
        fprintf(stderr, "Montgomery multiplication test failed:"
                        " a*b != b*a.\n");
        return 0;
    }
    bigstring = glue(bn1strings);
    BN_hex2bn(&n, bigstring);
    OPENSSL_free(bigstring);
    bigstring = glue(bn2strings);
    BN_hex2bn(&a, bigstring);
    OPENSSL_free(bigstring);
    BN_free(b);
    b = BN_dup(a);
    BN_MONT_CTX_set(mont, n, ctx);
    BN_mod_mul_montgomery(c, a, a, mont, ctx);
    BN_mod_mul_montgomery(d, a, b, mont, ctx);
    if (BN_cmp(c, d)) {
        fprintf(stderr, "Montgomery multiplication test failed:"
                        " a**2 != a*a.\n");
        return 0;
    }
    BN_bntest_rand(p, 1024, 0, 0);
    BN_zero(a);
    if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
        return 0;
    if (!BN_is_zero(d)) {
        fprintf(stderr, "Modular exponentiation test failed!\n");
        return 0;
    }
    BN_one(a);
    BN_MONT_CTX_set(mont, m, ctx);
    if (!BN_from_montgomery(e, a, mont, ctx))
        return 0;
    if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
        return 0;
    if (!BN_mod_exp_simple(a, e, p, m, ctx))
        return 0;
    if (BN_cmp(a, d) != 0) {
        printf("Modular exponentiation test failed!\n");
        return 0;
    }
    BN_bntest_rand(e, 1024, 0, 0);
    if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
        return 0;
    if (!BN_mod_exp_simple(a, e, p, m, ctx))
        return 0;
    if (BN_cmp(a, d) != 0) {
        printf("Modular exponentiation test failed!\n");
        return 0;
    }
    BN_MONT_CTX_free(mont);
    BN_free(a);
    BN_free(p);
    BN_free(m);
    BN_free(d);
    BN_free(e);
    BN_free(b);
    BN_free(n);
    BN_free(c);
    return 1;
}
['static int test_modexp_mont5()\n{\n    BIGNUM *a, *p, *m, *d, *e, *b, *n, *c;\n    BN_MONT_CTX *mont;\n    char *bigstring;\n    a = BN_new();\n    p = BN_new();\n    m = BN_new();\n    d = BN_new();\n    e = BN_new();\n    b = BN_new();\n    n = BN_new();\n    c = BN_new();\n    mont = BN_MONT_CTX_new();\n    BN_bntest_rand(m, 1024, 0, 1);\n    BN_bntest_rand(a, 1024, 0, 0);\n    BN_zero(p);\n    if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))\n        return 0;\n    if (!BN_is_one(d)) {\n        printf("Modular exponentiation test failed!\\n");\n        return 0;\n    }\n    BN_hex2bn(&a,\n        "7878787878787878787878787878787878787878787878787878787878787878"\n        "7878787878787878787878787878787878787878787878787878787878787878"\n        "7878787878787878787878787878787878787878787878787878787878787878"\n        "7878787878787878787878787878787878787878787878787878787878787878");\n    BN_hex2bn(&b,\n        "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"\n        "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"\n        "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"\n        "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81");\n    BN_hex2bn(&n,\n        "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"\n        "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"\n        "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"\n        "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF");\n    BN_MONT_CTX_set(mont, n, ctx);\n    BN_mod_mul_montgomery(c, a, b, mont, ctx);\n    BN_mod_mul_montgomery(d, b, a, mont, ctx);\n    if (BN_cmp(c, d)) {\n        fprintf(stderr, "Montgomery multiplication test failed:"\n                        " a*b != b*a.\\n");\n        return 0;\n    }\n    bigstring = glue(bn1strings);\n    BN_hex2bn(&n, bigstring);\n    OPENSSL_free(bigstring);\n    bigstring = glue(bn2strings);\n    BN_hex2bn(&a, bigstring);\n    OPENSSL_free(bigstring);\n    BN_free(b);\n    b = BN_dup(a);\n    BN_MONT_CTX_set(mont, n, ctx);\n    BN_mod_mul_montgomery(c, a, a, mont, ctx);\n    BN_mod_mul_montgomery(d, a, b, mont, ctx);\n    if (BN_cmp(c, d)) {\n        fprintf(stderr, "Montgomery multiplication test failed:"\n                        " a**2 != a*a.\\n");\n        return 0;\n    }\n    BN_bntest_rand(p, 1024, 0, 0);\n    BN_zero(a);\n    if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))\n        return 0;\n    if (!BN_is_zero(d)) {\n        fprintf(stderr, "Modular exponentiation test failed!\\n");\n        return 0;\n    }\n    BN_one(a);\n    BN_MONT_CTX_set(mont, m, ctx);\n    if (!BN_from_montgomery(e, a, mont, ctx))\n        return 0;\n    if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))\n        return 0;\n    if (!BN_mod_exp_simple(a, e, p, m, ctx))\n        return 0;\n    if (BN_cmp(a, d) != 0) {\n        printf("Modular exponentiation test failed!\\n");\n        return 0;\n    }\n    BN_bntest_rand(e, 1024, 0, 0);\n    if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))\n        return 0;\n    if (!BN_mod_exp_simple(a, e, p, m, ctx))\n        return 0;\n    if (BN_cmp(a, d) != 0) {\n        printf("Modular exponentiation test failed!\\n");\n        return 0;\n    }\n    BN_MONT_CTX_free(mont);\n    BN_free(a);\n    BN_free(p);\n    BN_free(m);\n    BN_free(d);\n    BN_free(e);\n    BN_free(b);\n    BN_free(n);\n    BN_free(c);\n    return 1;\n}', 'BN_MONT_CTX *BN_MONT_CTX_new(void)\n{\n    BN_MONT_CTX *ret;\n    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)\n        return (NULL);\n    BN_MONT_CTX_init(ret);\n    ret->flags = BN_FLG_MALLOCED;\n    return (ret);\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n    void *ret = NULL;\n    if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n        return malloc_impl(num, file, line);\n    if (num <= 0)\n        return NULL;\n    FAILTEST();\n    allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n    if (call_malloc_debug) {\n        CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n        ret = malloc(num);\n        CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n    } else {\n        ret = malloc(num);\n    }\n#else\n    osslargused(file); osslargused(line);\n    ret = malloc(num);\n#endif\n    return ret;\n}', 'void BN_MONT_CTX_init(BN_MONT_CTX *ctx)\n{\n    ctx->ri = 0;\n    bn_init(&(ctx->RR));\n    bn_init(&(ctx->N));\n    bn_init(&(ctx->Ni));\n    ctx->n0[0] = ctx->n0[1] = 0;\n    ctx->flags = 0;\n}', 'int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n    return bnrand(2, rnd, bits, top, bottom);\n}']