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

In EC keys store the data as EC_Scalar / EC_AffinePoint #4203

Merged
merged 1 commit into from
Jul 24, 2024

Conversation

randombit
Copy link
Owner

In the short term we still have to retain everything in the old types since there are getters that return a const reference to those values.

#4027

@randombit randombit force-pushed the jack/ecc-key-struct branch 3 times, most recently from 89a63c4 to fbc9b0e Compare July 12, 2024 22:02
@coveralls
Copy link

coveralls commented Jul 12, 2024

Coverage Status

coverage: 91.657% (-0.01%) from 91.67%
when pulling 317ee07 on jack/ecc-key-struct
into 3e2a7ce on master.

@randombit randombit force-pushed the jack/ecc-key-struct branch from fbc9b0e to d37a192 Compare July 14, 2024 12:39
@randombit randombit requested a review from reneme July 14, 2024 12:40
@randombit randombit added this to the Botan 3.6.0 milestone Jul 14, 2024
@randombit randombit force-pushed the jack/ecc-key-struct branch 2 times, most recently from b5a142b to 17e2266 Compare July 14, 2024 12:53
@randombit randombit force-pushed the jack/ecc-key-struct branch 2 times, most recently from ae3aed2 to 329f500 Compare July 22, 2024 13:20
@randombit
Copy link
Owner Author

@reneme Ping on this one - this is blocking other work that I'm loathe to start knowing the merge conflicts will get messy so I'd like to get this merged within ~~ the next week if possible.

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.

Looks good to me. Several std::move() related nits in the constructors. I realize that the inner structure of the affected structures doesn't benefit a lot from moving. I find it good measure, nevertheless.

I tried the suggestions locally, so here's a patch, if you just want to apply it: 220ee66

Comment on lines 19 to 20
EC_PrivateKey_Data::EC_PrivateKey_Data(const EC_Group& group, const BigInt& x) :
m_group(group), m_scalar(EC_Scalar::from_bigint(m_group, x)), m_legacy_x(m_scalar.to_bigint()) {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Moving BigInt down the from_bigint() line would also make sense. Eventually it would end up in EC_Scalar_Data_BN and moved in place. That would require further adaptions down this line, though.

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'm not sure if this one makes sense in the long run - in fairly short order the vast majority of EC_Scalar::from_bigint is going to throw away the BigInt argument because it's converting the value to a PrimeOrderCurve::Scalar instead. So it seems to me better to avoid the copy; the move optimizations will only be helpful for obscure/deprecated curves.

src/lib/pubkey/ecc_key/ec_key_data.cpp Outdated Show resolved Hide resolved
src/lib/pubkey/ecc_key/ecc_key.cpp Outdated Show resolved Hide resolved
src/lib/pubkey/ecc_key/ecc_key.cpp Outdated Show resolved Hide resolved
src/lib/pubkey/ecc_key/ecc_key.h Show resolved Hide resolved
src/lib/pubkey/eckcdsa/eckcdsa.cpp Outdated Show resolved Hide resolved
src/lib/pubkey/gost_3410/gost_3410.cpp Outdated Show resolved Hide resolved
Comment on lines +80 to +84
std::vector<uint8_t> encoding;
encoding.reserve(bits.size() + 1);
encoding.push_back(0x04);
encoding.insert(encoding.end(), bits.rbegin() + part_size, bits.rend());
encoding.insert(encoding.end(), bits.rbegin(), bits.rend() - part_size);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps that's a good vehicle to try and use std::ranges::views (and perhaps even integrate this with concat()), to end up with a construction like so:

namespace v = std::ranges::views;
auto encoding =
   concat<std::vector<uint8_t>>(
      '\x04',
      v::reverse(std::span{bits}.first(part_size)),
      v::reverse(std::span{bits}.last(part_size)));

... certainly not here, though.

src/lib/pubkey/sm2/sm2.cpp Outdated Show resolved Hide resolved
Comment on lines +77 to +80
hash.update(group.get_a().serialize(p_bytes));
hash.update(group.get_b().serialize(p_bytes));
hash.update(group.get_g_x().serialize(p_bytes));
hash.update(group.get_g_y().serialize(p_bytes));
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you wanted to avoid a few heap allocations (not sure this is worth it, though):

Suggested change
hash.update(group.get_a().serialize(p_bytes));
hash.update(group.get_b().serialize(p_bytes));
hash.update(group.get_g_x().serialize(p_bytes));
hash.update(group.get_g_y().serialize(p_bytes));
auto update_with_scalar = [bytes = std::vector<uint8_t>(group.get_p_bytes())](HashFunction& h,
const BigInt& scalar) mutable {
scalar.serialize_to(bytes);
h.update(bytes);
};
update_with_scalar(hash, group.get_a());
update_with_scalar(hash, group.get_b());
update_with_scalar(hash, group.get_g_x());
update_with_scalar(hash, group.get_g_y());

@randombit randombit force-pushed the jack/ecc-key-struct branch 6 times, most recently from e0c6d38 to 317ee07 Compare July 24, 2024 00:03
This is analagous to the DL scheme key types added in #3210, but here
we have to retain the existing classes as we are constrained by SemVer.

The new types contain both our old types (BigInt, EC_Point) and new types
(EC_Scalar, EC_AffinePoint). Eventually the legacy types will be removed,
but we can't do that until the next major version. GH #4027

Co-Authored-By: René Meusel <[email protected]>
@randombit randombit merged commit 14ddd8d into master Jul 24, 2024
39 checks passed
@randombit randombit deleted the jack/ecc-key-struct branch July 24, 2024 01:02
@reneme reneme mentioned this pull request Jul 24, 2024
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