-
Notifications
You must be signed in to change notification settings - Fork 334
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
CIP-0058? | Bitwise primitives #283
Conversation
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.
Looking good!
CIP-?/README.md
Outdated
``` | ||
Performs a bitwise shift of the first argument by a number of bit positions | ||
equal to the absolute value of the second argument, the direction of the shift | ||
being indicated by the sign of the second argument. |
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.
This still doesn't say how the direction is indicated!
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.
Clarified in the text, also for rotations.
CIP-?/README.md
Outdated
of the represented byte sequence being treated in little-endian, | ||
least-significant-bit-first encoding. For example, consider the byte sequence | ||
$s = 110110011100000$; the `BuiltinByteString` literal corresponding to this would | ||
be `"\217\224"`. |
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.
Aha, are you referring to how the IsString
instance works? I think it would be clearer to avoid that. For the purposes of this document BuiltinByteString
s are just bytestrings.
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.
Rewritten to avoid completely.
CIP-?/README.md
Outdated
all $i \in \\{ 0,1,\ldots,n \\}$, $s_i \in \\{ 0, 1 \\}$. A bit sequence | ||
$s = s_n s_{n-1} \ldots s_0$ is a *byte sequence* if $n = 8k - 1$ for some | ||
$k \in \mathbb{N}$. We denote the *empty bit sequence* (and, indeed, empty byte | ||
sequence as well) by $\emptyset$. |
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.
How about this:
"...by
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've gone for a slightly different rephrasing: the ordering is still specified, but a little more clearly.
CIP-?/README.md
Outdated
|
||
We assume that `BuiltinByteString`s represent byte sequences, with the indexes | ||
of the represented byte sequence being treated in little-endian, | ||
least-significant-bit-first encoding. For example, consider the byte sequence |
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.
endianness is about integer encoding!
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.
This has now been moved to the relevant section.
CIP-?/README.md
Outdated
We describe the translation of `BuiltinInteger` into `BuiltinByteString` which | ||
is implemented as the `integerToByteString` primitive. Informally, we represent | ||
`BuiltinInteger`s with the least significant bit at bit position $0$, using a | ||
twos-complement representation. More precisely, let $i \in \mathbb{N}^{+}$. We |
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.
"This corresponds to a little-endian, least-significant-bit first encoding."
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.
Amended.
CIP-?/README.md
Outdated
We describe the semantics of `shiftByteString` and `rotateByteString`. | ||
Informally, both of these are 'index modifiers' for bit sequences: given a | ||
positive $i$, the index of a bit in $s$ 'increases' in the result; given a | ||
negative $i$, the index of a bit in $s$ 'decreases' in the result. This can mean |
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.
Now you specify it. I just think this should be in the brief description too.
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.
Now done.
applying same labelling as held by deprecated version (#268 (comment)) |
3bdad5a
to
f8057e5
Compare
f8057e5
to
a1b9ff0
Compare
``` | ||
|
||
Convert a non-negative number to its bitwise representation, erroring if given a | ||
negative number. |
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 reckon that the conversion uses big endianness. It could be nice to clarify it even though the interface described below seems pretty much agnostic to this.
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.
Pretty sure it's supposed to be little endian.
We observe that `byteStringToInteger` 'undoes' `integerToByteString`: | ||
|
||
```haskell | ||
byteStringToInteger . integerToByteString = id |
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.
byteStringToInteger . integerToByteString = id | |
byteStringToInteger . integerToByteString = identity |
(because readers may not be familiar with Haskell, let's not force the bad choices of the base Prelude on them).
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.
This assumes instead that they are familiar with one of the preludes that renames id
to identity
, which is even more unlikely than someone working on Cardano or Plutus not knowing Haskell.
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.
No, it assumes they are familiar with word 'identity' which is pervasive in mathematics.
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 mean seriously, for anyone outside of the the tiny Haskell bubble, 'id' means 'identifier'.
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.
Except the laws are specifically written in Haskell terms throughout. Furthermore, I really don't think anybody reading this won't be familiar with Haskell.
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.
This is merely a suggestion. Do as you please, but we aware that most people building on Cardano and participating in CIPs aren't actually that familiar with Haskell.
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.
p.s. oops, I accidentally approved by ticking the wrong box... so we will wait for that commit before merging 🤪 |
For what it is worth, I made a similar implementation that this cip aims to achieve building on other builtins (this is thus not as efficient as the solution of this cip). Its still a work in progress. I mostly followed the interface of the modules Examples, of some basic functions
I am yet to implement the benchmarking of these functions (and optimize them). But if someone needs them, they can. |
Co-authored-by: Matthias Benkort <[email protected]>
@rphair I've committed all suggestions except the controversial one about |
@L-as might be helpful to add an Implementation Plan and Implementors as per the new CIP-001, since I assume you're now the implementor? |
Yes, indeed, I will do that. |
There is also an implementation of https://github.com/OpShin/opshin/blob/main/opshin/std/bitmap.py |
* Add CIP * Notation fix-ups * Fix heading levels on use cases * Rewrite how shifts work * Clarify findFirstSet description * Clarify representation * Clarifications * Replace FFS with TZCNT and LZCNT, add finite field arithmetic example * Rewrite * Fix formatting * Apply suggestions from code review Co-authored-by: Matthias Benkort <[email protected]> --------- Co-authored-by: goverthrow <[email protected]> Co-authored-by: Las Safin <[email protected]> Co-authored-by: Matthias Benkort <[email protected]>
This improves upon, and supercedes, the original. Specifically, the formatting has been designed to be clear and readable on Github, and concerns vis-a-vis semantics and wording raised here.
A few issues await @michaelpj's responses; in particular, the following questions need to be addressed:
bytestring
instead ofBuiltinByteString
See rendered markdown.