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

Help making this work with rp2 (badger2040) #6

Open
dipunm opened this issue Jan 29, 2023 · 0 comments
Open

Help making this work with rp2 (badger2040) #6

dipunm opened this issue Jan 29, 2023 · 0 comments

Comments

@dipunm
Copy link

dipunm commented Jan 29, 2023

I have a project defined here: https://github.com/dipunm/bitcoin-starter-kit/

The dependencies are listed in /firmware/dependencies.env, and there are a few patch files to help me apply changes to files both inside secp256k1-embedded and secp256k1 which I have been using to debug an issue.

I am a little out of my depth here, so I have been using Github Actions to build (it is the only way I can get it to build successfully) and I am running the following code in repl mode on the device to observe any changes:

import secp256k1
import hashlib
from binascii import hexlify
import time
import uasyncio
import _thread

secret = hashlib.sha256(b"secret key").digest()[:-8]
secretx = hashlib.sha256(b"secret key").digest()
print("Secret key:", hexlify(secretx).decode())
time.sleep(1)
print("Secret key:", hexlify(secret).decode())
time.sleep(0.1)


async def crash():
    try:
        if not secp256k1.ec_seckey_verify(secret):
            print("ERRORRORORORORO")
        else:
            print("true")
    except Exception as e:
        print(e)

async def start():
    print("starting")
    time.sleep(1)
    try:
        await uasyncio.wait_for(crash(), 10)
    finally:
        print("timeout ended")

print("main indentity", _thread.get_ident()) # 537106508
def _main():
    print("indentity", _thread.get_ident()) # 536926912
    uasyncio.run(start())

thread = _thread.start_new_thread(_main, ())
print("thread started")
time.sleep(10)
print("ending thread from main")

The issue I have been diagnosing is that when running any secp256k1 code, the device becomes unresponsive. I found that by running this code on a second thread, the device doesn't become completely unresponsive (which is good) and when the main thread ends, so does any spawned threads.

If the secp256k1.ec_seckey_verify(secret) function returns, then I see a printed "ERRORRORORORORO" message. The reason is that I have already shortcircuited the secp256k1.ec_seckey_verify method (via patch file) to return mp_const_none. It will do that immediately after calling maybe_init_ctx(); which is its first line, and I identified that the issue occurs within that line as placing the return BEFORE that line, I get an immediate response.

So I then continued to debug by following the code into secp256k1_context_preallocated_create which is defined in secp256k1/src/secp256k1.c ~line 124.

I used return NULL as a shortcircuit, and placing it AFTER:

    ret = (secp256k1_context*)manual_alloc(&prealloc, sizeof(secp256k1_context), base, prealloc_size);

Works fine.

Placing it after the next line:

    ret->illegal_callback = default_illegal_callback;

Is where the problem starts. So I seem to have isolated the issue to the line above.

What is annoying is that I see no error messages, it just crashes or hangs without giving any feedback to my computer. In fact even printf didn't seem to output anything like the print method in python does.

Hopefully, from this issue, someone can give some useful tips based on experience that helps me out because I am really and utterly stumped.

Even suggestions on how I can change my development setup such that I may be more likely to see error messages would be helpful at this point.

Thanks,

Dipun.

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

No branches or pull requests

1 participant