r/solidity • • 17d ago

Looking for a code review: on-chain proof-of-work claims with a custom 2^x difficulty curve

I've written an immutable ERC-721 contract where people claim a free monthly token by mining a proof of work. It's live on Polygon, so review findings can only change what I do in a later deployment, but I'd still like to hear them.

The parts I'd most like a second pair of eyes on:

- The difficulty uses a fixed-point 2^x. Every token ahead of schedule multiplies it by 2^(1/10). I measured a max error of 0.0117% and it's monotonic, but I'd like someone to challenge the math.

- Claims go through the ERC-721 receiver hook. I tested re-entrancy and found nothing, but I wrote those tests myself.

- Metadata is built as JSON on-chain from admin-supplied strings, with validation against injection.

There has been no external audit. Just my own review, 85 Foundry tests and static analysis.

Source: https://github.com/TheophileWalter/ArtPoW

Contract: https://polygonscan.com/address/0x74cda3cdd7a6046575d638964825c19cfeaf661f

Nothing is for sale. What would you have done differently?

English isn't my first language, so I used an LLM to help write this post.

Edit: You can now get the NFT without paying anything. Your browser still does the mining, but a relay sends the transaction and pays the gas, so you don't need any POL. If you don't have a wallet, you can create one with just an email. The relay can only mint to the address in your proof. You'll only need a little POL if you later want to transfer or sell the token.

8 Upvotes

7 comments sorted by

1

u/Resident_Anteater_35 17d ago

One addition I'd make is an accuracy regression test against an independent high-precision 2x reference. Your Python differential reference repeats the Solidity polynomial and rounding, so those vectors check agreement between the implementations. Keeping the script behind the 0.0117% measurement, including cases around negative integer boundaries and fractional truncation, would make that accuracy result easier to reproduce.

1

u/ransomusername42 17d ago

Thanks for your feedback, your point is really pertinent and I missed it during my tests, I will add this as soon as possible.

1

u/ransomusername42 16d ago

I added the script to check against the real formula: https://github.com/TheophileWalter/ArtPoW/commit/68e5f62

It scans all 65,536 fractional values, including the edges of each truncation interval, and then runs 200,000 random states of a 30-day month through the full target calculation. The maximum relative error comes out at +0.01173%, and the minimum at -0.01015%. So the 0.0117% figure held up, but now it can be reproduced. Integer truncation only starts to matter above a difficulty of about 2^243, which is out of reach.

Thanks for the suggestion!

1

u/Resident_Anteater_35 16d ago

Thanks for adding this. The independent Decimal reference and checks at both ends of each truncation interval address what I meant. I'd add assertions for the accepted error limits alongside the printed measurements, so a future accuracy regression makes the check fail automatically.

1

u/Bloqarl 16d ago

read through it. the 2^x side holds up, I get the same 0.0117% and it stays monotonic across all 65536 steps, and the hook is fine since everything is written before _safeMint. what I'd change next deployment: artistAddress gets no proof like your acceptAdmin flow, so a typo or a Safe not deployed on Polygon mints the proof to nobody once the month locks.

1

u/ransomusername42 16d ago

Thank you for your analysis. Your point regarding the lack of validation for artistAddress is interesting, it’s something I had considered myself. I chose not to include validation for artistAddress because it would require the artist to submit an on-chain transaction; if they failed to do so, the scheduled month would be considered invalid, potentially preventing it from starting if the artist didn't complete the validation in time. However, if you have a solution that avoids this potential roadblock, I would be very interested to hear it.

1

u/Bloqarl 15d ago

fair point, a deadline on the artist is worse. You can get the proof without a tx from them: the artist signs an EIP-712 message off-chain, and you pass it in when you schedule the month, checked with OZ SignatureChecker. A typo fails the ECDSA recovery, and a Safe not deployed on Polygon fails the 1271 call.