Skip to content

Conversation

jonasnick
Copy link
Contributor

@jonasnick jonasnick commented Apr 17, 2020

There currently is a single branch in the ecmul_const function that is not being exercised by the tests. This branch is unreachable and therefore I'm suggesting to remove it.

For your convenience the paper the wnaf algorithm can be found here (The Width-w NAF Method Provides Small Memory and Fast Elliptic Scalar Multiplications Secure against Side Channel Attacks). Similarly, unless I'm missing something important, I don't see how their algorithm needs to consider sign(u[i-1]) unless d can be negative - which doesn't make much sense to me either.

@jonasnick jonasnick force-pushed the ecmult_const_playground branch 2 times, most recently from 0f0030e to f95fb31 Compare April 28, 2020 18:56
@real-or-random
Copy link
Contributor

ACK modulo this:

It took me some more effort to think about the invariant in the first loop iteration, which depends on all that negation/flipping stuff before the loop.

This made me add two tests tests, which for example catch a bug if we'd change flip to secp256k1_scalar_is_high(&s) here: https://github.com/bitcoin-core/secp256k1/pull/741/files#diff-bb69027d01eaa0554f24272ea41380a0L97`. So this was not covered by the tests previously. But it's not worth to have a separate PR. Can you cherry-pick the two top commits from mhttps://github.com/real-or-random/secp256k1/tree/ecmult_const_playground and squash this if you agree with the additions?

@jonasnick jonasnick force-pushed the ecmult_const_playground branch from f95fb31 to 0a80990 Compare April 29, 2020 12:33
@jonasnick
Copy link
Contributor Author

Thanks for the additional edge case tests. Cherry-picked and squashed.

@jonasnick jonasnick force-pushed the ecmult_const_playground branch from 0a80990 to 37dba32 Compare April 29, 2020 12:37
Before, test_constant_wnaf used scalar_cadd_bit to correct for the skew. But
this function does not correctly deal with overflows which is why num = -1
couldn't be tested.

This commit also adds tests for 0, 1/2 and 1/2-1 as they are corner cases
in constant_wnaf.
@real-or-random
Copy link
Contributor

ACK 37dba32 I verified the correctness of the change and claimed invariant by manual inspection. I tested the code, both with 32bit and 64bit scalars.

Copy link
Contributor

@real-or-random real-or-random left a comment

Choose a reason for hiding this comment

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

ACK

@gmaxwell
Copy link
Contributor

ACK. I reviewed the code and agree that it cannot be negative. I also added a test to check vastly more values, tested with endomorphism on-and-off, and introduced several faults and confirmed that the tests work.

@real-or-random real-or-random removed the request for review from apoelstra July 26, 2020 10:17
@real-or-random real-or-random merged commit 3e5cfc5 into bitcoin-core:master Jul 26, 2020
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Sep 27, 2020
Summary:
 * Fix test_constant_wnaf for -1 and add a test for it.

Before, test_constant_wnaf used scalar_cadd_bit to correct for the skew. But
this function does not correctly deal with overflows which is why num = -1
couldn't be tested.

This commit also adds tests for 0, 1/2 and 1/2-1 as they are corner cases
in constant_wnaf.

 * Remove unnecessary sign variable from wnaf_const

This is a backport of libsecp256k1 [[bitcoin-core/secp256k1#741 | PR741]]

Test Plan:
  ninja check-secp256k1

Reviewers: #bitcoin_abc, majcosta

Reviewed By: #bitcoin_abc, majcosta

Differential Revision: https://reviews.bitcoinabc.org/D7595
deadalnix pushed a commit to Bitcoin-ABC/secp256k1 that referenced this pull request Sep 28, 2020
Summary:
 * Fix test_constant_wnaf for -1 and add a test for it.

Before, test_constant_wnaf used scalar_cadd_bit to correct for the skew. But
this function does not correctly deal with overflows which is why num = -1
couldn't be tested.

This commit also adds tests for 0, 1/2 and 1/2-1 as they are corner cases
in constant_wnaf.

 * Remove unnecessary sign variable from wnaf_const

This is a backport of libsecp256k1 [[bitcoin-core/secp256k1#741 | PR741]]

Test Plan:
  ninja check-secp256k1

Reviewers: #bitcoin_abc, majcosta

Reviewed By: #bitcoin_abc, majcosta

Differential Revision: https://reviews.bitcoinabc.org/D7595
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants