Math.test.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 { 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('tryModExp', function () {
  121. it('is correctly returning true and calculating modulus', async function () {
  122. const base = 3n;
  123. const exponent = 200n;
  124. const modulus = 50n;
  125. expect(await this.mock.$tryModExp(base, exponent, modulus)).to.deep.equal([true, base ** exponent % modulus]);
  126. });
  127. it('is correctly returning false when modulus is 0', async function () {
  128. const base = 3n;
  129. const exponent = 200n;
  130. const modulus = 0n;
  131. expect(await this.mock.$tryModExp(base, exponent, modulus)).to.deep.equal([false, 0n]);
  132. });
  133. });
  134. describe('max', function () {
  135. it('is correctly detected in both position', async function () {
  136. await testCommutative(this.mock.$max, 1234n, 5678n, max(1234n, 5678n));
  137. });
  138. });
  139. describe('min', function () {
  140. it('is correctly detected in both position', async function () {
  141. await testCommutative(this.mock.$min, 1234n, 5678n, min(1234n, 5678n));
  142. });
  143. });
  144. describe('average', function () {
  145. it('is correctly calculated with two odd numbers', async function () {
  146. const a = 57417n;
  147. const b = 95431n;
  148. expect(await this.mock.$average(a, b)).to.equal((a + b) / 2n);
  149. });
  150. it('is correctly calculated with two even numbers', async function () {
  151. const a = 42304n;
  152. const b = 84346n;
  153. expect(await this.mock.$average(a, b)).to.equal((a + b) / 2n);
  154. });
  155. it('is correctly calculated with one even and one odd number', async function () {
  156. const a = 57417n;
  157. const b = 84346n;
  158. expect(await this.mock.$average(a, b)).to.equal((a + b) / 2n);
  159. });
  160. it('is correctly calculated with two max uint256 numbers', async function () {
  161. const a = ethers.MaxUint256;
  162. expect(await this.mock.$average(a, a)).to.equal(a);
  163. });
  164. });
  165. describe('ceilDiv', function () {
  166. it('reverts on zero division', async function () {
  167. const a = 2n;
  168. const b = 0n;
  169. // It's unspecified because it's a low level 0 division error
  170. await expect(this.mock.$ceilDiv(a, b)).to.be.revertedWithPanic(PANIC_CODES.DIVISION_BY_ZERO);
  171. });
  172. it('does not round up a zero result', async function () {
  173. const a = 0n;
  174. const b = 2n;
  175. const r = 0n;
  176. expect(await this.mock.$ceilDiv(a, b)).to.equal(r);
  177. });
  178. it('does not round up on exact division', async function () {
  179. const a = 10n;
  180. const b = 5n;
  181. const r = 2n;
  182. expect(await this.mock.$ceilDiv(a, b)).to.equal(r);
  183. });
  184. it('rounds up on division with remainders', async function () {
  185. const a = 42n;
  186. const b = 13n;
  187. const r = 4n;
  188. expect(await this.mock.$ceilDiv(a, b)).to.equal(r);
  189. });
  190. it('does not overflow', async function () {
  191. const a = ethers.MaxUint256;
  192. const b = 2n;
  193. const r = 1n << 255n;
  194. expect(await this.mock.$ceilDiv(a, b)).to.equal(r);
  195. });
  196. it('correctly computes max uint256 divided by 1', async function () {
  197. const a = ethers.MaxUint256;
  198. const b = 1n;
  199. const r = ethers.MaxUint256;
  200. expect(await this.mock.$ceilDiv(a, b)).to.equal(r);
  201. });
  202. });
  203. describe('mulDiv', function () {
  204. it('divide by 0', async function () {
  205. const a = 1n;
  206. const b = 1n;
  207. const c = 0n;
  208. await expect(this.mock.$mulDiv(a, b, c, Rounding.Floor)).to.be.revertedWithPanic(PANIC_CODES.DIVISION_BY_ZERO);
  209. });
  210. it('reverts with result higher than 2 ^ 256', async function () {
  211. const a = 5n;
  212. const b = ethers.MaxUint256;
  213. const c = 2n;
  214. await expect(this.mock.$mulDiv(a, b, c, Rounding.Floor)).to.be.revertedWithPanic(
  215. PANIC_CODES.ARITHMETIC_UNDER_OR_OVERFLOW,
  216. );
  217. });
  218. describe('does round down', function () {
  219. it('small values', async function () {
  220. for (const rounding of RoundingDown) {
  221. expect(await this.mock.$mulDiv(3n, 4n, 5n, rounding)).to.equal(2n);
  222. expect(await this.mock.$mulDiv(3n, 5n, 5n, rounding)).to.equal(3n);
  223. }
  224. });
  225. it('large values', async function () {
  226. for (const rounding of RoundingDown) {
  227. expect(await this.mock.$mulDiv(42n, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding)).to.equal(41n);
  228. expect(await this.mock.$mulDiv(17n, ethers.MaxUint256, ethers.MaxUint256, rounding)).to.equal(17n);
  229. expect(
  230. await this.mock.$mulDiv(ethers.MaxUint256 - 1n, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding),
  231. ).to.equal(ethers.MaxUint256 - 2n);
  232. expect(
  233. await this.mock.$mulDiv(ethers.MaxUint256, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding),
  234. ).to.equal(ethers.MaxUint256 - 1n);
  235. expect(await this.mock.$mulDiv(ethers.MaxUint256, ethers.MaxUint256, ethers.MaxUint256, rounding)).to.equal(
  236. ethers.MaxUint256,
  237. );
  238. }
  239. });
  240. });
  241. describe('does round up', function () {
  242. it('small values', async function () {
  243. for (const rounding of RoundingUp) {
  244. expect(await this.mock.$mulDiv(3n, 4n, 5n, rounding)).to.equal(3n);
  245. expect(await this.mock.$mulDiv(3n, 5n, 5n, rounding)).to.equal(3n);
  246. }
  247. });
  248. it('large values', async function () {
  249. for (const rounding of RoundingUp) {
  250. expect(await this.mock.$mulDiv(42n, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding)).to.equal(42n);
  251. expect(await this.mock.$mulDiv(17n, ethers.MaxUint256, ethers.MaxUint256, rounding)).to.equal(17n);
  252. expect(
  253. await this.mock.$mulDiv(ethers.MaxUint256 - 1n, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding),
  254. ).to.equal(ethers.MaxUint256 - 1n);
  255. expect(
  256. await this.mock.$mulDiv(ethers.MaxUint256, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding),
  257. ).to.equal(ethers.MaxUint256 - 1n);
  258. expect(await this.mock.$mulDiv(ethers.MaxUint256, ethers.MaxUint256, ethers.MaxUint256, rounding)).to.equal(
  259. ethers.MaxUint256,
  260. );
  261. }
  262. });
  263. });
  264. });
  265. describe('invMod', function () {
  266. for (const factors of [
  267. [0n],
  268. [1n],
  269. [2n],
  270. [17n],
  271. [65537n],
  272. [0xffffffff00000001000000000000000000000000ffffffffffffffffffffffffn],
  273. [3n, 5n],
  274. [3n, 7n],
  275. [47n, 53n],
  276. ]) {
  277. const p = factors.reduce((acc, f) => acc * f, 1n);
  278. describe(`using p=${p} which is ${p > 1 && factors.length > 1 ? 'not ' : ''}a prime`, function () {
  279. it('trying to inverse 0 returns 0', async function () {
  280. expect(await this.mock.$invMod(0, p)).to.equal(0n);
  281. expect(await this.mock.$invMod(p, p)).to.equal(0n); // p is 0 mod p
  282. });
  283. if (p != 0) {
  284. for (const value of Array.from({ length: 16 }, generators.uint256)) {
  285. const isInversible = factors.every(f => value % f);
  286. it(`trying to inverse ${value}`, async function () {
  287. const result = await this.mock.$invMod(value, p);
  288. if (isInversible) {
  289. expect((value * result) % p).to.equal(1n);
  290. } else {
  291. expect(result).to.equal(0n);
  292. }
  293. });
  294. }
  295. }
  296. });
  297. }
  298. });
  299. describe('modExp', function () {
  300. it('is correctly calculating modulus', async function () {
  301. const base = 3n;
  302. const exponent = 200n;
  303. const modulus = 50n;
  304. expect(await this.mock.$modExp(base, exponent, modulus)).to.equal(base ** exponent % modulus);
  305. });
  306. it('is correctly reverting when modulus is zero', async function () {
  307. const base = 3n;
  308. const exponent = 200n;
  309. const modulus = 0n;
  310. await expect(this.mock.$modExp(base, exponent, modulus)).to.be.revertedWithPanic(PANIC_CODES.DIVISION_BY_ZERO);
  311. });
  312. });
  313. describe('sqrt', function () {
  314. it('rounds down', async function () {
  315. for (const rounding of RoundingDown) {
  316. expect(await this.mock.$sqrt(0n, rounding)).to.equal(0n);
  317. expect(await this.mock.$sqrt(1n, rounding)).to.equal(1n);
  318. expect(await this.mock.$sqrt(2n, rounding)).to.equal(1n);
  319. expect(await this.mock.$sqrt(3n, rounding)).to.equal(1n);
  320. expect(await this.mock.$sqrt(4n, rounding)).to.equal(2n);
  321. expect(await this.mock.$sqrt(144n, rounding)).to.equal(12n);
  322. expect(await this.mock.$sqrt(999999n, rounding)).to.equal(999n);
  323. expect(await this.mock.$sqrt(1000000n, rounding)).to.equal(1000n);
  324. expect(await this.mock.$sqrt(1000001n, rounding)).to.equal(1000n);
  325. expect(await this.mock.$sqrt(1002000n, rounding)).to.equal(1000n);
  326. expect(await this.mock.$sqrt(1002001n, rounding)).to.equal(1001n);
  327. expect(await this.mock.$sqrt(ethers.MaxUint256, rounding)).to.equal(340282366920938463463374607431768211455n);
  328. }
  329. });
  330. it('rounds up', async function () {
  331. for (const rounding of RoundingUp) {
  332. expect(await this.mock.$sqrt(0n, rounding)).to.equal(0n);
  333. expect(await this.mock.$sqrt(1n, rounding)).to.equal(1n);
  334. expect(await this.mock.$sqrt(2n, rounding)).to.equal(2n);
  335. expect(await this.mock.$sqrt(3n, rounding)).to.equal(2n);
  336. expect(await this.mock.$sqrt(4n, rounding)).to.equal(2n);
  337. expect(await this.mock.$sqrt(144n, rounding)).to.equal(12n);
  338. expect(await this.mock.$sqrt(999999n, rounding)).to.equal(1000n);
  339. expect(await this.mock.$sqrt(1000000n, rounding)).to.equal(1000n);
  340. expect(await this.mock.$sqrt(1000001n, rounding)).to.equal(1001n);
  341. expect(await this.mock.$sqrt(1002000n, rounding)).to.equal(1001n);
  342. expect(await this.mock.$sqrt(1002001n, rounding)).to.equal(1001n);
  343. expect(await this.mock.$sqrt(ethers.MaxUint256, rounding)).to.equal(340282366920938463463374607431768211456n);
  344. }
  345. });
  346. });
  347. describe('log', function () {
  348. describe('log2', function () {
  349. it('rounds down', async function () {
  350. for (const rounding of RoundingDown) {
  351. expect(await this.mock.$log2(0n, rounding)).to.equal(0n);
  352. expect(await this.mock.$log2(1n, rounding)).to.equal(0n);
  353. expect(await this.mock.$log2(2n, rounding)).to.equal(1n);
  354. expect(await this.mock.$log2(3n, rounding)).to.equal(1n);
  355. expect(await this.mock.$log2(4n, rounding)).to.equal(2n);
  356. expect(await this.mock.$log2(5n, rounding)).to.equal(2n);
  357. expect(await this.mock.$log2(6n, rounding)).to.equal(2n);
  358. expect(await this.mock.$log2(7n, rounding)).to.equal(2n);
  359. expect(await this.mock.$log2(8n, rounding)).to.equal(3n);
  360. expect(await this.mock.$log2(9n, rounding)).to.equal(3n);
  361. expect(await this.mock.$log2(ethers.MaxUint256, rounding)).to.equal(255n);
  362. }
  363. });
  364. it('rounds up', async function () {
  365. for (const rounding of RoundingUp) {
  366. expect(await this.mock.$log2(0n, rounding)).to.equal(0n);
  367. expect(await this.mock.$log2(1n, rounding)).to.equal(0n);
  368. expect(await this.mock.$log2(2n, rounding)).to.equal(1n);
  369. expect(await this.mock.$log2(3n, rounding)).to.equal(2n);
  370. expect(await this.mock.$log2(4n, rounding)).to.equal(2n);
  371. expect(await this.mock.$log2(5n, rounding)).to.equal(3n);
  372. expect(await this.mock.$log2(6n, rounding)).to.equal(3n);
  373. expect(await this.mock.$log2(7n, rounding)).to.equal(3n);
  374. expect(await this.mock.$log2(8n, rounding)).to.equal(3n);
  375. expect(await this.mock.$log2(9n, rounding)).to.equal(4n);
  376. expect(await this.mock.$log2(ethers.MaxUint256, rounding)).to.equal(256n);
  377. }
  378. });
  379. });
  380. describe('log10', function () {
  381. it('rounds down', async function () {
  382. for (const rounding of RoundingDown) {
  383. expect(await this.mock.$log10(0n, rounding)).to.equal(0n);
  384. expect(await this.mock.$log10(1n, rounding)).to.equal(0n);
  385. expect(await this.mock.$log10(2n, rounding)).to.equal(0n);
  386. expect(await this.mock.$log10(9n, rounding)).to.equal(0n);
  387. expect(await this.mock.$log10(10n, rounding)).to.equal(1n);
  388. expect(await this.mock.$log10(11n, rounding)).to.equal(1n);
  389. expect(await this.mock.$log10(99n, rounding)).to.equal(1n);
  390. expect(await this.mock.$log10(100n, rounding)).to.equal(2n);
  391. expect(await this.mock.$log10(101n, rounding)).to.equal(2n);
  392. expect(await this.mock.$log10(999n, rounding)).to.equal(2n);
  393. expect(await this.mock.$log10(1000n, rounding)).to.equal(3n);
  394. expect(await this.mock.$log10(1001n, rounding)).to.equal(3n);
  395. expect(await this.mock.$log10(ethers.MaxUint256, rounding)).to.equal(77n);
  396. }
  397. });
  398. it('rounds up', async function () {
  399. for (const rounding of RoundingUp) {
  400. expect(await this.mock.$log10(0n, rounding)).to.equal(0n);
  401. expect(await this.mock.$log10(1n, rounding)).to.equal(0n);
  402. expect(await this.mock.$log10(2n, rounding)).to.equal(1n);
  403. expect(await this.mock.$log10(9n, rounding)).to.equal(1n);
  404. expect(await this.mock.$log10(10n, rounding)).to.equal(1n);
  405. expect(await this.mock.$log10(11n, rounding)).to.equal(2n);
  406. expect(await this.mock.$log10(99n, rounding)).to.equal(2n);
  407. expect(await this.mock.$log10(100n, rounding)).to.equal(2n);
  408. expect(await this.mock.$log10(101n, rounding)).to.equal(3n);
  409. expect(await this.mock.$log10(999n, rounding)).to.equal(3n);
  410. expect(await this.mock.$log10(1000n, rounding)).to.equal(3n);
  411. expect(await this.mock.$log10(1001n, rounding)).to.equal(4n);
  412. expect(await this.mock.$log10(ethers.MaxUint256, rounding)).to.equal(78n);
  413. }
  414. });
  415. });
  416. describe('log256', function () {
  417. it('rounds down', async function () {
  418. for (const rounding of RoundingDown) {
  419. expect(await this.mock.$log256(0n, rounding)).to.equal(0n);
  420. expect(await this.mock.$log256(1n, rounding)).to.equal(0n);
  421. expect(await this.mock.$log256(2n, rounding)).to.equal(0n);
  422. expect(await this.mock.$log256(255n, rounding)).to.equal(0n);
  423. expect(await this.mock.$log256(256n, rounding)).to.equal(1n);
  424. expect(await this.mock.$log256(257n, rounding)).to.equal(1n);
  425. expect(await this.mock.$log256(65535n, rounding)).to.equal(1n);
  426. expect(await this.mock.$log256(65536n, rounding)).to.equal(2n);
  427. expect(await this.mock.$log256(65537n, rounding)).to.equal(2n);
  428. expect(await this.mock.$log256(ethers.MaxUint256, rounding)).to.equal(31n);
  429. }
  430. });
  431. it('rounds up', async function () {
  432. for (const rounding of RoundingUp) {
  433. expect(await this.mock.$log256(0n, rounding)).to.equal(0n);
  434. expect(await this.mock.$log256(1n, rounding)).to.equal(0n);
  435. expect(await this.mock.$log256(2n, rounding)).to.equal(1n);
  436. expect(await this.mock.$log256(255n, rounding)).to.equal(1n);
  437. expect(await this.mock.$log256(256n, rounding)).to.equal(1n);
  438. expect(await this.mock.$log256(257n, rounding)).to.equal(2n);
  439. expect(await this.mock.$log256(65535n, rounding)).to.equal(2n);
  440. expect(await this.mock.$log256(65536n, rounding)).to.equal(2n);
  441. expect(await this.mock.$log256(65537n, rounding)).to.equal(3n);
  442. expect(await this.mock.$log256(ethers.MaxUint256, rounding)).to.equal(32n);
  443. }
  444. });
  445. });
  446. });
  447. });