u256 add and sub return wrong results
s1na opened this issue · comments
Sina M commented
Here are a few simple test cases:
it("Should add 0 and 1", () => {
var a = u256.Zero;
var b = u256.One;
expect<u256>(a + b).toStrictEqual(b);
var c = new u256(0xffffffffffffffff, 0, 0, 0);
var res = new u256(0, 1, 0, 0);
expect<u256>(b + c).toStrictEqual(res);
c = new u256(0xffffffffffffffff, 0xffffffffffffffff, 0, 0);
res = new u256(0, 0, 1, 0);
expect<u256>(b + c).toStrictEqual(res);
});
it("Should sub numbers", () => {
var a = u256.Zero;
var b = u256.One;
expect<u256>(b - a).toStrictEqual(b);
expect<u256>(b - b).toStrictEqual(a);
});
I see two issues. First this should probably be a.lo2
and b.lo2
here:
Then, mid
's carry is lost, I don't think this is correct:
I think generally since u128
doesn't return carry, it makes it hard to use it in u256
.
Max Graey commented
Thanks. u256
is not properly implemented and tested yet.