Skip to content

use compressed representation for reply key#496

Merged
DanGould merged 2 commits intopayjoin:masterfrom
nothingmuch:compress-reply-key
Jan 20, 2025
Merged

use compressed representation for reply key#496
DanGould merged 2 commits intopayjoin:masterfrom
nothingmuch:compress-reply-key

Conversation

@nothingmuch
Copy link
Collaborator

Closes #492

@coveralls
Copy link
Collaborator

coveralls commented Jan 19, 2025

Pull Request Test Coverage Report for Build 12867632160

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 22 of 23 (95.65%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.1%) to 60.549%

Changes Missing Coverage Covered Lines Changed/Added Lines %
payjoin/src/hpke.rs 22 23 95.65%
Totals Coverage Status
Change from base Build 12832868611: 0.1%
Covered Lines: 2956
Relevant Lines: 4882

💛 - Coveralls

@nothingmuch
Copy link
Collaborator Author

@DanGould thoughts on modifying the HPKE crate to support ellswift & compressed serializations more directly? arguably that's out of scope, but it wouldn't add any dependencies. alternatively, what about exposing a pub method that returns the internal secp256k1::PublicKey (but not making the type member itself pub). this is also a bit meh because it would expose implementation details of the hpke crate.

i think i'd still be in favor because pretending the bitcoin hpke stuff is agnostic to secp256k1 seems kind of pedantic, and this conversion is a bit roundabout (harming readability), adds boilerplate, and is also a bit wasteful (although i think the wastefulness can be mostly addressed by using the unchecked variants when going through the uncompressed form, saving a modular inversion on the field which is the expensive part of checking that a point is on the curve).

@DanGould
Copy link
Contributor

thoughts on modifying the HPKE crate to support ellswift & compressed serializations more directly? arguably that's out of scope, but it wouldn't add any dependencies.

I'm cool with it. Seems like other applications might have a use for it, and the abstraction seems more appropriate and ergonomic there.

what about exposing a pub method that returns the internal secp256k1::PublicKey

Opposed because like you say then it varies a bit more from the generic HPKE implementation, but in favor because that bitcoin_hpke crate is specifically about the use case here AND for potential nostr OpenMLS implementations, which I reckon would benefit from the secp256k1 specifics.

I'd need to more deeply consider how big the change is, and especially the maintenance cost of diverging from rust-hpke in that way.

@DanGould DanGould added the api label Jan 20, 2025
Copy link
Contributor

@DanGould DanGould left a comment

Choose a reason for hiding this comment

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

ACK e9c7050


fn pubkey_from_compressed_bytes(pk_bytes: &[u8]) -> Result<HpkePublicKey, HpkeError> {
let uncompressed_pk_bytes =
bitcoin::secp256k1::PublicKey::from_slice(pk_bytes)?.serialize_uncompressed();
Copy link
Contributor

Choose a reason for hiding this comment

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

May you import bitcoin::secp256k1 to abbreviate calls here and elsewhere in the file?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

initially I did but it makes it kinda confusing as there's already these types in hpke.rs:

pub type PublicKey = <SecpK256HkdfSha256 as hpke::Kem>::PublicKey;
pub struct HpkePublicKey(pub PublicKey);
pub type EncappedKey = <SecpK256HkdfSha256 as hpke::Kem>::EncappedKey;

all of which are representations of public keys that are actually meaningful, whereas this one is just for the serialization

maybe importing only in the functions to emphasize that makes sense? although it doesn't actually reduce the boilerplate

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oh wait do you mean only use bitcoin::secp256k1 and write it as secp256k1::PublicKey to omit the bitcoin:: prefix?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes the latter use bitcoin:secp256k1 to omit the bitcoin:: prefix

@nothingmuch
Copy link
Collaborator Author

Opposed because like you say then it varies a bit more from the generic HPKE implementation, but in favor because that bitcoin_hpke crate is specifically about the use case here AND for potential nostr OpenMLS implementations, which I reckon would benefit from the secp256k1 specifics.

Hmm, arguably the HpkeError::Secp256k1 variant also leaks implementation details

@DanGould
Copy link
Contributor

DanGould commented Jan 20, 2025

Hmm, arguably the HpkeError::Secp256k1 variant also leaks implementation details

I have no problem exposing this as an opaque error variant without an encapsulated secp256k1::Error if you also find that more appropriate. From what I can tell these are always thrown from secp256k1::PublicKey::from_slice as Err(InvalidPublicKey), and our own HpkeError::InvalidPublicKey variant would handle much the same way as encapsulating secp256k1::Error currently does

Copy link
Contributor

@DanGould DanGould left a comment

Choose a reason for hiding this comment

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

ACK c5b6c13

@DanGould DanGould merged commit 5e0267b into payjoin:master Jan 20, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reply key should be compressed

3 participants