-
Notifications
You must be signed in to change notification settings - Fork 67
dlog token driver: improve the use of mathlib #1287 #1288
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
adecaro
wants to merge
17
commits into
main
Choose a base branch
from
1287-dlog-token-driver-improve-the-use-of-mathlib
base: main
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.
+687
−167
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
648ed53
curve extension + ipa use of mul2
adecaro 5002b6d
minor
adecaro a743a18
improvement + updated mathlib
adecaro cd7998f
avoid a copy
adecaro 4314a5a
improvements
adecaro b0727f8
bug fix
adecaro 26c3f86
improvements
adecaro 909c498
bulletproof improvements
adecaro 8324369
mathlib dep update
adecaro 93edd9c
more commnets and test
adecaro 7f186f8
lint fix
adecaro c1c9476
rebase cleanup
adecaro f40b313
more benchmarks
adecaro 5cd4503
mathlib bd86b4ed336b
adecaro 1e772e7
mathlib f52c9be3cb35
adecaro e0a17be
mathlib 21c357e3e46fa7a7fd43910b0be6c88f60cf3da0
adecaro 8dda5ee
cleanup transfer
adecaro 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
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,41 @@ | ||
| /* | ||
| Copyright IBM Corp. All Rights Reserved. | ||
| SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package crypto | ||
|
|
||
| import "encoding/binary" | ||
|
|
||
| // AppendFixed32 appends slices prefixed with a 4-byte Little Endian length. | ||
AkramBitar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Format: [Len(4 bytes)][Data]... | ||
| func AppendFixed32(dst []byte, s [][]byte) []byte { | ||
| // 1. Precise Size Calculation | ||
| // We calculate the exact total growth needed (4 bytes header + data length per slice). | ||
| // This allows us to perform exactly one allocation. | ||
| const headerSize = 4 | ||
| n := 0 | ||
| for _, v := range s { | ||
| n += headerSize + len(v) | ||
| } | ||
|
|
||
| // 2. Single Growth / Allocation | ||
| // If the capacity is insufficient, we grow the slice exactly once. | ||
| // This avoids the 2x growth strategy of standard append(), saving | ||
| // potentially 25-50% memory overhead on large buffers. | ||
| if cap(dst)-len(dst) < n { | ||
| newDst := make([]byte, len(dst), len(dst)+n) | ||
| copy(newDst, dst) | ||
| dst = newDst | ||
| } | ||
|
|
||
| // 3. Append Loop (Branch-free) | ||
| for _, v := range s { | ||
| // AppendUint32 is inlinable and highly optimized in Go 1.19+ [web:22] | ||
| dst = binary.LittleEndian.AppendUint32(dst, uint32(len(v))) | ||
| dst = append(dst, v...) | ||
| } | ||
|
|
||
| return dst | ||
| } | ||
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,120 @@ | ||
| /* | ||
| Copyright IBM Corp. All Rights Reserved. | ||
| SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package crypto | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/binary" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| // Unit Test: Verifies correct Little Endian encoding and data integrity | ||
| func TestAppendFixed32(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input [][]byte | ||
| expected []byte | ||
| }{ | ||
| { | ||
| name: "Basic Join", | ||
| input: [][]byte{ | ||
| []byte("Go"), | ||
| []byte("Lang"), | ||
| }, | ||
| // Expect: [Len:2][G][o] [Len:4][L][a][n][g] | ||
| // Little Endian 2: 0x02, 0x00, 0x00, 0x00 | ||
| expected: []byte{ | ||
| 0x02, 0x00, 0x00, 0x00, 'G', 'o', | ||
| 0x04, 0x00, 0x00, 0x00, 'L', 'a', 'n', 'g', | ||
| }, | ||
| }, | ||
| { | ||
| name: "Empty Input", | ||
| input: [][]byte{}, | ||
| expected: nil, // Or empty slice depending on init | ||
| }, | ||
| { | ||
| name: "Contains Empty Slice", | ||
| input: [][]byte{ | ||
| []byte("Hi"), | ||
| {}, | ||
| }, | ||
| // Expect: [Len:2][H][i] [Len:0] | ||
| expected: []byte{ | ||
| 0x02, 0x00, 0x00, 0x00, 'H', 'i', | ||
| 0x00, 0x00, 0x00, 0x00, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| // Using nil as dst to force new allocation | ||
| result := AppendFixed32(nil, tt.input) | ||
| assert.Equal(t, tt.expected, result) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // Verification Test: Reusing an existing buffer | ||
| func TestAppendFixed32_ReuseBuffer(t *testing.T) { | ||
| buffer := make([]byte, 0, 1024) | ||
| buffer = append(buffer, 0xFF) // Simulating existing data (dirty buffer) | ||
|
|
||
| input := [][]byte{[]byte("A")} | ||
| result := AppendFixed32(buffer, input) | ||
|
|
||
| // Check that we didn't lose the prefix 0xFF | ||
| assert.Equal(t, byte(0xFF), result[0]) | ||
| // Check the new data starts at index 1 | ||
| // Len: 1 (0x01 00 00 00) + 'A' | ||
| expectedPayload := []byte{0x01, 0x00, 0x00, 0x00, 'A'} | ||
| assert.Equal(t, expectedPayload, result[1:]) | ||
| } | ||
|
|
||
| // --- Benchmarks --- | ||
|
|
||
| // Setup helper for benchmarks | ||
| func generateBenchmarkData(count, size int) [][]byte { | ||
| data := make([][]byte, count) | ||
| for i := 0; i < count; i++ { | ||
| data[i] = bytes.Repeat([]byte{'a'}, size) | ||
| } | ||
| return data | ||
| } | ||
|
|
||
| // Optimized Approach | ||
| func BenchmarkAppendFixed32(b *testing.B) { | ||
| // Scenario: 1000 items, 256 bytes each (~250KB total) | ||
| data := generateBenchmarkData(1000, 256) | ||
|
|
||
| b.ReportAllocs() | ||
| b.ResetTimer() | ||
|
|
||
| for i := 0; i < b.N; i++ { | ||
| // Use nil to strictly measure allocation of the result | ||
| _ = AppendFixed32(nil, data) | ||
| } | ||
| } | ||
|
|
||
| // Comparison: Naive Loop (No pre-calculation) | ||
| func BenchmarkAppendFixed32_Naive(b *testing.B) { | ||
AkramBitar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| data := generateBenchmarkData(1000, 256) | ||
|
|
||
| b.ReportAllocs() | ||
| b.ResetTimer() | ||
|
|
||
| for i := 0; i < b.N; i++ { | ||
| var dst []byte | ||
| for _, v := range data { | ||
| dst = binary.LittleEndian.AppendUint32(dst, uint32(len(v))) | ||
| dst = append(dst, v...) | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.