-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Avoid branching following crypto/subtle #1079
Comments
I'd agree. Using crypto/subtle will probably also improve the readability of the btcec package. |
Great - also I realized that one can keep similar performance to == for the equality check by using int64 like in the inequality checks in crypto/subtle - see the updates to my example repo. |
I gave this a try on this branch in Current
With Changes
I think it would be difficult to do much better than this in terms of optimization and therefore I am less sure that my suggestion is worth the loss in performance. Perhaps this could be solved if there was a direct way to convert bool->int in golang. If the different in time is primarily due to processor optimizations different between branches then one would prefer to give up the performance in favor of a constant time implementation. |
UPDATE: This performance appears to be purely due to compiler optimizations. For instance, if one changes |
Update: There is an explicit timing difference in This comment therefore exposes a potential timing attack based on the non-constant runtime of |
Does this really have any relevance to btcd? There is no encrypted data on chain, no second party interactive session, and no contract between nodes and users regarding timeliness, best effort only. It is not likely a huge component of the sync slowness but making it perfectly constant time will definitely add 5% or more to initial sync time. |
Following a question by @stevenroose in #621 I noticed there are a number of constant-time functions using branches that represent obviously equal computation time, particularly for comparison logic (<,<=,==). To better secure against timing attacks based on branch prediction it would be good to change this type of logic to pure bitwise operations following crypto/subtle.
To analyze the computation cost I made an example repo . Also, in my suggested changes to #621 I already started using some of this type of logic - see here.
I hope to make a PR of this sort myself sometime, but if there are any objections I would be interested to hear them ahead of time. Thanks!
The text was updated successfully, but these errors were encountered: