Hint function giving wrong answer #711
Replies: 1 comment 1 reply
-
I recommend reading first a bit about modular arithmetic. For example https://nrich.maths.org/4350, https://vitalik.ca/general/2017/11/22/starks_part_2.html#:~:text=A%20Modular%20Math%20Interlude. All arithmetic in a gnark circuit is done modulo the scalar field which you define compile time. Regarding that Sqrt result is 1, please see method documentation: https://pkg.go.dev/math/big#Int.Sqrt. It is expected that the result is rounded down. gnark does not support floating- nor fixed point arithmetic. Even if you set the witness elements to floats, they are cast to finite field elements (represented as integers). So either you have to implement fixed point arithmetic on top or try to represent the arithmetic using integers only. For example - I guess you need square root for computing a distance between some points. Instead of L2 metric you could instead use L1 metric (see https://en.wikipedia.org/wiki/Taxicab_geometry). |
Beta Was this translation helpful? Give feedback.
-
Hi,
I have written a hint function to find square root of an integer as follows.
I call it as follows in my circuit.
where
anormsq = 2
.I get output in
sqrtout[0]
and it is equal to1
. But it should be1.414
.The function works correctly when I ask for sqrts of perfect squares. So this issue is due to floating point. How do I get over the issue since I cannot use assert statement in the circuit as square of 1 won't be 2?
Beta Was this translation helpful? Give feedback.
All reactions