secp256k1 recovery id and how does the code work Here I have some questions regarding the recovery id (recid) defined in secp256k1 implementation.
I had found few posts about how the recid is defined and what does it mean, also the source code (https://github.com/bitcoin-core/secp256k1/blob/master/src/modules/recovery/main_impl.h).
As explain by bitcoin developers like Peter Wuille the recid is [27~30] for compressed pubkey and [31~34] for non-compressed pubkey.
my question are:
1). from the secp256k1 code I did not find 27~34 value, all I found are 0~3; so where the difference come from?
2). I guess [27~30 and [31~34] such value set does not come from nonsense, but why using these two value set to map to [0~3]? Guess some tricky hidden inside--is that because of some trick on bit-level operation?
3). can someone help to explain this piece of code (come from https://github.com/bitcoin-core/secp256k1/blob/master/src/modules/recovery/main_impl.h#L104-L112)
looks for me recid &amp;2 (0x10) actually select recid=2/3 case.
and if recid !=2/3 all left code will be executed--is that understanding correct?
if (recid &amp; 2) {
    if (secp256k1_fe_cmp_var(&amp;fx, &amp;secp256k1_ecdsa_const_p_minus_order) &gt;= 0) {
        return 0;
    }
    secp256k1_fe_add(&amp;fx, &amp;secp256k1_ecdsa_const_order_as_fe);
}
if (!secp256k1_ge_set_xo_var(&amp;x, &amp;fx, recid &amp; 1)) {
    return 0;
}
1). from the secp256k1 code I did not find 27~34 value, all I found are 0~3; so where the difference come from?

The serialization as a 65-byte signature + recovery byte happens on the Bitcoin Core side, not in libsecp256k1. See key.cpp:
vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);


2). I guess [27~30 and [31~34] such value set does not come from nonsense, but why using these two value set to map to [0~3]? Guess some tricky hidden inside--is that because of some trick on bit-level operation?

There is nothing special about it. I just picked an arbitrary offset so the signature byte arrays wouldn't be confused with public keys or other signatures. But there is no special meaning to the number 27 (and in retrospect, it's a rather strange choice).
I'll try to expand this answer later to explain the code.