Imagine you have a blockchain where the Proof of Work scheme is integer factorization. There is an opcode that takes two integers $N,M$ where it returns true if $M\not\in \{0,1,N\}$ and $N \mod M \equiv 0$. Now, suppose we want a number factored, say $M_g$. It could be a Cunningham number, a brilliant number or any other number where there is an interest in knowing its factorization.
We can create a transaction where the locking script for the funds of the transaction is this opcode with $M_g$ as the first input. This way the unlocking script is just any non-trivial factor of $M_g$. This would allow anyone on the blockchain to claim the reward for this factorization.
Two issues: miner-in-the-middle attack and reorganization attack.
Miner-in-the-Middle Attack: The miner who receives a transaction with the unlocking script will be able to see the factor in plain sight and replace that transaction with one where the funds are sent to their wallets instead of the solver's.
Reorganization Attack If the value of the blockchain's coin is high there is also an incentive to attack the block in which a solver transaction was mined by re-solving that block with a replaced transaction accrediting themselves(the attacker) and attempting to create a longer chain from this new block.
So,the question becomes, how can solutions be submitted safely in this blockchain? Is a new opcode needed? Is it possible to do it in two transactions, or are three transactions required?
Thank you.
Note: I could not find good tags for this question so I choose anything even remotely related. It also seems like the bitcoin stack exchange is strictly for bitcoin.