Do you think it's wrong to ask my boss why my coworker was promoted instead of me
Maybe. Do you think you already know the reason?
I think my boss is having an affair with him
If you think there's proof of it, you should definitely call him out.  It's wrong, and usually termination-worthy, to sleep with the people who work for you.  He doesn't deserve his position if he's doing stuff like that.
Maybe I should because the coworker is my friend, I'm helping him by telling on the boss that he's having an affair with. Right?
If he's in an involuntary relationship with the boss then it could help him. People in power positions sometimes use that power to make employees feel like they can't say no to them.