Math.test.js 22 KB

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