-
Notifications
You must be signed in to change notification settings - Fork 8
Review of poly1305 #2
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
Open
fwessels
wants to merge
7
commits into
aead:master
Choose a base branch
from
fwessels:feature/review
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5cb422b
Minor enhancements including spelling corrections, const definitions …
fwessels e83916c
Add header, fix correct arguments length and go vet errors
fwessels 6d4c059
Remove semi-colon and move initialize function to top (more logical o…
fwessels d4ac586
Move key definition inside test vector loop (to avoid any spil over f…
fwessels 96f6aca
Initialize both key and msg for benchmark tests
fwessels 3ec29cb
Run Write test over messages of different lengths
fwessels 116eee5
Add extra test vectors
fwessels File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,11 @@ import ( | |
| "io" | ||
| ) | ||
|
|
||
| const ( | ||
| AVX2Size = 512 | ||
| AVX2Buffer = 8 * TagSize | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here... |
||
| ) | ||
|
|
||
| var useAVX2 = supportsAVX2() | ||
|
|
||
| //go:noescape | ||
|
|
@@ -19,21 +24,21 @@ func supportsAVX2() bool | |
| func initialize(state *[7]uint64, key *[32]byte) | ||
|
|
||
| //go:noescape | ||
| func initializeAVX2(state *[512]byte, key *[32]byte) | ||
| func initializeAVX2(state *[AVX2Size]byte, key *[32]byte) | ||
|
|
||
| //go:noescape | ||
| func update(state *[7]uint64, msg []byte) | ||
|
|
||
| //go:noescape | ||
| func updateAVX2(state *[512]byte, msg []byte) | ||
| func updateAVX2(state *[AVX2Size]byte, msg []byte) | ||
|
|
||
| //go:noescape | ||
| func finalize(tag *[TagSize]byte, state *[7]uint64) | ||
|
|
||
| //go:noescape | ||
| func finalizeAVX2(tag *[TagSize]byte, state *[512]byte) | ||
| func finalizeAVX2(tag *[TagSize]byte, state *[AVX2Size]byte) | ||
|
|
||
| // compiler asserts - check that poly1305Hash and poly1305HashAVX2 implements the hash interface | ||
| // compiler asserts - check that poly1305Hash and poly1305HashAVX2 implement the hash interface | ||
| var ( | ||
| _ (hash) = &poly1305Hash{} | ||
| _ (hash) = &poly1305HashAVX2{} | ||
|
|
@@ -53,8 +58,8 @@ func Sum(msg []byte, key [32]byte) [TagSize]byte { | |
| msg = []byte{} | ||
| } | ||
| var out [TagSize]byte | ||
| if useAVX2 && len(msg) > 8*TagSize { | ||
| var state [512]byte | ||
| if useAVX2 && len(msg) > AVX2Buffer { | ||
| var state [AVX2Size]byte | ||
| initializeAVX2(&state, &key) | ||
| updateAVX2(&state, msg) | ||
| finalizeAVX2(&out, &state) | ||
|
|
@@ -95,7 +100,7 @@ func (h *Hash) Size() int { return TagSize } | |
| // Write adds more data to the running Poly1305 hash. | ||
| // This function should return a non-nil error if a call | ||
| // to Write happens after a call to Sum. So it is not possible | ||
| // to compute the checksum and than add more data. | ||
| // to compute the checksum and then add more data. | ||
| func (h *Hash) Write(msg []byte) (int, error) { | ||
| if h.done { | ||
| return 0, errWriteAfterSum | ||
|
|
@@ -123,7 +128,7 @@ func (h *poly1305Hash) Write(p []byte) (n int, err error) { | |
| n = len(p) | ||
| if h.off > 0 { | ||
| dif := TagSize - h.off | ||
| if n <= dif { | ||
| if n < dif { | ||
| h.off += copy(h.buf[h.off:], p) | ||
| return n, nil | ||
| } | ||
|
|
@@ -132,7 +137,7 @@ func (h *poly1305Hash) Write(p []byte) (n int, err error) { | |
| p = p[dif:] | ||
| h.off = 0 | ||
| } | ||
| // process full 16-byte blocks | ||
| // process full multiples of 16-byte blocks | ||
| if nn := len(p) & (^(TagSize - 1)); nn > 0 { | ||
| update(&(h.state), p[:nn]) | ||
| p = p[nn:] | ||
|
|
@@ -155,7 +160,7 @@ func (h *poly1305Hash) Sum(b []byte) []byte { | |
|
|
||
| type poly1305HashAVX2 struct { | ||
| // r[0] | r^2[0] | r[1] | r^2[1] | r[2] | r^2[2] | r[3] | r^2[3] | r[4] | r^2[4] | r[1]*5 | r^2[1]*5 | r[2]*5 | r^2[2]*5 r[3]*5 | r^2[3]*5 r[4]*5 | r^2[4]*5 | ||
| state [512]byte | ||
| state [AVX2Size]byte | ||
|
|
||
| buffer [8 * TagSize]byte | ||
| offset int | ||
|
|
@@ -165,7 +170,7 @@ func (h *poly1305HashAVX2) Write(p []byte) (n int, err error) { | |
| n = len(p) | ||
| if h.offset > 0 { | ||
| remaining := 8*TagSize - h.offset | ||
| if n <= remaining { | ||
| if n < remaining { | ||
| h.offset += copy(h.buffer[h.offset:], p) | ||
| return n, nil | ||
| } | ||
|
|
@@ -174,7 +179,7 @@ func (h *poly1305HashAVX2) Write(p []byte) (n int, err error) { | |
| p = p[remaining:] | ||
| h.offset = 0 | ||
| } | ||
| // process full 8*16-byte blocks | ||
| // process full multiples of 8*16-byte blocks | ||
| if nn := len(p) & (^(8*TagSize - 1)); nn > 0 { | ||
| updateAVX2(&h.state, p[:nn]) | ||
| p = p[nn:] | ||
|
|
||
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe don't export the constant, clients should not know anything about the internal implementation...