fix: incorrect calc of surfeit related value in EmulatedFpVar
#157
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
Currently, the following invariant is not maintained when computing
num_of_additions_over_normal_form
insub_without_reduce
andprod_of_num_of_additions
inmul_without_reduce
AllocatedEmulatedFpVar
, no limb has a value > (num_of_additions_over_normal_form + 1) * (2^{bits_per_limb} - 1).AllocatedMulResultVar
, no limb has a value > (prod_of_num_of_additions + 1) * (2^{bits_per_limb} - 1).More specifically, these values are currently underestimated.
This will result in generating constraints in
group_and_check_equality
that can't be satisfied, since thesurfeit
value is used to determine how many limbs to put into a group. For example, in my overload, underestimating thesurfeit
value resulted in putting one more limb value in a group, which resulted in an overflow inlet eqn_left = left_total_limb + pad_limb + &carry_in - right_total_limb;
and madeeqn_left. conditional_enforce_equal(&eqn_right, &Boolean::<BaseF>::TRUE)
not satisfiable.The patch also fixed the
overhead!
macro, which did not computeceil(log2(x))
correctly.Since it's not easy to create test cases that will trigger the constraint system to be unsatisfied, I've just added two tests to ensure that the above invariant is satisfied. Hopefully these two test cases will also help people contribute to this part of the library in the future by documenting the invariant used throughout the implementation.
A few more questions
I'm not sure why in
post_add_reduce
the reduction is only performed when2 * params.bits_per_limb + surfeit + 1 >= BaseF::MODULUS_BIT_SIZE
. I think that checking thatparams.bits_per_limb + some constant >= BaseF::MODULUS_BIT_SIZE
is sufficient. This similar pattern is also present insub_without_reduce
. If there is no particular reason, I think we can fix it to accurately reflect the upper bounds of the bit size.Currently, the
add
andadd_constant
implementations inAllocatedMulResultVar
don't seem to reduce the results whenprod_of_num_of_additions
is large enough. This seems to be an issue that needs to be fixed.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