-
Notifications
You must be signed in to change notification settings - Fork 375
CIP-0168? | More BuiltinValue Functions
#1090
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
27f4a42
a64128e
a32e0b0
560158d
6707d00
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,150 @@ | ||||||||||
| --- | ||||||||||
| CIP: 168 | ||||||||||
| Title: More `BuiltinValue` Functions | ||||||||||
| Category: Plutus | ||||||||||
| Status: Proposed | ||||||||||
| Authors: | ||||||||||
| - fallen-icarus <[email protected]> | ||||||||||
| Implementors: [] | ||||||||||
| Discussions: | ||||||||||
| - https://github.com/cardano-foundation/CIPs/pull/1090 | ||||||||||
| Created: 2025-10-01 | ||||||||||
| License: CC-BY-4.0 | ||||||||||
| --- | ||||||||||
|
|
||||||||||
| ## Abstract | ||||||||||
|
|
||||||||||
| [CIP-0153][1] added a new `BuiltinValue` type to make working with on-chain values more efficient. | ||||||||||
| However, it added only a few builtin functions for working with this new `BuiltinValue` type which | ||||||||||
| limits its real world usability. Adding and maintaining builtin functions is costly, but the | ||||||||||
| importance of validating values in the eUTxO model justifies a wider range of builtin functions for | ||||||||||
| this purpose. With this in mind, this CIP proposes a few extra builtin functions to improve the | ||||||||||
| usability of this new `BuiltinValue` type while still trying to minimize the overall maintenance | ||||||||||
| burden. | ||||||||||
|
|
||||||||||
| ## Motivation: why is this CIP necessary? | ||||||||||
|
|
||||||||||
| > UPLC is not a general-purpose programming language. It is a language specifically designed to | ||||||||||
| > write validation logic to set constraints for the creation and transfer of Value and Data across | ||||||||||
| > the Cardano blockchain. **Given that half of the entire purpose of this language is to express | ||||||||||
| > constraints over the creation and transfer of** `Value`, why is Value treated as a standard | ||||||||||
| > library concept rather than a first-class language primitive? | ||||||||||
|
|
||||||||||
| This was the motivation for [CIP-0153][1]. Given the overwhelming importance for validating `Value`, | ||||||||||
| it makes sense for `BuiltinValue` to have a reasonably disproportionate number of builtin operations | ||||||||||
| as compared to `BuiltinList` or `BuiltinArray`. The current builtins added in CIP-0153 do not cover | ||||||||||
| many of the use cases for `Value`. As a reference, see the Aiken stdlib's [functions on `Value`][2]. | ||||||||||
| Functions like `tokens`, `negate`, and `policies` are not possible to implement using the CIP-0153 | ||||||||||
| builtin functions which means they will not benefit from the new `BuiltinValue` type. Plutus needs | ||||||||||
| enough builtins to cover the main operations on `BuiltinValue`, either through generalizable | ||||||||||
| builtins or dedicated ones. | ||||||||||
|
|
||||||||||
| ## Specification | ||||||||||
|
|
||||||||||
| This CIP proposes adding the following new builtins functions: | ||||||||||
|
|
||||||||||
| ```haskell | ||||||||||
| type BuiltinCurrencySymbol = BuiltinByteString | ||||||||||
|
|
||||||||||
| -- | Negate all values in a `BuiltinValue`. | ||||||||||
| negate :: BuiltinValue -> BuiltinValue | ||||||||||
|
|
||||||||||
| -- | Intersection of two `BuiltinValue`s. Returns data in the first value for the (policy ids, token | ||||||||||
| -- names) existing in both values. | ||||||||||
| intersection :: BuiltinValue -> BuiltinValue -> BuiltinValue | ||||||||||
|
|
||||||||||
| -- | Returns all policy ids found in the value. | ||||||||||
| policies :: BuiltinValue -> List [BuiltinCurrencySymbol] | ||||||||||
|
||||||||||
| policies :: BuiltinValue -> List [BuiltinCurrencySymbol] | |
| policies :: BuiltinValue -> List BuiltinCurrencySymbol |
or
| policies :: BuiltinValue -> List [BuiltinCurrencySymbol] | |
| policies :: BuiltinValue -> [BuiltinCurrencySymbol] |
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but I'm not sure what is the right syntax for Data . I went off of CIP-0153.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be BuiltinList BuiltinCurrencySymbol where type BuiltinCurrencySymbol = BuiltinData
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find the naming a bit confusing since it returns the same type it is given. How about restrictPolicyTo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps it is better to make restrictPolicyTo be:
restrictPolicyTo :: [BuiltinCurrencySymbol] -> BuiltinValue -> BuiltinValueThis would support a higher-level lookupTokens as well as aiken's restricted_to (here).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be a bit annoying: it would make this proposal much more concrete to have an appendix listing all the interesting functions you want to implement with their implementations using the new primitives. I think they're presumably all quite simple, and it would make the case quite compelling.
Ideally we'd also be able to see how this improves the costing behaviour of these functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding
negatetouniongives us a group onValue. The obvious missing thing to me is scalar multiplication (to give us a module). Does anyone need that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't personally have a use for scalar multiplication (right now), but I could see it being useful to others.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
negatejust scalar multiplication with the scalar being-1? In that case I think instead of addingnegatewe should just addscale?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@colll78 could this do what you need to do for #1090 (comment)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could, but it would be significantly less efficient than
negateValue(roughly 64% more expensive) if we are to assume there is a similar costing difference tomultiplyInteger -1 nandsubtractInteger 0 nThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two builtins only perform the respective arithmetic operations,
scaleValuewill have to traverse the givenValue, which is going to make the discrepancy less pronounced.There's a different point however:
multiplyIntegerwas surprisingly tricky to cost, so that's gonna propagate intoscaleValue(so much so that it may not even be feasible to implement that with the current costing machinery, not sure), whilenegateValuewould be very straightforward. @kwxm what do you think?If Kenneth agrees costing
scaleValueis hard, then I personally don't mind addingnegateValueright away, given your reasoning here, because even with fastercaseListandcasePair, the derivativenegateValueis probably going to be at least an order of magnitude more expensive than a builtin one (not least becausenegateValueis guaranteed to produce a well-formedValueso we don't need to check any of the invariants (except for quantities being in their range), while a derivative implementation will have to, because we don't provide an unsafe builtin that could create aValuewhile avoiding the checks).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
multiplyInteger -1 nis 64% more expensive thansubtractInteger 0 n? If that's the case I'd be surprised, and it would suggest that eithermultiplyIntegerorsubtractIntegeris probably not costed properly.Regarding costing of
multiplyIntegerin general - since we cap the range of quantities, we should be able to consider it constant time.Whether it's
negateValueorscaleValue, it will be linear whether it's a builtin or not. Certainly the constant factors will differ greatly, but a builtin would be much more useful if it is asymptotically more expensive without it. If we are going to add one more builtin for the intra-era HF, are we absolutely sure that it should benegateValue/scaleValue?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that actually doesn't matter. We're now restricting the
Quantitytype to be in the range[-2^127, 2^127-1](although it might make sense to make the lower bound-2^127 + 1to avoid surprises), so if were scaling the valuevbynandvis nonempty andnis outside theQuantityrange then the scalar multiplication is bound to fail for at least one of the tokens and we might as well fail right at the start. This means that we never need to consider scalars of more than size 2, so we can probably just ignore the size of the scalar and base the cost on the total number of tokens.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can also try both
negateValueandscaleValueand if scaling by -1 isn't significantly more expnsive than negation (and I suspect that it won't be) then we may as well go forscaleValue. We could also implementscaleValueand check if the scalar is -1 and use negation internally in that case if it's faster, although it might be a little tricky to reflect any improvement from doing that in the costing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great points, thank you, I withdraw my concerns.