.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/structures/timestamps/plot_01_linked_timestamps.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via JupyterLite. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_structures_timestamps_plot_01_linked_timestamps.py: Linked timestamps: tamper evidence by hashing the past (Haber and Stornetta 1991) ================================================================================= Haber and Stornetta asked how to timestamp a digital document so that not even the timestamping service could backdate it. Their answer: each certificate includes the hash of the previous one. Changing any old record changes its hash, which breaks every later link. A blockchain is this chain of linked timestamps, with blocks as the records. What to look for ---------------- Edit one record in the middle and every link after it fails. Rewriting history requires recomputing every later record, and anyone holding a recent hash detects the change. The history behind this experiment: :doc:`/history/structures_breakthroughs`. .. GENERATED FROM PYTHON SOURCE LINES 22-24 Link ten records ---------------- .. GENERATED FROM PYTHON SOURCE LINES 24-45 .. code-block:: Python from dataclasses import replace import matplotlib.pyplot as plt import blockchainkit as bk documents = [f"lab notebook, page {page}".encode() for page in range(10)] records = [] previous = bytes(32) for time, document in enumerate(documents): header = bk.structures.BlockHeader(previous, bk.crypto.sha256(document), time, time, 0, 0) records.append(header) previous = header.hash def broken_links(chain): return [i for i in range(1, len(chain)) if chain[i].previous_hash != chain[i - 1].hash] assert broken_links(records) == [] .. GENERATED FROM PYTHON SOURCE LINES 46-48 Backdate page 4 --------------- .. GENERATED FROM PYTHON SOURCE LINES 48-53 .. code-block:: Python tampered = list(records) tampered[4] = replace(records[4], merkle_root=bk.crypto.sha256(b"a better result")) print("broken links after editing page 4:", broken_links(tampered)) assert broken_links(tampered) == [5] .. rst-class:: sphx-glr-script-out .. code-block:: none broken links after editing page 4: [5] .. GENERATED FROM PYTHON SOURCE LINES 54-56 Repairing the break means rewriting everything after it ------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 56-62 .. code-block:: Python rewritten = list(tampered) for i in range(5, len(rewritten)): rewritten[i] = replace(rewritten[i], previous_hash=rewritten[i - 1].hash) assert broken_links(rewritten) == [] assert rewritten[-1].hash != records[-1].hash # Anyone holding the latest hash notices. .. GENERATED FROM PYTHON SOURCE LINES 63-74 .. code-block:: Python fig, ax = plt.subplots(figsize=(8, 2.6)) status = ["#16a34a"] * 4 + ["#dc2626"] + ["#f59e0b"] * 5 ax.bar(range(10), [1] * 10, color=status) ax.set( xticks=range(10), yticks=[], xlabel="record", title="Edited (red) and every record that must be rewritten (amber)", ) fig.tight_layout() .. image-sg:: /api/gallery/structures/timestamps/images/sphx_glr_plot_01_linked_timestamps_001.png :alt: Edited (red) and every record that must be rewritten (amber) :srcset: /api/gallery/structures/timestamps/images/sphx_glr_plot_01_linked_timestamps_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 75-80 Exercise -------- Haber and Stornetta also proposed publishing the latest hash in a newspaper. Why does one widely witnessed hash protect every earlier record? What plays the newspaper's role in Bitcoin? .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.087 seconds) .. _sphx_glr_download_api_gallery_structures_timestamps_plot_01_linked_timestamps.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../../../lite/lab/index.html?path=api/gallery/structures/timestamps/plot_01_linked_timestamps.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_linked_timestamps.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_linked_timestamps.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_linked_timestamps.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_