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

ellswift: introduce ElligatorSwift encoding and decoding funcs #2219

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Crypt-iQ
Copy link
Collaborator

@Crypt-iQ Crypt-iQ commented Jul 25, 2024

The BIP324 ElligatorSwift test vectors are also included. They can be found here and here. This code could be more optimized, possibly by not using so many new(FieldVal) invocations among some other things.

The BIP324 ElligatorSwift test vectors are also included.
@Crypt-iQ Crypt-iQ added the btcec label Jul 25, 2024
@coveralls
Copy link

Pull Request Test Coverage Report for Build 10097360170

Details

  • 83 of 278 (29.86%) changed or added relevant lines in 1 file are covered.
  • 6 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.2%) to 57.083%

Changes Missing Coverage Covered Lines Changed/Added Lines %
btcec/ellswift.go 83 278 29.86%
Files with Coverage Reduction New Missed Lines %
peer/peer.go 6 74.16%
Totals Coverage Status
Change from base Build 10067454383: -0.2%
Covered Lines: 29917
Relevant Lines: 52410

💛 - Coveralls

@lnliz
Copy link

lnliz commented Jul 31, 2024

This is great! fwiw, I had great success using the csv files from the BIP324 directly for the tests and it gave me great test coverage too, see esp the big test running through all the "packet_encoding_test_vectors.csv" rows here

@saubyk
Copy link

saubyk commented Aug 21, 2024

cc: @ellemouton @ProofOfKeags for review

t.Fatalf("t value different than expected")
}

return
Copy link

Choose a reason for hiding this comment

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

I think this needs to be "continue" because otherwise the entire TestXSwiftECInvVectors() test suite ends here.

Copy link
Contributor

@ProofOfKeags ProofOfKeags left a comment

Choose a reason for hiding this comment

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

Looks to be a correct implementation of the BIP324 gear.

// public key (u, t) and return the point on the curve it maps to.
// TODO: Rewrite these so to avoid new(FieldVal).Add(...) usage?
// NOTE: u, t MUST be normalized. The result x is normalized.
func XSwiftEC(u, t *FieldVal) *FieldVal {
Copy link
Contributor

Choose a reason for hiding this comment

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

🫡 Leaving this comment for myself but XSwiftEC looks good.

// ElligatorSwift encoding.
// TODO: Rewrite these so to avoid new(FieldVal).Add(...) usage?
// NOTE: u, x MUST be normalized. The result `t` is normalized.
func XSwiftECInv(u, x *FieldVal, caseNum int) *FieldVal {
Copy link
Contributor

Choose a reason for hiding this comment

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

🫡 also good

// XElligatorSwift takes the x-coordinate of a point on secp256k1 and generates
// ElligatorSwift encoding of that point composed of two field elements (u, t).
// NOTE: x MUST be normalized. The return values u, t are normalized.
func XElligatorSwift(x *FieldVal) (*FieldVal, *FieldVal, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

🫡

// We'll choose a random `u` value and a random case so that we can
// generate a `t` value.
// TODO: How does this loop need to be bounded, see secp256k1 repo's impl.
for i := 0; i < 15000; i++ {
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah this seems like it's not part of the spec. I'd imagine you did this as a "just in case". Given that the loop starts with rand generation there is exponential dropoff in the likelihood of failing every loop?


// EllswiftCreate generates a random private key and returns that along with
// the ElligatorSwift encoding of its corresponding public key.
func EllswiftCreate() (*PrivateKey, [64]byte, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

🫡


// EllswiftECDHXOnly takes the ElligatorSwift-encoded public key of a
// counter-party and performs ECDH with our private key.
func EllswiftECDHXOnly(ellswiftTheirs [64]byte, privKey *PrivateKey) ([32]byte,
Copy link
Contributor

Choose a reason for hiding this comment

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

🫡

msg = append(msg, ellswiftOurs[:]...)
msg = append(msg, ellswiftTheirs[:]...)
msg = append(msg, ecdhPoint[:]...)
return chainhash.TaggedHash([]byte("bip324_ellswift_xonly_ecdh"), msg),
Copy link
Contributor

Choose a reason for hiding this comment

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

extract to single const?

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.

5 participants