-
Notifications
You must be signed in to change notification settings - Fork 195
Preparation for ARM64 Implementation of poly operations for dilithium package. #562
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
Merged
+136
−2
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9849e35
add arm64 file for conditional compilation
elementrics bc94122
add arm64 implementation for poly.Add
elementrics 6be96d7
add simple tests that cover assembly implementation assumptions
elementrics 1dfa2c5
only run test on arm64
elementrics fe38271
fix typo
elementrics 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| //go:build arm64 && !purego | ||
| // +build arm64,!purego | ||
|
|
||
| package dilithium | ||
|
|
||
| // Execute an in-place forward NTT on as. | ||
| // | ||
| // Assumes the coefficients are in Montgomery representation and bounded | ||
| // by 2*Q. The resulting coefficients are again in Montgomery representation, | ||
| // but are only bounded bt 18*Q. | ||
| func (p *Poly) NTT() { | ||
| p.nttGeneric() | ||
| } | ||
|
|
||
| // Execute an in-place inverse NTT and multiply by Montgomery factor R | ||
| // | ||
| // Assumes the coefficients are in Montgomery representation and bounded | ||
| // by 2*Q. The resulting coefficients are again in Montgomery representation | ||
| // and bounded by 2*Q. | ||
| func (p *Poly) InvNTT() { | ||
| p.invNttGeneric() | ||
| } | ||
|
|
||
| // Sets p to the polynomial whose coefficients are the pointwise multiplication | ||
| // of those of a and b. The coefficients of p are bounded by 2q. | ||
| // | ||
| // Assumes a and b are in Montgomery form and that the pointwise product | ||
| // of each coefficient is below 2³² q. | ||
| func (p *Poly) MulHat(a, b *Poly) { | ||
| p.mulHatGeneric(a, b) | ||
| } | ||
|
|
||
| // Sets p to a + b. Does not normalize polynomials. | ||
| func (p *Poly) Add(a, b *Poly) { | ||
| polyAdd(p, a, b) | ||
| } | ||
|
|
||
| // Sets p to a - b. | ||
| // | ||
| // Warning: assumes coefficients of b are less than 2q. | ||
| // Sets p to a + b. Does not normalize polynomials. | ||
| func (p *Poly) Sub(a, b *Poly) { | ||
| p.subGeneric(a, b) | ||
| } | ||
|
|
||
| // Writes p whose coefficients are in [0, 16) to buf, which must be of | ||
| // length N/2. | ||
| func (p *Poly) PackLe16(buf []byte) { | ||
| p.packLe16Generic(buf) | ||
| } | ||
|
|
||
| // Reduces each of the coefficients to <2q. | ||
| func (p *Poly) ReduceLe2Q() { | ||
| p.reduceLe2QGeneric() | ||
| } | ||
|
|
||
| // Reduce each of the coefficients to <q. | ||
| func (p *Poly) Normalize() { | ||
| p.normalizeGeneric() | ||
| } | ||
|
|
||
| // Normalize the coefficients in this polynomial assuming they are already | ||
| // bounded by 2q. | ||
| func (p *Poly) NormalizeAssumingLe2Q() { | ||
| p.normalizeAssumingLe2QGeneric() | ||
| } | ||
|
|
||
| // Checks whether the "supnorm" (see sec 2.1 of the spec) of p is equal | ||
| // or greater than the given bound. | ||
| // | ||
| // Requires the coefficients of p to be normalized. | ||
| func (p *Poly) Exceeds(bound uint32) bool { | ||
| return p.exceedsGeneric(bound) | ||
| } | ||
|
|
||
| // Sets p to 2ᵈ q without reducing. | ||
| // | ||
| // So it requires the coefficients of p to be less than 2³²⁻ᴰ. | ||
| func (p *Poly) MulBy2toD(q *Poly) { | ||
| p.mulBy2toDGeneric(q) | ||
| } | ||
|
|
||
| //go:noescape | ||
| func polyAdd(p, a, b *Poly) |
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 |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| //go:build arm64 && !purego | ||
|
|
||
| #include "textflag.h" | ||
|
|
||
| // func polyAdd(p, a, b *Poly) | ||
| TEXT ·polyAdd(SB), NOSPLIT|NOFRAME, $0-24 | ||
| MOVD p+0(FP), R0 | ||
| MOVD a+8(FP), R1 | ||
| MOVD b+16(FP), R2 | ||
|
|
||
| // loop counter (each iteration processes 16 elements so 16 * 16 = 256 = N) | ||
| // manually unrolling could also be done, for now skipped | ||
| MOVW $16, R3 | ||
|
|
||
| add: | ||
| VLD1.P (64)(R1), [V0.S4, V1.S4, V2.S4, V3.S4] | ||
| VLD1.P (64)(R2), [V4.S4, V5.S4, V6.S4, V7.S4] | ||
|
|
||
| VADD V4.S4, V0.S4, V8.S4 | ||
|
Member
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. Note: it's not necessary here, but you can reuse
Contributor
Author
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. you are absolutely right! |
||
| VADD V5.S4, V1.S4, V9.S4 | ||
| VADD V6.S4, V2.S4, V10.S4 | ||
| VADD V7.S4, V3.S4, V11.S4 | ||
|
|
||
| VST1.P [V8.S4, V9.S4, V10.S4, V11.S4], (64)(R0) | ||
|
|
||
| SUBS $1, R3, R3 | ||
| BGT add | ||
|
|
||
| RET | ||
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 |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| //go:build arm64 && !purego | ||
| // +build arm64,!purego | ||
|
|
||
| package dilithium | ||
|
|
||
| import ( | ||
| "testing" | ||
| "unsafe" | ||
| ) | ||
|
|
||
| func TestAssemblyImplementationAssumptions(t *testing.T) { | ||
| var p Poly | ||
|
|
||
| if len(p) != 256 { | ||
| t.Fatal("length of p must be 256. (assumption of the arm64 implementations)") | ||
| } | ||
|
|
||
| if unsafe.Sizeof(p) != 1024 { | ||
| t.Fatal("sizeof p be must be 1024 bytes. (assumption of the arm64 implementations)") | ||
| } | ||
| } |
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.
nit: you can just call this
loopThere 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.
okey, I will take it under consideration on the next PR's!