Packed provable type which can save constraints #1376
Merged
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.
This is an idea I got while studying the list / tree data structures used in @mrmr1993's token zkapp examples.
Those examples represent big dynamic data structures by a hash, and perform complex branching-type logic on the hashes instead of the original values. This can save a lot of constraints compared to branching on the full values.
Even without hashing, just representing values as fewer field elements in any way can result in a reduction of constraints. Here, I introduce and export a
Packed<T>
provable type, which uses information about the bit length of types to pack them into as few field elements as possible.I use
Packed
to pack elliptic curve points in the ECDSA gadget from 6 to 3 field elements, and get rid of 2k constraints in the part where we select a point from an array.Note: This is not a replacement for o1js-pack, because
Packed<T>
doesn't currently support unpacking the values from the packed field elements alone, so it can't be used to save onchain state.Packed
also holds onto anUnconstrained
which contains the original value. For the pattern where we represent values by their hash, instead of their packed field elements, knowing the original value is of course necessary.