I have an ECDSA implementation in Mathematica over secp256k1 and $r$ and $s$ are always positive numbers. But if you encode them as bytes, the most significant byte can be over 0x80
, which would make it a negative number if the big-number is interpreted as being signed.
I'm asking because the bitcoin implementation has these checks which I don't understand:
if (S[0] & 0x80)
return error("Non-canonical signature: S value negative");
if (R[0] & 0x80)
return error("Non-canonical signature: R value negative");
The $r$ and $s$ values that I produce seem that they would fail these checks when they are larger than $2^{255}$
r
ands
values. In fact it rejects a lot of DER-valid inputs with the explicit goal that each algebraic signature $(s, r)$ has exactly one encoding. The reason is that re-encoding signatures changes the byte representation of a transaction, which in turn changes its txid, which in many multi-transaction protocols is expected not to change. The keyword to search about this is "transaction malleability". $\endgroup$