-
-
Notifications
You must be signed in to change notification settings - Fork 206
Conversation
secp256k1_pubkey pk; | ||
if (!secp256k1_ecdsa_recover(ctx, &pk, &ec_sig, (const uint8_t *)dig.buf)) { | ||
// Copying "return None" behaviour from secp256k1 module; but why do | ||
// we do this rather than raising an exception? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a TODO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is still open. We raise an exception for every other failure mode, so I see no reason why we should not raise an exception here. Any ideas?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, so the documentation of the function says that it should return None
if the signature is invalid, so None
makes sense. However, the actual behavior of the function is that either returns None
or raises ValueError
if the signature is invalid.
This inconsistency lead to a bug (#422), which was fixed in d5fb2a4 by handling both None
and an exceptions equally in the caller code. (The Ethereum app is the only caller.)
I think this can be improved by making the function consistently return None
no matter what the failure is, but in a different PR. I will remove your comment then at least here. If you agree with this conclusion, I'm volunteering to create that other PR, it should just take a few minutes.
Note to self: We should also call |
|
No problem, then we should first focus on just getting zkp imported and included in the build. I'll update this PR then. Anyway, it was still good to have these other commits to demonstrate that the build worked and can actually be used, and for the future.
Okay, I can also just do it when I update this. |
Sure, I 100% agree. To be clear, I am not opposed to using secp256k1_zkp globally for ECDSA/ECDH operations. But let's test that first. |
Maybe we can leverage the fact the tests for from trezor.crypto.curve import secp256k1, secp256k1_zkp
class Secp256k1Common(unittest.TestCase):
# all existing unit tests go here
def test_foo(self):
# use impl.method instead of secp256k1.method
class TestCryptoSecp256k1(Secp256k1Common):
def __init__(self):
self.impl = secp256k1
class TestCryptoSecp256k1Zkp(Secp256k1Common):
def __init__(self):
self.impl = secp256k1_zkp |
f8b5bbc
to
55a3948
Compare
A small update to the PR (following a discussion with @real-or-random): |
Good! |
5e42337
to
4c8fb0c
Compare
I added two commits to address the file name issues and the size of the context. @prusnak Do the callbacks look right to you? |
98acb08
to
1ab15b0
Compare
@real-or-random offtopic: How hard is to add NIST P-256 curve to secp256k1 library? Is this even possible or desired? |
@prusnak it would basically mean rewriting the library. It is neither possible nor desired :) |
Got it :) |
We have now BlockstreamResearch/secp256k1-zkp#53 which (when merged) removes the need to use a modified branch/version of secp256k-zkp. The remaining issues are the top three items in the list in first comment here in this PR |
Yes
Yes
I think that's fine as a PoC. |
Can you please squash the changes into a single commit and resolve conflicts (rebase on top of the current master)? We have reformatted our code using clang-format. Config is in the |
1ab15b0
to
59eea86
Compare
d6d2299
to
f1934b9
Compare
Okay, I squashed and changed it to remove the commits that change the apps.
|
Yes, indeed. I think in a PR that actually changes apps to use secp256k1-zkp, it's best to change this to a model where the verification table is only created when it's necessary (which is rare). Currently, we would create the verification table even when we would call into secp256k1-zkp for signing, which is kind of silly because signing is much more common and we don't need to build a table for signing (because the signing table it's statically built). |
I added two commits (will squash again if you approve):
With these two commits, this PR is ready to be merged except that we need to wait for BlockstreamResearch/secp256k1-zkp#53 to be merged and adjust the submodule. |
82996bf
to
809c2e3
Compare
Squashed. |
809c2e3
to
bd53fb2
Compare
Okay, sorry, I pushed again because I additionally enabled some ASM optimizations for ASM in secp256k1-zkp. |
Okay @real-or-random, great job! We are ready to merge. We'll wait until you merge BlockstreamResearch/secp256k1-zkp#53 and adjust the submodule in this PR. |
bd53fb2
to
65bb972
Compare
Okay force-pushed, I touched only |
Merged the other PR. |
@real-or-random do you want to change the revision of the included submodule to reflect the last change from @apoelstra or should we merge as is? |
Oh yes I should have done this. I'll update it later. :) |
This includes the https://github.com/ElementsProject/secp256k1-zkp library (which is a fork from sipa/secp256k1 used in Bitcoin Core) as a module. It is currently not used in any app. This commit the first step towards integrating Liquid (tracking issue #282). Note that this creates a new 64 kiB read-only data section in .flash2 for pre-computed tables (secp256k1_ecmult_static_context) which speed up signature creation and related private key operations. Co-authored-by: Roman Zeyde <me@romanzey.de> Co-authored-by: Andrew Poelstra <apoelstra@wpsoftware.net> Co-authored-by: Pavol Rusnak <pavol@rusnak.io>
65bb972
to
c542cb3
Compare
fixed |
Great work @real-or-random, thanks for the contribution! |
d4d270a Allow field_10x26_arm.s to compile for ARMv7 architecture (Roman Zeyde) Pull request description: It would allow using optimized field operations on the TREZOR device, which is using ARMv7 Cortex-M4. Following trezor/trezor-core#500 and part of trezor/trezor-firmware#66. Tree-SHA512: 73c0f03503feff01c6f4efd884e916ae1f43f55d525e8c3ea9372cf777aef6901585b74774c316dd7937abfff5e86be5b1acb569f9eeee9b73ae088f0f6b589d
This includes the https://github.com/ElementsProject/secp256k1-zkp library (which is a fork from sipa/secp256k1 used in Bitcoin Core) and uses it for signing transactions and signing and verifying messages for ECDSA on the secp256k1 curve.
The Ethereum app is left untouched. I assume because they use Keccak-256 as a message digest and not SHA256. @apoelstra
This is the first step towards solving #282.
Some background to understand the open issues below:
secp256k1-zkp (and in fact sipa/secp256k1) relies on two precomputation tables to speed up signing and for verification. secp256k1(-zkp) supports that the precomputed table values for the signing table are stored in the executable (by defining
USE_ECMULT_STATIC_PRECOMPUTATION
) and thus do not need to be created at running time.It's WIP because the following is still open (and maybe more):
abort()
andfprintf()
, so we need to replace them. I replaced them by functions which throw Python exceptions. Is this the right thing to do on trezor?secp256k1_context
which is currently created when it's first used. Is that okay or do we want to create at program startup?USE_ECMULT_STATIC_PRECOMPUTATION
but it increases the executable by 64 KiB, which is about 17 KiB too much after we rebased on master. To make the build work I've added a HACK commit that disables `USE_ECMULT_STATIC_PRECOMPUTATION in the firmware build again. But that means calling into secp256k1-zkp functions will fail at runtime because we don't give it enough memory to create the table. Note that it's probably really better to build this in the executable because otherwise we need 64 KiB of RAM instead of flash. Also, we need to keep in mind that currently no rangeproof functions are used, so those will be removed from the executable. If we really call these, the size will increase further.secp256k1.h
. For some reason the build currently works (at least on my machine) but this is probably fragile. Our goal is to make all of this work without the need to vendor specific secp256k1-zkp changes, and we'd rather not want to rename our header. I think it's easiest to rename your currently usedsecp256k1.h
? Is that fine for you? Otherwise we can also use #includes with directories.We opened this mostly to get some feedback on the build system. We can also decide to use this PR just for the build system and handle the other stuff in a different PR if that works better for you.
cc @romanz @apoelstra