Add VEIL: a raffle with a hidden draw and an anonymous winner - #12
Open
midasbal wants to merge 1 commit into
Open
Add VEIL: a raffle with a hidden draw and an anonymous winner#12midasbal wants to merge 1 commit into
midasbal wants to merge 1 commit into
Conversation
Ticket stakes are shielded and paid for through an SRC20 token, so no ticket count is ever a public value. The winning ticket number is never declassified, and claim() can only run in a block after draw(), so neither an off-chain read of the result nor an atomic bundle of draw() and claim() can be used to force or preview a win. 25 tests cover correctness, guards, and both grinding attacks this design defends against, plus a defense-in-depth reentrancy check on claim()'s payout ordering.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds Veil, a raffle where nobody can see how many tickets anyone holds, and the winner stays anonymous unless they come forward to claim.
Entrants pay in a shielded SRC20 token, so the number of tickets someone buys never lands in a public field. Each entry reserves a private range inside a running total, and that total stays hidden until the draw opens it up. At draw time the RNG builtin picks a number, but the number itself stays shielded, so there's nothing for anyone to read off-chain and react to.
The shielding isn't only about privacy here, it's also what keeps the draw honest. draw() doesn't branch on the value it produces, so once it lands in a block it's final. And claim() can't run until a later block, so you can't bundle draw() and claim() into one transaction and revert the whole thing if you didn't win. Between those two, there's no way to grind the result: you can't preview the number, and you can't throw away a draw you don't like.
The tradeoff, and the part I'd want a reviewer to look at: because the winning number is never revealed, the draw isn't auditable after the fact. You can confirm a payout happened, but you can't independently recompute who should have won and check it against who claimed. That's the cost of keeping the winner anonymous. A publicly verifiable draw would be a different design.
Two more things worth being upfront about. The count privacy only means much with a real crowd. With one entrant the total gives away their exact count and they win by default, and with two, each can work out the other's count by subtracting their own. Also, someone has to call draw() after the deadline; nothing in the contract incentivizes it beyond wanting the prize.
There are two small leaks I left in and noted in the README: trying to enter with zero tickets is visible before it reverts, and a losing claim tells the caller they lost. Both are self-only, and neither can shift the outcome.
Builds and tests clean on ssolc 0.8.31. 25 tests, including ones that actually reproduce the two grinding attacks and show them failing, plus a reentrancy check on the payout.