btcec: Constant time field normalization.#1084
btcec: Constant time field normalization.#1084bmperrea wants to merge 1 commit intobtcsuite:masterfrom
Conversation
c96a89f to
9d15be7
Compare
Unfortunately, there is a slowdown here comparable to the speedup found in a previous change to |
|
Thanks for the credits :) Cool that you managed to do a proper constant time, I struggled with it at that time! This change would be compatible with #621, right? |
|
Also, you seem to have forgotten to run gofmt. It's always a good idea to run the |
9d15be7 to
68ba708
Compare
|
Thanks @stevenroose. It looks like that fixed the tests. This is compatible with my suggested branch for #621, although it will probably require a simple rebase. If I am able to get this reviewed and merged then I would want to spend some more time with #621 to improve its performance and get it finished off as well. Great work on this stuff btw. |
|
Wait until Schnorr arrives, we're gonna need you here! :) |
|
I added a commit with where I change the internals of |
davecgh
left a comment
There was a problem hiding this comment.
Thanks for the PR. Overall it looks great in terms of functionality. I did include some inline comments as a first pass that are mostly nitpicky in nature to keep the code consistency high.
In addition, please squash the commits into a single commit and make its title match the PR title I modified to satisfy the code contribution guidelines.
980e4e3 to
e99fb8e
Compare
|
Thanks for the excellent review @davecgh. I have made the updates, squashed, and renamed the commit. |
|
Any chance we can get this reviewed? |
4cc7412 to
deed52f
Compare
|
Updated based on good feedback in gcash/bchd#78 . The timing got faster than it previously was for this PR since doing so - thanks @jimpo and @markblundeberg UPDATE: actually based on results from benchstat none of the differences in timing here or before are statistically significant so I don't want to make too strong of a claim. |
deed52f to
06d3c38
Compare
|
@jcvernaleo (as per #1530)
|
davecgh
left a comment
There was a problem hiding this comment.
I have verified the correctness of the latest iteration of this code and will be doing something similar in the dcrec/secp256k1 code which has also received a ton of other enhancements as compared to this older version of the code.
The current implementation of
field.Normalize()has a timing vulnerability exposed in issue #1079. This PR solves that issue by using methods outlined in the crypto/subtle reference library.Several constant time comparison operators that return integers for use in further bitwise operations are defined and should be used in place of other branching statements in the btcec library (even if-else statements with equal-time branches are vulnerable to timing attacks based on branch-prediction!). I think this solves #1079, but please comment if you knows of any other place following this same-time branching statements pattern. For details comparing the performance of these functions to their same-time branching counterparts see my example repo. For a more full set of constant time functions see this other branch.
Credits to @stevenroose for questioning this pattern in #621. Also, a plug for everyone to suggest there be a direct
bool2intconversion function in golang!