Skip to content

KeyPair FromStr and Deserialize impls crash #491

Closed
@elsirion

Description

@elsirion

When trying to deserialize a KeyPair the thread crashes with

[libsecp256k1] illegal argument. rustsecp256k1_v0_6_1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)

this can be reproduced using the following test without special feature configurations by running cargo test test_broken_keypair_serde. I originally tested using serde, but could simplify it to FromStr.

    #[test]
    fn test_broken_keypair_serde() {
        let ctx = crate::Secp256k1::new();
        let keypair = KeyPair::new(&ctx, &mut thread_rng());
        let msg = keypair.secret_key().secret_bytes().to_hex();
        dbg!(&msg);
        let parsed_key: KeyPair = msg.parse().unwrap();
        assert_eq!(parsed_key, keypair);
    }

I'm not familiar enough with the low-level C API of libsecp to propose a fix keeping the unsafe code but would propose to use the global context instead and feature gate these impls accordingly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions