Skip to content

Add Debug implementation for EcGroup{,Ref}#2548

Merged
botovq merged 1 commit intorust-openssl:masterfrom
buytenh:ecgroup-debug
Dec 21, 2025
Merged

Add Debug implementation for EcGroup{,Ref}#2548
botovq merged 1 commit intorust-openssl:masterfrom
buytenh:ecgroup-debug

Conversation

@buytenh
Copy link
Copy Markdown
Contributor

@buytenh buytenh commented Dec 21, 2025

This allows deriving Debug on user-defined structs that contain
instances of EcGroup.

For standard groups, the debug output will look something like this:

    EcGroup { curve_name: "secp521r1" }

For groups not based on a standard curve, the Debug impl will try to
extract and print the curve's components, such that:

    let mut p = BigNum::new().unwrap();
    let mut a = BigNum::new().unwrap();
    let mut b = BigNum::new().unwrap();
    let mut ctx = BigNumContext::new().unwrap();

    EcGroup::from_curve_name(Nid::SECP224R1)
        .unwrap()
        .components_gfp(&mut p, &mut a, &mut b, &mut ctx)
        .unwrap();

    // reconstruct the group from its components
    let group = EcGroup::from_components(p, a, b, &mut ctx).unwrap();

    println!("{:#?}", group);

will print:

    EcGroup {
        p: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001",
        a: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE",
        b: "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4",
    }

If EcGroup::components_gfp() fails, or if any other error occurs, we'll
instead print only:

    EcGroup

Calling EcGroup::components_gfp() on a curve over GF(2^m) unexpectedly
works, and there does not seem to be an explicit way to distinguish
custom curves over GF(2^m) from custom curves over GF(p), and so, custom
curves over GF(2^m) will be Debug'd by the same logic. (rust-openssl
does not allow constructing custom curves over GF(2^m), so we can't add
test cases for this.)

Comment thread openssl/src/ec.rs
Comment thread openssl/src/ec.rs Outdated
This allows deriving Debug on user-defined structs that contain
instances of EcGroup.

For standard groups, the debug output will look something like this:

    EcGroup { curve_name: "secp521r1" }

For groups not based on a standard curve, the Debug impl will try to
extract and print the curve's components, such that:

    let mut p = BigNum::new().unwrap();
    let mut a = BigNum::new().unwrap();
    let mut b = BigNum::new().unwrap();
    let mut ctx = BigNumContext::new().unwrap();

    EcGroup::from_curve_name(Nid::SECP224R1)
        .unwrap()
        .components_gfp(&mut p, &mut a, &mut b, &mut ctx)
        .unwrap();

    // reconstruct the group from its components
    let group = EcGroup::from_components(p, a, b, &mut ctx).unwrap();

    println!("{:#?}", group);

will print:

    EcGroup {
        p: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001",
        a: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE",
        b: "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4",
    }

If EcGroup::components_gfp() fails, or if any other error occurs, we'll
instead print only:

    EcGroup

Calling EcGroup::components_gfp() on a curve over GF(2^m) unexpectedly
works, and there does not seem to be an explicit way to distinguish
custom curves over GF(2^m) from custom curves over GF(p), and so, custom
curves over GF(2^m) will be Debug'd by the same logic.  (rust-openssl
does not allow constructing custom curves over GF(2^m), so we can't add
test cases for this.)
@botovq botovq merged commit 1789fe7 into rust-openssl:master Dec 21, 2025
75 checks passed
@buytenh buytenh deleted the ecgroup-debug branch December 21, 2025 21:32
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

Successfully merging this pull request may close these issues.

2 participants