MaxGraey / as-bignum

Fixed length big numbers for AssemblyScript 🚀

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

u256 add and sub return wrong results

s1na opened this issue · comments

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:

https://github.com/MaxGraey/bignum.wasm/blob/5403267df2d9a6792fdc6d34f4f11854ccf49719/assembly/integer/u256.ts#L250-L251

Then, mid's carry is lost, I don't think this is correct:

https://github.com/MaxGraey/bignum.wasm/blob/5403267df2d9a6792fdc6d34f4f11854ccf49719/assembly/integer/u256.ts#L253

I think generally since u128 doesn't return carry, it makes it hard to use it in u256.

Thanks. u256 is not properly implemented and tested yet.