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

Error thrown from usubMag() when trying to assign dig[1]=0 #16

Open
lebecki opened this issue Apr 15, 2020 · 5 comments
Open

Error thrown from usubMag() when trying to assign dig[1]=0 #16

lebecki opened this issue Apr 15, 2020 · 5 comments

Comments

@lebecki
Copy link
Contributor

lebecki commented Apr 15, 2020

Found now error (?) in usubMag(). I tried to apply changes to make it more similar to uaddMag(). Hopefully I am right.
See: my fork

@bwakell
Copy link
Owner

bwakell commented Apr 17, 2020 via email

@bwakell
Copy link
Owner

bwakell commented Apr 17, 2020

If there is an issue, it must be in one of the methods calling it (i.e. some invariant breaks). So if you have a concrete example of a failing scenario (which is entirely possible ^^), do share.

@lebecki
Copy link
Contributor Author

lebecki commented Apr 17, 2020

Example sent to you. I think in this example the call that is causing the trouble looks like BigInt(3).sub(1) - rather simple.
The reason being the line:
dig[1] = (int)dif;
that is called always, even if len==1

@lebecki
Copy link
Contributor Author

lebecki commented Apr 17, 2020

More precisely speaking, here is the code of a simple failing scenario:
BigInt sum = new BigInt(3);
long one = 1; // Important: long, not int!
sum.sub(one); // Error thrown here:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at org.huldra.math.BigInt.usubMag(BigInt.java:844)
at org.huldra.math.BigInt.usub(BigInt.java:897)
at org.huldra.math.BigInt.sub(BigInt.java:1097)
at Glowna.main(Glowna.java:78)

@bwakell
Copy link
Owner

bwakell commented Apr 17, 2020

Oh yeah a classic issue of assuming that just because it's a long it takes up more than 32 bits.

Part of the issue is in usub(final long a) where this statement:
|| ah==0&&(dig[0]&mask)>=al

Should instead be in an else if and call usubMag((int)al).

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