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 test_sm2(const EC_GROUP *group,\n                    const EVP_MD *digest,\n                    const char *privkey_hex,\n                    const char *message,\n                    const char *k_hex, const char *ctext_hex)\n{\n    const size_t msg_len = strlen(message);\n    BIGNUM *priv = NULL;\n    EC_KEY *key = NULL;\n    EC_POINT *pt = NULL;\n    unsigned char *expected = OPENSSL_hexstr2buf(ctext_hex, NULL);\n    size_t ctext_len = 0;\n    size_t ptext_len = 0;\n    uint8_t *ctext = NULL;\n    uint8_t *recovered = NULL;\n    size_t recovered_len = msg_len;\n    int rc = 0;\n    BN_hex2bn(&priv, privkey_hex);\n    key = EC_KEY_new();\n    EC_KEY_set_group(key, group);\n    EC_KEY_set_private_key(key, priv);\n    pt = EC_POINT_new(group);\n    EC_POINT_mul(group, pt, priv, NULL, NULL, NULL);\n    EC_KEY_set_public_key(key, pt);\n    BN_free(priv);\n    EC_POINT_free(pt);\n    ctext_len = SM2_ciphertext_size(key, digest, msg_len);\n    ctext = OPENSSL_zalloc(ctext_len);\n    if (ctext == NULL)\n        goto done;\n    start_fake_rand(k_hex);\n    rc = SM2_encrypt(key, digest,\n                     (const uint8_t *)message, msg_len, ctext, &ctext_len);\n    restore_rand();\n    TEST_mem_eq(ctext, ctext_len, expected, ctext_len);\n    if (rc == 0)\n        goto done;\n    ptext_len = SM2_plaintext_size(key, digest, ctext_len);\n    TEST_int_eq(ptext_len, msg_len);\n    recovered = OPENSSL_zalloc(ptext_len);\n    if (recovered == NULL)\n        goto done;\n    rc = SM2_decrypt(key, digest, ctext, ctext_len, recovered, &recovered_len);\n    TEST_int_eq(recovered_len, msg_len);\n    TEST_mem_eq(recovered, recovered_len, message, msg_len);\n    if (rc == 0)\n        return 0;\n    rc = 1;\n done:\n    OPENSSL_free(ctext);\n    OPENSSL_free(recovered);\n    OPENSSL_free(expected);\n    EC_KEY_free(key);\n    return rc;\n}', 'int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)\n{\n    if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)\n        return 0;\n    EC_GROUP_free(key->group);\n    key->group = EC_GROUP_dup(group);\n    return (key->group == NULL) ? 0 : 1;\n}', 'EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)\n{\n    EC_GROUP *t = NULL;\n    int ok = 0;\n    if (a == NULL)\n        return NULL;\n    if ((t = EC_GROUP_new(a->meth)) == NULL)\n        return NULL;\n    if (!EC_GROUP_copy(t, a))\n        goto err;\n    ok = 1;\n err:\n    if (!ok) {\n        EC_GROUP_free(t);\n        return NULL;\n    }\n        return t;\n}', 'int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)\n{\n    if (dest->meth->group_copy == 0) {\n        ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n        return 0;\n    }\n    if (dest->meth != src->meth) {\n        ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);\n        return 0;\n    }\n    if (dest == src)\n        return 1;\n    dest->pre_comp_type = src->pre_comp_type;\n    switch (src->pre_comp_type) {\n    case PCT_none:\n        dest->pre_comp.ec = NULL;\n        break;\n    case PCT_nistz256:\n#ifdef ECP_NISTZ256_ASM\n        dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);\n#endif\n        break;\n#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128\n    case PCT_nistp224:\n        dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);\n        break;\n    case PCT_nistp256:\n        dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);\n        break;\n    case PCT_nistp521:\n        dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);\n        break;\n#else\n    case PCT_nistp224:\n    case PCT_nistp256:\n    case PCT_nistp521:\n        break;\n#endif\n    case PCT_ec:\n        dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);\n        break;\n    }\n    if (src->mont_data != NULL) {\n        if (dest->mont_data == NULL) {\n            dest->mont_data = BN_MONT_CTX_new();\n            if (dest->mont_data == NULL)\n                return 0;\n        }\n        if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))\n            return 0;\n    } else {\n        BN_MONT_CTX_free(dest->mont_data);\n        dest->mont_data = NULL;\n    }\n    if (src->generator != NULL) {\n        if (dest->generator == NULL) {\n            dest->generator = EC_POINT_new(dest);\n            if (dest->generator == NULL)\n                return 0;\n        }\n        if (!EC_POINT_copy(dest->generator, src->generator))\n            return 0;\n    } else {\n        EC_POINT_clear_free(dest->generator);\n        dest->generator = NULL;\n    }\n    if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {\n        if (!BN_copy(dest->order, src->order))\n            return 0;\n        if (!BN_copy(dest->cofactor, src->cofactor))\n            return 0;\n    }\n    dest->curve_name = src->curve_name;\n    dest->asn1_flag = src->asn1_flag;\n    dest->asn1_form = src->asn1_form;\n    if (src->seed) {\n        OPENSSL_free(dest->seed);\n        dest->seed = OPENSSL_malloc(src->seed_len);\n        if (dest->seed == NULL)\n            return 0;\n        if (!memcpy(dest->seed, src->seed, src->seed_len))\n            return 0;\n        dest->seed_len = src->seed_len;\n    } else {\n        OPENSSL_free(dest->seed);\n        dest->seed = NULL;\n        dest->seed_len = 0;\n    }\n    return dest->meth->group_copy(dest, src);\n}', 'BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from)\n{\n    if (to == from)\n        return to;\n    if (!BN_copy(&(to->RR), &(from->RR)))\n        return NULL;\n    if (!BN_copy(&(to->N), &(from->N)))\n        return NULL;\n    if (!BN_copy(&(to->Ni), &(from->Ni)))\n        return NULL;\n    to->ri = from->ri;\n    to->n0[0] = from->n0[0];\n    to->n0[1] = from->n0[1];\n    return to;\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}']