-
Notifications
You must be signed in to change notification settings - Fork 31
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
feat: reuse encrypted data to avoid memory allocation #242
Conversation
src/crypto/streaming.ts
Outdated
const encrypted = chunk.subarray(i, end) | ||
// reuse the encrypted Uint8Array to avoid memory allocation | ||
const dst = chunk.subarray(i, end - TAG_LENGTH) | ||
const { plaintext: decrypted, valid } = await handshake.decrypt(encrypted, handshake.session, dst) |
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.
I get that this is the main feature of this PR.
This seems strange though, basically using the same buffer as both the input and the output.
It seems that by writing where we're reading, the input could be modified before/during processing?
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.
@wemeetagain yeah agree that this looks strange, but if this is proved to be correct, for the performance reason (as shared in the PR description) I think we can go with it.
It seems that by writing where we're reading, the input could be modified before/during processing?
no, input is read byte by byte 1 time and immediately set to output, see https://github.com/StableLib/stablelib/blob/a89a438fcbf855de6b2e9faa2630f03c3f3b3a54/packages/chacha/chacha.ts#L174
in assemblyscript
implementation, I have a lot of unit tests for it https://github.com/ChainSafe/as-chacha20poly1305/pull/1/files#diff-25252846b58979dcaf4e41d47b3eadd7e4f335e7fb98da6c049b1f9cd011f381R48
I will add more comments for the context
24b2de9
to
7e9e0aa
Compare
Testing this PR along with |
@wemeetagain I found no issue with this branch after testing on some lodestar nodes, see ChainSafe/lodestar#4787 (comment) |
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.
LGTM. Thanks for digging into this one.
Motivation
As tested in lodestar, memory allocation is expensive for
decrypt()
flowDescription
dst?: Uint8Array
parameter in its chacha20poly1305 implementation, the newas-chacha20poly1305
should follow the same interface