Skip to content

Optimize auth module for bytecode size and gas usage #7096

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

Merged
merged 4 commits into from
Apr 19, 2025

Conversation

ironcev
Copy link
Member

@ironcev ironcev commented Apr 17, 2025

Description

This PR optimizes the std::auth module for bytecode size and gas cost by using optimization techniques similar to those used in #7087 and #7092.

The optimized public functions are listed in the below code snippet. The bytecode size of that code got reduced from 2480 bytes to 1200 bytes.

fn main() {
    let _ = std::auth::predicate_address();
    let _ = std::auth::caller_addresses();
    let _ = std::auth::caller_address();
}

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.

@ironcev ironcev requested a review from bitzoic April 17, 2025 14:32
@ironcev ironcev self-assigned this Apr 17, 2025
@ironcev ironcev added the lib: std Standard library label Apr 17, 2025
@ironcev ironcev marked this pull request as ready for review April 17, 2025 21:26
@ironcev ironcev requested review from a team as code owners April 17, 2025 21:26
bitzoic
bitzoic previously approved these changes Apr 18, 2025
@ironcev ironcev enabled auto-merge (squash) April 18, 2025 08:40
@ironcev ironcev merged commit 7cb7809 into master Apr 19, 2025
39 checks passed
@ironcev ironcev deleted the ironcev/optimize-auth-module branch April 19, 2025 10:42
ironcev added a commit that referenced this pull request May 7, 2025
## Description

This PR optimizes the `std::crypto` module for bytecode size and gas
cost by using optimization techniques similar to those used in #7087,
#7092, and #7096.

The optimized public functions are listed in the below code snippet.
**The bytecode size of that code got reduced from 46104 bytes to 44288
bytes.**

We actually expect a bigger gain here. At one point in the optimization,
the bytecode size went down to 33896 bytes. Bouncing back to ~44600 is
related to the issue described in #7150. Once that issue is solved, we
expect even smaller bytcode sizes.

```sway
script;

use std::crypto::ed25519::*;
use std::crypto::point2d::*;
use std::crypto::public_key::*;
use std::crypto::scalar::*;
use std::crypto::secp256k1::*;
use std::crypto::secp256r1::*;
use std::crypto::signature::*;

use std::vm::evm::evm_address::EvmAddress;

fn main() {
    let ed25519 = std::crypto::ed25519::Ed25519::new();
    let _ = ed25519 == ed25519;

    let point2d = std::crypto::point2d::Point2D::new();
    let _ = point2d == point2d;

    let public_key = std::crypto::public_key::PublicKey::new();
    let _ = public_key == public_key;

    let _ = public_key.is_zero();

    let scalar = std::crypto::scalar::Scalar::new();
    let _ = scalar == scalar;

    let message = std::crypto::message::Message::new();

    let secp256k1 = std::crypto::secp256k1::Secp256k1::new();
    let _ = secp256k1 == secp256k1;
    let _ = secp256k1.address(message);
    let _ = secp256k1.evm_address(message);
    let _ = secp256k1.verify(public_key, message);
    let _ = secp256k1.verify_address(Address::zero(), message);
    let _ = secp256k1.verify_evm_address(EvmAddress::zero(), message);

    let secp256r1 = std::crypto::secp256r1::Secp256r1::new();
    let _ = secp256r1 == secp256r1;
    let _ = secp256r1.address(message);
    let _ = secp256r1.evm_address(message);
    let _ = secp256r1.verify(public_key, message);
    let _ = secp256r1.verify_address(Address::zero(), message);
    let _ = secp256r1.verify_evm_address(EvmAddress::zero(), message);
}
```

## Checklist

- [x] I have linked to any relevant issues.
- [x] 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).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] 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.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lib: std Standard library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants