int main() {
    uint8_t priv[32];
    EC_KEY *key;
    uint8_t *msg;
    size_t msg_len;
    uint8_t digest[32];
    uint8_t *sig;
    unsigned int sig_len;

    /* */

    /* keypair */
    bbp_parse_hex(priv, "16260783e40b16731673622ac8a5b045fc3ea4af70f727f3f9e92bdd3a1ddc42");
    key = bbp_ec_new_keypair(priv);

    /* message */
    msg = bbp_alloc_hex("0100000001f3a27f485f9833c8318c490403307fef1397121b5dd8fe70777236e7371c4ef3000000001976a9146bf19e55f94d986b4640c154d86469934191951188acffffffff02e0fe7e01000000001976a91418ba14b3682295cb05230e31fecb00089240660888ace084b003000000001976a9146bf19e55f94d986b4640c154d86469934191951188ac0000000001000000", &msg_len);

    /* signature */
    bbp_hash256(digest, msg, msg_len);
    sig_len = ECDSA_size(key);
    sig = malloc(sig_len);
    ECDSA_sign(0, digest, sizeof(digest), sig, &sig_len, key);

    /* */

    bbp_print_hex("digest", digest, sizeof(digest));
    bbp_print_hex("signature", sig, sig_len);

    free(sig);
    free(msg);
    EC_KEY_free(key);

    return 0;
}
int main() {
    uint8_t pub_bytes[33] = {
        0x02,
        0x82, 0x00, 0x6e, 0x93, 0x98, 0xa6, 0x98, 0x6e,
        0xda, 0x61, 0xfe, 0x91, 0x67, 0x4c, 0x3a, 0x10,
        0x8c, 0x39, 0x94, 0x75, 0xbf, 0x1e, 0x73, 0x8f,
        0x19, 0xdf, 0xc2, 0xdb, 0x11, 0xdb, 0x1d, 0x28
    };
    uint8_t der_bytes[] = {
        0x30, 0x44, 0x02, 0x20, 0x2b, 0x2b, 0x52, 0x9b,
        0xdb, 0xdc, 0x93, 0xe7, 0x8a, 0xf7, 0xe0, 0x02,
        0x28, 0xb1, 0x79, 0x91, 0x8b, 0x03, 0x2d, 0x76,
        0x90, 0x2f, 0x74, 0xef, 0x45, 0x44, 0x26, 0xf7,
        0xd0, 0x6c, 0xd0, 0xf9, 0x02, 0x20, 0x62, 0xdd,
        0xc7, 0x64, 0x51, 0xcd, 0x04, 0xcb, 0x56, 0x7c,
        0xa5, 0xc5, 0xe0, 0x47, 0xe8, 0xac, 0x41, 0xd3,
        0xd4, 0xcf, 0x7c, 0xb9, 0x24, 0x34, 0xd5, 0x5c,
        0xb4, 0x86, 0xcc, 0xcf, 0x6a, 0xf2
    };
    const char message[] = "This is a very confidential message\n";

    EC_KEY *key;
    const uint8_t *der_bytes_copy;
    ECDSA_SIG *signature;
    uint8_t digest[32];
    int verified;

    key = bbp_ec_new_pubkey(pub_bytes, sizeof(pub_bytes));
    if (!key) {
        puts("Unable to create keypair");
        return -1;
    }

    der_bytes_copy = der_bytes;
    signature = d2i_ECDSA_SIG(NULL, &der_bytes_copy, sizeof(der_bytes));
    printf("r: %s\n", BN_bn2hex(signature->r));
    printf("s: %s\n", BN_bn2hex(signature->s));

    bbp_sha256(digest, (uint8_t *)message, strlen(message));
    bbp_print_hex("digest", digest, 32);
    verified = ECDSA_do_verify(digest, sizeof(digest), signature, key);

    switch (verified) {
        case 1:
            puts("verified");
            break;
        case 0:
            puts("not verified");
            break;
        case -1:
            puts("library error");
            break;
    }

    ECDSA_SIG_free(signature);
    EC_KEY_free(key);

    return 0;
}