Skip to content

R23-T059: Document 16^difficulty complexity of proof_of_work, backed by measurements - #69

Open
code-paul-creator wants to merge 2 commits into
S-AI-Projects:masterfrom
code-paul-creator:R23-T059
Open

code-paul-creator wants to merge 2 commits into
S-AI-Projects:masterfrom
code-paul-creator:R23-T059

Conversation

@code-paul-creator

Copy link
Copy Markdown

Description

Checks the real running time of blockchain/simple_proof_of_work.py against its theoretical complexity. A hash starts with difficulty hex 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 the proof_of_work docstring.

Related Issue

Fixes: #59

Type of Change

  • Bug Fix
  • New Feature
  • UI/UX Improvement
  • Performance Improvement
  • Tests
  • Documentation
  • Refactoring / Maintenance
  • CI/CD

Changes Made

  • blockchain/simple_proof_of_work.py: added a Complexity paragraph to the proof_of_work docstring (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: times proof_of_work with timeit for difficulty 0-5 (100 / 20000 / 5000 / 1000 / 400 / 60 runs), writes results.csv and a plot.
  • work/R23-T059/COMPLEXITY.md: derivation, results table, plot, and notes on variance.

Screenshots / Recordings

pow_benchmark

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 -> passed
  • ruff check blockchain/simple_proof_of_work.py -> all checks passed
  • python 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^d

Checklist

  • I have tested my changes locally.
  • I have added or updated tests, if needed.
  • I have updated documentation, if needed.
  • I have followed the project's coding guidelines.
  • I have run the relevant tests and checks.
  • I have addressed previous review feedback, if applicable.

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Paulami Bhosle — R23-T059: Measure how blockchain/simple_proof_of_work.py scales

1 participant