Repository navigation
R23-T059: Document 16^difficulty complexity of proof_of_work, backed by measurements - #69
Open
code-paul-creator wants to merge 2 commits into
Open
code-paul-creator wants to merge 2 commits into
code-paul-creator wants to merge 2 commits into
Conversation
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.
Description
Checks the real running time of
blockchain/simple_proof_of_work.pyagainst its theoretical complexity. A hash starts withdifficultyhex zeros with probability 16^-difficulty, so the expected number of attempts is 16^difficulty. This PR measures attempts and time for difficulty 0-5 and documents the complexity and evidence in theproof_of_workdocstring.Related Issue
Fixes: #59
Type of Change
Changes Made
blockchain/simple_proof_of_work.py: added a Complexity paragraph to theproof_of_workdocstring (derivation, O(16^difficulty) time, O(1) space, measured mean attempts). No code or doctest changes; the 7>>>lines and the negative-difficulty ValueError are unchanged.work/R23-T059/bench.py: timesproof_of_workwithtimeitfor difficulty 0-5 (100 / 20000 / 5000 / 1000 / 400 / 60 runs), writesresults.csvand a plot.work/R23-T059/COMPLEXITY.md: derivation, results table, plot, and notes on variance.Screenshots / Recordings
The benchmark plot is
work/R23-T059/pow_benchmark.png(attempts and time vs difficulty, measured points on the 16^difficulty line).Testing
python -m pytest --doctest-modules blockchain/simple_proof_of_work.py-> passedruff check blockchain/simple_proof_of_work.py-> all checks passedpython work/R23-T059/bench.py-> mean attempts 16.1 / 258.7 / 4,254 / 64,150 / 1,077,340 for difficulty 1-5 (expected 16 / 256 / 4,096 / 65,536 / 1,048,576); growth per level x16.1, x16.1, x16.4, x15.1, x16.8; every 95% CI contains 16^dChecklist
Additional Notes
Attempts are exactly reproducible (SHA-256 is deterministic); times depend on the machine. Individual runs vary a lot (standard deviation about equal to the mean), so only averages follow 16^d; difficulty 5 used 60 runs, so its interval is about +-23%. An early 100-run sample at difficulty 4 gave 0.75x the expected value; re-running with 800 runs gave 0.955x, so it was sampling noise (see COMPLEXITY.md).