feat: add bounded comparator functions#530
Conversation
This commit adds some functions, with relatively low circuit complexity, that perform comparison when the difference of two numbers is bounded. Signed-off-by: aybehrouz <behrouz_ayati@yahoo.com>
|
Hi @aybehrouz, thanks for the contribution. I had an initial look at the PR. I'm not sure it is worth it. As you write yourself in the comments then the user has to be really really careful to use it correctly (either solver error, undefined behaviour or correct behaviour depending on the configuration and the inputs). And I would avoid giving the users more tools to create unsound proofs. Lets keep the PR open for now, so someone else on the team can also have a look to see if it makes sense to include. |
|
Hi, thanks for taking the time @ivokub , I think the documentation currently is a bit confusing and looks somehow scary 😄 but using the functions is pretty straightforward. Assume we have two numbers Now what happens if you were wrong about the bound? For example if our numbers were not 32bit? Well first of all that's a programming error. The complex part of the documentation explains this situation. Anyway, in summary, as long as the difference will not cause an overflow in the underlying field, you're gonna be fine.
|
After merging the latest commits, `AssertIsLess` tests do not fail anymore, and can be included.
This commit also fixes an issue in checking the boundary conditions for `absDiffUpp`.
|
Interestingly I found a better implementation for the func Slice(api frontend.API, start, end frontend.Variable, input []frontend.Variable) []frontend.Variable {
startMask := stepMask(api, len(input), start, 0, 1)
endMask := stepMask(api, len(input), end, 0, 1)
out := make([]frontend.Variable, len(input))
for i := 0; i < len(out); i++ {
out[i] = api.Mul(api.Sub(startMask[i], endMask[i]), input[i])
}
comparator := cmp.NewComparator(api, big.NewInt(int64(len(input))))
comparator.AssertIsLessEq(start, end)
return out
} |
We wrap comment lines at 80 instead of 100, and wrap normal code lines at 120.
This commit adds functions for comparing any two numbers and improves PR Consensys#637. Signed-off-by: aybehrouz <behrouz_ayati@yahoo.com>
|
ivokub
left a comment
There was a problem hiding this comment.
PR is a lot better now. There still are some stylistic suggestions.
What I still would like to avoid is having a notation of signed and unsigned integers. Even though you can assign negative values to the witness, then in practice during solving time gnark mod reduces the value. All arithmetic in-circuit is done on the field elements which do not have a sign.
I feel that this may lead to problems in the future if we say that inputs are signed ints etc. In general I think the PR looks good and I think it would be good to merge after rephrasing.
Btw, package is also missing package documentation and title for browsing.
ivokub
left a comment
There was a problem hiding this comment.
I'm really really sorry, @aybehrouz. I have been busy with Linea and EthCC and completely forgot about the PR.
Thanks for the fixes and your contribution! It is really appreciated!
This PR adds some functions, with relatively low circuit complexity, that perform comparison when the difference of two numbers is bounded.
Sometimes the fuzzer fails, and I have no idea why.
I had written these functions, I thought they might come in handy, so I made a PR. It should be reviewed carefully before merging.