-
Notifications
You must be signed in to change notification settings - Fork 479
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
On-chain function to convert from Integer to ByteString, or to parse ByteString into Integer #3657
Comments
Hi, thanks @longngn. This is actually already being worked on to allow for Marlowe scalability improvements. I'll link a PR once there's one ready. |
Thank James for working on this feature! Is it just serialize/deserialize Integer or any arbitrary IsData instance? |
This might also fix #4168 . Is there any update on this? |
Actually I've written an Integer -> BS function for our internal contracts. Happy to write the reverse direction (BS -> Integer) and property-based testing and to create a PR -- Convert from an integer to its text representation. Example: 123 => "123"
{-# INLINEABLE integerToBS #-}
integerToBS :: Integer -> BuiltinByteString
integerToBS x
-- 45 is ASCII code for '-'
| x < 0 = consByteString 45 $ integerToBS (negate x)
-- x is single-digit
| x `quotient` 10 == 0 = digitToBS x
| otherwise = integerToBS (x `quotient` 10) <> digitToBS (x `remainder` 10)
where
digitToBS :: Integer -> BuiltinByteString
-- 48 is ASCII code for '0'
digitToBS d = consByteString (d + 48) emptyByteString |
Thanks! Ah, you meant ASCII? I thought this issue was for getting a binary representation of it. |
+1 for binary representation as we're trying to enable efficient cryptographic behaviors. |
As you point out here, there is no unique |
Isn't there only one obvious one? I.e. a base 256 representation.
|
No, there are many:
|
Little-endian is probably best because that's probably what most of us are used to dealing with. |
Perhaps we can use ZigZag encoding to encode variable-length signed integer like in Plutus Core |
We now have
I don't know if we have any parsing capabilities. @zliu41 do you happen to have any input on this issue? |
Requests for adding new builtin functions should be discussed in https://github.com/cardano-foundation/CIPs. For Plutus Tx library functions, my main concern is that converting integers to/from bytestrings without using new builtins, regardless of encoding, can be quite expensive (perhaps except |
To iterate on what @michaelpj and @zliu41 said, the choices that we have are:
So given the lack of consensus and what appears to be a low priority problem, I'm going to label the issue with "Low priority". |
There now exist the |
Area
Describe the feature you'd like
Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
Additional context / screenshots
Add any other context or screenshots about the feature request here.
The text was updated successfully, but these errors were encountered: