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);
    bn_check_top(a);
    return 1;
}
['int SM2_encrypt(const EC_KEY *key,\n                const EVP_MD *digest,\n                const uint8_t *msg,\n                size_t msg_len, uint8_t *ciphertext_buf, size_t *ciphertext_len)\n{\n    int rc = 0;\n    size_t i;\n    BN_CTX *ctx = NULL;\n    BIGNUM *k = NULL;\n    BIGNUM *x1 = NULL;\n    BIGNUM *y1 = NULL;\n    BIGNUM *x2 = NULL;\n    BIGNUM *y2 = NULL;\n    EVP_MD_CTX *hash = EVP_MD_CTX_new();\n    struct SM2_Ciphertext_st ctext_struct;\n    const EC_GROUP *group = EC_KEY_get0_group(key);\n    const BIGNUM *order = EC_GROUP_get0_order(group);\n    const EC_POINT *P = EC_KEY_get0_public_key(key);\n    EC_POINT *kG = NULL;\n    EC_POINT *kP = NULL;\n    uint8_t *msg_mask = NULL;\n    uint8_t *x2y2 = NULL;\n    uint8_t *C3 = NULL;\n    const size_t field_size = EC_field_size(group);\n    const size_t C3_size = EVP_MD_size(digest);\n    if (field_size == 0 || C3_size == 0)\n       goto done;\n    kG = EC_POINT_new(group);\n    kP = EC_POINT_new(group);\n    if (kG == NULL || kP == NULL)\n       goto done;\n    ctx = BN_CTX_new();\n    if (ctx == NULL)\n       goto done;\n    BN_CTX_start(ctx);\n    k = BN_CTX_get(ctx);\n    x1 = BN_CTX_get(ctx);\n    x2 = BN_CTX_get(ctx);\n    y1 = BN_CTX_get(ctx);\n    y2 = BN_CTX_get(ctx);\n    if (y2 == NULL)\n       goto done;\n    x2y2 = OPENSSL_zalloc(2 * field_size);\n    C3 = OPENSSL_zalloc(C3_size);\n    if (x2y2 == NULL || C3 == NULL)\n       goto done;\n    memset(ciphertext_buf, 0, *ciphertext_len);\n    BN_priv_rand_range(k, order);\n    if (EC_POINT_mul(group, kG, k, NULL, NULL, ctx) == 0)\n        goto done;\n    if (EC_POINT_get_affine_coordinates_GFp(group, kG, x1, y1, ctx) == 0)\n        goto done;\n    if (EC_POINT_mul(group, kP, NULL, P, k, ctx) == 0)\n        goto done;\n    if (EC_POINT_get_affine_coordinates_GFp(group, kP, x2, y2, ctx) == 0)\n        goto done;\n    BN_bn2binpad(x2, x2y2, field_size);\n    BN_bn2binpad(y2, x2y2 + field_size, field_size);\n    msg_mask = OPENSSL_zalloc(msg_len);\n    if (msg_mask == NULL)\n       goto done;\n    if (ECDH_KDF_X9_62(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0, digest)\n        == 0)\n        goto done;\n    for (i = 0; i != msg_len; ++i)\n        msg_mask[i] ^= msg[i];\n    if (EVP_DigestInit(hash, digest) == 0)\n        goto done;\n    if (EVP_DigestUpdate(hash, x2y2, field_size) == 0)\n        goto done;\n    if (EVP_DigestUpdate(hash, msg, msg_len) == 0)\n        goto done;\n    if (EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0)\n        goto done;\n    if (EVP_DigestFinal(hash, C3, NULL) == 0)\n        goto done;\n    ctext_struct.C1x = x1;\n    ctext_struct.C1y = y1;\n    ctext_struct.C3 = ASN1_OCTET_STRING_new();\n    ASN1_OCTET_STRING_set(ctext_struct.C3, C3, C3_size);\n    ctext_struct.C2 = ASN1_OCTET_STRING_new();\n    ASN1_OCTET_STRING_set(ctext_struct.C2, msg_mask, msg_len);\n    *ciphertext_len = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);\n    ASN1_OCTET_STRING_free(ctext_struct.C2);\n    ASN1_OCTET_STRING_free(ctext_struct.C3);\n    rc = 1;\n done:\n    OPENSSL_free(msg_mask);\n    OPENSSL_free(x2y2);\n    OPENSSL_free(C3);\n    EVP_MD_CTX_free(hash);\n    BN_CTX_free(ctx);\n    EC_POINT_free(kG);\n    EC_POINT_free(kP);\n    return rc;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n    BIGNUM *ret;\n    CTXDBG_ENTRY("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    ctx->used++;\n    CTXDBG_RET(ctx, ret);\n    return ret;\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    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}', 'int BN_priv_rand_range(BIGNUM *r, const BIGNUM *range)\n{\n    return bnrand_range(PRIVATE, r, range);\n}', 'static int bnrand_range(BNRAND_FLAG flag, BIGNUM *r, const BIGNUM *range)\n{\n    int n;\n    int count = 100;\n    if (range->neg || BN_is_zero(range)) {\n        BNerr(BN_F_BNRAND_RANGE, BN_R_INVALID_RANGE);\n        return 0;\n    }\n    n = BN_num_bits(range);\n    if (n == 1)\n        BN_zero(r);\n    else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) {\n        do {\n            if (!bnrand(flag, r, n + 1, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))\n                return 0;\n            if (BN_cmp(r, range) >= 0) {\n                if (!BN_sub(r, r, range))\n                    return 0;\n                if (BN_cmp(r, range) >= 0)\n                    if (!BN_sub(r, r, range))\n                        return 0;\n            }\n            if (!--count) {\n                BNerr(BN_F_BNRAND_RANGE, BN_R_TOO_MANY_ITERATIONS);\n                return 0;\n            }\n        }\n        while (BN_cmp(r, range) >= 0);\n    } else {\n        do {\n            if (!bnrand(flag, r, n, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))\n                return 0;\n            if (!--count) {\n                BNerr(BN_F_BNRAND_RANGE, BN_R_TOO_MANY_ITERATIONS);\n                return 0;\n            }\n        }\n        while (BN_cmp(r, range) >= 0);\n    }\n    bn_check_top(r);\n    return 1;\n}']