static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom,
                  BN_CTX *ctx)
{
    unsigned char *buf = NULL;
    int b, ret = 0, bit, bytes, mask;
    OPENSSL_CTX *libctx = bn_get_lib_ctx(ctx);
    if (bits == 0) {
        if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY)
            goto toosmall;
        BN_zero(rnd);
        return 1;
    }
    if (bits < 0 || (bits == 1 && top > 0))
        goto toosmall;
    bytes = (bits + 7) / 8;
    bit = (bits - 1) % 8;
    mask = 0xff << (bit + 1);
    buf = OPENSSL_malloc(bytes);
    if (buf == NULL) {
        BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);
        goto err;
    }
    b = flag == NORMAL ? rand_bytes_ex(libctx, buf, bytes)
                       : rand_priv_bytes_ex(libctx, buf, bytes);
    if (b <= 0)
        goto err;
    if (flag == TESTING) {
        int i;
        unsigned char c;
        for (i = 0; i < bytes; i++) {
            if (rand_bytes_ex(libctx, &c, 1) <= 0)
                goto err;
            if (c >= 128 && i > 0)
                buf[i] = buf[i - 1];
            else if (c < 42)
                buf[i] = 0;
            else if (c < 84)
                buf[i] = 255;
        }
    }
    if (top >= 0) {
        if (top) {
            if (bit == 0) {
                buf[0] = 1;
                buf[1] |= 0x80;
            } else {
                buf[0] |= (3 << (bit - 1));
            }
        } else {
            buf[0] |= (1 << bit);
        }
    }
    buf[0] &= ~mask;
    if (bottom)
        buf[bytes - 1] |= 1;
    if (!BN_bin2bn(buf, bytes, rnd))
        goto err;
    ret = 1;
 err:
    OPENSSL_clear_free(buf, bytes);
    bn_check_top(rnd);
    return ret;
toosmall:
    BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);
    return 0;
}
['static int char2_curve_test(int n)\n{\n    int r = 0;\n    BN_CTX *ctx = NULL;\n    BIGNUM *p = NULL, *a = NULL, *b = NULL;\n    BIGNUM *x = NULL, *y = NULL, *z = NULL, *cof = NULL, *yplusone = NULL;\n    EC_GROUP *group = NULL, *variable = NULL;\n    EC_POINT *P = NULL, *Q = NULL, *R = NULL;\n    const EC_POINT *points[3];\n    const BIGNUM *scalars[3];\n    struct c2_curve_test *const test = char2_curve_tests + n;\n    if (!TEST_ptr(ctx = BN_CTX_new())\n        || !TEST_ptr(p = BN_new())\n        || !TEST_ptr(a = BN_new())\n        || !TEST_ptr(b = BN_new())\n        || !TEST_ptr(x = BN_new())\n        || !TEST_ptr(y = BN_new())\n        || !TEST_ptr(z = BN_new())\n        || !TEST_ptr(yplusone = BN_new())\n        || !TEST_true(BN_hex2bn(&p, test->p))\n        || !TEST_true(BN_hex2bn(&a, test->a))\n        || !TEST_true(BN_hex2bn(&b, test->b))\n        || !TEST_true(group = EC_GROUP_new(EC_GF2m_simple_method()))\n        || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))\n        || !TEST_ptr(P = EC_POINT_new(group))\n        || !TEST_ptr(Q = EC_POINT_new(group))\n        || !TEST_ptr(R = EC_POINT_new(group))\n        || !TEST_true(BN_hex2bn(&x, test->x))\n        || !TEST_true(BN_hex2bn(&y, test->y))\n        || !TEST_true(BN_add(yplusone, y, BN_value_one())))\n        goto err;\n# ifdef OPENSSL_EC_BIN_PT_COMP\n    if (!TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone, ctx))\n        || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x,\n                                                          test->y_bit,\n                                                          ctx))\n        || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)\n        || !TEST_true(BN_hex2bn(&z, test->order))\n        || !TEST_true(BN_hex2bn(&cof, test->cof))\n        || !TEST_true(EC_GROUP_set_generator(group, P, z, cof))\n        || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))\n        goto err;\n    TEST_info("%s -- Generator", test->name);\n    test_output_bignum("x", x);\n    test_output_bignum("y", y);\n    if (!TEST_true(BN_hex2bn(&z, test->y))\n        || !TEST_BN_eq(y, z))\n        goto err;\n# else\n    if (!TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone, ctx))\n        || !TEST_true(EC_POINT_set_affine_coordinates(group, P, x, y, ctx))\n        || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)\n        || !TEST_true(BN_hex2bn(&z, test->order))\n        || !TEST_true(BN_hex2bn(&cof, test->cof))\n        || !TEST_true(EC_GROUP_set_generator(group, P, z, cof)))\n        goto err;\n    TEST_info("%s -- Generator:", test->name);\n    test_output_bignum("x", x);\n    test_output_bignum("y", y);\n# endif\n    if (!TEST_int_eq(EC_GROUP_get_degree(group), test->degree)\n        || !group_order_tests(group)\n        || !TEST_ptr(variable = EC_GROUP_new(EC_GROUP_method_of(group)))\n        || !TEST_true(EC_GROUP_copy(variable, group)))\n        goto err;\n    if (n == OSSL_NELEM(char2_curve_tests) - 1) {\n        if (!TEST_true(EC_POINT_set_affine_coordinates(group, P, x, y, ctx))\n            || !TEST_true(EC_POINT_copy(Q, P))\n            || !TEST_false(EC_POINT_is_at_infinity(group, Q))\n            || !TEST_true(EC_POINT_dbl(group, P, P, ctx))\n            || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)\n            || !TEST_true(EC_POINT_invert(group, Q, ctx))\n            || !TEST_true(EC_POINT_add(group, R, P, Q, ctx))\n            || !TEST_true(EC_POINT_add(group, R, R, Q, ctx))\n            || !TEST_true(EC_POINT_is_at_infinity(group, R))\n            || !TEST_false(EC_POINT_is_at_infinity(group, Q)))\n            goto err;\n        points[0] = Q;\n        points[1] = Q;\n        points[2] = Q;\n        if (!TEST_true(BN_add(y, z, BN_value_one()))\n            || !TEST_BN_even(y)\n            || !TEST_true(BN_rshift1(y, y)))\n            goto err;\n        scalars[0] = y;\n        scalars[1] = y;\n        TEST_note("combined multiplication ...");\n        if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))\n            || !TEST_true(EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))\n            || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))\n            || !TEST_int_eq(0, EC_POINT_cmp(group, R, Q, ctx)))\n            goto err;\n        if (!TEST_true(BN_rand(y, BN_num_bits(y), 0, 0))\n            || !TEST_true(BN_add(z, z, y)))\n            goto err;\n        BN_set_negative(z, 1);\n        scalars[0] = y;\n        scalars[1] = z;\n        if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))\n            || !TEST_true(EC_POINT_is_at_infinity(group, P)))\n            goto err;\n        if (!TEST_true(BN_rand(x, BN_num_bits(y) - 1, 0, 0))\n            || !TEST_true(BN_add(z, x, y)))\n            goto err;\n        BN_set_negative(z, 1);\n        scalars[0] = x;\n        scalars[1] = y;\n        scalars[2] = z;\n        if (!TEST_true(EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx))\n            || !TEST_true(EC_POINT_is_at_infinity(group, P)))\n            goto err;;\n    }\n    r = 1;\nerr:\n    BN_CTX_free(ctx);\n    BN_free(p);\n    BN_free(a);\n    BN_free(b);\n    BN_free(x);\n    BN_free(y);\n    BN_free(z);\n    BN_free(yplusone);\n    BN_free(cof);\n    EC_POINT_free(P);\n    EC_POINT_free(Q);\n    EC_POINT_free(R);\n    EC_GROUP_free(group);\n    EC_GROUP_free(variable);\n    return r;\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n    int i = a->top - 1;\n    bn_check_top(a);\n    if (BN_is_zero(a))\n        return 0;\n    return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n    return bnrand(NORMAL, rnd, bits, top, bottom, NULL);\n}', 'static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom,\n                  BN_CTX *ctx)\n{\n    unsigned char *buf = NULL;\n    int b, ret = 0, bit, bytes, mask;\n    OPENSSL_CTX *libctx = bn_get_lib_ctx(ctx);\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_ex(libctx, buf, bytes)\n                       : rand_priv_bytes_ex(libctx, 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_ex(libctx, &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}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n    void *ret = NULL;\n    INCREMENT(malloc_count);\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    if (allow_customize) {\n        allow_customize = 0;\n    }\n#if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODE)\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    (void)(file); (void)(line);\n    ret = malloc(num);\n#endif\n    return ret;\n}']