Proposal for a library for defining packed records#359
Open
rodrigogribeiro wants to merge 2 commits into
Open
Conversation
Collaborator
|
What's the difference between |
Collaborator
Author
Basically, the same. Since I wanted to make it self contained, I decided to follow the standard of Haskell libraries and use |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Packed Records via Generic Programming (SOP)
Motivation
In Solidity, packed records are structs whose fields fit within a single
256-bit EVM storage slot. Core Solidity currently has no generic mechanism for
describing or deriving the bit layout of such structures. This proposal uses the
sum-of-products (SOP) representation, which the compiler already uses
internally in Hull, to derive encode/decode operations generically.
Core design
The system has three layers:
Generic(rep): a multi-parameter type class that captures theisomorphism between a user-defined type and its SOP representation. Users
write this instance manually; it is the only instance they ever need to write
for a new type.
Packable: a single-parameter type class with structural instances forall SOP building blocks (
(),bool,word, products, sums). A bridgeinstance makes every type with a
Genericinstance automaticallyPackable.Storable: a type class for EVM storage read/write, derivedautomatically from
Packable.Components
1. The
Genericclass (std/Generic.solc)This is a multi-parameter type class (MPTC) in the same style as
Typedef. Foreach user-defined data type, one instance is written mapping the type to its
nested binary sum-of-products representation.
2. The
Packableclass (std/Packable.solc)3. Bit helpers (
std/Bits.solc)Low-level word operations used by
Packable:4. Fixed-width integer types (
std/Uint.solc)Since the type system lacks dependent integers, common widths are explicit
newtypes:
5. EVM storage (
std/Storage.solc)For types with
bitWidth > 256, the user writes a specialisedStorableinstance that distributes the value across multiple slots using derived slot
addresses.