Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional UInt64 subtraction bug #895

Closed
Isaac-DeFrain opened this issue May 8, 2023 · 3 comments
Closed

Conditional UInt64 subtraction bug #895

Isaac-DeFrain opened this issue May 8, 2023 · 3 comments

Comments

@Isaac-DeFrain
Copy link

This took me so long to pin down!
There seems to be a bug when evaluating conditional UInt64 subtraction in SnarkyJS 0.9.8.

Occurrences
Ordinary conditional (throws an error when condition is false)
image

Circuit conditional (throws errors in both cases)
image

image

Converting the condition toBoolean() fixes the ordinary conditional
image
but not the Circuit conditional

@Isaac-DeFrain
Copy link
Author

I realized that I wasn't using the latest version of node in those screenshots. I have confirmed these errors still occur with the latest node v20.1.0.

@mitschabaude
Copy link
Member

Thanks for reporting @Isaac-DeFrain! This isn't a bug. UInt64.sub() asserts that the result doesn't underflow zero, and throws when it does. Since Circuit.if is a normal function call, the else case is evaluated, there's no way around that.

So, a safe subtraction of the smaller value from the larger one needs to be implemented differently. For example, you could first determine the min and max with 2 Circuit.ifs, and then do max.sub(min).

What this definitely highlights is that we should improve the error message!

@Isaac-DeFrain
Copy link
Author

If there's a way to report the line in the smart contract method where the error occurs, that would be like a 10x improvement! When running tests with jest, you only get the line in the test where the error occurs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants