-
Notifications
You must be signed in to change notification settings - Fork 9
Library for generic ABI encoding of algebraic data types #465
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
Merged
+118
−130
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,120 +1,17 @@ | ||
| pragma no-patterson-condition ABIAttribs, ABIEncode; | ||
| pragma no-bounded-variable-condition ABIAttribs, ABIEncode; | ||
| pragma no-patterson-condition; | ||
| pragma no-bounded-variable-condition; | ||
|
|
||
| export { | ||
| Sum(*), | ||
| Generic | ||
| }; | ||
| export { Generic }; | ||
|
|
||
| import std.{*}; | ||
|
|
||
| // Binary tagged union: the only new representation type. | ||
| // Native () and (f, g) are reused as Unit and Prod respectively; | ||
| // they already have full ABIAttribs / ABIEncode / ABIDecode support in std.solc. | ||
| data Sum(f, g) = Inl(f) | Inr(g); | ||
|
|
||
| // MPTC: isomorphism between a user type and its SOP representation. | ||
| // Pattern mirrors Typedef(rep) in std.solc. | ||
| // The representation 'rep' is built from primitive Solcore types: | ||
| // sum(f, g) with constructors inl / inr | ||
| // (f, g) pair (product) | ||
| // () unit | ||
| forall a rep. | ||
| class a : Generic(rep) { | ||
| function from(x : a) -> rep; | ||
| function to(x : rep) -> a; | ||
| } | ||
|
|
||
| // Maximum of two words, used for Sum headSize. | ||
| function maxWord(a : word, b : word) -> word { | ||
| match gtWord(a, b) { | ||
| | true => return a; | ||
| | false => return b; | ||
| } | ||
| } | ||
|
|
||
| // ABIAttribs for Sum: | ||
| // headSize = 32 (tag word) + max(headSize(f), headSize(g)) | ||
| forall f g . f:ABIAttribs, g:ABIAttribs => | ||
| instance Sum(f, g) : ABIAttribs { | ||
| function headSize(ty : Proxy(Sum(f, g))) -> word { | ||
| let pf : Proxy(f); | ||
| let pg : Proxy(g); | ||
| return 32 + maxWord(ABIAttribs.headSize(pf), ABIAttribs.headSize(pg)); | ||
| } | ||
| function isStatic(ty : Proxy(Sum(f, g))) -> bool { | ||
| let pf : Proxy(f); | ||
| let pg : Proxy(g); | ||
| return and(ABIAttribs.isStatic(pf), ABIAttribs.isStatic(pg)); | ||
| } | ||
| } | ||
|
|
||
| // ABIEncode for Sum. | ||
| // Wire layout (static sums only): | ||
| // [offset + 0 .. offset + 31] : tag word (0 = Inl, 1 = Inr) | ||
| // [offset + 32 .. ] : encoded branch payload | ||
| // For uniform sums (headSize(f) == headSize(g)) no bytes are left uninitialised. | ||
| forall f g . f:ABIAttribs, f:ABIEncode, g:ABIAttribs, g:ABIEncode => | ||
| instance Sum(f, g) : ABIEncode { | ||
| function encodeInto(x : Sum(f, g), basePtr : word, offset : word, tail : word) -> word { | ||
| match x { | ||
| | Sum.Inl(v) => | ||
| mstore(basePtr + offset, 0); | ||
| return ABIEncode.encodeInto(v, basePtr, offset + 32, tail); | ||
| | Sum.Inr(v) => | ||
| mstore(basePtr + offset, 1); | ||
| return ABIEncode.encodeInto(v, basePtr, offset + 32, tail); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // ABIDecode for Sum. | ||
| // Reads the tag word at headOffset; dispatches to the f or g decoder at headOffset + 32. | ||
| forall f g reader . | ||
| reader:WordReader, | ||
| f:ABIAttribs, | ||
| ABIDecoder(f, reader) : ABIDecode(f), | ||
| ABIDecoder(g, reader) : ABIDecode(g) => | ||
| instance ABIDecoder(Sum(f, g), reader) : ABIDecode(Sum(f, g)) { | ||
| function decode(ptr : ABIDecoder(Sum(f, g), reader), headOffset : word) -> Sum(f, g) { | ||
| match ptr { | ||
| | ABIDecoder(rdr) => | ||
| let tag = WordReader.read(WordReader.advance(rdr, headOffset)); | ||
| match tag { | ||
| | 0 => | ||
| let dec_f : ABIDecoder(f, reader) = ABIDecoder(rdr); | ||
| return Sum.Inl(ABIDecode.decode(dec_f, headOffset + 32)); | ||
| | _ => | ||
| let dec_g : ABIDecoder(g, reader) = ABIDecoder(rdr); | ||
| return Sum.Inr(ABIDecode.decode(dec_g, headOffset + 32)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Bridge: any type with a Generic(rep) instance gains ABIAttribs automatically. | ||
| forall a rep . a:Generic(rep), rep:ABIAttribs => | ||
| default instance a : ABIAttribs { | ||
| function headSize(ty : Proxy(a)) -> word { | ||
| let prx : Proxy(rep); | ||
| return ABIAttribs.headSize(prx); | ||
| } | ||
| function isStatic(ty : Proxy(a)) -> bool { | ||
| let prx : Proxy(rep); | ||
| return ABIAttribs.isStatic(prx); | ||
| } | ||
| } | ||
|
|
||
| // Bridge: any type with a Generic(rep) instance gains ABIEncode automatically. | ||
| forall a rep . a:Generic(rep), rep:ABIAttribs, rep:ABIEncode => | ||
| default instance a : ABIEncode { | ||
| function encodeInto(x : a, basePtr : word, offset : word, tail : word) -> word { | ||
| let r : rep = Generic.from(x); | ||
| return ABIEncode.encodeInto(r, basePtr, offset, tail); | ||
| } | ||
| } | ||
|
|
||
| // Note: ABIDecode bridge omitted. The class head ABIDecoder(a, reader) is a type | ||
| // application, not a plain variable, so Solcore's "default instance" restriction | ||
| // makes automatic derivation impossible. To decode a Generic type, create an | ||
| // ABIDecoder for the representation type and call Generic.to on the result: | ||
| // | ||
| // let dec : ABIDecoder(Rep, MemoryWordReader) = ABIDecoder.ABIDecoder(reader); | ||
| // let rep : Rep = ABIDecode.decode(dec, offset); | ||
| // let val : MyType = Generic.to(rep); | ||
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
Oops, something went wrong.
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.
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.
Are these pragmas needed? The test seem to run fine without them