Skip to content
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

Packed provable type which can save constraints #1376

Merged
merged 13 commits into from
Feb 1, 2024

Conversation

mitschabaude
Copy link
Member

@mitschabaude mitschabaude commented Jan 18, 2024

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 an Unconstrained 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.

@mitschabaude mitschabaude requested a review from a team as a code owner January 18, 2024 10:02
@mitschabaude mitschabaude merged commit 9c55fba into main Feb 1, 2024
13 checks passed
@mitschabaude mitschabaude deleted the feature/packed-provable branch February 1, 2024 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants