Math.test.js 17 KB

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