Skip to content
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

Add builtin ByteArray type #2933

Merged
merged 28 commits into from
Aug 13, 2024
Merged

Add builtin ByteArray type #2933

merged 28 commits into from
Aug 13, 2024

Conversation

paulcadman
Copy link
Collaborator

@paulcadman paulcadman commented Aug 1, 2024

This PR adds support for a builtin ByteArray type and associated functions for constructing a ByteArray from a list of bytes and a function to query the size of the ByteArray. It is only available in the Anoma backend.

In Core / Tree, ByteArray constant is stored using a Haskell ByteString.

In Anoma the ByteArray is stored as a cell where the head is the length of the ByteArray and the tail is an integer is an integer formed by concatenating the bytes in the array using little-endian byte ordering.

The Nock for constructing a ByteArray uses the length, add, folder and lsh functions from the Anoma hoon stdlib. See the code comment for more details.

Example:

module test082;

import Stdlib.Prelude open;
import Stdlib.Debug.Trace open;

builtin bytearray
axiom ByteArray : Type;

builtin bytearray-from-list-byte
axiom mkByteArray : List Byte -> ByteArray;

builtin bytearray-size
axiom size : ByteArray -> Nat;

bs0 : ByteArray := mkByteArray [];

bs1 : ByteArray := mkByteArray [0x0; 0x0; 0x0];

bs2 : ByteArray := mkByteArray [0x1; 0x0; 0x0; 0x0];

bs3 : ByteArray := mkByteArray [0x2; 0x1];

bs4 : ByteArray := mkByteArray [0x100];

main : ByteArray :=
  trace (size bs0)
   >-> trace bs0
   >-> trace (size bs1)
    >-> trace bs1
    >-> trace (size bs2)
    >-> trace bs2
    >-> trace (size bs3)
    >-> trace bs3
    >-> trace (size bs4)
    >-> bs4;

Output using tests/Anoma/Compilation/positive/test082.juvix

$ juvix compile anoma -g test082.juvix
$ juvix dev nockma run test082.pretty.nockma
0
[0 0]
3
[3 0]
4
[4 1]
2
[2 258]
1
[1 0]

@paulcadman paulcadman added this to the 0.6.5 milestone Aug 1, 2024
@paulcadman paulcadman self-assigned this Aug 1, 2024
@paulcadman paulcadman force-pushed the support-bytearray branch 6 times, most recently from a35367c to fa068a3 Compare August 8, 2024 15:57
@paulcadman paulcadman marked this pull request as ready for review August 9, 2024 12:51
@paulcadman paulcadman merged commit ce5c2c5 into main Aug 13, 2024
4 checks passed
@paulcadman paulcadman deleted the support-bytearray branch August 13, 2024 10:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants