As brought up by @afk11 on #secp256k1 there are a couple of undocumented and inconsistent surprises in the ec arithmetic functions (that is secp256k1_ec_pub/privkey_neg, _tweak_add and tweak_mul.
In particular:
-
Right now,
ec_privkey_neg,ec_privkey_tweak_addandec_privkey_tweak_muldo not fail if the seckey is invalid, but they may give unexpected results in that case (f.e. #488). In order to avoid that, the user should callec_seckey_verifybefore calling these functions, but that’s not documented. Alternatively, these functions could callec_seckey_verifythemselves. In any case we should define what a validseckeyis (in the documentation ofec_seckey_verifyfor example) and refer to that in the argument documentation of other functions. -
ec_privkey_tweak_addandec_privkey_tweak_mulcan fail (return 0). As a result the seckey argument will be zeroized. This is not documented. There’s conflicting ideas whether this is a good idea:- “preserving any entropy in the bad input is the safer behaviour (e.g. the data in it might get fed elsewhere into some rng hash or something)” (@gmaxwell in #668).
- vs.
However, right now we wouldn’t detect the 0 key in
017:25 < andytoshi> leaving it alone sounds very dangerous, presumably the original key will "work" in subsequent algos 117:25 < andytoshi> while a 0 key is pretty visible and will cause failures pretty-much no matter what you do with itec_privkey_tweak_addandtweak_mul
-
(EDIT: actually the privkey can not overflow in these functions)privkey_tweak_addandprivkey_tweak_mulfail if the resulting privkey is 0 but not if it overflows. That’s not ideal because you won’t be able to sign for it. Additionally, the documentation forprivkey_tweak_addis wrong:returns: [...] 0 the resulting private key [...] would be invalid (only when the tweak is the complement of the corresponding private key)(same forpubkey_tweak_add). An overflowing private key is also invalid. -
All this behavior should be tested
-
See #670 for naming improvements (seckey vs. privkey)