-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
GenerateSecp256k1
doesn't use the provided random reader source.
#2791
Comments
This should probably be documented, but likely isn't going to be fixed. |
Why do you feel it shouldn't be fixed? The function takes in an argument that doesn't do anything. Additionally, the equivalent function in the crypto package for generating Ed25519 keys which has the same So it's inconsistent behavior between equivalent functions? |
Seems like a reasonable request. feel free to open a PR and I can review. @Stebalien am I missing some reason for why we shouldn't fix this? |
Ah, I didn't realize decred actually provided a way to do this. My response comes from the fact that:
I agree that taking an argument that doesn't do anything isn't great, but I expect it's that way for historical reasons (i.e., from before the switch to decred). But... we probably shouldn't have taken a random source in the first place.
|
I can certainly open a PR as I have a branch I'm using for this. @Stebalien at least from my POV it's not about trusting crypto.Rand, but intentionally wanting deterministic keys for certain testing scenarios. It should be also noted that some of the standard lib crypto libs (eg ecdsa) has specific behavior for supplied Specifically
My only preference for the libp2p crypto pkg is to be consistent with itself. If that means support the supplied Lmk |
My point is that this behavior, unfortunately, is hard to rely on. E.g., see the comment on There's no reason not to fix it in go-libp2p given that it's easy, but you'll likely feel the pain somewhere down the line when this breaks. |
I'm fine doing the same thing as the Go stdlib's practice of |
Just want to add that currently the libp2p crypto generation of Ed25519 keys, which uses It has the following note:
So the question becomes, do we want equivalent behavior for both libp2p generate ed25519 & secp256k1 keys, or make the change now only for secp256k1 to use |
Thinking about this a bit more, I don't think we should be deciding what to do with the reader. We aren't the crypto library and don't have the context on whether it makes sense to use the reader as is or not. I think we should just forward the call to the underlying library. |
We don't have to read the extra byte, but I also don't want to make any guarantees. Otherwise, it becomes hard to change the underlying secp256k1 library. E.g., in Filecoin we're looking into switching to https://gitlab.com/yawning/secp256k1-voi which doesn't even support reading from a user-specified random source. |
In this example, the library doesn't specify it because its a lower level utility, the So from the I think for the sake of compatibility it makes more sense to provide guarantees so that you can swap the underlying crypto package and not break things, regardless of what that guarantee is (either deterministic or not). Any low level elliptic curve package that deals with points and scalars behave the same, since its just (elliptic) arithmetic. Its up to you guys to determine how to "generate private keys" from said arithmetic. Again, happy to implement and open a PR for either direction. |
Agreed on not making any future guarantees on determinism. We may decide to switch to a library that doesn't even take a reader and that should be fine. For tests, maybe it makes sense to use a fixed key rather than a fixed seed? It's not as elegant, but we can and will guarantee a key will always behave the same for a given cipher. My suggestion would be for us to simply forward the arguments and not add our opinion to what the underlying crypto library does. As well as update the doc comment to assert that we make no guarantees on determinism of the key generation between go-libp2p versions. That might mean it doesn't server your use case @jsimnz of using a seed to deterministically generate keys, but that would break if we ever switch to a library that doesn't support an io.Reader or doesn't support deterministic generation of keys (which some do in an attempt to be foolproof). Would using a fixed key instead (as suggested earlier in my comment) help? |
It does not provide such a method because the absolutely overwhelming majority of the time, the system cryptographic entropy source is the right answer and the API is intentionally opinionated to try to minimize foot+guns. There are escape hatches in the
The output (from skimming the other implementation) will be identical to the dcrd package, except in the "unlikely but theoretically possible" case where more than MaxRetries samples are required to produce a valid key, since the other library does not give up. ps: "clamping" is Ed25519 specific. |
Apologies, was only referencing the root package there, which only contained the EC arithmetic code. Didn't realize @Stebalien may have been referencing the high-level API in the sub package. Which as you point out doesn't accept a reader on the |
No worries, the secp256k1-voi API is rather opinionated throughout. I currently do not currently provide a wide-reduction method for a scalar (though it would be easy to add, or implement on top of the existing Scalar API). Given such a routine, for a Ed25519-esque seed, it would be possible to do something like:
However the algorithm would be non-standard (eg: BIP-32 opts for rejection sampling of a 256-bit scalar instead of doing a wide-reduction). |
Based on the latest version (33.2).
Currently the
GenerateSecp256k1Key(src io.Reader)
accepts aio.Reader
parameter to use as its source of random ness to generate the new key, however it doesn't actually use the provided reader, and instead uses the default random reader within thesecp256k1
package.However, the underlying cryptographic package does support using a provided source of randomness as seen here.
The text was updated successfully, but these errors were encountered: