Strang Runtime Behaviour of SHA256

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Hardcore96
Posts: 2
Joined: Tue Aug 18, 2020 2:26 pm

Strang Runtime Behaviour of SHA256

Post by Hardcore96 » Thu Aug 20, 2020 11:51 am

Hello,

when I run the function profile_sha256() on ESP32 and a custom STM32 port I see a strange behaviour in the runtime of sha256.

Here's the code:

Code: Select all

from uhashlib import sha256
import time

def profile_sha256():
    data1 = b'00000000'
    data2 = b'11111111'
    data3 = b'0000000000000000'
    data4 = b'0101010101010101010101010101010101010101010101010101010101010101'
    data5 = data4 + data4
    data6 = data5 + data5 + data5 + data5
    data6 = data6 + data6 + data6 + data6

    N = 10000

    for data in [data1, data2, data3, data4, data5, data6]:
        start = time.ticks_ms()
        for _ in range(N):
            X = sha256()
            X.update(data)
            # Y = X.digest()   ### line commented out

        end = time.ticks_ms()
        duration = end - start

        print("======== For data length %d bytes: " % len(data))
        print("    Running %d sha256 in %f seconds" % (N, duration/1000))
        print("    Meaning 1 SHA256 calculation takes %f ms" % (duration/N))
I get these results on an ESP32:

Code: Select all

======== For data length 8 bytes: 
    Running 10000 sha256 in 1.754000 seconds
    Meaning 1 SHA256 calculation takes 0.175400 ms
======== For data length 8 bytes: 
    Running 10000 sha256 in 1.773000 seconds
    Meaning 1 SHA256 calculation takes 0.177300 ms
======== For data length 16 bytes: 
    Running 10000 sha256 in 1.779000 seconds
    Meaning 1 SHA256 calculation takes 0.177900 ms
======== For data length 64 bytes: 
    Running 10000 sha256 in 2.150000 seconds
    Meaning 1 SHA256 calculation takes 0.215000 ms
======== For data length 128 bytes: 
    Running 10000 sha256 in 2.403000 seconds
    Meaning 1 SHA256 calculation takes 0.240300 ms
======== For data length 2048 bytes: 
    Running 10000 sha256 in 9.807000 seconds
    Meaning 1 SHA256 calculation takes 0.980700 ms
However, when I uncomment the line

Code: Select all

Y = X.digest()
, the runtime significantly decreases. Can somebody explain what is going on here? How can the program be faster, when I want to get the digest, compared to when I do not want to get the digest?

Code: Select all

======== For data length 8 bytes: 
    Running 10000 sha256 in 0.768000 seconds
    Meaning 1 SHA256 calculation takes 0.076800 ms
======== For data length 8 bytes: 
    Running 10000 sha256 in 0.771000 seconds
    Meaning 1 SHA256 calculation takes 0.077100 ms
======== For data length 16 bytes: 
    Running 10000 sha256 in 0.771000 seconds
    Meaning 1 SHA256 calculation takes 0.077100 ms
======== For data length 64 bytes: 
    Running 10000 sha256 in 1.015000 seconds
    Meaning 1 SHA256 calculation takes 0.101500 ms
======== For data length 128 bytes: 
    Running 10000 sha256 in 1.261000 seconds
    Meaning 1 SHA256 calculation takes 0.126100 ms
======== For data length 2048 bytes: 
    Running 10000 sha256 in 8.659000 seconds
    Meaning 1 SHA256 calculation takes 0.865900 ms
Thank you!

Post Reply