Consensys / gnark

gnark is a fast zk-SNARK library that offers a high-level API to design circuits. The library is open source and developed under the Apache 2.0 license
https://hackmd.io/@gnark
Apache License 2.0
1.4k stars 358 forks source link

Can division only be integer division? #218

Closed JameWade closed 2 years ago

JameWade commented 2 years ago

like api.div(31,3),it returns
图片

vck3000 commented 2 years ago

Hey Jame,

I'm still new to this field so I might be wrong, but you may find your answer in Vitalik's post introducing zk-SNARKs. Have a look at the section in Finite Fields and his paragraph on division.

In summary, zk-SNARKs operate on a finite field where only integers are allowed. Since our traditional division method could result in an answer that is non-integer, there is a different method to perform divisions in finite fields to mimic traditional division. A consequence of this however, is that non-integer divisions will result in answers that we don't normally expect in our normal math world.

To answer your question, I believe non-integer divisions are allowed, just that they don't behave in the normal way we expect in traditional maths. In finite fields, that big answer you see is technically correct. If you wish to do floating point division in zk-SNARKs I imagine you'll have to implement your own floating point library on top of gnark.

gbotrel commented 2 years ago

Correct, a math/float and non native field arithmetic (ie working with a different modulus than the one on which the zkSNARK is "instantiated" with) is currently not supported.

It is in our backlog.

JameWade commented 2 years ago

dear vck3000 and gbotrel @vck3000 @gbotrel thanks for your reply,I have an idea about the implement of div.I read the code in jsnark. when a div b,they compute c = a div b,then verify a = b mul c. I want implement this func like xjsnark,but i failed. Can I do that?

ivokub commented 2 years ago

Divisions on emulated fields are now available in #302.