Math.test.js 19 KB

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