2

I want to sign data in Java and then veify it in c++. When I sign the data in c++ with bitcoin-core/secp256k1 library and then verify it in c++ it works flawlessly. But when I sign it in Java and then transfer it to c++ it does not work anymore.

        secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);

        secp256k1_pubkey pubkey;
        secp256k1_ecdsa_signature sig;

        signature_len = 71;
        // I paste in the generated signature from java
        const char* signature2 = "3045022100C5C52074B7F86130D52FC6286D20E95D974D2A9457B8F39223FA571CA1419B5602203B5DA5922770EE472A2A6235B2FC07C1125763DC4C896A6561D4FD959FAA4C7D";
        char signature3[72];
        hex2bin(signature2, signature3);
        memcpy(signature, signature3, signature_len);
        
        // I paste in the generated public key from java
        const char* pub_key2 = "0300F447B363996717EB807D950BC8C35C648F1AD27E98AD9E23487F4A4E7EEA2F";
        char pub_key3[33];
        hex2bin(pub_key2, pub_key3);
        memcpy(public_key, pub_key3, 33);
        
        std::string data = Vendor::sha256("BANANA");
        memcpy(hash, data.c_str(), 32);

        if (secp256k1_ec_pubkey_parse(ctx, &pubkey, public_key, 33) == 0) {
            LOG_WNL("Error parsing public key!");
        }

        if (secp256k1_ecdsa_signature_parse_der(ctx, &sig, signature, signature_len) == 0) {
            LOG_WNL("Error parsing signature!");
        }

        int res = secp256k1_ecdsa_verify(ctx, &sig, hash, &pubkey);


        return res == 1;

Java code:

            Signature dsa = Signature.getInstance("SHA256withECDSA");

            dsa.initSign(privateKey);

            String str = "BANANA";

            Signature ecdsaSign = Signature.getInstance("SHA256withECDSA");
            ecdsaSign.initSign(privateKey);
            ecdsaSign.update(str.getBytes("ASCII"));

            byte[] signature = ecdsaSign.sign();
            String sig = getHex(signature);

            return sig;

I would greatly appriciate an answer!

Wolfiwolf
  • 35
  • 1
  • 4
  • The Java code works (signature cross-checked with another tool). On the other hand, I can't believe that the `secp256k1_X()` methods in the C++ code don't work. Have you checked if `signature`, `public_key` and `hash` in the C++ code contain the correct data? Also, why is `signature3` 72 bytes when the signature is only 71 bytes (although this is probably not critical)? – Topaco Jan 08 '22 at 13:43
  • I have checked that the same private key generates the same public key in both java and c++. But when u sign in java and c++ the signatures are different. There is probably some difference in the signature format. In c++ library i saw that it is in lower s form. The size of array is 72 because the length of signature varies from 70 - 72. I have basicly given up on making this thing work because the lack of time. I am now signing in c++ in a seperate application. – Wolfiwolf Jan 09 '22 at 15:43

0 Answers0