Bitcoin Forum
April 30, 2024, 12:59:51 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Why does bitcoin manipulate the signature beyond ECDSA_do_sign?  (Read 1139 times)
alkor (OP)
Full Member
***
Offline Offline

Activity: 136
Merit: 100


View Profile
August 27, 2014, 03:40:12 PM
 #1

I am looking at how Bitcoin signs transactions in the src/key.cpp file, in particular the RAII wrapper around the OpenSSL implementation of ECDSA.  

In the function
Code:
bool CECKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig)

I see the regular call to ECDA which returns the signature in sig
Code:
ECDSA_SIG *sig = ECDSA_do_sign((unsigned char*)&hash, sizeof(hash), pkey);
However, after that there are some additional manipulations of the signature

 
Code:
        BN_CTX *ctx = BN_CTX_new();
        BN_CTX_start(ctx);
        const EC_GROUP *group = EC_KEY_get0_group(pkey);
        BIGNUM *order = BN_CTX_get(ctx);
        BIGNUM *halforder = BN_CTX_get(ctx);
        EC_GROUP_get_order(group, order, ctx);
        BN_rshift1(halforder, order);
        if (BN_cmp(sig->s, halforder) > 0) {
            // enforce low S values, by negating the value (modulo the order) if above order/2.
            BN_sub(sig->s, order, sig->s);
        }
        BN_CTX_end(ctx);
        BN_CTX_free(ctx);

What purpose do these manipulations serve? If one were to remove that code snippet, would that invalidate the signature? Or would that leak information about the private key?
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
DeathAndTaxes
Donator
Legendary
*
Offline Offline

Activity: 1218
Merit: 1079


Gerald Davis


View Profile
August 27, 2014, 04:17:40 PM
Last edit: August 27, 2014, 09:34:15 PM by DeathAndTaxes
 #2

There are two possible signatures for a given message and k value (nonce).  This is due to the s value being a value over a finite field.  The two values will have symmetry around a line drawn at half the domain order.  If you have one an s value you can compute its reciprocal.  One s value is always greater than half the domain order (hi) and the other is always less (lo).  Since the signature is public knowledge and relayed from peer to peer any peer can mutate the txn by replacing the s value with its reciprocal.

To reduce malleability, the bitcoin protocol now considers only lo s values (those smaller than half the order parameter) as canonical signatures.  Canonical signatures are relayed normally.  Non-canonical signatures are still valid for now however most newer nodes however will not relay them, they will not be added to the memory pool, and miners (unless running custom software) will not include them in blocks.  Non canonical signatures are still valid so if included in a block the block and txn will be accepted by the network.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!