Skip to content

Commit

Permalink
Fix bignumber import
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianBorst committed Jun 12, 2024
1 parent 2ef279a commit 9596ac5
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 125 deletions.
20 changes: 0 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"@nomicfoundation/hardhat-chai-matchers": "^2.0.2",
"@nomicfoundation/hardhat-ethers": "^3.0.5",
"@nomicfoundation/hardhat-toolbox": "^4.0.0",
"@types/bignumber.js": "^5.0.0",
"@types/command-line-args": "^5.2.3",
"bignumber.js": "^9.1.2",
"hardhat": "^2.19.2",
Expand Down
78 changes: 39 additions & 39 deletions test/FixedPoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@ import { ethers } from 'hardhat';
import { TestFixedPoint } from "../typechain-types";
// import { BigNumber } from 'typescript-bignumber';
import {assert, BigNumberish} from "ethers";
import * as BN from "bignumber.js";
import {BigNumber} from "bignumber.js";

const PRECISION = 100000000
const PRECISION_BN = BN.BigNumber(PRECISION)
export const Q_64 = BN.BigNumber(2).pow(64);
export const Q_128 = BN.BigNumber(2).pow(128);
const PRECISION_BN = BigNumber(PRECISION)
export const Q_64 = BigNumber(2).pow(64);
export const Q_128 = BigNumber(2).pow(128);
export const ZERO_ADDR = "0x0000000000000000000000000000000000000000"

export function toQ64 (val: number | BN.BigNumber): bigint {
var multFixed: BN.BigNumber;
export function toQ64 (val: number | BigNumber): bigint {
var multFixed: BigNumber;
if (typeof val === 'number') {
multFixed = BN.BigNumber(val);
multFixed = BigNumber(val);
} else {
multFixed = val;
}
let res = (multFixed.times(Q_64).integerValue())
return BigInt(res.toFixed())
}

export function fromQ64 (val: bigint): BN.BigNumber {
return BN.BigNumber(val.toString()).div(Q_64);
export function fromQ64 (val: bigint): BigNumber {
return BigNumber(val.toString()).div(Q_64);
}

describe('TestFixedMath', () => {
let fixed: TestFixedPoint;
const Q64_MIN = BN.BigNumber(2).pow(63).minus(1).negated();
const TT64 = BN.BigNumber(2).pow(64);
const TT128 = BN.BigNumber(2).pow(128);
const TT192 = BN.BigNumber(2).pow(192);
const Q64_MIN = BigNumber(2).pow(63).minus(1).negated();
const TT64 = BigNumber(2).pow(64);
const TT128 = BigNumber(2).pow(128);
const TT192 = BigNumber(2).pow(192);

beforeEach("deploy", async () => {
const libFactory = await ethers.getContractFactory("TestFixedPoint");
Expand All @@ -47,18 +47,18 @@ describe('TestFixedMath', () => {
it("mulQ64", async () => {
let result = await fixed.testMulQ64(toQ64(3.5).toString(), toQ64(5.25).toString());
let bn_fixed = fromQ64(result);
let bn_expected = BN.BigNumber("18.375");
let bn_expected = BigNumber("18.375");
expect(bn_fixed.eq(bn_expected)).to.be.true;

bn_expected = BN.BigNumber("340282366920938463463374607431768211456"); // 2^63 * 2 before fromQ64()
let bn_result = BN.BigNumber((await fixed.testMulQ64(toQ64((BN.BigNumber(2).pow(63))), toQ64(2))).toString());
bn_expected = BigNumber("340282366920938463463374607431768211456"); // 2^63 * 2 before fromQ64()
let bn_result = BigNumber((await fixed.testMulQ64(toQ64((BigNumber(2).pow(63))), toQ64(2))).toString());
expect(bn_result.eq(bn_expected)).to.be.true;

let BI_TT128M1 = BigInt(TT128.minus(1).toFixed());
let BN_TT128M1 = TT128.minus(1);
result = await fixed.testMulQ64(BI_TT128M1, BI_TT128M1);
bn_expected = BN_TT128M1.times(BN_TT128M1).div(TT64).integerValue();
expect(BN.BigNumber(result.toString()).eq(bn_expected)).to.be.true;
expect(BigNumber(result.toString()).eq(bn_expected)).to.be.true;

result = await fixed.testMulQ64(toQ64(1), toQ64(2.0));
expect(fromQ64(result).eq(2.0)).to.be.true;
Expand All @@ -76,7 +76,7 @@ describe('TestFixedMath', () => {

it("divQ64 Precision", async () => {
let result = await fixed.testDivQ64(BigInt(2)**BigInt(126), BigInt(2)**BigInt(3));
expect(fromQ64(result).eq(BN.BigNumber(2).pow(123))).to.be.true;
expect(fromQ64(result).eq(BigNumber(2).pow(123))).to.be.true;
});

// This test still uses Q64 numbers, but JS doesn't share the same concept of fixed point numbers
Expand All @@ -88,12 +88,12 @@ describe('TestFixedMath', () => {
expect(fromQ64(result).eq(4)).to.be.true;

let bn_expected = TT128;
let x = (BN.BigNumber(2).pow(127));
let x = (BigNumber(2).pow(127));
let bn_result = await fixed.testMulQ128(toQ64(x), toQ64(2.0));
expect(fromQ64(bn_result).eq(bn_expected)).to.be.true;

let target = BN.BigNumber(2).pow(191).minus(1);
x = (BN.BigNumber(2).pow(124));
let target = BigNumber(2).pow(191).minus(1);
x = (BigNumber(2).pow(124));
let y = (target.div(x)); // Want x * y = 2^192 - 1
let expected = x.times(y).integerValue();
result = await fixed.testMulQ128(toQ64(x), toQ64(y));
Expand All @@ -102,42 +102,42 @@ describe('TestFixedMath', () => {
result = await fixed.testMulQ128(toQ64(1), toQ64(2.0));
expect(fromQ64(result).eq(2.0)).to.be.true;

x = BN.BigNumber(2).pow(128).minus(1)
y = BN.BigNumber(1.0)
x = BigNumber(2).pow(128).minus(1)
y = BigNumber(1.0)
expected = x.times(y).integerValue();
result = await fixed.testMulQ128(toQ64(x), toQ64(y));
expect(fromQ64(result).eq(expected)).to.be.true;
});

it("divQ128", async () => {
let x = BN.BigNumber(18000000);
let y = BN.BigNumber(80855071);
let expected = BN.BigNumber("4106624225545135308");
let x = BigNumber(18000000);
let y = BigNumber(80855071);
let expected = BigNumber("4106624225545135308");
let result = await fixed.testDivQ128(toQ64(x), toQ64(y));
expect(BN.BigNumber(result.toString()).eq(expected)).to.be.true;
expect(BigNumber(result.toString()).eq(expected)).to.be.true;

x = BN.BigNumber(10).pow(18);
y = BN.BigNumber(1).div(4);
x = BigNumber(10).pow(18);
y = BigNumber(1).div(4);
result = await fixed.testDivQ128(toQ64(x), toQ64(y));
expect(fromQ64(result).eq(4 * 10**18)).to.be.true;

x = (BN.BigNumber(2).pow(126));
y = BN.BigNumber(0.5);
x = (BigNumber(2).pow(126));
y = BigNumber(0.5);
result = await fixed.testDivQ128(toQ64(x), toQ64(y));
expect(fromQ64(result).eq(BN.BigNumber(2).pow(127))).to.be.true;
expect(fromQ64(result).eq(BigNumber(2).pow(127))).to.be.true;

x = (BN.BigNumber(2).pow(127));
x = (BigNumber(2).pow(127));
result = await fixed.testDivQ128(toQ64(x), toQ64(y));
expect(fromQ64(result).eq(BN.BigNumber(2).pow(128))).to.be.true;
expect(fromQ64(result).eq(BigNumber(2).pow(128))).to.be.true;

x = BN.BigNumber(1);
y = BN.BigNumber(10000000000000000000000000);
x = BigNumber(1);
y = BigNumber(10000000000000000000000000);
result = await fixed.testDivQ128(toQ64(x), toQ64(y));
expect(fromQ64(result).eq(BN.BigNumber(1).div(10000000000000000000000000))).to.be.true;
expect(fromQ64(result).eq(BigNumber(1).div(10000000000000000000000000))).to.be.true;

y = BN.BigNumber(100000000000000000000000000000000000000);
y = BigNumber(100000000000000000000000000000000000000);
result = await fixed.testDivQ128(toQ64(x), toQ64(y));
expect(fromQ64(result).eq(BN.BigNumber(1).div(100000000000000000000000000000000000000))).to.be.true;
expect(fromQ64(result).eq(BigNumber(1).div(100000000000000000000000000000000000000))).to.be.true;
});

})
Loading

0 comments on commit 9596ac5

Please sign in to comment.