# How to determine first byte (recovery ID) for signatures (message signing)?

I do realize that in bitcoin when we return the signature result for signing a message the first byte includes information for recovering the public key required for verification using a formula like this:

``````27 + (4 if comp. 0 if not) + (0<=num<=3)
``````

What I am struggling with is how do we determine that `num` number? According to section 4.1.6 of SEC:1 for recovering public keys:

1. `x = r + jn`
2. Calculate `Q` based on `R` if invalid move to next
3. Calculate `Q` based on `-R`

Since j (cofactor of secp256k1) is 0 and 1 and there are two values for R. So there should be 4 possible public keys. Is `num` the number of public keys that we rejected before reaching the correct one? Because that is the only explanation that I can come up with based on comparisons I have made between signatures generated using Electrum and public key recovery I have done myself using SEC1.

Bonus question: Why was 27 chosen?

• I'm not sure what `num` means there either, but I wrote this a while ago which might interest you. It explains exactly how to encode and decode the recid byte : github.com/fivepiece/sign-verify-message/blob/master/… Dec 19, 2018 at 12:34
• @arubi Thanks for the reply. Based on your comment and another answer I've received on bitcointalk, I believe the main cause of my confusion is that I was thinking you find that `num` after signing and finding `r,s`. But it seems like you must find it during using the y coordinate of `r` which is discarded and doesn't come out of sign function. Now I am trying to figure out the details... Dec 19, 2018 at 13:03
• Dec 19, 2018 at 22:58

FWIW this is what I have found regarding this question. Basically there are two ways of finding what libraries call "recid" or recovery ID. Most of them use the first method but there is another way:

# Method 1:

• Only requires `r` and `s` so it can be performed by anyone as long as you have the signature and the "message" that was signed.
• It is so much slower because it has 3 scalar multiplication and based on cofactor of the elliptic curve used the whole operation may be repeated 2*(h+1) times, which is up to 4 times for bitcoin with secp256k1 curve (99% of the time it is 1 or 2 times).

The steps are explained in section 4.1.6 of SEC:1, I won't repeat them here.

To find `recid` you check the calculated public key with the given public key or hash of it and report number of rejected public keys as `recid`. For example if `x=r+(0*order)` and `Q` was used then `recid=0` and if `-Q` was used then `recid=1` and so on.

# method 2:

• It requires full `r` or `R=(xR, yR)` so it can only be done while signing (requires having the private key).
• It is much faster because there is no additional calculations. It just needs value checks which are fast.
• This is usually referred to as `v` in libraries

`byte v = if(R.X > curve.N) then 2 else 0) | (if R.Y.IsEven then 0 else 1);`

Basically it is check to see if `xR` is bigger than curve order (N) and whether `yR` is even or odd. Something that should not be forgotten is a "flip" that is performed based on `s` and whether `s` was used in the signature or `-s`

`if s > curve.N/2 then v^=1 else do nothing`

** Note that in the end you need to calculate the following:
`recid = 27 + v + (if compressed 4 else 0)`

• IMO you should also accept this as the answer Dec 22, 2018 at 11:16

This will be less of a theoretical answer and more of practical guidance; while developing Secp256k1 or Bitcoin libraries, I always found the following table handy:

y-parity x-order compression recovery id `v`
even less than `n` false 0 27
odd less than `n` false 1 28
even more than `n` false 2 29
odd more than `n` false 3 30
even less than `n` true 0 31
odd less than `n` true 1 32
even more than `n` true 2 33
odd more than `n` true 3 34

let me explain the columns:

• `y-parity` or often just `parity` describes whether the `y`-coordinate of the public key point `R(x, y)` is even. this is important because in most cases, the signature only contains `r = R.x` and not `R.y`. This speeds up signature recovery. (Secp256k1)
• `x-order` is a super rare edge-case where a point's `x`-coordinate is actually above the order of the curve `n`. In 99.99999999% of the cases, this is false. (Secp256k1)
• `compression` is just an indicator whether you want a compressed or an uncompressed public key. (Bitcoin)
• `recovery_id` just indicates which of the four potential point's `y`-coordinates is to be used. (Secp256k1)
• `v` is how we encode this value. For uncompressed keys we use `v = recovery_id + 27` and for compressed `v = recovery_id + 31`. (Bitcoin)

If you want to understand how this works for Ethereum, you would have to read EIP-155 which also introduces an additional variable of `chain_id` for replay protection: `v = 2 * chain_id + 35 + recovery_id`. (Ethereum)

What I am struggling with is how do we determine that `num` number?

That's the recovery ID from the table above.

There may be another method to determine the Recovery ID(rec_id). Since the Recovery ID can only have 4 possible values(0/1/2/3), if you already have the message to sign, public key, and signature, you can try each Recovery ID value and determine which one yields the correct public key value.

There is already an implementation of this function called "ecRecover". For example, in Python, you can use `ecdsa_raw_recover` from `py_ecc` library(source code). This function allows you to derive possible public key from the given Recovery ID:

``````def ecdsa_raw_recover(msghash: bytes, vrs: Tuple[int, int, int]) -> "PlainPoint2D":
v, r, s = vrs
if not (27 <= v <= 34):
raise ValueError("%d must in range 27-31" % v)
x = r
xcubedaxb = (x * x * x + A * x + B) % P
beta = pow(xcubedaxb, (P + 1) // 4, P)
y = beta if v % 2 ^ beta % 2 else (P - beta)
# If xcubedaxb is not a quadratic residue, then r cannot be the x coord
# for a point on the curve, and so the sig is invalid
if (xcubedaxb - y * y) % P != 0 or not (r % N) or not (s % N):
raise ValueError("sig is invalid, %d cannot be the x coord for point on curve" % r)
z = bytes_to_int(msghash)
Gz = jacobian_multiply(cast("PlainPoint3D", (Gx, Gy, 1)), (N - z) % N)
XY = jacobian_multiply(cast("PlainPoint3D", (x, y, 1)), s)