Math.test.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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, modExp } = require('../../helpers/math');
  7. const { generators } = require('../../helpers/random');
  8. const { product, range } = require('../../helpers/iterate');
  9. const RoundingDown = [Rounding.Floor, Rounding.Trunc];
  10. const RoundingUp = [Rounding.Ceil, Rounding.Expand];
  11. const bytes = (value, width = undefined) => ethers.Typed.bytes(ethers.toBeHex(value, width));
  12. const uint256 = value => ethers.Typed.uint256(value);
  13. bytes.zero = '0x';
  14. uint256.zero = 0n;
  15. const testCommutative = (fn, lhs, rhs, expected, ...extra) =>
  16. Promise.all([
  17. expect(fn(lhs, rhs, ...extra)).to.eventually.deep.equal(expected),
  18. expect(fn(rhs, lhs, ...extra)).to.eventually.deep.equal(expected),
  19. ]);
  20. const splitHighLow = n => [n / (1n << 256n), n % (1n << 256n)];
  21. async function fixture() {
  22. const mock = await ethers.deployContract('$Math');
  23. // disambiguation, we use the version with explicit rounding
  24. mock.$mulDiv = mock['$mulDiv(uint256,uint256,uint256,uint8)'];
  25. mock.$sqrt = mock['$sqrt(uint256,uint8)'];
  26. mock.$log2 = mock['$log2(uint256,uint8)'];
  27. mock.$log10 = mock['$log10(uint256,uint8)'];
  28. mock.$log256 = mock['$log256(uint256,uint8)'];
  29. return { mock };
  30. }
  31. describe('Math', function () {
  32. beforeEach(async function () {
  33. Object.assign(this, await loadFixture(fixture));
  34. });
  35. describe('add512', function () {
  36. it('adds correctly without reverting', async function () {
  37. const values = [0n, 1n, 17n, 42n, ethers.MaxUint256 - 1n, ethers.MaxUint256];
  38. for (const [a, b] of product(values, values)) {
  39. await expect(this.mock.$add512(a, b)).to.eventually.deep.equal(splitHighLow(a + b));
  40. }
  41. });
  42. });
  43. describe('mul512', function () {
  44. it('multiplies correctly without reverting', async function () {
  45. const values = [0n, 1n, 17n, 42n, ethers.MaxUint256 - 1n, ethers.MaxUint256];
  46. for (const [a, b] of product(values, values)) {
  47. await expect(this.mock.$mul512(a, b)).to.eventually.deep.equal(splitHighLow(a * b));
  48. }
  49. });
  50. });
  51. describe('tryAdd', function () {
  52. it('adds correctly', async function () {
  53. const a = 5678n;
  54. const b = 1234n;
  55. await testCommutative(this.mock.$tryAdd, a, b, [true, a + b]);
  56. });
  57. it('reverts on addition overflow', async function () {
  58. const a = ethers.MaxUint256;
  59. const b = 1n;
  60. await testCommutative(this.mock.$tryAdd, a, b, [false, 0n]);
  61. });
  62. });
  63. describe('trySub', function () {
  64. it('subtracts correctly', async function () {
  65. const a = 5678n;
  66. const b = 1234n;
  67. await expect(this.mock.$trySub(a, b)).to.eventually.deep.equal([true, a - b]);
  68. });
  69. it('reverts if subtraction result would be negative', async function () {
  70. const a = 1234n;
  71. const b = 5678n;
  72. await expect(this.mock.$trySub(a, b)).to.eventually.deep.equal([false, 0n]);
  73. });
  74. });
  75. describe('tryMul', function () {
  76. it('multiplies correctly', async function () {
  77. const a = 1234n;
  78. const b = 5678n;
  79. await testCommutative(this.mock.$tryMul, a, b, [true, a * b]);
  80. });
  81. it('multiplies by zero correctly', async function () {
  82. const a = 0n;
  83. const b = 5678n;
  84. await testCommutative(this.mock.$tryMul, a, b, [true, a * b]);
  85. });
  86. it('reverts on multiplication overflow', async function () {
  87. const a = ethers.MaxUint256;
  88. const b = 2n;
  89. await testCommutative(this.mock.$tryMul, a, b, [false, 0n]);
  90. });
  91. });
  92. describe('tryDiv', function () {
  93. it('divides correctly', async function () {
  94. const a = 5678n;
  95. const b = 5678n;
  96. await expect(this.mock.$tryDiv(a, b)).to.eventually.deep.equal([true, a / b]);
  97. });
  98. it('divides zero correctly', async function () {
  99. const a = 0n;
  100. const b = 5678n;
  101. await expect(this.mock.$tryDiv(a, b)).to.eventually.deep.equal([true, a / b]);
  102. });
  103. it('returns complete number result on non-even division', async function () {
  104. const a = 7000n;
  105. const b = 5678n;
  106. await expect(this.mock.$tryDiv(a, b)).to.eventually.deep.equal([true, a / b]);
  107. });
  108. it('reverts on division by zero', async function () {
  109. const a = 5678n;
  110. const b = 0n;
  111. await expect(this.mock.$tryDiv(a, b)).to.eventually.deep.equal([false, 0n]);
  112. });
  113. });
  114. describe('tryMod', function () {
  115. describe('modulos correctly', function () {
  116. it('when the dividend is smaller than the divisor', async function () {
  117. const a = 284n;
  118. const b = 5678n;
  119. await expect(this.mock.$tryMod(a, b)).to.eventually.deep.equal([true, a % b]);
  120. });
  121. it('when the dividend is equal to the divisor', async function () {
  122. const a = 5678n;
  123. const b = 5678n;
  124. await expect(this.mock.$tryMod(a, b)).to.eventually.deep.equal([true, a % b]);
  125. });
  126. it('when the dividend is larger than the divisor', async function () {
  127. const a = 7000n;
  128. const b = 5678n;
  129. await expect(this.mock.$tryMod(a, b)).to.eventually.deep.equal([true, a % b]);
  130. });
  131. it('when the dividend is a multiple of the divisor', async function () {
  132. const a = 17034n; // 17034 == 5678 * 3
  133. const b = 5678n;
  134. await expect(this.mock.$tryMod(a, b)).to.eventually.deep.equal([true, a % b]);
  135. });
  136. });
  137. it('reverts with a 0 divisor', async function () {
  138. const a = 5678n;
  139. const b = 0n;
  140. await expect(this.mock.$tryMod(a, b)).to.eventually.deep.equal([false, 0n]);
  141. });
  142. });
  143. describe('max', function () {
  144. it('is correctly detected in both position', async function () {
  145. await testCommutative(this.mock.$max, 1234n, 5678n, max(1234n, 5678n));
  146. });
  147. });
  148. describe('min', function () {
  149. it('is correctly detected in both position', async function () {
  150. await testCommutative(this.mock.$min, 1234n, 5678n, min(1234n, 5678n));
  151. });
  152. });
  153. describe('average', function () {
  154. it('is correctly calculated with two odd numbers', async function () {
  155. const a = 57417n;
  156. const b = 95431n;
  157. await expect(this.mock.$average(a, b)).to.eventually.equal((a + b) / 2n);
  158. });
  159. it('is correctly calculated with two even numbers', async function () {
  160. const a = 42304n;
  161. const b = 84346n;
  162. await expect(this.mock.$average(a, b)).to.eventually.equal((a + b) / 2n);
  163. });
  164. it('is correctly calculated with one even and one odd number', async function () {
  165. const a = 57417n;
  166. const b = 84346n;
  167. await expect(this.mock.$average(a, b)).to.eventually.equal((a + b) / 2n);
  168. });
  169. it('is correctly calculated with two max uint256 numbers', async function () {
  170. const a = ethers.MaxUint256;
  171. await expect(this.mock.$average(a, a)).to.eventually.equal(a);
  172. });
  173. });
  174. describe('ceilDiv', function () {
  175. it('reverts on zero division', async function () {
  176. const a = 2n;
  177. const b = 0n;
  178. // It's unspecified because it's a low level 0 division error
  179. await expect(this.mock.$ceilDiv(a, b)).to.be.revertedWithPanic(PANIC_CODES.DIVISION_BY_ZERO);
  180. });
  181. it('does not round up a zero result', async function () {
  182. const a = 0n;
  183. const b = 2n;
  184. const r = 0n;
  185. await expect(this.mock.$ceilDiv(a, b)).to.eventually.equal(r);
  186. });
  187. it('does not round up on exact division', async function () {
  188. const a = 10n;
  189. const b = 5n;
  190. const r = 2n;
  191. await expect(this.mock.$ceilDiv(a, b)).to.eventually.equal(r);
  192. });
  193. it('rounds up on division with remainders', async function () {
  194. const a = 42n;
  195. const b = 13n;
  196. const r = 4n;
  197. await expect(this.mock.$ceilDiv(a, b)).to.eventually.equal(r);
  198. });
  199. it('does not overflow', async function () {
  200. const a = ethers.MaxUint256;
  201. const b = 2n;
  202. const r = 1n << 255n;
  203. await expect(this.mock.$ceilDiv(a, b)).to.eventually.equal(r);
  204. });
  205. it('correctly computes max uint256 divided by 1', async function () {
  206. const a = ethers.MaxUint256;
  207. const b = 1n;
  208. const r = ethers.MaxUint256;
  209. await expect(this.mock.$ceilDiv(a, b)).to.eventually.equal(r);
  210. });
  211. });
  212. describe('mulDiv', function () {
  213. it('divide by 0', async function () {
  214. const a = 1n;
  215. const b = 1n;
  216. const c = 0n;
  217. await expect(this.mock.$mulDiv(a, b, c, Rounding.Floor)).to.be.revertedWithPanic(PANIC_CODES.DIVISION_BY_ZERO);
  218. });
  219. it('reverts with result higher than 2 ^ 256', async function () {
  220. const a = 5n;
  221. const b = ethers.MaxUint256;
  222. const c = 2n;
  223. await expect(this.mock.$mulDiv(a, b, c, Rounding.Floor)).to.be.revertedWithPanic(
  224. PANIC_CODES.ARITHMETIC_UNDER_OR_OVERFLOW,
  225. );
  226. });
  227. describe('does round down', function () {
  228. it('small values', async function () {
  229. for (const rounding of RoundingDown) {
  230. await expect(this.mock.$mulDiv(3n, 4n, 5n, rounding)).to.eventually.equal(2n);
  231. await expect(this.mock.$mulDiv(3n, 5n, 5n, rounding)).to.eventually.equal(3n);
  232. }
  233. });
  234. it('large values', async function () {
  235. for (const rounding of RoundingDown) {
  236. await expect(this.mock.$mulDiv(42n, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding)).to.eventually.equal(
  237. 41n,
  238. );
  239. await expect(this.mock.$mulDiv(17n, ethers.MaxUint256, ethers.MaxUint256, rounding)).to.eventually.equal(17n);
  240. await expect(
  241. this.mock.$mulDiv(ethers.MaxUint256 - 1n, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding),
  242. ).to.eventually.equal(ethers.MaxUint256 - 2n);
  243. await expect(
  244. this.mock.$mulDiv(ethers.MaxUint256, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding),
  245. ).to.eventually.equal(ethers.MaxUint256 - 1n);
  246. await expect(
  247. this.mock.$mulDiv(ethers.MaxUint256, ethers.MaxUint256, ethers.MaxUint256, rounding),
  248. ).to.eventually.equal(ethers.MaxUint256);
  249. }
  250. });
  251. });
  252. describe('does round up', function () {
  253. it('small values', async function () {
  254. for (const rounding of RoundingUp) {
  255. await expect(this.mock.$mulDiv(3n, 4n, 5n, rounding)).to.eventually.equal(3n);
  256. await expect(this.mock.$mulDiv(3n, 5n, 5n, rounding)).to.eventually.equal(3n);
  257. }
  258. });
  259. it('large values', async function () {
  260. for (const rounding of RoundingUp) {
  261. await expect(this.mock.$mulDiv(42n, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding)).to.eventually.equal(
  262. 42n,
  263. );
  264. await expect(this.mock.$mulDiv(17n, ethers.MaxUint256, ethers.MaxUint256, rounding)).to.eventually.equal(17n);
  265. await expect(
  266. this.mock.$mulDiv(ethers.MaxUint256 - 1n, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding),
  267. ).to.eventually.equal(ethers.MaxUint256 - 1n);
  268. await expect(
  269. this.mock.$mulDiv(ethers.MaxUint256, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding),
  270. ).to.eventually.equal(ethers.MaxUint256 - 1n);
  271. await expect(
  272. this.mock.$mulDiv(ethers.MaxUint256, ethers.MaxUint256, ethers.MaxUint256, rounding),
  273. ).to.eventually.equal(ethers.MaxUint256);
  274. }
  275. });
  276. });
  277. });
  278. describe('mulShr', function () {
  279. it('reverts with result higher than 2 ^ 256', async function () {
  280. const a = 5n;
  281. const b = ethers.MaxUint256;
  282. const c = 1n;
  283. await expect(this.mock.$mulShr(a, b, c, Rounding.Floor)).to.be.revertedWithPanic(
  284. PANIC_CODES.ARITHMETIC_UNDER_OR_OVERFLOW,
  285. );
  286. });
  287. describe('does round down', function () {
  288. it('small values', async function () {
  289. for (const rounding of RoundingDown) {
  290. await expect(this.mock.$mulShr(3n, 5n, 1n, rounding)).to.eventually.equal(7n);
  291. await expect(this.mock.$mulShr(3n, 5n, 2n, rounding)).to.eventually.equal(3n);
  292. }
  293. });
  294. it('large values', async function () {
  295. for (const rounding of RoundingDown) {
  296. await expect(this.mock.$mulShr(42n, ethers.MaxUint256, 255n, rounding)).to.eventually.equal(83n);
  297. await expect(this.mock.$mulShr(17n, ethers.MaxUint256, 255n, rounding)).to.eventually.equal(33n);
  298. await expect(this.mock.$mulShr(ethers.MaxUint256, ethers.MaxInt256 + 1n, 255n, rounding)).to.eventually.equal(
  299. ethers.MaxUint256,
  300. );
  301. await expect(this.mock.$mulShr(ethers.MaxUint256, ethers.MaxInt256, 255n, rounding)).to.eventually.equal(
  302. ethers.MaxUint256 - 2n,
  303. );
  304. }
  305. });
  306. });
  307. describe('does round up', function () {
  308. it('small values', async function () {
  309. for (const rounding of RoundingUp) {
  310. await expect(this.mock.$mulShr(3n, 5n, 1n, rounding)).to.eventually.equal(8n);
  311. await expect(this.mock.$mulShr(3n, 5n, 2n, rounding)).to.eventually.equal(4n);
  312. }
  313. });
  314. it('large values', async function () {
  315. for (const rounding of RoundingUp) {
  316. await expect(this.mock.$mulShr(42n, ethers.MaxUint256, 255n, rounding)).to.eventually.equal(84n);
  317. await expect(this.mock.$mulShr(17n, ethers.MaxUint256, 255n, rounding)).to.eventually.equal(34n);
  318. await expect(this.mock.$mulShr(ethers.MaxUint256, ethers.MaxInt256 + 1n, 255n, rounding)).to.eventually.equal(
  319. ethers.MaxUint256,
  320. );
  321. await expect(this.mock.$mulShr(ethers.MaxUint256, ethers.MaxInt256, 255n, rounding)).to.eventually.equal(
  322. ethers.MaxUint256 - 1n,
  323. );
  324. }
  325. });
  326. });
  327. });
  328. describe('invMod', function () {
  329. for (const factors of [
  330. [0n],
  331. [1n],
  332. [2n],
  333. [17n],
  334. [65537n],
  335. [0xffffffff00000001000000000000000000000000ffffffffffffffffffffffffn],
  336. [3n, 5n],
  337. [3n, 7n],
  338. [47n, 53n],
  339. ]) {
  340. const p = factors.reduce((acc, f) => acc * f, 1n);
  341. describe(`using p=${p} which is ${p > 1 && factors.length > 1 ? 'not ' : ''}a prime`, function () {
  342. it('trying to inverse 0 returns 0', async function () {
  343. await expect(this.mock.$invMod(0, p)).to.eventually.equal(0n);
  344. await expect(this.mock.$invMod(p, p)).to.eventually.equal(0n); // p is 0 mod p
  345. });
  346. if (p != 0) {
  347. for (const value of Array.from({ length: 16 }, generators.uint256)) {
  348. const isInversible = factors.every(f => value % f);
  349. it(`trying to inverse ${value}`, async function () {
  350. const result = await this.mock.$invMod(value, p);
  351. if (isInversible) {
  352. expect((value * result) % p).to.equal(1n);
  353. } else {
  354. expect(result).to.equal(0n);
  355. }
  356. });
  357. }
  358. }
  359. });
  360. }
  361. });
  362. describe('modExp', function () {
  363. for (const [name, type] of Object.entries({ uint256, bytes })) {
  364. describe(`with ${name} inputs`, function () {
  365. it('is correctly calculating modulus', async function () {
  366. const b = 3n;
  367. const e = 200n;
  368. const m = 50n;
  369. await expect(this.mock.$modExp(type(b), type(e), type(m))).to.eventually.equal(type(b ** e % m).value);
  370. });
  371. it('is correctly reverting when modulus is zero', async function () {
  372. const b = 3n;
  373. const e = 200n;
  374. const m = 0n;
  375. await expect(this.mock.$modExp(type(b), type(e), type(m))).to.be.revertedWithPanic(
  376. PANIC_CODES.DIVISION_BY_ZERO,
  377. );
  378. });
  379. });
  380. }
  381. describe('with large bytes inputs', function () {
  382. for (const [[b, log2b], [e, log2e], [m, log2m]] of product(
  383. range(320, 512, 64).map(e => [2n ** BigInt(e) + 1n, e]),
  384. range(320, 512, 64).map(e => [2n ** BigInt(e) + 1n, e]),
  385. range(320, 512, 64).map(e => [2n ** BigInt(e) + 1n, e]),
  386. )) {
  387. it(`calculates b ** e % m (b=2**${log2b}+1) (e=2**${log2e}+1) (m=2**${log2m}+1)`, async function () {
  388. const mLength = ethers.dataLength(ethers.toBeHex(m));
  389. await expect(this.mock.$modExp(bytes(b), bytes(e), bytes(m))).to.eventually.equal(
  390. bytes(modExp(b, e, m), mLength).value,
  391. );
  392. });
  393. }
  394. });
  395. });
  396. describe('tryModExp', function () {
  397. for (const [name, type] of Object.entries({ uint256, bytes })) {
  398. describe(`with ${name} inputs`, function () {
  399. it('is correctly calculating modulus', async function () {
  400. const b = 3n;
  401. const e = 200n;
  402. const m = 50n;
  403. await expect(this.mock.$tryModExp(type(b), type(e), type(m))).to.eventually.deep.equal([
  404. true,
  405. type(b ** e % m).value,
  406. ]);
  407. });
  408. it('is correctly reverting when modulus is zero', async function () {
  409. const b = 3n;
  410. const e = 200n;
  411. const m = 0n;
  412. await expect(this.mock.$tryModExp(type(b), type(e), type(m))).to.eventually.deep.equal([false, type.zero]);
  413. });
  414. });
  415. }
  416. describe('with large bytes inputs', function () {
  417. for (const [[b, log2b], [e, log2e], [m, log2m]] of product(
  418. range(320, 513, 64).map(e => [2n ** BigInt(e) + 1n, e]),
  419. range(320, 513, 64).map(e => [2n ** BigInt(e) + 1n, e]),
  420. range(320, 513, 64).map(e => [2n ** BigInt(e) + 1n, e]),
  421. )) {
  422. it(`calculates b ** e % m (b=2**${log2b}+1) (e=2**${log2e}+1) (m=2**${log2m}+1)`, async function () {
  423. const mLength = ethers.dataLength(ethers.toBeHex(m));
  424. await expect(this.mock.$tryModExp(bytes(b), bytes(e), bytes(m))).to.eventually.deep.equal([
  425. true,
  426. bytes(modExp(b, e, m), mLength).value,
  427. ]);
  428. });
  429. }
  430. });
  431. });
  432. describe('sqrt', function () {
  433. it('rounds down', async function () {
  434. for (const rounding of RoundingDown) {
  435. await expect(this.mock.$sqrt(0n, rounding)).to.eventually.equal(0n);
  436. await expect(this.mock.$sqrt(1n, rounding)).to.eventually.equal(1n);
  437. await expect(this.mock.$sqrt(2n, rounding)).to.eventually.equal(1n);
  438. await expect(this.mock.$sqrt(3n, rounding)).to.eventually.equal(1n);
  439. await expect(this.mock.$sqrt(4n, rounding)).to.eventually.equal(2n);
  440. await expect(this.mock.$sqrt(144n, rounding)).to.eventually.equal(12n);
  441. await expect(this.mock.$sqrt(999999n, rounding)).to.eventually.equal(999n);
  442. await expect(this.mock.$sqrt(1000000n, rounding)).to.eventually.equal(1000n);
  443. await expect(this.mock.$sqrt(1000001n, rounding)).to.eventually.equal(1000n);
  444. await expect(this.mock.$sqrt(1002000n, rounding)).to.eventually.equal(1000n);
  445. await expect(this.mock.$sqrt(1002001n, rounding)).to.eventually.equal(1001n);
  446. await expect(this.mock.$sqrt(ethers.MaxUint256, rounding)).to.eventually.equal(
  447. 340282366920938463463374607431768211455n,
  448. );
  449. }
  450. });
  451. it('rounds up', async function () {
  452. for (const rounding of RoundingUp) {
  453. await expect(this.mock.$sqrt(0n, rounding)).to.eventually.equal(0n);
  454. await expect(this.mock.$sqrt(1n, rounding)).to.eventually.equal(1n);
  455. await expect(this.mock.$sqrt(2n, rounding)).to.eventually.equal(2n);
  456. await expect(this.mock.$sqrt(3n, rounding)).to.eventually.equal(2n);
  457. await expect(this.mock.$sqrt(4n, rounding)).to.eventually.equal(2n);
  458. await expect(this.mock.$sqrt(144n, rounding)).to.eventually.equal(12n);
  459. await expect(this.mock.$sqrt(999999n, rounding)).to.eventually.equal(1000n);
  460. await expect(this.mock.$sqrt(1000000n, rounding)).to.eventually.equal(1000n);
  461. await expect(this.mock.$sqrt(1000001n, rounding)).to.eventually.equal(1001n);
  462. await expect(this.mock.$sqrt(1002000n, rounding)).to.eventually.equal(1001n);
  463. await expect(this.mock.$sqrt(1002001n, rounding)).to.eventually.equal(1001n);
  464. await expect(this.mock.$sqrt(ethers.MaxUint256, rounding)).to.eventually.equal(
  465. 340282366920938463463374607431768211456n,
  466. );
  467. }
  468. });
  469. });
  470. describe('log', function () {
  471. describe('log2', function () {
  472. it('rounds down', async function () {
  473. for (const rounding of RoundingDown) {
  474. await expect(this.mock.$log2(0n, rounding)).to.eventually.equal(0n);
  475. await expect(this.mock.$log2(1n, rounding)).to.eventually.equal(0n);
  476. await expect(this.mock.$log2(2n, rounding)).to.eventually.equal(1n);
  477. await expect(this.mock.$log2(3n, rounding)).to.eventually.equal(1n);
  478. await expect(this.mock.$log2(4n, rounding)).to.eventually.equal(2n);
  479. await expect(this.mock.$log2(5n, rounding)).to.eventually.equal(2n);
  480. await expect(this.mock.$log2(6n, rounding)).to.eventually.equal(2n);
  481. await expect(this.mock.$log2(7n, rounding)).to.eventually.equal(2n);
  482. await expect(this.mock.$log2(8n, rounding)).to.eventually.equal(3n);
  483. await expect(this.mock.$log2(9n, rounding)).to.eventually.equal(3n);
  484. await expect(this.mock.$log2(ethers.MaxUint256, rounding)).to.eventually.equal(255n);
  485. }
  486. });
  487. it('rounds up', async function () {
  488. for (const rounding of RoundingUp) {
  489. await expect(this.mock.$log2(0n, rounding)).to.eventually.equal(0n);
  490. await expect(this.mock.$log2(1n, rounding)).to.eventually.equal(0n);
  491. await expect(this.mock.$log2(2n, rounding)).to.eventually.equal(1n);
  492. await expect(this.mock.$log2(3n, rounding)).to.eventually.equal(2n);
  493. await expect(this.mock.$log2(4n, rounding)).to.eventually.equal(2n);
  494. await expect(this.mock.$log2(5n, rounding)).to.eventually.equal(3n);
  495. await expect(this.mock.$log2(6n, rounding)).to.eventually.equal(3n);
  496. await expect(this.mock.$log2(7n, rounding)).to.eventually.equal(3n);
  497. await expect(this.mock.$log2(8n, rounding)).to.eventually.equal(3n);
  498. await expect(this.mock.$log2(9n, rounding)).to.eventually.equal(4n);
  499. await expect(this.mock.$log2(ethers.MaxUint256, rounding)).to.eventually.equal(256n);
  500. }
  501. });
  502. });
  503. describe('log10', function () {
  504. it('rounds down', async function () {
  505. for (const rounding of RoundingDown) {
  506. await expect(this.mock.$log10(0n, rounding)).to.eventually.equal(0n);
  507. await expect(this.mock.$log10(1n, rounding)).to.eventually.equal(0n);
  508. await expect(this.mock.$log10(2n, rounding)).to.eventually.equal(0n);
  509. await expect(this.mock.$log10(9n, rounding)).to.eventually.equal(0n);
  510. await expect(this.mock.$log10(10n, rounding)).to.eventually.equal(1n);
  511. await expect(this.mock.$log10(11n, rounding)).to.eventually.equal(1n);
  512. await expect(this.mock.$log10(99n, rounding)).to.eventually.equal(1n);
  513. await expect(this.mock.$log10(100n, rounding)).to.eventually.equal(2n);
  514. await expect(this.mock.$log10(101n, rounding)).to.eventually.equal(2n);
  515. await expect(this.mock.$log10(999n, rounding)).to.eventually.equal(2n);
  516. await expect(this.mock.$log10(1000n, rounding)).to.eventually.equal(3n);
  517. await expect(this.mock.$log10(1001n, rounding)).to.eventually.equal(3n);
  518. await expect(this.mock.$log10(ethers.MaxUint256, rounding)).to.eventually.equal(77n);
  519. }
  520. });
  521. it('rounds up', async function () {
  522. for (const rounding of RoundingUp) {
  523. await expect(this.mock.$log10(0n, rounding)).to.eventually.equal(0n);
  524. await expect(this.mock.$log10(1n, rounding)).to.eventually.equal(0n);
  525. await expect(this.mock.$log10(2n, rounding)).to.eventually.equal(1n);
  526. await expect(this.mock.$log10(9n, rounding)).to.eventually.equal(1n);
  527. await expect(this.mock.$log10(10n, rounding)).to.eventually.equal(1n);
  528. await expect(this.mock.$log10(11n, rounding)).to.eventually.equal(2n);
  529. await expect(this.mock.$log10(99n, rounding)).to.eventually.equal(2n);
  530. await expect(this.mock.$log10(100n, rounding)).to.eventually.equal(2n);
  531. await expect(this.mock.$log10(101n, rounding)).to.eventually.equal(3n);
  532. await expect(this.mock.$log10(999n, rounding)).to.eventually.equal(3n);
  533. await expect(this.mock.$log10(1000n, rounding)).to.eventually.equal(3n);
  534. await expect(this.mock.$log10(1001n, rounding)).to.eventually.equal(4n);
  535. await expect(this.mock.$log10(ethers.MaxUint256, rounding)).to.eventually.equal(78n);
  536. }
  537. });
  538. });
  539. describe('log256', function () {
  540. it('rounds down', async function () {
  541. for (const rounding of RoundingDown) {
  542. await expect(this.mock.$log256(0n, rounding)).to.eventually.equal(0n);
  543. await expect(this.mock.$log256(1n, rounding)).to.eventually.equal(0n);
  544. await expect(this.mock.$log256(2n, rounding)).to.eventually.equal(0n);
  545. await expect(this.mock.$log256(255n, rounding)).to.eventually.equal(0n);
  546. await expect(this.mock.$log256(256n, rounding)).to.eventually.equal(1n);
  547. await expect(this.mock.$log256(257n, rounding)).to.eventually.equal(1n);
  548. await expect(this.mock.$log256(65535n, rounding)).to.eventually.equal(1n);
  549. await expect(this.mock.$log256(65536n, rounding)).to.eventually.equal(2n);
  550. await expect(this.mock.$log256(65537n, rounding)).to.eventually.equal(2n);
  551. await expect(this.mock.$log256(ethers.MaxUint256, rounding)).to.eventually.equal(31n);
  552. }
  553. });
  554. it('rounds up', async function () {
  555. for (const rounding of RoundingUp) {
  556. await expect(this.mock.$log256(0n, rounding)).to.eventually.equal(0n);
  557. await expect(this.mock.$log256(1n, rounding)).to.eventually.equal(0n);
  558. await expect(this.mock.$log256(2n, rounding)).to.eventually.equal(1n);
  559. await expect(this.mock.$log256(255n, rounding)).to.eventually.equal(1n);
  560. await expect(this.mock.$log256(256n, rounding)).to.eventually.equal(1n);
  561. await expect(this.mock.$log256(257n, rounding)).to.eventually.equal(2n);
  562. await expect(this.mock.$log256(65535n, rounding)).to.eventually.equal(2n);
  563. await expect(this.mock.$log256(65536n, rounding)).to.eventually.equal(2n);
  564. await expect(this.mock.$log256(65537n, rounding)).to.eventually.equal(3n);
  565. await expect(this.mock.$log256(ethers.MaxUint256, rounding)).to.eventually.equal(32n);
  566. }
  567. });
  568. });
  569. });
  570. });