-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
base: master
Are you sure you want to change the base?
Conversation
The BIP324 ElligatorSwift test vectors are also included.
Pull Request Test Coverage Report for Build 10097360170Details
💛 - Coveralls |
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 |
cc: @ellemouton @ProofOfKeags for review |
t.Fatalf("t value different than expected") | ||
} | ||
|
||
return |
There was a problem hiding this comment.
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.
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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++ { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extract to single const?
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.