Skip to content
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

Merged
merged 1 commit into from
Nov 22, 2022

Conversation

twoeths
Copy link
Contributor

@twoeths twoeths commented Oct 25, 2022

Motivation
As tested in lodestar, memory allocation is expensive for decrypt() flow

Screen Shot 2022-10-25 at 11 13 58

Description

  • Stablelib already supports option dst?: Uint8Array parameter in its chacha20poly1305 implementation, the new as-chacha20poly1305 should follow the same interface
  • When decrypting, reuse the same memory as encrypted message is not used after that

@twoeths twoeths requested a review from a team as a code owner October 25, 2022 09:08
Comment on lines 37 to 40
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)
Copy link
Member

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?

Copy link
Contributor Author

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

@twoeths
Copy link
Contributor Author

twoeths commented Nov 18, 2022

Testing this PR along with as-chacha20poly1305 on some lodestar nodes to confirm the correctness of this work

@twoeths
Copy link
Contributor Author

twoeths commented Nov 21, 2022

Testing this PR along with as-chacha20poly1305 on some lodestar nodes to confirm the correctness of this work

@wemeetagain I found no issue with this branch after testing on some lodestar nodes, see ChainSafe/lodestar#4787 (comment)

Copy link
Member

@wemeetagain wemeetagain left a 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.

@mpetrunic mpetrunic merged commit 0b4b7f6 into master Nov 22, 2022
@mpetrunic mpetrunic deleted the tuyen/reuse-decrypt-allocation branch November 22, 2022 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants