Math.test.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
  5. const { min, max } = require('../../helpers/math');
  6. const {
  7. bigint: { Rounding },
  8. } = require('../../helpers/enums.js');
  9. const RoundingDown = [Rounding.Floor, Rounding.Trunc];
  10. const RoundingUp = [Rounding.Ceil, Rounding.Expand];
  11. async function testCommutative(fn, lhs, rhs, expected, ...extra) {
  12. expect(await fn(lhs, rhs, ...extra)).to.deep.equal(expected);
  13. expect(await fn(rhs, lhs, ...extra)).to.deep.equal(expected);
  14. }
  15. async function fixture() {
  16. const mock = await ethers.deployContract('$Math');
  17. // disambiguation, we use the version with explicit rounding
  18. mock.$mulDiv = mock['$mulDiv(uint256,uint256,uint256,uint8)'];
  19. mock.$sqrt = mock['$sqrt(uint256,uint8)'];
  20. mock.$log2 = mock['$log2(uint256,uint8)'];
  21. mock.$log10 = mock['$log10(uint256,uint8)'];
  22. mock.$log256 = mock['$log256(uint256,uint8)'];
  23. return { mock };
  24. }
  25. describe('Math', function () {
  26. beforeEach(async function () {
  27. Object.assign(this, await loadFixture(fixture));
  28. });
  29. describe('tryAdd', function () {
  30. it('adds correctly', async function () {
  31. const a = 5678n;
  32. const b = 1234n;
  33. await testCommutative(this.mock.$tryAdd, a, b, [true, a + b]);
  34. });
  35. it('reverts on addition overflow', async function () {
  36. const a = ethers.MaxUint256;
  37. const b = 1n;
  38. await testCommutative(this.mock.$tryAdd, a, b, [false, 0n]);
  39. });
  40. });
  41. describe('trySub', function () {
  42. it('subtracts correctly', async function () {
  43. const a = 5678n;
  44. const b = 1234n;
  45. expect(await this.mock.$trySub(a, b)).to.deep.equal([true, a - b]);
  46. });
  47. it('reverts if subtraction result would be negative', async function () {
  48. const a = 1234n;
  49. const b = 5678n;
  50. expect(await this.mock.$trySub(a, b)).to.deep.equal([false, 0n]);
  51. });
  52. });
  53. describe('tryMul', function () {
  54. it('multiplies correctly', async function () {
  55. const a = 1234n;
  56. const b = 5678n;
  57. await testCommutative(this.mock.$tryMul, a, b, [true, a * b]);
  58. });
  59. it('multiplies by zero correctly', async function () {
  60. const a = 0n;
  61. const b = 5678n;
  62. await testCommutative(this.mock.$tryMul, a, b, [true, a * b]);
  63. });
  64. it('reverts on multiplication overflow', async function () {
  65. const a = ethers.MaxUint256;
  66. const b = 2n;
  67. await testCommutative(this.mock.$tryMul, a, b, [false, 0n]);
  68. });
  69. });
  70. describe('tryDiv', function () {
  71. it('divides correctly', async function () {
  72. const a = 5678n;
  73. const b = 5678n;
  74. expect(await this.mock.$tryDiv(a, b)).to.deep.equal([true, a / b]);
  75. });
  76. it('divides zero correctly', async function () {
  77. const a = 0n;
  78. const b = 5678n;
  79. expect(await this.mock.$tryDiv(a, b)).to.deep.equal([true, a / b]);
  80. });
  81. it('returns complete number result on non-even division', async function () {
  82. const a = 7000n;
  83. const b = 5678n;
  84. expect(await this.mock.$tryDiv(a, b)).to.deep.equal([true, a / b]);
  85. });
  86. it('reverts on division by zero', async function () {
  87. const a = 5678n;
  88. const b = 0n;
  89. expect(await this.mock.$tryDiv(a, b)).to.deep.equal([false, 0n]);
  90. });
  91. });
  92. describe('tryMod', function () {
  93. describe('modulos correctly', function () {
  94. it('when the dividend is smaller than the divisor', async function () {
  95. const a = 284n;
  96. const b = 5678n;
  97. expect(await this.mock.$tryMod(a, b)).to.deep.equal([true, a % b]);
  98. });
  99. it('when the dividend is equal to the divisor', async function () {
  100. const a = 5678n;
  101. const b = 5678n;
  102. expect(await this.mock.$tryMod(a, b)).to.deep.equal([true, a % b]);
  103. });
  104. it('when the dividend is larger than the divisor', async function () {
  105. const a = 7000n;
  106. const b = 5678n;
  107. expect(await this.mock.$tryMod(a, b)).to.deep.equal([true, a % b]);
  108. });
  109. it('when the dividend is a multiple of the divisor', async function () {
  110. const a = 17034n; // 17034 == 5678 * 3
  111. const b = 5678n;
  112. expect(await this.mock.$tryMod(a, b)).to.deep.equal([true, a % b]);
  113. });
  114. });
  115. it('reverts with a 0 divisor', async function () {
  116. const a = 5678n;
  117. const b = 0n;
  118. expect(await this.mock.$tryMod(a, b)).to.deep.equal([false, 0n]);
  119. });
  120. });
  121. describe('max', function () {
  122. it('is correctly detected in both position', async function () {
  123. await testCommutative(this.mock.$max, 1234n, 5678n, max(1234n, 5678n));
  124. });
  125. });
  126. describe('min', function () {
  127. it('is correctly detected in both position', async function () {
  128. await testCommutative(this.mock.$min, 1234n, 5678n, min(1234n, 5678n));
  129. });
  130. });
  131. describe('average', function () {
  132. it('is correctly calculated with two odd numbers', async function () {
  133. const a = 57417n;
  134. const b = 95431n;
  135. expect(await this.mock.$average(a, b)).to.equal((a + b) / 2n);
  136. });
  137. it('is correctly calculated with two even numbers', async function () {
  138. const a = 42304n;
  139. const b = 84346n;
  140. expect(await this.mock.$average(a, b)).to.equal((a + b) / 2n);
  141. });
  142. it('is correctly calculated with one even and one odd number', async function () {
  143. const a = 57417n;
  144. const b = 84346n;
  145. expect(await this.mock.$average(a, b)).to.equal((a + b) / 2n);
  146. });
  147. it('is correctly calculated with two max uint256 numbers', async function () {
  148. const a = ethers.MaxUint256;
  149. expect(await this.mock.$average(a, a)).to.equal(a);
  150. });
  151. });
  152. describe('ceilDiv', function () {
  153. it('reverts on zero division', async function () {
  154. const a = 2n;
  155. const b = 0n;
  156. // It's unspecified because it's a low level 0 division error
  157. await expect(this.mock.$ceilDiv(a, b)).to.be.revertedWithPanic(PANIC_CODES.DIVISION_BY_ZERO);
  158. });
  159. it('does not round up a zero result', async function () {
  160. const a = 0n;
  161. const b = 2n;
  162. const r = 0n;
  163. expect(await this.mock.$ceilDiv(a, b)).to.equal(r);
  164. });
  165. it('does not round up on exact division', async function () {
  166. const a = 10n;
  167. const b = 5n;
  168. const r = 2n;
  169. expect(await this.mock.$ceilDiv(a, b)).to.equal(r);
  170. });
  171. it('rounds up on division with remainders', async function () {
  172. const a = 42n;
  173. const b = 13n;
  174. const r = 4n;
  175. expect(await this.mock.$ceilDiv(a, b)).to.equal(r);
  176. });
  177. it('does not overflow', async function () {
  178. const a = ethers.MaxUint256;
  179. const b = 2n;
  180. const r = 1n << 255n;
  181. expect(await this.mock.$ceilDiv(a, b)).to.equal(r);
  182. });
  183. it('correctly computes max uint256 divided by 1', async function () {
  184. const a = ethers.MaxUint256;
  185. const b = 1n;
  186. const r = ethers.MaxUint256;
  187. expect(await this.mock.$ceilDiv(a, b)).to.equal(r);
  188. });
  189. });
  190. describe('muldiv', function () {
  191. it('divide by 0', async function () {
  192. const a = 1n;
  193. const b = 1n;
  194. const c = 0n;
  195. await expect(this.mock.$mulDiv(a, b, c, Rounding.Floor)).to.be.revertedWithPanic(PANIC_CODES.DIVISION_BY_ZERO);
  196. });
  197. it('reverts with result higher than 2 ^ 256', async function () {
  198. const a = 5n;
  199. const b = ethers.MaxUint256;
  200. const c = 2n;
  201. await expect(this.mock.$mulDiv(a, b, c, Rounding.Floor)).to.be.revertedWithCustomError(
  202. this.mock,
  203. 'MathOverflowedMulDiv',
  204. );
  205. });
  206. describe('does round down', function () {
  207. it('small values', async function () {
  208. for (const rounding of RoundingDown) {
  209. expect(await this.mock.$mulDiv(3n, 4n, 5n, rounding)).to.equal(2n);
  210. expect(await this.mock.$mulDiv(3n, 5n, 5n, rounding)).to.equal(3n);
  211. }
  212. });
  213. it('large values', async function () {
  214. for (const rounding of RoundingDown) {
  215. expect(await this.mock.$mulDiv(42n, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding)).to.equal(41n);
  216. expect(await this.mock.$mulDiv(17n, ethers.MaxUint256, ethers.MaxUint256, rounding)).to.equal(17n);
  217. expect(
  218. await this.mock.$mulDiv(ethers.MaxUint256 - 1n, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding),
  219. ).to.equal(ethers.MaxUint256 - 2n);
  220. expect(
  221. await this.mock.$mulDiv(ethers.MaxUint256, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding),
  222. ).to.equal(ethers.MaxUint256 - 1n);
  223. expect(await this.mock.$mulDiv(ethers.MaxUint256, ethers.MaxUint256, ethers.MaxUint256, rounding)).to.equal(
  224. ethers.MaxUint256,
  225. );
  226. }
  227. });
  228. });
  229. describe('does round up', function () {
  230. it('small values', async function () {
  231. for (const rounding of RoundingUp) {
  232. expect(await this.mock.$mulDiv(3n, 4n, 5n, rounding)).to.equal(3n);
  233. expect(await this.mock.$mulDiv(3n, 5n, 5n, rounding)).to.equal(3n);
  234. }
  235. });
  236. it('large values', async function () {
  237. for (const rounding of RoundingUp) {
  238. expect(await this.mock.$mulDiv(42n, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding)).to.equal(42n);
  239. expect(await this.mock.$mulDiv(17n, ethers.MaxUint256, ethers.MaxUint256, rounding)).to.equal(17n);
  240. expect(
  241. await this.mock.$mulDiv(ethers.MaxUint256 - 1n, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding),
  242. ).to.equal(ethers.MaxUint256 - 1n);
  243. expect(
  244. await this.mock.$mulDiv(ethers.MaxUint256, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding),
  245. ).to.equal(ethers.MaxUint256 - 1n);
  246. expect(await this.mock.$mulDiv(ethers.MaxUint256, ethers.MaxUint256, ethers.MaxUint256, rounding)).to.equal(
  247. ethers.MaxUint256,
  248. );
  249. }
  250. });
  251. });
  252. });
  253. describe('sqrt', function () {
  254. it('rounds down', async function () {
  255. for (const rounding of RoundingDown) {
  256. expect(await this.mock.$sqrt(0n, rounding)).to.equal(0n);
  257. expect(await this.mock.$sqrt(1n, rounding)).to.equal(1n);
  258. expect(await this.mock.$sqrt(2n, rounding)).to.equal(1n);
  259. expect(await this.mock.$sqrt(3n, rounding)).to.equal(1n);
  260. expect(await this.mock.$sqrt(4n, rounding)).to.equal(2n);
  261. expect(await this.mock.$sqrt(144n, rounding)).to.equal(12n);
  262. expect(await this.mock.$sqrt(999999n, rounding)).to.equal(999n);
  263. expect(await this.mock.$sqrt(1000000n, rounding)).to.equal(1000n);
  264. expect(await this.mock.$sqrt(1000001n, rounding)).to.equal(1000n);
  265. expect(await this.mock.$sqrt(1002000n, rounding)).to.equal(1000n);
  266. expect(await this.mock.$sqrt(1002001n, rounding)).to.equal(1001n);
  267. expect(await this.mock.$sqrt(ethers.MaxUint256, rounding)).to.equal(340282366920938463463374607431768211455n);
  268. }
  269. });
  270. it('rounds up', async function () {
  271. for (const rounding of RoundingUp) {
  272. expect(await this.mock.$sqrt(0n, rounding)).to.equal(0n);
  273. expect(await this.mock.$sqrt(1n, rounding)).to.equal(1n);
  274. expect(await this.mock.$sqrt(2n, rounding)).to.equal(2n);
  275. expect(await this.mock.$sqrt(3n, rounding)).to.equal(2n);
  276. expect(await this.mock.$sqrt(4n, rounding)).to.equal(2n);
  277. expect(await this.mock.$sqrt(144n, rounding)).to.equal(12n);
  278. expect(await this.mock.$sqrt(999999n, rounding)).to.equal(1000n);
  279. expect(await this.mock.$sqrt(1000000n, rounding)).to.equal(1000n);
  280. expect(await this.mock.$sqrt(1000001n, rounding)).to.equal(1001n);
  281. expect(await this.mock.$sqrt(1002000n, rounding)).to.equal(1001n);
  282. expect(await this.mock.$sqrt(1002001n, rounding)).to.equal(1001n);
  283. expect(await this.mock.$sqrt(ethers.MaxUint256, rounding)).to.equal(340282366920938463463374607431768211456n);
  284. }
  285. });
  286. });
  287. describe('log', function () {
  288. describe('log2', function () {
  289. it('rounds down', async function () {
  290. for (const rounding of RoundingDown) {
  291. expect(await this.mock.$log2(0n, rounding)).to.equal(0n);
  292. expect(await this.mock.$log2(1n, rounding)).to.equal(0n);
  293. expect(await this.mock.$log2(2n, rounding)).to.equal(1n);
  294. expect(await this.mock.$log2(3n, rounding)).to.equal(1n);
  295. expect(await this.mock.$log2(4n, rounding)).to.equal(2n);
  296. expect(await this.mock.$log2(5n, rounding)).to.equal(2n);
  297. expect(await this.mock.$log2(6n, rounding)).to.equal(2n);
  298. expect(await this.mock.$log2(7n, rounding)).to.equal(2n);
  299. expect(await this.mock.$log2(8n, rounding)).to.equal(3n);
  300. expect(await this.mock.$log2(9n, rounding)).to.equal(3n);
  301. expect(await this.mock.$log2(ethers.MaxUint256, rounding)).to.equal(255n);
  302. }
  303. });
  304. it('rounds up', async function () {
  305. for (const rounding of RoundingUp) {
  306. expect(await this.mock.$log2(0n, rounding)).to.equal(0n);
  307. expect(await this.mock.$log2(1n, rounding)).to.equal(0n);
  308. expect(await this.mock.$log2(2n, rounding)).to.equal(1n);
  309. expect(await this.mock.$log2(3n, rounding)).to.equal(2n);
  310. expect(await this.mock.$log2(4n, rounding)).to.equal(2n);
  311. expect(await this.mock.$log2(5n, rounding)).to.equal(3n);
  312. expect(await this.mock.$log2(6n, rounding)).to.equal(3n);
  313. expect(await this.mock.$log2(7n, rounding)).to.equal(3n);
  314. expect(await this.mock.$log2(8n, rounding)).to.equal(3n);
  315. expect(await this.mock.$log2(9n, rounding)).to.equal(4n);
  316. expect(await this.mock.$log2(ethers.MaxUint256, rounding)).to.equal(256n);
  317. }
  318. });
  319. });
  320. describe('log10', function () {
  321. it('rounds down', async function () {
  322. for (const rounding of RoundingDown) {
  323. expect(await this.mock.$log10(0n, rounding)).to.equal(0n);
  324. expect(await this.mock.$log10(1n, rounding)).to.equal(0n);
  325. expect(await this.mock.$log10(2n, rounding)).to.equal(0n);
  326. expect(await this.mock.$log10(9n, rounding)).to.equal(0n);
  327. expect(await this.mock.$log10(10n, rounding)).to.equal(1n);
  328. expect(await this.mock.$log10(11n, rounding)).to.equal(1n);
  329. expect(await this.mock.$log10(99n, rounding)).to.equal(1n);
  330. expect(await this.mock.$log10(100n, rounding)).to.equal(2n);
  331. expect(await this.mock.$log10(101n, rounding)).to.equal(2n);
  332. expect(await this.mock.$log10(999n, rounding)).to.equal(2n);
  333. expect(await this.mock.$log10(1000n, rounding)).to.equal(3n);
  334. expect(await this.mock.$log10(1001n, rounding)).to.equal(3n);
  335. expect(await this.mock.$log10(ethers.MaxUint256, rounding)).to.equal(77n);
  336. }
  337. });
  338. it('rounds up', async function () {
  339. for (const rounding of RoundingUp) {
  340. expect(await this.mock.$log10(0n, rounding)).to.equal(0n);
  341. expect(await this.mock.$log10(1n, rounding)).to.equal(0n);
  342. expect(await this.mock.$log10(2n, rounding)).to.equal(1n);
  343. expect(await this.mock.$log10(9n, rounding)).to.equal(1n);
  344. expect(await this.mock.$log10(10n, rounding)).to.equal(1n);
  345. expect(await this.mock.$log10(11n, rounding)).to.equal(2n);
  346. expect(await this.mock.$log10(99n, rounding)).to.equal(2n);
  347. expect(await this.mock.$log10(100n, rounding)).to.equal(2n);
  348. expect(await this.mock.$log10(101n, rounding)).to.equal(3n);
  349. expect(await this.mock.$log10(999n, rounding)).to.equal(3n);
  350. expect(await this.mock.$log10(1000n, rounding)).to.equal(3n);
  351. expect(await this.mock.$log10(1001n, rounding)).to.equal(4n);
  352. expect(await this.mock.$log10(ethers.MaxUint256, rounding)).to.equal(78n);
  353. }
  354. });
  355. });
  356. describe('log256', function () {
  357. it('rounds down', async function () {
  358. for (const rounding of RoundingDown) {
  359. expect(await this.mock.$log256(0n, rounding)).to.equal(0n);
  360. expect(await this.mock.$log256(1n, rounding)).to.equal(0n);
  361. expect(await this.mock.$log256(2n, rounding)).to.equal(0n);
  362. expect(await this.mock.$log256(255n, rounding)).to.equal(0n);
  363. expect(await this.mock.$log256(256n, rounding)).to.equal(1n);
  364. expect(await this.mock.$log256(257n, rounding)).to.equal(1n);
  365. expect(await this.mock.$log256(65535n, rounding)).to.equal(1n);
  366. expect(await this.mock.$log256(65536n, rounding)).to.equal(2n);
  367. expect(await this.mock.$log256(65537n, rounding)).to.equal(2n);
  368. expect(await this.mock.$log256(ethers.MaxUint256, rounding)).to.equal(31n);
  369. }
  370. });
  371. it('rounds up', async function () {
  372. for (const rounding of RoundingUp) {
  373. expect(await this.mock.$log256(0n, rounding)).to.equal(0n);
  374. expect(await this.mock.$log256(1n, rounding)).to.equal(0n);
  375. expect(await this.mock.$log256(2n, rounding)).to.equal(1n);
  376. expect(await this.mock.$log256(255n, rounding)).to.equal(1n);
  377. expect(await this.mock.$log256(256n, rounding)).to.equal(1n);
  378. expect(await this.mock.$log256(257n, rounding)).to.equal(2n);
  379. expect(await this.mock.$log256(65535n, rounding)).to.equal(2n);
  380. expect(await this.mock.$log256(65536n, rounding)).to.equal(2n);
  381. expect(await this.mock.$log256(65537n, rounding)).to.equal(3n);
  382. expect(await this.mock.$log256(ethers.MaxUint256, rounding)).to.equal(32n);
  383. }
  384. });
  385. });
  386. });
  387. });