secp256k1: Direct byte clears. - #3750
Open
davecgh wants to merge 1 commit into
Open
Conversation
jholdstock
reviewed
Jul 27, 2026
jholdstock
left a comment
Member
There was a problem hiding this comment.
Did you deliberately leave ModNScalar.Zero() as it was?
The current implementation either clears fixed-size buffers by copying a zeroed array over them, looping over each entry and setting it zero, or manually setting each entry to 0. All of these approaches work as intended, but a cleaner and more efficent approach is to simply set it equal to the zero value of an array of the appropriate size. This package requires a minimum of Go 1.17 and so all supported versions of the Go compiler recognize this pattern and compile it to efficient memory-clearing instructions. While Go does not guarantee constant time operations, inspection of the generated code for supported architectures shows this produces a fixed sequence of stores as desired. Moreover, this approach avoids relying on runtime.memmove, which makes the generated code simpler and easier to reason about with respect to timing behavior.
davecgh
force-pushed
the
secp256k1_direct_byte_clears
branch
from
July 27, 2026 06:23
7c6e5fe to
c361c8f
Compare
Member
Author
Well, I actually missed that because I did the work already on top of #3731 and then ended up rebasing it for easier review, but I guess that's also why it doesn't really matter. It's entirely replaced by the 4x64 implementation there which uses the same technique. Nevertheless, that's a good catch and I went ahead and updated it though for completeness in the mean time. |
jholdstock
approved these changes
Jul 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The current implementation either clears fixed-size buffers by copying a zeroed array over them, looping over each entry and setting it zero, or manually setting each entry to 0. All of these approaches work as intended, but a cleaner and more efficent approach is to simply set it equal to the zero value of an array of the appropriate size.
This package requires a minimum of Go 1.17 and so all supported versions of the Go compiler recognize this pattern and compile it to efficient memory-clearing instructions.
While Go does not guarantee constant time operations, inspection of the generated code for supported architectures shows this produces a fixed sequence of stores as desired.
Moreover, this approach avoids relying on
runtime.memmove, which makes the generated code simpler and easier to reason about with respect to timing behavior.