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

Cleanups relating to CurveGFp #4436

Merged
merged 1 commit into from
Nov 22, 2024
Merged

Cleanups relating to CurveGFp #4436

merged 1 commit into from
Nov 22, 2024

Conversation

randombit
Copy link
Owner

@randombit randombit commented Nov 15, 2024

This removes CurveGFp_Repr and its subclasses. Now the Montgomery logic previously implemented there is delegated to the already existing Montgomery_Params.

This also guts CurveGFp; at this point it could easily be removed but unfortunately this type is used in a couple of interfaces which, while already deprecated, are still part of the public API. So complete removal will have to wait for Botan4.

#4027

@randombit randombit force-pushed the jack/cleanup-curvegfp branch 3 times, most recently from 54629dd to 3de01c7 Compare November 15, 2024 12:56
@coveralls
Copy link

coveralls commented Nov 15, 2024

Coverage Status

coverage: 91.25% (+0.01%) from 91.24%
when pulling ff5656e on jack/cleanup-curvegfp
into c5ca299 on master.

@randombit randombit requested a review from reneme November 15, 2024 14:34
Copy link
Collaborator

@reneme reneme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nits, otherwise LGTM.

Comment on lines +271 to +284
/**
* Note this constructor should *only* be called by EC_Group_Data::create.
*
* It is only public to allow use of std::make_shared
*/
EC_Group_Data(const BigInt& p,
const BigInt& a,
const BigInt& b,
const BigInt& g_x,
const BigInt& g_y,
const BigInt& order,
const BigInt& cofactor,
const OID& oid,
EC_Group_Source source);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bugs me so often... Typically, I default to using std::shared_ptr(new SomeType(...)) for such cases. IIRC, EC_Group_Data is internal, so no hard opinion. Otherwise, I'd say that hiding the constructor should probably take precedence over the avoidance of new.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That got me thinking: CppCoreGuidelines has this covered, they suggest making the constructor public and letting it take a private Token type that cannot be created/obtained by anyone outside the class. Quite smart, but also a little sneaky, I find:

class Foo {
    private:
        class Token{};

    public:
        Foo(Token) {};

        static std::shared_ptr<Foo> create() {
            return std::make_shared<Foo>(Token{});
        }
};

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would tend to agree on using new rather than exposing a constructor we don't want called, but in this case the type is entirely internal, so a comment seemed sufficient.

Comment on lines +76 to +78
* This EC_Group_Data is not owned because instead the EC_Group_Data
* owns this CurveGFp, so we can always access it safely. If it was
* a shared_ptr this would cause a reference cycle.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::weak_ptr perhaps?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would work but it would complicate things because we'd have to pay the weak->shared upgrade cost each time the group is used (eg for each point addition/doubling!). Not a huge fan of raw pointers, for the obvious reasons, but here the potential for misuse is relatively low; the only way a CurveGFp can be constructed at all is via a private/friend only constructor, it is only constructed in one place, and that code is careful to maintain the liveness invariant.

If this was something we were stuck with long term I'd probably look for a cleaner solution but this seems to me good enough to work until such time as the whole class can just be deleted.

src/lib/pubkey/ec_group/ec_point.h Outdated Show resolved Hide resolved
src/lib/pubkey/ec_group/ec_point.cpp Show resolved Hide resolved
src/lib/pubkey/ec_group/ec_point.cpp Outdated Show resolved Hide resolved
This removes CurveGFp_Repr and its subclasses. Now the Montgomery
logic previously implemented there is delegated to the already
existing Montgomery_Params.

This also guts CurveGFp; at this point it could easily be removed but
unfortunately this type is used in a couple of interfaces which, while
already deprecated, are still part of the public API. So complete
removal will have to wait for Botan4.
@randombit randombit force-pushed the jack/cleanup-curvegfp branch from 3de01c7 to ff5656e Compare November 21, 2024 22:09
@randombit randombit merged commit d62fc66 into master Nov 22, 2024
38 checks passed
@randombit randombit deleted the jack/cleanup-curvegfp branch November 22, 2024 12:26
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.

3 participants