Skip to content

separate type casting and padding into separate behavior #298

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

Open
mr-zwets opened this issue Mar 27, 2025 · 2 comments
Open

separate type casting and padding into separate behavior #298

mr-zwets opened this issue Mar 27, 2025 · 2 comments
Labels
breaking breaking change cashc-compiler Relates to the cashc compiler enhancement New feature or request
Milestone

Comments

@mr-zwets
Copy link
Member

the current situation of when padding occurs is not very consistent and requires a confusing workaround to cast a bytes item to a certain size

// from int to bytes
bytes(int) -> typecast
bytes6(int) -> padding
bytes(int, 6) -> padding

// from bytes to bytes
bytes6(bytes) -> typecast
bytes6(int(bytes)) -> padding
bytes(int(bytes), 6) -> padding

I suggest a new function toBytesX(item, byteLength) which takes care of the padding

So the new way would be

// from int to bytes
bytes(int) -> typecast
toBytesX(int, 6) -> padding

// from bytes to bytes
bytes6(bytes) -> typecast
toBytesX(bytes, 6) -> padding

possible alternative syntaxes toBytesLength, padBytes, or resizeBytes.

what would break

The second argument in bytes(item, length) would simply be deprecated, this can be done over time, first with a warning.

bytes6(int) would simply be disallowed and result in a type error at compile time, only bytes type would be able to cast to a bounded bytes type

@mr-zwets mr-zwets added breaking breaking change cashc-compiler Relates to the cashc compiler enhancement New feature or request labels Mar 27, 2025
@mr-zwets
Copy link
Member Author

mr-zwets commented May 7, 2025

this issue also came up in telegram, raised by @dagurval (https://t.me/CashScriptBCH/423)

int n = int(tx.inputs[0].nftCommitment);
bytes nextCommitment = bytes(n + 1);

cashscript will use OP_NUM2BIN to encode nextCommitment

but what’s the length of the nextCommitment value? will it support full in64 (8 bytes) or will it overflow at some lower number?

I mean OP_NUM2BIN takes two stack items:
number to be converted and the length to encode to

what does cashscript pass as the length to encode to when casting like this “bytes(n + 1)”

Image

ok it looks like a byte-cast is simply a noop, no call to NUM2BIN needed

@rkalis rkalis added this to the v0.12.0 milestone May 8, 2025
@dagurval
Copy link
Contributor

dagurval commented May 9, 2025

My issue was that I assumed bytes cast ment a call to OP_NUM2BIN, but knowing that OP_NUM2BIN takes length as an argument, I didn't know the range of integers it would support. I was concerned that it would default to a small length and my contract would unexpectedly break at some point.

Just documenting bytes( behaviour would be sufficient for my case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking breaking change cashc-compiler Relates to the cashc compiler enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants