min
and max
operations for small field elements
#179
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.
Description
Note: This is the first PR porting ideas from my repo, as discussed with @Pratyush on Discord
Motivation
Sometimes in a circuit we have field elements with guaranteed value bounds. It turns out that many common arithmetic operations like
min
,max
,abs_diff
, comparisons or saturating subtraction can be done much more efficiently than for arbitrary values in general.By using slack variables instead of direct bitwise comparison, we can reduce number of constraints and auxiliary variables to ~2 ·
BITS
, whereBITS
is the binary logarithm of the value bound.For a direct usage, please check our Arkworks circuits for image transformations in the VIMz project.
Technical details, correctness proof and benchmarks
Please check the README in https://github.com/pmikolajczyk41/arkworks-small-values-ops.
Why not UInt?
The first commit (reverted right away) on this PR's branch contains an adaptation of
UInt::is_ge
to this new technique. However, it is less efficient there: we have to do 4 bit decompositions (self
,other
, two slack variables), while the current version does 3. However, if we already start withFpVar
s with assumed/constrained bounds (as this PR suggests), we need only 2 bit decompositions (for slack variables).Next steps
If maintainers agree, I'm happy to expand this API for other operations in the following PRs.
Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.
Pending
section inCHANGELOG.md
Files changed
in the Github PR explorer