Skip to content

Commit

Permalink
add new test for u256 mul
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Jun 16, 2023
1 parent 4bd07f8 commit 0c070dd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
48 changes: 34 additions & 14 deletions assembly/__tests__/u256.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { u128 } from '../integer';
import { u128 } from '../integer/u128';
import { u256 } from '../integer/u256';

describe("String Conversion", () => {
Expand Down Expand Up @@ -368,23 +368,25 @@ describe("Basic Operations", () => {
expect(b * a).toStrictEqual(r);
});

it("Should multiply two random u256 numbers", () => {
let i = 0;
while(i< 20) {
const randa = <u64>Math.trunc(Math.random() * 0xFFFF)
const a = u256.from(randa);
const randb = <u64>Math.trunc(Math.random() * 0xFFFF)
const b = u256.from(randb);
const r = new u256(randa * randb);
it("Should multiply two u256 numbers - 2", () => {
var a = u256.from(11);
var b = new u256(0, 2);
var r = new u256(0, 22);

expect(a * b).toStrictEqual(r);
expect(b * a).toStrictEqual(r);
i++;
}
expect(a * b).toStrictEqual(r);
expect(b * a).toStrictEqual(r);
});

it("Should multiply two u256 numbers - 3", () => {
var a = new u256(0, 3);
var b = new u256(0, 0,3);
var r = new u256(0, 0,0,9);

it("Should multiply two u256 numbers - 2", () => {
expect(a * b).toStrictEqual(r);
expect(b * a).toStrictEqual(r);
});

it("Should multiply two u256 numbers - 4", () => {
var a = u256.from(new u128(14083847773837265618, 6692605942));
var b = u256.from(new u128(18444665141527514289, 5354084802));
var r = new u256(5659639222556316466, 4474720309748468391, 17386035696907167262, 1);
Expand Down Expand Up @@ -419,4 +421,22 @@ describe("Basic Operations", () => {
expect(a * a).toStrictEqual(new u256(1, 0, 2));
});

it("Should multiply two u256 numbers with overflow - 3", () => {
var a = new u256(6, 0, 0, 420);
var b = new u256(0, 7, 0, 0);
var r = new u256(0, 42, 0, 0);

expect(a * b).toStrictEqual(r);
expect(b * a).toStrictEqual(r);
});

it("Should multiply two u256 numbers with overflow - 3", () => {
var a = new u256(2, 666, 666, 666);
var b = new u256(0, 0, 0, 3);
var r = new u256(0, 0, 0, 6);

expect(a * b).toStrictEqual(r);
expect(b * a).toStrictEqual(r);
});

});
1 change: 1 addition & 0 deletions assembly/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export function __uadd64(x: u64, y: u64, carry: u64 = 0): u64 {

}

// u256 * u256 => u256 implementation based on https://github.com/holiman/uint256
// @ts-ignore: decorator
@global
export function __mul256(x0: u64, x1: u64, x2: u64, x3: u64, y0: u64, y1: u64, y2: u64, y3: u64): u256 {
Expand Down

0 comments on commit 0c070dd

Please sign in to comment.