Avoid curve25519 "left shift of negative value" #333
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Cast to unsigned before performing the left shift. The shifted result is then sign extended back before subtraction, giving the same output. Generated assembly is unaltered with this change, checking gcc 14.2.0 and clang 19.1.1 (x86-64).
The same unsigned cast fix is present in crypto++'s tweetnacl version. weidai11/cryptopp@5be0c0a#diff-067e6f20b212f32eb7fcdec3395fb1d145612f5d44736d041c1dfc9bc44a9d5b
Jeffery Walton's modification to modL() is also applied here.
Running with -fsanitize=undefined could report a left shift of a negative value from car25519
This is valid for gcc (and presumably clang), but the sanitizer report is a problem.
https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Integers-implementation.html
Fixes #312 on github