-
Notifications
You must be signed in to change notification settings - Fork 101
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
test large overflows and underflows for EnsureIntegerInRange #1170
base: master
Are you sure you want to change the base?
Conversation
Need unit test to demonstrate the problem. |
I have not come up with any test case to demonstrate that AND is wrong, but I will try it in 24 hrs |
For BigIntenger Negative values it may be the error if I understood correctly |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should ensure that is well covered with the current UT
In tests I found that AND works well, while MOD is wrong. In C#, (-1 mod 5) returns -1. In other languages the result is often 4 😰 |
adjustTarget.Instruction = Push(mask); | ||
AddInstruction(OpCode.AND); | ||
adjustTarget.Instruction = Push(mask + 1); | ||
AddInstruction(OpCode.MOD); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expression x AND mask
is not equal to x MOD (mask + 1)
when x is negative, so MOD
cannot replace AND
here?
For expample:
var x = new BigInteger(-3);
var y = new BigInteger(255);
Console.WriteLine($"mod {x % (y+1)}, and {x & y}");
x = new BigInteger(3);
y = new BigInteger(255);
Console.WriteLine($"mod {x % (y+1)}, and {x & y}");
Output:
mod -3, and 253
mod 3, and 3
Other languages are Python? C, Java, Rust etc. have the same semantics as C#. |
My fault. OpCode.AND is always correct in tests. I will add tests for large overflows and underflows. |
4092fa4
to
b136d01
Compare
The correctness of OpCode.AND depends on the representation of BigInteger in bytes, and I am really suspicious whether AND is correct. Here are some examples of bytes representations.
https://learn.microsoft.com/en-us/dotnet/api/system.numerics.biginteger.tobytearray?view=net-8.0
MOD is safe, no matter how BigInteger is implemented in bytes.