lib/ecc/src/ecdsa.js
function calcPubKeyRecoveryParam(curve, e, signature, Q) {
  for (var i = 0; i < 4; i++) {
    var Qprime = recoverPubKey(curve, e, signature, i)

    // 1.6.2 Verify Q
    if (Qprime.equals(Q)) {
      return i
    }
  }

  throw new Error('Unable to find valid recovery factor')
}
function calcPubKeyRecoveryParam(curve, e, signature, Q) {
  for (var i = 0; i < 4; i++) {
    var Qprime = recoverPubKey(curve, e, signature, i)

    // 1.6.2 Verify Q
    if (Qprime.equals(Q)) {
      return i
    }
  }

  throw new Error('Unable to find valid recovery factor')
}
Calculate pubkey extraction parameter.

When extracting a pubkey from a signature, we have to
distinguish four different cases. Rather than putting this
burden on the verifier, Bitcoin includes a 2-bit value with the
signature.

This function simply tries all four cases and returns the value
that resulted in a successful pubkey recovery.