ZoKrates icon indicating copy to clipboard operation
ZoKrates copied to clipboard

Modulus functionality for field element

Open sameersubudhi opened this issue 5 years ago • 3 comments

Hi,

Is it possible to extend the FieldPrime implementation to include the support for modulus operator for determining the remainder? Ref: https://doc.rust-lang.org/std/ops/trait.Rem.html

Is it not implemented on purpose? If so, could you please offer a brief explanation?

Best regards, Sameer

sameersubudhi avatar Oct 27 '19 17:10 sameersubudhi

Hi Sameer,

Yes this is on purpose as arbitrary modulo operations are expensive on prime fields, as they require a range check: a = q*b + r s.t. r < b which is expensive.

Can you share your use case here?

Schaeff avatar Oct 28 '19 12:10 Schaeff

Hi Thibaut ( @Schaeff ),

In my program, I need to determine the exact quotient value for further processing and the remainder part doesn't have any impact on the logic. Thus, I can afford to sacrifice some accuracy.

But since FieldPrime implements the division method as a multiplication of numerator and the multiplicative inverse mod P of the denominator, the combinations that have non-zero remainder cause huge numbers and thus do not really work out in my use-case.

Alternatively, a floor division implementation (similar to // operator in Python) could be of great help to me. Or could you suggest me an approach to handle this scenario with ZoKrates in its current state?

Regards, Sameer

sameersubudhi avatar Oct 28 '19 12:10 sameersubudhi

@nono2357 As per my understanding (FieldPrime implementation in ZoKrates), your recommended solution will always return 0, no matter what input values you provide.

d*(nb/d) will always evaluate to nb.

sameersubudhi avatar Nov 14 '19 10:11 sameersubudhi