.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/crypto/curves/plot_01_elliptic_curves.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_crypto_curves_plot_01_elliptic_curves.py: Elliptic-curve cryptography: a finite group you can draw (Miller and Koblitz 1985) ================================================================================== Replace modular exponentiation with repeated addition of curve points. The public key Q=xG is easy to compute; recovering x is the discrete-log problem. Our 19-element example is small enough to solve exhaustively. What to look for ---------------- Follow repeated addition around a tiny set of points. Exhaustive search recovers the private step count here because this example is deliberately small. Read cells in order. An ``assert`` that produces no output has passed. The final exercise asks you to change an input and explain the result. The history behind this experiment: :doc:`/history/crypto_breakthroughs`. See :doc:`/exercises/crypto` for a worked solution to the exercise. .. GENERATED FROM PYTHON SOURCE LINES 23-36 .. code-block:: Python import matplotlib.pyplot as plt import blockchainkit as bk curve = bk.crypto.TOY_CURVE points = [(x, y) for x in range(curve.p) for y in range(curve.p) if curve.contains((x, y))] assert len(points) + 1 == curve.order # Include infinity. assert bk.crypto.multiply(curve.order, curve.generator, curve) is None public = bk.crypto.public_key(7, curve) recovered = next(k for k in range(1, curve.order) if bk.crypto.public_key(k, curve) == public) assert recovered == 7 print("Public key:", public, "; recovered tiny private key:", recovered) .. rst-class:: sphx-glr-script-out .. code-block:: none Public key: (0, 6) ; recovered tiny private key: 7 .. GENERATED FROM PYTHON SOURCE LINES 37-53 .. code-block:: Python fig, ax = plt.subplots(figsize=(6, 5)) ax.scatter(*zip(*points, strict=True), color="#2563eb", s=55) for k in (1, 2, 3, 7): x, y = bk.crypto.public_key(k, curve) ax.annotate(f"{k}G", (x, y), xytext=(6, 6), textcoords="offset points") ax.set( xlabel="x modulo 17", ylabel="y modulo 17", title="y² = x³ + 2x + 2 over F₁₇", xlim=(-1, 17), ylim=(-1, 17), ) ax.set_aspect("equal") ax.grid(alpha=0.2) fig.tight_layout() .. image-sg:: /api/gallery/crypto/curves/images/sphx_glr_plot_01_elliptic_curves_001.png :alt: y² = x³ + 2x + 2 over F₁₇ :srcset: /api/gallery/crypto/curves/images/sphx_glr_plot_01_elliptic_curves_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 54-56 Larger parameters, same API --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 56-60 .. code-block:: Python large_public = bk.crypto.public_key(7) # secp256k1 by default. assert bk.crypto.SECP256K1.contains(large_public) print("secp256k1 public point:", bk.crypto.encode_point(large_public).hex()) .. rst-class:: sphx-glr-script-out .. code-block:: none secp256k1 public point: 045cbdf0646e5db4eaa398f365f2ea7a0e3d419b7e0330e39ce92bddedcac4f9bc6aebca40ba255960a3178d6d861a54dba813d0b813fde7b5a5082628087264da .. GENERATED FROM PYTHON SOURCE LINES 61-66 Exercise -------- Verify G + (-G) = infinity and (a+b)G = aG+bG. Explain why the plot consists of isolated points rather than a smooth curve. Inspect double-and-add and identify where its control flow depends on private scalar bits. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.159 seconds) .. _sphx_glr_download_api_gallery_crypto_curves_plot_01_elliptic_curves.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/crypto/curves/plot_01_elliptic_curves.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_elliptic_curves.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_elliptic_curves.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_elliptic_curves.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_