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

Introduce crypto module and expand cryptographic functions #6837

Merged
merged 21 commits into from
Jan 25, 2025

Conversation

bitzoic
Copy link
Member

@bitzoic bitzoic commented Jan 17, 2025

Description

This PR replaces #5747 and intends to introduce the crypto module.

Note: #6832 will also use the crypto module.

The std-lib currently contains the ecr.sw and vm/evm/ecr.sw files which have the following functions:

  • ec_recover()
  • ec_recover_r1()
  • ed_verify()
  • ec_recover_address()
  • ec_recover_address_r1()
  • ec_recover_evm_address()

There are a number of issues with this including no type safety for signatures from different elliptic curves, functions split across multiple files, poor naming, and generic arguments. All of these are resolved by this PR which deprecates both ecr.sw files and replaces them with a crypto module which syntactically matches Rust.

The following new types are introduced:

  • PublicKey - An Asymmetric public key, supporting both 64 and 32-byte public keys
  • Message - Hashed message authenticated by a signature type that handles variable lengths
  • Secp256k1 - A secp256k1 signature
  • Secp256r1 - A secp256r1 signature
  • Ed25519 - An ed25519 signature
  • Signature - An ECDSA signature

All original functionality is retained with the new module:

  • Secp256k1::recover() - Recovers a public key.
  • Secp256r1::recover() - Recovers a public key.
  • Secp256k1::address() - Recovers an address.
  • Secp256r1::address() - Recovers an address.
  • Secp256k1::evm_address() - Recovers an EVM address.
  • Ed25519::verify() - Verify that a signature matches the given public key.

The following new functionality has been added:

  • Secp256k1::verify() - Verify that a signature matches the given public key.
  • Secp256r1::verify() - Verify that a signature matches the given public key.
  • Secp256k1::verify_address() - Verify that a signature matches the given address.
  • Secp256r1::verify_address() - Verify that a signature matches the given address.
  • Secp256k1::verify_evm_address() - Verify that a signature matches the given EVM address.
  • Secp256r1::verify_evm_address() - Verify that a signature matches the given EVM address.
  • Secp256r1::evm_address() - Recovers an EVM address.

The following functions have been deprecated:

  • std::ecr::ec_recover()
  • std::ecr::ec_recover_r1()
  • std::ecr::ed_verify()
  • std::ecr::ec_recover_address()
  • std::ecr::ec_recover_address_r1()
  • std::vm::evm::ecr::ec_recover_evm_address()

Example of changes for recovering a public key:

// Before
fn foo(signature: B512, message: b256) {
     let recovered_public_key: B512 = ec_recover(signature, message).unwrap();
}

// After
fn bar(signature: Signature, message: Message) {
     let recovered_public_key: PublicKey = signature.recover(message).unwrap();
}

Example of changes for recovering an Address:

// Before
fn foo(signature: B512, message: b256) {
     let recovered_address: Address = ec_recover_address(signature, message).unwrap();
}

// After
fn bar(signature: Signature, message: Message) {
     let recovered_address: Address = signature.address(message).unwrap();
}

Complete recovery example using the Signature type:

use std::crypto::{Message, PublicKey, Secp256r1, Signature};

fn foo() {
    let secp256r1_signature = Signature::Secp256r1(Secp256r1::from((
        0xbd0c9b8792876712afadbff382e1bf31c44437823ed761cc3600d0016de511ac,
        0x44ac566bd156b4fc71a4a4cb2655d3da360c695edb27dc3b64d621e122fea23d,
    )));
    let signed_message = Message::from(0x1e45523606c96c98ba970ff7cf9511fab8b25e1bcd52ced30b81df1e4a9c4323);
    
    // A recovered public key pair.
    let secp256r1_public_key = secp256r1_signature.recover(signed_message);
    assert(secp256r1_public_key.is_ok());
    assert(
        secp256r1_public_key
            .unwrap() == PublicKey::from((
            0xd6ea577a54ae42411fbc78d686d4abba2150ca83540528e4b868002e346004b2,
            0x62660ecce5979493fe5684526e8e00875b948e507a89a47096bc84064a175452,
        )),
    );
}

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@bitzoic bitzoic self-assigned this Jan 17, 2025
@bitzoic bitzoic marked this pull request as ready for review January 20, 2025 12:01
@bitzoic bitzoic requested review from a team as code owners January 20, 2025 12:01
IGI-111
IGI-111 previously approved these changes Jan 20, 2025
@bitzoic bitzoic added enhancement New feature or request lib: std Standard library labels Jan 20, 2025
IGI-111
IGI-111 previously approved these changes Jan 20, 2025
jjcnn
jjcnn previously approved these changes Jan 20, 2025
Copy link
Contributor

@jjcnn jjcnn left a comment

Choose a reason for hiding this comment

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

I don't understand the cryptography that is going on, but the changes to std look fine. I've added a few nits, but nothing that would prevent approval.

sway-lib-std/src/ecr.sw Outdated Show resolved Hide resolved
sway-lib-std/src/ecr.sw Outdated Show resolved Hide resolved
sway-lib-std/src/ecr.sw Outdated Show resolved Hide resolved
sway-lib-std/src/ecr.sw Outdated Show resolved Hide resolved
sway-lib-std/src/ecr.sw Outdated Show resolved Hide resolved
sway-lib-std/src/crypto/ed25519.sw Show resolved Hide resolved
K1-R1
K1-R1 previously approved these changes Jan 21, 2025
Copy link
Member

@K1-R1 K1-R1 left a comment

Choose a reason for hiding this comment

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

🚀

@bitzoic bitzoic dismissed stale reviews from K1-R1, jjcnn, and IGI-111 via 8bc9635 January 21, 2025 15:32
@bitzoic bitzoic requested a review from K1-R1 January 21, 2025 15:32
K1-R1
K1-R1 previously approved these changes Jan 21, 2025
@bitzoic bitzoic requested review from IGI-111 and jjcnn January 22, 2025 09:16
jjcnn
jjcnn previously approved these changes Jan 22, 2025
@bitzoic bitzoic dismissed stale reviews from jjcnn and K1-R1 via 16d7e27 January 23, 2025 10:49
@bitzoic bitzoic requested a review from K1-R1 January 23, 2025 10:49
K1-R1
K1-R1 previously approved these changes Jan 23, 2025
Copy link
Collaborator

@SwayStar123 SwayStar123 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 otherwise

sway-lib-std/src/crypto/ed25519.sw Outdated Show resolved Hide resolved
sway-lib-std/src/crypto/secp256r1.sw Outdated Show resolved Hide resolved
@bitzoic bitzoic requested a review from jjcnn January 24, 2025 11:01
@bitzoic bitzoic merged commit b03c76e into master Jan 25, 2025
43 checks passed
@bitzoic bitzoic deleted the bitzoic-signature branch January 25, 2025 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request lib: std Standard library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants