Skip to content

feat: Better ByteArray manipulation and reading #7991

@adambor

Description

@adambor

Feature Request

Describe the Feature Request

Currently the ByteArray implementation has just a rudimentary set of operations and doesn't allow reading/writting primitive types other than u8. This means a project which wants to read i.e. u32 or u64 from an array is forced to create an own implementation which reads the data byte-by-byte from the underlying ByteArray, example here. Having a possibility to directly read/write primitive types (u8 - u256) from/to ByteArray can significantly improve developer experience and also result in a significant reduction of step count.

Describe Preferred Solution

Since the underlying data structure of the ByteArray is an array of bytes31, all of the primitive read/write operations (u8 - u256) can be done in at most 2 array accesses, this can significantly reduce step count of programs relying heavily on reading/writting binary data.

Describe Alternatives

The Alexandria library also offers a variety of functions to read/write primitive types from the ByteArray however all of those only read the values byte-by-byte and don't leverage the possibility to read the values as a whole in a fewer array accesses.

Related Code

An example code of how this can be accomplished is available here. This also provides a to_u32_array which is useful when trying to sha256 hash the ByteArray (currently the core library uses byte-by-byte reading when passing the ByteArray to the sha256 hash function).

If the feature request is approved, would you be willing to submit a PR?
(Help can be provided if you need assistance submitting a PR)

  • Yes
  • No

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions