Skip to content

Inlining a function with an asm block accepting an array as input blows up the bytecode size #7150

@ironcev

Description

@ironcev

Inlining a function with a single asm block accepting an array as input blows up the bytecode size.

A minimum repro is given below.

When the eq function is not inlined, the bytecode size is 1656 (without new encoding).
When the eq function is inlined, the bytecode size is 4952 (without new encoding).

script;

use std::crypto::message::Message;
use std::crypto::public_key::PublicKey;
use std::hash::sha256;

enum SignatureError {
    InvalidSignature: (),
}

pub struct Secp256k1 {
    bits: [u8; 64],
}

impl Secp256k1 {
    pub fn new() -> Self {
        Self {
            bits: [0u8; 64],
        }
    }

    pub fn recover(self, message: Message) -> Result<PublicKey, SignatureError> {
        Ok(PublicKey::from(message.try_into().unwrap()))
    }

    pub fn address(self, message: Message) -> Result<Address, SignatureError> {
        match self.recover(message) {
            Ok(pub_key) => Ok(Address::from(sha256(pub_key))),
            Err(e) => Err(e),
        }
    }
}

impl PartialEq for Secp256k1 {
    // #[inline(always)]
    #[inline(never)]
    fn eq(self, other: Self) -> bool {
        asm(result, r2: self.bits, r3: other.bits, r4: 64) {
            meq result r2 r3 r4;
            result: bool
        }
    }
}

fn main() {
    let message = Message::new();
    let secp256k1 = Secp256k1::new();
    let _ = secp256k1 == secp256k1;
    let _ = secp256k1.address(message);
    let _ = secp256k1.address(message);
}

Metadata

Metadata

Assignees

Labels

compilerGeneral compiler. Should eventually become more specific as the issue is triagedcompiler: irIRgen and sway-ir including optimization passesteam:compilerCompiler Team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions