Open koirikivi opened 1 year ago
Here is the way I went about it, I saw that about the signatures being in the same order of the pubkeys. But, I'm not quite sure why that is necessary so long as: 1) Each signature can be verified against one of the pubkeys successfully, and 2) The pubkey is removed from the list once it is matched with a signature
I solved it using list .remove(). I don't think things necessarily have to be in order. It seems to cover all the test cases I've tried. Am I missing something or would this work just as well?
try:
pubkeys_parsed = [S256Point.parse(p) for p in sec_pubkeys]
signatures_parsed = [Signature.parse(p) for p in signatures]
for sig in signatures_parsed:
if len(pubkeys_parsed) == 0:
return False
signature_verified = False
for pubkey in pubkeys_parsed:
if pubkey.verify(z, sig):
print(f'Verified point: {pubkey} with signature: {sig}')
signature_verified = True
pubkeys_parsed.remove(pubkey)
break
if not signature_verified:
return False # Signature did not match a pubkey
stack.append(encode_num(1))
@mattacus From my understanding, the actual implementation of op_checkmultisig
in Bitcoin requires the signatures and pubkeys to be in the same order. At least according to these sources:
Right, I saw that, in the docs:
Because public keys are not checked again if they fail any signature comparison, signatures must be placed in the scriptSig using the same order as their corresponding public keys
After thinking about it some more it makes more sense to do it that way since signature verification time could add up if you have, say, 100s of signature that each node must verify, and you don't pop each pubkey each time it is visited. (In my case I am removing them, but only if the signature is verified, so there could be more iterations). So it seems like they chose that approach for efficiency reasons.
The given answer for
op_checkmultisig
in Chapter 8 has a bug where the last signature is never verified:The fail condition is in the following check:
but it is only executed at the beginning of each iteration and crucially NOT after we have iterated over all
sigs
. So if we have a 1-of-1op_checkmultisig
, it will pass with any signature and any pubkey, and if we have a 2-of-2op_checkmultisig
, it will pass as long as the first checked signature matches the first checked pubkey (and so on).Example test code to reproduce
Relates to #214 as that issue also talks about problems with
op_checkmultisig
.From my understanding, the signatures passed to
op_checkmultisig
need to be in the same order as the pubkeys. If that's the case, we can fix the implementation using thewhile ... else
construct: