Math.test.js 22 KB

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