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 test_swap(void)\n{\n    BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;\n    int top, cond, st = 0;\n    if (!TEST_ptr(a = BN_new())\n            || !TEST_ptr(b = BN_new())\n            || !TEST_ptr(c = BN_new())\n            || !TEST_ptr(d = BN_new()))\n        goto err;\n    if (!(TEST_true(BN_bntest_rand(a, 1024, 1, 0))\n            && TEST_true(BN_bntest_rand(b, 1024, 1, 0))\n            && TEST_ptr(BN_copy(c, a))\n            && TEST_ptr(BN_copy(d, b))))\n        goto err;\n    top = BN_num_bits(a) / BN_BITS2;\n    BN_swap(a, b);\n    if (!equalBN("swap", a, d)\n            || !equalBN("swap", b, c))\n        goto err;\n    cond = 1;\n    BN_consttime_swap(cond, a, b, top);\n    if (!equalBN("cswap true", a, c)\n            || !equalBN("cswap true", b, d))\n        goto err;\n    cond = 0;\n    BN_consttime_swap(cond, a, b, top);\n    if (!equalBN("cswap false", a, c)\n            || !equalBN("cswap false", b, d))\n        goto err;\n    BN_set_flags(a, BN_FLG_CONSTTIME);\n    BN_swap(a, b);\n    if (!equalBN("swap, flags", a, d)\n            || !equalBN("swap, flags", b, c)\n            || !TEST_true(BN_get_flags(b, BN_FLG_CONSTTIME))\n            || !TEST_false(BN_get_flags(a, BN_FLG_CONSTTIME)))\n        goto err;\n    cond = 1;\n    BN_consttime_swap(cond, a, b, top);\n    if (!equalBN("cswap true, flags", a, c)\n            || !equalBN("cswap true, flags", b, d)\n            || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))\n            || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))\n        goto err;\n    cond = 0;\n    BN_consttime_swap(cond, a, b, top);\n    if (!equalBN("cswap false, flags", a, c)\n            || !equalBN("cswap false, flags", b, d)\n            || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))\n            || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))\n        goto err;\n    st = 1;\n err:\n    BN_free(a);\n    BN_free(b);\n    BN_free(c);\n    BN_free(d);\n    return st;\n}', 'int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n    return bnrand(TESTING, rnd, bits, top, bottom);\n}', 'static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom)\n{\n    unsigned char *buf = NULL;\n    int b, ret = 0, bit, bytes, mask;\n    if (bits == 0) {\n        if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY)\n            goto toosmall;\n        BN_zero(rnd);\n        return 1;\n    }\n    if (bits < 0 || (bits == 1 && top > 0))\n        goto toosmall;\n    bytes = (bits + 7) / 8;\n    bit = (bits - 1) % 8;\n    mask = 0xff << (bit + 1);\n    buf = OPENSSL_malloc(bytes);\n    if (buf == NULL) {\n        BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    b = flag == NORMAL ? RAND_bytes(buf, bytes) : RAND_priv_bytes(buf, bytes);\n    if (b <= 0)\n        goto err;\n    if (flag == TESTING) {\n        int i;\n        unsigned char c;\n        for (i = 0; i < bytes; i++) {\n            if (RAND_bytes(&c, 1) <= 0)\n                goto err;\n            if (c >= 128 && i > 0)\n                buf[i] = buf[i - 1];\n            else if (c < 42)\n                buf[i] = 0;\n            else if (c < 84)\n                buf[i] = 255;\n        }\n    }\n    if (top >= 0) {\n        if (top) {\n            if (bit == 0) {\n                buf[0] = 1;\n                buf[1] |= 0x80;\n            } else {\n                buf[0] |= (3 << (bit - 1));\n            }\n        } else {\n            buf[0] |= (1 << bit);\n        }\n    }\n    buf[0] &= ~mask;\n    if (bottom)\n        buf[bytes - 1] |= 1;\n    if (!BN_bin2bn(buf, bytes, rnd))\n        goto err;\n    ret = 1;\n err:\n    OPENSSL_clear_free(buf, bytes);\n    bn_check_top(rnd);\n    return ret;\ntoosmall:\n    BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);\n    return 0;\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}']