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

Move box/unbox message into Rust #1872

Merged
merged 23 commits into from
Aug 10, 2022
Merged

Move box/unbox message into Rust #1872

merged 23 commits into from
Aug 10, 2022

Conversation

mat-if
Copy link
Contributor

@mat-if mat-if commented Jul 27, 2022

Summary

Replaces tweetnacl with napi bindings to a similar Rust API. Perf testing shows its 10-30x faster, with tasks generally taking <0.05ms, which is faster than it takes to send a job to the worker pool, so this should be an overall slight performance increase.

Closes IRO-2448

Testing Plan

Breaking Change

Is this a breaking change? If yes, add notes below on why this is breaking and
what additional work is required, if any.

[ ] Yes

ff = "0.12.0"
group = "0.12.0"
jubjub = "0.9.0"
lazy_static = "1.4.0"
rand = "0.8.5"
rust-crypto-wasm = "0.3.1" # in favor of rust-crypto as this one is wasm friendly
tiny-bip39 = "1.0.0"
tiny-bip39 = "0.8"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were on 0.8 until today when I merged the rust dep upgrades, but 1.0 has a version conflict with some library deeeeeeeep in crypto_box so rolling back for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oof, that's inconvenient.

@dguenther
Copy link
Member

I can review this once it's ready, but you might consider adding a test decrypting messages generated by tweetnacl. It'd be nice to have the reverse as well (checking that tweetnacl can decrypt messages encrypted by this version), but it'd probably require us to keep tweetnacl around as a dev dependency which is a bit of a pain.

@NullSoldier
Copy link
Contributor

Since we're moving this into the main thread, can you convince me that this won't block the event loop now compared to it being off the main thread? Some performance numbers would suffice. What's your thinking here?

@mat-if
Copy link
Contributor Author

mat-if commented Jul 27, 2022

Since we're moving this into the main thread, can you convince me that this won't block the event loop now compared to it being off the main thread? Some performance numbers would suffice. What's your thinking here?

Ah, I meant to include the perf screenshot that kicked this off. Derek mentioned that he was curious about box/unbox in rust, so I ran some tests. This is 25-30x faster, taking roughly 100 _micro_seconds, which is substantially less time than it even takes to serialize the data to the worker pool (as seen by comparing the direct node function call and the worker pool node call). The messages I used to test this were real webrtc messages grabbed from my live node.
image

@mat-if
Copy link
Contributor Author

mat-if commented Jul 30, 2022

notes to self:

speed comparison of "newKeyPair" in ms:

NODE: 7.387
RUST: 0.088

NODE: 0.67
RUST: 0.048

NODE: 0.597
RUST: 0.044

NODE: 0.606
RUST: 0.043

NODE: 0.661
RUST: 0.046

NODE: 1.397
RUST: 0.047

NODE: 0.593
RUST: 0.04

NODE: 0.594
RUST: 0.039

NODE: 0.584
RUST: 0.04

NODE: 0.593
RUST: 0.046

}

impl Error for IronfishError {}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think eventually we might think about putting our errors into one enum to make them a little easier to work with, similar to https://doc.rust-lang.org/std/io/enum.ErrorKind.html or something. I don't know exactly the best approach yet, but I think this works for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the current error handling setup is kind of inconvenient. Not too concerned with making a better one as part of this PR

ironfish-rust/src/lib.rs Outdated Show resolved Hide resolved
message = null
}

return { message }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is strange - previously it would just kind of silently fail. Now we have errors being thrown. I could make it fail silently like the previous approach, but that doesn't seem right. I'm a little too rust-brained at the moment to think of a better way to deal with this right now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know that the previous approach was correct, but I think it's fine to maintain the behavior here and fix it in a different PR if necessary.

@@ -0,0 +1,83 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a tests folder for this test file. It's just a compatibility test with tweetnacl, which has been moved to a dev dependency. I think it's probably fine to delete this file a couple weeks after this merges, and then we can remove the tweetnacl dependency completely.

@mat-if mat-if marked this pull request as ready for review August 6, 2022 22:05
@mat-if mat-if requested a review from a team as a code owner August 6, 2022 22:05
ironfish/package.json Outdated Show resolved Hide resolved
@linear
Copy link

linear bot commented Aug 10, 2022

@dguenther dguenther merged commit 15e5bb1 into staging Aug 10, 2022
@dguenther dguenther deleted the mat/rust-box branch August 10, 2022 19:51
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.

None yet

3 participants