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

[Rust]: Implement Private and Public keys #3107

Merged
merged 24 commits into from
Apr 26, 2023
Merged

[Rust]: Implement Private and Public keys #3107

merged 24 commits into from
Apr 26, 2023

Conversation

satoshiotomakan
Copy link
Collaborator

@satoshiotomakan satoshiotomakan commented Apr 21, 2023

Description

Transaction signing is the key responsibility of wallet-core. We need to implement the PrivateKey and PublicKey structures to be able to move one of the blockchains from C++ to Rust.

How to test

Run Rust and C++ tests

Types of changes

  • Add and implement TWPrivateKey and TWPublicKey structures in Rust.
  • Export Private and Public keys FFI
  • Implement the secp256k1 curve.

Note

Found an issue in k256 crate - RustCrypto/elliptic-curves#886
Fixed by disabling the precomputed-tables feature.

Checklist

* Add tests to check if `k256` suits our requirements
* Add `tw_hash::Hash<N>`
* Implement `tw_keeper::secp256k1` Private, Public keys, signing and verifying
* Export FFI interface
* Add `secp256k1::KeyPair` structure
* Move `TWPrivateKey` and `TWPublicKey` to the `tw_keypair::tw` module
* Refactor `TWPublicKey` by making it enum
* TODO add `TWKeyPair`
* Add a documentation
* Comment out `secp256k1::PrivateKey::shared_key_hash` as it's not finished yet
@satoshiotomakan satoshiotomakan self-assigned this Apr 21, 2023
@satoshiotomakan satoshiotomakan linked an issue Apr 21, 2023 that may be closed by this pull request
6 tasks
@satoshiotomakan satoshiotomakan changed the base branch from master to dev April 24, 2023 08:34
@satoshiotomakan satoshiotomakan marked this pull request as ready for review April 24, 2023 09:54
Copy link
Contributor

@lamafab lamafab left a comment

Choose a reason for hiding this comment

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

Looks good, nicely designed structures and impls. The types should have //! documentation, however. Also, I'd like to note that the RustCrypto project explicitly states:

The secp256k1 elliptic curve arithmetic contained in this crate has never been independently audited!

Just wanted to mention that, but I assume you already discussed this with @Milerius. Regarding the testing custom cryptography implementations, I'd generally recommend testing this against well-established tools such as openssl (e.g. generating tons of random data, signing the data with both tools and then comparing the output), but if you copied the tests from C++ then this is probably fine.

rust/tw_keypair/src/tw/public.rs Show resolved Hide resolved
rust/tw_starknet/src/ffi.rs Outdated Show resolved Hide resolved
rust/tw_keypair/src/tw/private.rs Show resolved Hide resolved
rust/tw_keypair/src/tw/private.rs Outdated Show resolved Hide resolved
rust/tw_keypair/src/secp256k1/private.rs Show resolved Hide resolved
Copy link
Collaborator

@Milerius Milerius left a comment

Choose a reason for hiding this comment

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

Can you add support for this crate: https://crates.io/crates/zeroize to zeroize memory after usage and signing in general?

Probably ok to delete C++ tests that are replicated/moved to rust then.

Copy link
Collaborator

@Milerius Milerius left a comment

Choose a reason for hiding this comment

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

first review iteration, globally excellent job, let's ensure for top clarity here

rust/tw_keypair/src/secp256k1/signature.rs Outdated Show resolved Hide resolved
rust/tw_keypair/src/starkex/mod.rs Outdated Show resolved Hide resolved
* CI Run tests in WASM
Copy link
Contributor

@hewigovens hewigovens left a comment

Choose a reason for hiding this comment

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

It's not clear in the pr description that we introduce k256 to implement ecdsa signature; I think we don't need to use TW prefix like TWPrivateKey, it made me confused with the C interface.

rust/Cargo.toml Outdated Show resolved Hide resolved
rust/tw_hash/src/hash_array.rs Show resolved Hide resolved
tests/chains/Ethereum/SignerTests.cpp Show resolved Hide resolved
rust/tw_keypair/src/tw/mod.rs Outdated Show resolved Hide resolved
@hewigovens
Copy link
Contributor

Also this note from https://github.com/RustCrypto/elliptic-curves/tree/master/k256 should be highlighted, do we have a checklist for the auditor to take more eyes on?

⚠️ Security Warning
The secp256k1 elliptic curve arithmetic contained in this crate has never been independently audited!

This crate has been designed with the goal of ensuring that secret-dependent secp256k1 operations are performed in constant time (using the subtle crate and constant-time formulas). However, it has not been thoroughly assessed to ensure that generated assembly is constant time on common CPU architectures.

USE AT YOUR OWN RISK!

@satoshiotomakan
Copy link
Collaborator Author

@hewigovens @lamafab thank you for noticing. We've scheduled a call with the security team.

do we have a checklist for the auditor to take more eyes on?

@Milerius
Copy link
Collaborator

I think we don't need to use TW prefix like TWPrivateKey, it made me confused with the C interface.

It's actually C Interfaces generated Rust<->C through cbindgen, at some point the idea is to generate the TW bindings this way too.

@satoshiotomakan
Copy link
Collaborator Author

@hewigovens @Milerius @lamafab thank you for the advices!
The PR is ready for the next review.

Copy link
Contributor

@hewigovens hewigovens left a comment

Choose a reason for hiding this comment

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

lgtm

@Milerius Milerius merged commit cb5be68 into dev Apr 26, 2023
@Milerius Milerius deleted the s/rust-keeper branch April 26, 2023 10:26
SuperGNUS pushed a commit to GeniusVentures/wallet-core that referenced this pull request May 11, 2023
@satoshiotomakan satoshiotomakan mentioned this pull request Oct 9, 2023
12 tasks
EduMenges pushed a commit to GeniusVentures/wallet-core that referenced this pull request Jun 17, 2024
EduMenges pushed a commit to GeniusVentures/wallet-core that referenced this pull request Jun 18, 2024
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.

[Rust]: Implement Private and Public keys
4 participants