BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
{
    bn_check_top(b);
    if (a == b)
        return a;
    if (bn_wexpand(a, b->top) == NULL)
        return NULL;
    if (b->top > 0)
        memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
    a->top = b->top;
    a->neg = b->neg;
    bn_check_top(a);
    return a;
}
['static int run_srp_kat(void)\n{\n    int ret = 0;\n    BIGNUM *s = NULL;\n    BIGNUM *v = NULL;\n    BIGNUM *a = NULL;\n    BIGNUM *b = NULL;\n    BIGNUM *u = NULL;\n    BIGNUM *x = NULL;\n    BIGNUM *Apub = NULL;\n    BIGNUM *Bpub = NULL;\n    BIGNUM *Kclient = NULL;\n    BIGNUM *Kserver = NULL;\n    const SRP_gN *GN;\n    if (!TEST_ptr(GN = SRP_get_default_gN("1024")))\n        goto err;\n    BN_hex2bn(&s, "BEB25379D1A8581EB5A727673A2441EE");\n    if (!TEST_true(SRP_create_verifier_BN("alice", "password123", &s, &v, GN->N,\n                                          GN->g)))\n        goto err;\n    TEST_info("checking v");\n    if (!TEST_true(check_bn("v", v,\n                 "7E273DE8696FFC4F4E337D05B4B375BEB0DDE1569E8FA00A9886D812"\n                 "9BADA1F1822223CA1A605B530E379BA4729FDC59F105B4787E5186F5"\n                 "C671085A1447B52A48CF1970B4FB6F8400BBF4CEBFBB168152E08AB5"\n                 "EA53D15C1AFF87B2B9DA6E04E058AD51CC72BFC9033B564E26480D78"\n                 "E955A5E29E7AB245DB2BE315E2099AFB")))\n        goto err;\n    TEST_note("    okay");\n    BN_hex2bn(&b, "E487CB59D31AC550471E81F00F6928E01DDA08E974A004F49E61F5D1"\n                  "05284D20");\n    Bpub = SRP_Calc_B(b, GN->N, GN->g, v);\n    if (!TEST_true(SRP_Verify_B_mod_N(Bpub, GN->N)))\n        goto err;\n    TEST_info("checking B");\n    if (!TEST_true(check_bn("B", Bpub,\n                  "BD0C61512C692C0CB6D041FA01BB152D4916A1E77AF46AE105393011"\n                  "BAF38964DC46A0670DD125B95A981652236F99D9B681CBF87837EC99"\n                  "6C6DA04453728610D0C6DDB58B318885D7D82C7F8DEB75CE7BD4FBAA"\n                  "37089E6F9C6059F388838E7A00030B331EB76840910440B1B27AAEAE"\n                  "EB4012B7D7665238A8E3FB004B117B58")))\n        goto err;\n    TEST_note("    okay");\n    BN_hex2bn(&a, "60975527035CF2AD1989806F0407210BC81EDC04E2762A56AFD529DD"\n                  "DA2D4393");\n    Apub = SRP_Calc_A(a, GN->N, GN->g);\n    if (!TEST_true(SRP_Verify_A_mod_N(Apub, GN->N)))\n        goto err;\n    TEST_info("checking A");\n    if (!TEST_true(check_bn("A", Apub,\n                  "61D5E490F6F1B79547B0704C436F523DD0E560F0C64115BB72557EC4"\n                  "4352E8903211C04692272D8B2D1A5358A2CF1B6E0BFCF99F921530EC"\n                  "8E39356179EAE45E42BA92AEACED825171E1E8B9AF6D9C03E1327F44"\n                  "BE087EF06530E69F66615261EEF54073CA11CF5858F0EDFDFE15EFEA"\n                  "B349EF5D76988A3672FAC47B0769447B")))\n        goto err;\n    TEST_note("    okay");\n    u = SRP_Calc_u(Apub, Bpub, GN->N);\n    if (!TEST_true(check_bn("u", u,\n                    "CE38B9593487DA98554ED47D70A7AE5F462EF019")))\n        goto err;\n    x = SRP_Calc_x(s, "alice", "password123");\n    Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u);\n    TEST_info("checking client\'s key");\n    if (!TEST_true(check_bn("Client\'s key", Kclient,\n                  "B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D"\n                  "233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C"\n                  "41BB59B6D5979B5C00A172B4A2A5903A0BDCAF8A709585EB2AFAFA8F"\n                  "3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D"\n                  "C346D7E474B29EDE8A469FFECA686E5A")))\n        goto err;\n    TEST_note("    okay");\n    Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N);\n    TEST_info("checking server\'s key");\n    if (!TEST_true(check_bn("Server\'s key", Kserver,\n                  "B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D"\n                  "233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C"\n                  "41BB59B6D5979B5C00A172B4A2A5903A0BDCAF8A709585EB2AFAFA8F"\n                  "3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D"\n                  "C346D7E474B29EDE8A469FFECA686E5A")))\n        goto err;\n    TEST_note("    okay");\n    ret = 1;\nerr:\n    BN_clear_free(Kclient);\n    BN_clear_free(Kserver);\n    BN_clear_free(x);\n    BN_free(u);\n    BN_free(Apub);\n    BN_clear_free(a);\n    BN_free(Bpub);\n    BN_clear_free(b);\n    BN_free(s);\n    BN_clear_free(v);\n    return ret;\n}', 'int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,\n                           BIGNUM **verifier, const BIGNUM *N,\n                           const BIGNUM *g)\n{\n    int result = 0;\n    BIGNUM *x = NULL;\n    BN_CTX *bn_ctx = BN_CTX_new();\n    unsigned char tmp2[MAX_LEN];\n    BIGNUM *salttmp = NULL;\n    if ((user == NULL) ||\n        (pass == NULL) ||\n        (salt == NULL) ||\n        (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))\n        goto err;\n    if (*salt == NULL) {\n        if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)\n            goto err;\n        salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);\n    } else {\n        salttmp = *salt;\n    }\n    x = SRP_Calc_x(salttmp, user, pass);\n    *verifier = BN_new();\n    if (*verifier == NULL)\n        goto err;\n    if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {\n        BN_clear_free(*verifier);\n        goto err;\n    }\n    result = 1;\n    *salt = salttmp;\n err:\n    if (salt != NULL && *salt != salttmp)\n        BN_clear_free(salttmp);\n    BN_clear_free(x);\n    BN_CTX_free(bn_ctx);\n    return result;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n               BN_CTX *ctx)\n{\n    int ret;\n    bn_check_top(a);\n    bn_check_top(p);\n    bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n    if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n        if (a->top == 1 && !a->neg\n            && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)) {\n            BN_ULONG A = a->d[0];\n            ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n        } else\n# endif\n            ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n    } else\n#endif\n#ifdef RECP_MUL_MOD\n    {\n        ret = BN_mod_exp_recp(r, a, p, m, ctx);\n    }\n#else\n    {\n        ret = BN_mod_exp_simple(r, a, p, m, ctx);\n    }\n#endif\n    bn_check_top(r);\n    return (ret);\n}', 'int BN_is_odd(const BIGNUM *a)\n{\n    return (a->top > 0) && (a->d[0] & 1);\n}', 'int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n                    const BIGNUM *m, BN_CTX *ctx)\n{\n    int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n    int start = 1;\n    BIGNUM *aa;\n    BIGNUM *val[TABLE_SIZE];\n    BN_RECP_CTX recp;\n    if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {\n        BNerr(BN_F_BN_MOD_EXP_RECP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n        return 0;\n    }\n    bits = BN_num_bits(p);\n    if (bits == 0) {\n        if (BN_is_one(m)) {\n            ret = 1;\n            BN_zero(r);\n        } else {\n            ret = BN_one(r);\n        }\n        return ret;\n    }\n    BN_CTX_start(ctx);\n    aa = BN_CTX_get(ctx);\n    val[0] = BN_CTX_get(ctx);\n    if (!aa || !val[0])\n        goto err;\n    BN_RECP_CTX_init(&recp);\n    if (m->neg) {\n        if (!BN_copy(aa, m))\n            goto err;\n        aa->neg = 0;\n        if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0)\n            goto err;\n    } else {\n        if (BN_RECP_CTX_set(&recp, m, ctx) <= 0)\n            goto err;\n    }\n    if (!BN_nnmod(val[0], a, m, ctx))\n        goto err;\n    if (BN_is_zero(val[0])) {\n        BN_zero(r);\n        ret = 1;\n        goto err;\n    }\n    window = BN_window_bits_for_exponent_size(bits);\n    if (window > 1) {\n        if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx))\n            goto err;\n        j = 1 << (window - 1);\n        for (i = 1; i < j; i++) {\n            if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n                !BN_mod_mul_reciprocal(val[i], val[i - 1], aa, &recp, ctx))\n                goto err;\n        }\n    }\n    start = 1;\n    wvalue = 0;\n    wstart = bits - 1;\n    wend = 0;\n    if (!BN_one(r))\n        goto err;\n    for (;;) {\n        if (BN_is_bit_set(p, wstart) == 0) {\n            if (!start)\n                if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n                    goto err;\n            if (wstart == 0)\n                break;\n            wstart--;\n            continue;\n        }\n        j = wstart;\n        wvalue = 1;\n        wend = 0;\n        for (i = 1; i < window; i++) {\n            if (wstart - i < 0)\n                break;\n            if (BN_is_bit_set(p, wstart - i)) {\n                wvalue <<= (i - wend);\n                wvalue |= 1;\n                wend = i;\n            }\n        }\n        j = wend + 1;\n        if (!start)\n            for (i = 0; i < j; i++) {\n                if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n                    goto err;\n            }\n        if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx))\n            goto err;\n        wstart -= wend + 1;\n        wvalue = 0;\n        start = 0;\n        if (wstart < 0)\n            break;\n    }\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    BN_RECP_CTX_free(&recp);\n    bn_check_top(r);\n    return (ret);\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n    bn_check_top(b);\n    if (a == b)\n        return a;\n    if (bn_wexpand(a, b->top) == NULL)\n        return NULL;\n    if (b->top > 0)\n        memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n    a->top = b->top;\n    a->neg = b->neg;\n    bn_check_top(a);\n    return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n    return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']