Skip to content

Optimize slices and builders comparation (for addresses) #3438

@imartemy1524

Description

@imartemy1524

Right now comparison between slice and builder require one to convert builder to slice using ENDC CTOS (600+ gas), and then comparing to slices using SDEQ.

But this task can be done using 2 HASHEXT_SHA256 calls, for example:

int equal_slice_and_builder_bits?(slice s, builder b) asm """
OVER SBITS                             // a b bits
8 PUSHINT MOD 8 PUSHINT SUBR           // a b bits_to_add
TUCK STZEROES 1 PUSHINT HASHEXT_SHA256 // a bits_to_add hashb
ROT NEWC STSLICE                       // bits_to_add hashb b_a
ROT STZEROES 1 PUSHINT HASHEXT_SHA256 // hashb hasha
EQUAL
""";

By running function above, one can compare the sha256 hashes of bits of slice a and builder b, and it consumes less than 500 gas.

This is applicable for verifying the addresses of child contracts: instead of converting builder to a cell, then to a slice (so-called Address in tact) it is cheaper to compare the initial builder with slice using TVM HASHEXT_SHA256 function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind: gas!Gas consumption and fee-related things

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions