Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share the implementation of hashlib across ports #8749

Merged
merged 7 commits into from Jan 4, 2024

Conversation

jepler
Copy link
Member

@jepler jepler commented Dec 22, 2023

.. many platforms can use mbedtls to implement hashlib.

Compile-tested with pico-w. Tested on feather rp2040 dvi. Enabled on rp2040 for now. Could hypothetically be enabled on other ports.

Alternative to #8719 (ping @anecdata)

.. many platforms can use mbedtls to implement hashlib.

Compile-tested with pico-w. Tested on feather rp2040 dvi.
due to mbedtls version skew, some macros need to be provided.

The espressif common-hal implementation is no longer needed.

The copyright of hashlib/__init__.h comes from micropython
extmod/modhashlib.c where I found the macro definitions.
@Akkiesoft
Copy link

I don't know how to test it, however CI doesn't seem to build images for WIZnet boards.

@dhalbert
Copy link
Collaborator

@anecdata how are you on this?

@anecdata
Copy link
Member

anecdata commented Dec 29, 2023

I'm an observer (no relevant expertise), certainly no objections, seems a much more thorough solution than what I originally tried. This originally arose due to someone noticing that websockets in HTTPServer didn't have access to native hashlib on WIZnet / RP2040 boards (someone there should eventually re-test). But I think @Akkiesoft is right that there are no raspberrypi builds, WIZnet or otherwise.

@jepler
Copy link
Member Author

jepler commented Dec 30, 2023

Due to the way the CI optimizes building fewer boards when possible, the rp2040 builds are on a previous Actions run. I think you can get the artifact you'd want for testing from here: https://github.com/adafruit/circuitpython/actions/runs/7305598483?pr=8749

@anecdata
Copy link
Member

anecdata commented Dec 30, 2023

Performs a similar calculation as in HTTPServer websockets:

EXPAND...
import time

GUID = b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
ITERATIONS = 10_000

def hashme():
    key = "dGhlIHNhbXBsZSBub25jZQ=="
    start = time.monotonic_ns()
    for _ in range(0, ITERATIONS):
        response_key = hashlib.new("sha1", key.encode())
        response_key.update(GUID)
    return time.monotonic_ns() - start

import hashlib
duration = hashme()
print(f"{ITERATIONS} iterations in {duration} ns - hashlib")
#import adafruit_hashlib as hashlib
#duration = hashme()
#print(f"{ITERATIONS} iterations in {duration} ns - adafruit_hashlib")
Adafruit CircuitPython 9.0.0-alpha.6-5-g9a7fe7aef4 on 2023-12-23; W5500-EVB-Pico with rp2040
soft reboot

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
10000 iterations in 531250000 ns - hashlib

Native hashlib is now present and executes (an order of magnitude faster than the library)... LGTM!

Thanks, @jepler !

@Akkiesoft
Copy link

Thank you @jepler !

I tested with W5500-EVB-Pico.

I no longer get the hashlib error when I run the code used in the following Issue URL.
adafruit/Adafruit_CircuitPython_HTTPServer#73

LGTM.

@jepler jepler requested a review from tannewt January 2, 2024 18:40
Copy link
Member

@ladyada ladyada left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewed but did not test, however nothing looks amiss to me :)

@jepler jepler merged commit 1bc616c into adafruit:main Jan 4, 2024
173 checks passed
@jepler jepler deleted the mbedtls-hashlib branch January 4, 2024 16:34
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.

None yet

5 participants