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

Remove ecr.sw files and introduce crypto module #5747

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

bitzoic
Copy link
Member

@bitzoic bitzoic commented Mar 19, 2024

Description

The std-lib currently contains the ecr.sw and 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 removes both ecr.sw files replaces them with a crypto module which syntatically matches Rust.

The following new types are introduced:

  • PublicKey - 64 byte public key
  • Messege - Hashed message authenticated by a signature
  • Secp256k1 - A secp256k1 signature
  • Secp256r1 - A secp256r1 signature
  • Ed25519 - An ed25519 signature
  • Signature - An ECDSA signature

All original functionally is retained with the new module. The following new functionality is added:

  • verify() - Verify that a signature matches given public key. NOTE: Only new functionality for secp256k1 and secp256r1 signatures.
  • verify_address() - Verify that a signature matches given address.
  • verify_evm_address() - Verify that a signature matches given 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 sig_hi = 0xbd0c9b8792876712afadbff382e1bf31c44437823ed761cc3600d0016de511ac;
    let sig_lo = 0x44ac566bd156b4fc71a4a4cb2655d3da360c695edb27dc3b64d621e122fea23d;
    let msg_hash = 0x1e45523606c96c98ba970ff7cf9511fab8b25e1bcd52ced30b81df1e4a9c4323;
    let pub_hi = 0xd6ea577a54ae42411fbc78d686d4abba2150ca83540528e4b868002e346004b2;
    let pub_lo = 0x62660ecce5979493fe5684526e8e00875b948e507a89a47096bc84064a175452;
    
    let signature: Signature = Signature::Secp256r1(Secp256r1::from((sig_hi, sig_lo)));
    let message: Message = Message::from(msg_hash);
    let public_key: PublicKey = PublicKey::from((pub_hi, pub_lo));

    // A recovered public key pair.
    let result_public_key = signature.recover(message);

    assert(result_public_key.is_ok());
    assert(result_public_key.unwrap() == public_key);
}

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 added lib: std Standard library breaking May cause existing user code to break. Requires a minor or major release. labels Mar 19, 2024
@bitzoic bitzoic self-assigned this Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking May cause existing user code to break. Requires a minor or major release. lib: std Standard library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant