Skip to content

Investigate using blake2s logic from types-rs repo #2159

Open

Description

@gabrielbosio

Investigate if the VM can use the blake2s functions defined in types-rs to simplify codebase.

We need to check if the behavior and performance of the vm does not change.

Links:

Activity

  1. changed the title [-]Use blake2s logic from `types-rs` repo[/-] [+]Investigate using blake2s logic from `types-rs` repo[/+] on Aug 11, 2025
  2. JulianGCalderon commented on Aug 13, 2025

    @JulianGCalderon
    Contributor

    The current types-rs API does not work for our use case. Currently, it exposes:

    pub fn blake2s_to_felt(data: &[u8]) -> Felt;

    It hashes all the data at once, which is not what we need.

    From the Cairo VM (and also in Cairo Native), we need a much low-level API for Blake2. We would need something like this:

    pub fn blake2s_compress(
        state: &[u32; 8],
        message: &[u32; 16],
        t0: u32,
        t1: u32, // optional? always zero.
        f0: u32,
        f1: u32, // optional? always zero.
    ) -> [u32; 8];

    As this API requires the user to know the initial state, we also also need a way to obtain either the IV, or the initial state.

    pub fn get_initial_state() -> [u32; 8]; // or
    pub fn get_iv() -> [u32; 8];

    The Rust crate blake2 does not expose this functionality, so we would need to implement it manually. See blake2s_hash.rs.

    In addition to the mentioned functions, the following utilities would come in handy:

    pub fn encode_u128s_to_u32s_le(numbers: &[u128]) -> Vec<u32>;
    pub fn encode_u128s_to_u32s_be(numbers: &[u128]) -> Vec<u32>;

    The following utilities are already implemented, and can be used:

    pub fn encode_felts_to_u32s(felts: &[Felt]) -> Vec<u32>;
    pub fn pack_256_le_to_felt(bytes: &[u8]) -> Felt;
  3. added
    refactorCode chanegs aimed at improving maintainability or removing deprecated features.
    researchOpen questions requiring either research or requirements gathering
    on Aug 28, 2025
  4. moved this to Backlog in Starkneton Aug 28, 2025
  5. added
    check-outdatedSome research needed to verify the issue is not stale or should be closed.
    on Aug 28, 2025
  6. moved this from Backlog to Todo in Starkneton Aug 28, 2025
  7. removed
    check-outdatedSome research needed to verify the issue is not stale or should be closed.
    on Sep 1, 2025
  8. moved this from Todo to In Progress in Starkneton Sep 15, 2025
  9. gabrielbosio commented on Sep 16, 2025

    @gabrielbosio
    ContributorAuthor

    Moving blake2s logic from the VM to types-rs implies removing the blake2s_compress function which is public and not intended to be removed in the next releases.

  10. moved this from In Progress to Done in Starkneton Sep 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    refactorCode chanegs aimed at improving maintainability or removing deprecated features.researchOpen questions requiring either research or requirements gathering

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions