Skip to content

btcec: Optimize field inversion.#1087

Open
bmperrea wants to merge 1 commit intobtcsuite:masterfrom
bmperrea:speedup-inversion
Open

btcec: Optimize field inversion.#1087
bmperrea wants to merge 1 commit intobtcsuite:masterfrom
bmperrea:speedup-inversion

Conversation

@bmperrea
Copy link
Copy Markdown

@bmperrea bmperrea commented Dec 3, 2017

This uses the addition chain from Brian Smith's website to speed up
the inversion algorithm for btcec/field.go.

@bmperrea
Copy link
Copy Markdown
Author

bmperrea commented Dec 3, 2017

It gets back the sig-verify speed lost in #1084 😄 .

benchmark                          old ns/op     new ns/op     delta
----------------------------------------------------------------------
BenchmarkAddJacobian-8             550           552           +0.36%
BenchmarkAddJacobianNotZOne-8      1124          1128          +0.36%
BenchmarkScalarBaseMult-8          49320         48393         -1.88%
BenchmarkScalarBaseMultLarge-8     50016         48749         -2.53%
BenchmarkScalarMult-8              160763        158805        -1.22%
BenchmarkNAF-8                     867           870           +0.35%
BenchmarkSigVerify-8               256511        251033        -2.14%
BenchmarkFieldNormalize-8          13.7          13.7           0.00%

@bmperrea
Copy link
Copy Markdown
Author

@davecgh Any chance we can get this into 0.13.0?

@jakesylvestre
Copy link
Copy Markdown
Collaborator

@jcvernaleo (as per #1530)

  • Low priority
  • Enhancement

Copy link
Copy Markdown
Contributor

@onyb onyb left a comment

Choose a reason for hiding this comment

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

@bmperrea Great work on this one! 👏

While the implementation is correct, I would like to suggest some updates to the code comments:

  • The Haskell function secp256k1FieldInverseSquaredExponent is actually calculating the inverse directly, and not the squared inverse. This is because of the additional andThen add x1 added by you at the end.

    I think we should copy the algorithm verbatim, and our code should closely follow the same, to help future readers.

  • Following the above point, we should explain the addition chain for prime - 3, as opposed to prime - 2, and only present the final multiplication as an extra step to compute the inverse.

  • Finally, the explanation from "It follows the analogy..." until the end of the sentence could be framed in a better way to make it more understandable.

Here's a diff containing all the changes I proposed. Let me know if they make sense to you.

@onyb onyb added the btcec label Feb 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants