Math.test.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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('saturatingAdd', function () {
  144. it('adds correctly', async function () {
  145. const a = 5678n;
  146. const b = 1234n;
  147. await testCommutative(this.mock.$saturatingAdd, a, b, a + b);
  148. await testCommutative(this.mock.$saturatingAdd, a, 0n, a);
  149. await testCommutative(this.mock.$saturatingAdd, ethers.MaxUint256, 0n, ethers.MaxUint256);
  150. });
  151. it('bounds on addition overflow', async function () {
  152. await testCommutative(this.mock.$saturatingAdd, ethers.MaxUint256, 1n, ethers.MaxUint256);
  153. await expect(this.mock.$saturatingAdd(ethers.MaxUint256, ethers.MaxUint256)).to.eventually.equal(
  154. ethers.MaxUint256,
  155. );
  156. });
  157. });
  158. describe('saturatingSub', function () {
  159. it('subtracts correctly', async function () {
  160. const a = 5678n;
  161. const b = 1234n;
  162. await expect(this.mock.$saturatingSub(a, b)).to.eventually.equal(a - b);
  163. await expect(this.mock.$saturatingSub(a, a)).to.eventually.equal(0n);
  164. await expect(this.mock.$saturatingSub(a, 0n)).to.eventually.equal(a);
  165. await expect(this.mock.$saturatingSub(0n, a)).to.eventually.equal(0n);
  166. await expect(this.mock.$saturatingSub(ethers.MaxUint256, 1n)).to.eventually.equal(ethers.MaxUint256 - 1n);
  167. });
  168. it('bounds on subtraction overflow', async function () {
  169. await expect(this.mock.$saturatingSub(0n, 1n)).to.eventually.equal(0n);
  170. await expect(this.mock.$saturatingSub(1n, 2n)).to.eventually.equal(0n);
  171. await expect(this.mock.$saturatingSub(1n, ethers.MaxUint256)).to.eventually.equal(0n);
  172. await expect(this.mock.$saturatingSub(ethers.MaxUint256 - 1n, ethers.MaxUint256)).to.eventually.equal(0n);
  173. });
  174. });
  175. describe('saturatingMul', function () {
  176. it('multiplies correctly', async function () {
  177. const a = 1234n;
  178. const b = 5678n;
  179. await testCommutative(this.mock.$saturatingMul, a, b, a * b);
  180. });
  181. it('multiplies by zero correctly', async function () {
  182. const a = 0n;
  183. const b = 5678n;
  184. await testCommutative(this.mock.$saturatingMul, a, b, 0n);
  185. });
  186. it('bounds on multiplication overflow', async function () {
  187. const a = ethers.MaxUint256;
  188. const b = 2n;
  189. await testCommutative(this.mock.$saturatingMul, a, b, ethers.MaxUint256);
  190. });
  191. });
  192. describe('max', function () {
  193. it('is correctly detected in both position', async function () {
  194. await testCommutative(this.mock.$max, 1234n, 5678n, max(1234n, 5678n));
  195. });
  196. });
  197. describe('min', function () {
  198. it('is correctly detected in both position', async function () {
  199. await testCommutative(this.mock.$min, 1234n, 5678n, min(1234n, 5678n));
  200. });
  201. });
  202. describe('average', function () {
  203. it('is correctly calculated with two odd numbers', async function () {
  204. const a = 57417n;
  205. const b = 95431n;
  206. await expect(this.mock.$average(a, b)).to.eventually.equal((a + b) / 2n);
  207. });
  208. it('is correctly calculated with two even numbers', async function () {
  209. const a = 42304n;
  210. const b = 84346n;
  211. await expect(this.mock.$average(a, b)).to.eventually.equal((a + b) / 2n);
  212. });
  213. it('is correctly calculated with one even and one odd number', async function () {
  214. const a = 57417n;
  215. const b = 84346n;
  216. await expect(this.mock.$average(a, b)).to.eventually.equal((a + b) / 2n);
  217. });
  218. it('is correctly calculated with two max uint256 numbers', async function () {
  219. const a = ethers.MaxUint256;
  220. await expect(this.mock.$average(a, a)).to.eventually.equal(a);
  221. });
  222. });
  223. describe('ceilDiv', function () {
  224. it('reverts on zero division', async function () {
  225. const a = 2n;
  226. const b = 0n;
  227. // It's unspecified because it's a low level 0 division error
  228. await expect(this.mock.$ceilDiv(a, b)).to.be.revertedWithPanic(PANIC_CODES.DIVISION_BY_ZERO);
  229. });
  230. it('does not round up a zero result', async function () {
  231. const a = 0n;
  232. const b = 2n;
  233. const r = 0n;
  234. await expect(this.mock.$ceilDiv(a, b)).to.eventually.equal(r);
  235. });
  236. it('does not round up on exact division', async function () {
  237. const a = 10n;
  238. const b = 5n;
  239. const r = 2n;
  240. await expect(this.mock.$ceilDiv(a, b)).to.eventually.equal(r);
  241. });
  242. it('rounds up on division with remainders', async function () {
  243. const a = 42n;
  244. const b = 13n;
  245. const r = 4n;
  246. await expect(this.mock.$ceilDiv(a, b)).to.eventually.equal(r);
  247. });
  248. it('does not overflow', async function () {
  249. const a = ethers.MaxUint256;
  250. const b = 2n;
  251. const r = 1n << 255n;
  252. await expect(this.mock.$ceilDiv(a, b)).to.eventually.equal(r);
  253. });
  254. it('correctly computes max uint256 divided by 1', async function () {
  255. const a = ethers.MaxUint256;
  256. const b = 1n;
  257. const r = ethers.MaxUint256;
  258. await expect(this.mock.$ceilDiv(a, b)).to.eventually.equal(r);
  259. });
  260. });
  261. describe('mulDiv', function () {
  262. it('divide by 0', async function () {
  263. const a = 1n;
  264. const b = 1n;
  265. const c = 0n;
  266. await expect(this.mock.$mulDiv(a, b, c, Rounding.Floor)).to.be.revertedWithPanic(PANIC_CODES.DIVISION_BY_ZERO);
  267. });
  268. it('reverts with result higher than 2 ^ 256', async function () {
  269. const a = 5n;
  270. const b = ethers.MaxUint256;
  271. const c = 2n;
  272. await expect(this.mock.$mulDiv(a, b, c, Rounding.Floor)).to.be.revertedWithPanic(
  273. PANIC_CODES.ARITHMETIC_UNDER_OR_OVERFLOW,
  274. );
  275. });
  276. describe('does round down', function () {
  277. it('small values', async function () {
  278. for (const rounding of RoundingDown) {
  279. await expect(this.mock.$mulDiv(3n, 4n, 5n, rounding)).to.eventually.equal(2n);
  280. await expect(this.mock.$mulDiv(3n, 5n, 5n, rounding)).to.eventually.equal(3n);
  281. }
  282. });
  283. it('large values', async function () {
  284. for (const rounding of RoundingDown) {
  285. await expect(this.mock.$mulDiv(42n, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding)).to.eventually.equal(
  286. 41n,
  287. );
  288. await expect(this.mock.$mulDiv(17n, ethers.MaxUint256, ethers.MaxUint256, rounding)).to.eventually.equal(17n);
  289. await expect(
  290. this.mock.$mulDiv(ethers.MaxUint256 - 1n, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding),
  291. ).to.eventually.equal(ethers.MaxUint256 - 2n);
  292. await expect(
  293. this.mock.$mulDiv(ethers.MaxUint256, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding),
  294. ).to.eventually.equal(ethers.MaxUint256 - 1n);
  295. await expect(
  296. this.mock.$mulDiv(ethers.MaxUint256, ethers.MaxUint256, ethers.MaxUint256, rounding),
  297. ).to.eventually.equal(ethers.MaxUint256);
  298. }
  299. });
  300. });
  301. describe('does round up', function () {
  302. it('small values', async function () {
  303. for (const rounding of RoundingUp) {
  304. await expect(this.mock.$mulDiv(3n, 4n, 5n, rounding)).to.eventually.equal(3n);
  305. await expect(this.mock.$mulDiv(3n, 5n, 5n, rounding)).to.eventually.equal(3n);
  306. }
  307. });
  308. it('large values', async function () {
  309. for (const rounding of RoundingUp) {
  310. await expect(this.mock.$mulDiv(42n, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding)).to.eventually.equal(
  311. 42n,
  312. );
  313. await expect(this.mock.$mulDiv(17n, ethers.MaxUint256, ethers.MaxUint256, rounding)).to.eventually.equal(17n);
  314. await expect(
  315. this.mock.$mulDiv(ethers.MaxUint256 - 1n, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding),
  316. ).to.eventually.equal(ethers.MaxUint256 - 1n);
  317. await expect(
  318. this.mock.$mulDiv(ethers.MaxUint256, ethers.MaxUint256 - 1n, ethers.MaxUint256, rounding),
  319. ).to.eventually.equal(ethers.MaxUint256 - 1n);
  320. await expect(
  321. this.mock.$mulDiv(ethers.MaxUint256, ethers.MaxUint256, ethers.MaxUint256, rounding),
  322. ).to.eventually.equal(ethers.MaxUint256);
  323. }
  324. });
  325. });
  326. });
  327. describe('mulShr', function () {
  328. it('reverts with result higher than 2 ^ 256', async function () {
  329. const a = 5n;
  330. const b = ethers.MaxUint256;
  331. const c = 1n;
  332. await expect(this.mock.$mulShr(a, b, c, Rounding.Floor)).to.be.revertedWithPanic(
  333. PANIC_CODES.ARITHMETIC_UNDER_OR_OVERFLOW,
  334. );
  335. });
  336. describe('does round down', function () {
  337. it('small values', async function () {
  338. for (const rounding of RoundingDown) {
  339. await expect(this.mock.$mulShr(3n, 5n, 1n, rounding)).to.eventually.equal(7n);
  340. await expect(this.mock.$mulShr(3n, 5n, 2n, rounding)).to.eventually.equal(3n);
  341. }
  342. });
  343. it('large values', async function () {
  344. for (const rounding of RoundingDown) {
  345. await expect(this.mock.$mulShr(42n, ethers.MaxUint256, 255n, rounding)).to.eventually.equal(83n);
  346. await expect(this.mock.$mulShr(17n, ethers.MaxUint256, 255n, rounding)).to.eventually.equal(33n);
  347. await expect(this.mock.$mulShr(ethers.MaxUint256, ethers.MaxInt256 + 1n, 255n, rounding)).to.eventually.equal(
  348. ethers.MaxUint256,
  349. );
  350. await expect(this.mock.$mulShr(ethers.MaxUint256, ethers.MaxInt256, 255n, rounding)).to.eventually.equal(
  351. ethers.MaxUint256 - 2n,
  352. );
  353. }
  354. });
  355. });
  356. describe('does round up', function () {
  357. it('small values', async function () {
  358. for (const rounding of RoundingUp) {
  359. await expect(this.mock.$mulShr(3n, 5n, 1n, rounding)).to.eventually.equal(8n);
  360. await expect(this.mock.$mulShr(3n, 5n, 2n, rounding)).to.eventually.equal(4n);
  361. }
  362. });
  363. it('large values', async function () {
  364. for (const rounding of RoundingUp) {
  365. await expect(this.mock.$mulShr(42n, ethers.MaxUint256, 255n, rounding)).to.eventually.equal(84n);
  366. await expect(this.mock.$mulShr(17n, ethers.MaxUint256, 255n, rounding)).to.eventually.equal(34n);
  367. await expect(this.mock.$mulShr(ethers.MaxUint256, ethers.MaxInt256 + 1n, 255n, rounding)).to.eventually.equal(
  368. ethers.MaxUint256,
  369. );
  370. await expect(this.mock.$mulShr(ethers.MaxUint256, ethers.MaxInt256, 255n, rounding)).to.eventually.equal(
  371. ethers.MaxUint256 - 1n,
  372. );
  373. }
  374. });
  375. });
  376. });
  377. describe('invMod', function () {
  378. for (const factors of [
  379. [0n],
  380. [1n],
  381. [2n],
  382. [17n],
  383. [65537n],
  384. [0xffffffff00000001000000000000000000000000ffffffffffffffffffffffffn],
  385. [3n, 5n],
  386. [3n, 7n],
  387. [47n, 53n],
  388. ]) {
  389. const p = factors.reduce((acc, f) => acc * f, 1n);
  390. describe(`using p=${p} which is ${p > 1 && factors.length > 1 ? 'not ' : ''}a prime`, function () {
  391. it('trying to inverse 0 returns 0', async function () {
  392. await expect(this.mock.$invMod(0, p)).to.eventually.equal(0n);
  393. await expect(this.mock.$invMod(p, p)).to.eventually.equal(0n); // p is 0 mod p
  394. });
  395. if (p != 0) {
  396. for (const value of Array.from({ length: 16 }, generators.uint256)) {
  397. const isInversible = factors.every(f => value % f);
  398. it(`trying to inverse ${value}`, async function () {
  399. const result = await this.mock.$invMod(value, p);
  400. if (isInversible) {
  401. expect((value * result) % p).to.equal(1n);
  402. } else {
  403. expect(result).to.equal(0n);
  404. }
  405. });
  406. }
  407. }
  408. });
  409. }
  410. });
  411. describe('modExp', function () {
  412. for (const [name, type] of Object.entries({ uint256, bytes })) {
  413. describe(`with ${name} inputs`, function () {
  414. it('is correctly calculating modulus', async function () {
  415. const b = 3n;
  416. const e = 200n;
  417. const m = 50n;
  418. await expect(this.mock.$modExp(type(b), type(e), type(m))).to.eventually.equal(type(b ** e % m).value);
  419. });
  420. it('is correctly reverting when modulus is zero', async function () {
  421. const b = 3n;
  422. const e = 200n;
  423. const m = 0n;
  424. await expect(this.mock.$modExp(type(b), type(e), type(m))).to.be.revertedWithPanic(
  425. PANIC_CODES.DIVISION_BY_ZERO,
  426. );
  427. });
  428. });
  429. }
  430. describe('with large bytes inputs', function () {
  431. for (const [[b, log2b], [e, log2e], [m, log2m]] of product(
  432. range(320, 512, 64).map(e => [2n ** BigInt(e) + 1n, e]),
  433. range(320, 512, 64).map(e => [2n ** BigInt(e) + 1n, e]),
  434. range(320, 512, 64).map(e => [2n ** BigInt(e) + 1n, e]),
  435. )) {
  436. it(`calculates b ** e % m (b=2**${log2b}+1) (e=2**${log2e}+1) (m=2**${log2m}+1)`, async function () {
  437. const mLength = ethers.dataLength(ethers.toBeHex(m));
  438. await expect(this.mock.$modExp(bytes(b), bytes(e), bytes(m))).to.eventually.equal(
  439. bytes(modExp(b, e, m), mLength).value,
  440. );
  441. });
  442. }
  443. });
  444. });
  445. describe('tryModExp', function () {
  446. for (const [name, type] of Object.entries({ uint256, bytes })) {
  447. describe(`with ${name} inputs`, function () {
  448. it('is correctly calculating modulus', async function () {
  449. const b = 3n;
  450. const e = 200n;
  451. const m = 50n;
  452. await expect(this.mock.$tryModExp(type(b), type(e), type(m))).to.eventually.deep.equal([
  453. true,
  454. type(b ** e % m).value,
  455. ]);
  456. });
  457. it('is correctly reverting when modulus is zero', async function () {
  458. const b = 3n;
  459. const e = 200n;
  460. const m = 0n;
  461. await expect(this.mock.$tryModExp(type(b), type(e), type(m))).to.eventually.deep.equal([false, type.zero]);
  462. });
  463. });
  464. }
  465. describe('with large bytes inputs', function () {
  466. for (const [[b, log2b], [e, log2e], [m, log2m]] of product(
  467. range(320, 513, 64).map(e => [2n ** BigInt(e) + 1n, e]),
  468. range(320, 513, 64).map(e => [2n ** BigInt(e) + 1n, e]),
  469. range(320, 513, 64).map(e => [2n ** BigInt(e) + 1n, e]),
  470. )) {
  471. it(`calculates b ** e % m (b=2**${log2b}+1) (e=2**${log2e}+1) (m=2**${log2m}+1)`, async function () {
  472. const mLength = ethers.dataLength(ethers.toBeHex(m));
  473. await expect(this.mock.$tryModExp(bytes(b), bytes(e), bytes(m))).to.eventually.deep.equal([
  474. true,
  475. bytes(modExp(b, e, m), mLength).value,
  476. ]);
  477. });
  478. }
  479. });
  480. });
  481. describe('sqrt', function () {
  482. it('rounds down', async function () {
  483. for (const rounding of RoundingDown) {
  484. await expect(this.mock.$sqrt(0n, rounding)).to.eventually.equal(0n);
  485. await expect(this.mock.$sqrt(1n, rounding)).to.eventually.equal(1n);
  486. await expect(this.mock.$sqrt(2n, rounding)).to.eventually.equal(1n);
  487. await expect(this.mock.$sqrt(3n, rounding)).to.eventually.equal(1n);
  488. await expect(this.mock.$sqrt(4n, rounding)).to.eventually.equal(2n);
  489. await expect(this.mock.$sqrt(144n, rounding)).to.eventually.equal(12n);
  490. await expect(this.mock.$sqrt(999999n, rounding)).to.eventually.equal(999n);
  491. await expect(this.mock.$sqrt(1000000n, rounding)).to.eventually.equal(1000n);
  492. await expect(this.mock.$sqrt(1000001n, rounding)).to.eventually.equal(1000n);
  493. await expect(this.mock.$sqrt(1002000n, rounding)).to.eventually.equal(1000n);
  494. await expect(this.mock.$sqrt(1002001n, rounding)).to.eventually.equal(1001n);
  495. await expect(this.mock.$sqrt(ethers.MaxUint256, rounding)).to.eventually.equal(
  496. 340282366920938463463374607431768211455n,
  497. );
  498. }
  499. });
  500. it('rounds up', async function () {
  501. for (const rounding of RoundingUp) {
  502. await expect(this.mock.$sqrt(0n, rounding)).to.eventually.equal(0n);
  503. await expect(this.mock.$sqrt(1n, rounding)).to.eventually.equal(1n);
  504. await expect(this.mock.$sqrt(2n, rounding)).to.eventually.equal(2n);
  505. await expect(this.mock.$sqrt(3n, rounding)).to.eventually.equal(2n);
  506. await expect(this.mock.$sqrt(4n, rounding)).to.eventually.equal(2n);
  507. await expect(this.mock.$sqrt(144n, rounding)).to.eventually.equal(12n);
  508. await expect(this.mock.$sqrt(999999n, rounding)).to.eventually.equal(1000n);
  509. await expect(this.mock.$sqrt(1000000n, rounding)).to.eventually.equal(1000n);
  510. await expect(this.mock.$sqrt(1000001n, rounding)).to.eventually.equal(1001n);
  511. await expect(this.mock.$sqrt(1002000n, rounding)).to.eventually.equal(1001n);
  512. await expect(this.mock.$sqrt(1002001n, rounding)).to.eventually.equal(1001n);
  513. await expect(this.mock.$sqrt(ethers.MaxUint256, rounding)).to.eventually.equal(
  514. 340282366920938463463374607431768211456n,
  515. );
  516. }
  517. });
  518. });
  519. describe('log', function () {
  520. describe('log2', function () {
  521. it('rounds down', async function () {
  522. for (const rounding of RoundingDown) {
  523. await expect(this.mock.$log2(0n, rounding)).to.eventually.equal(0n);
  524. await expect(this.mock.$log2(1n, rounding)).to.eventually.equal(0n);
  525. await expect(this.mock.$log2(2n, rounding)).to.eventually.equal(1n);
  526. await expect(this.mock.$log2(3n, rounding)).to.eventually.equal(1n);
  527. await expect(this.mock.$log2(4n, rounding)).to.eventually.equal(2n);
  528. await expect(this.mock.$log2(5n, rounding)).to.eventually.equal(2n);
  529. await expect(this.mock.$log2(6n, rounding)).to.eventually.equal(2n);
  530. await expect(this.mock.$log2(7n, rounding)).to.eventually.equal(2n);
  531. await expect(this.mock.$log2(8n, rounding)).to.eventually.equal(3n);
  532. await expect(this.mock.$log2(9n, rounding)).to.eventually.equal(3n);
  533. await expect(this.mock.$log2(ethers.MaxUint256, rounding)).to.eventually.equal(255n);
  534. }
  535. });
  536. it('rounds up', async function () {
  537. for (const rounding of RoundingUp) {
  538. await expect(this.mock.$log2(0n, rounding)).to.eventually.equal(0n);
  539. await expect(this.mock.$log2(1n, rounding)).to.eventually.equal(0n);
  540. await expect(this.mock.$log2(2n, rounding)).to.eventually.equal(1n);
  541. await expect(this.mock.$log2(3n, rounding)).to.eventually.equal(2n);
  542. await expect(this.mock.$log2(4n, rounding)).to.eventually.equal(2n);
  543. await expect(this.mock.$log2(5n, rounding)).to.eventually.equal(3n);
  544. await expect(this.mock.$log2(6n, rounding)).to.eventually.equal(3n);
  545. await expect(this.mock.$log2(7n, rounding)).to.eventually.equal(3n);
  546. await expect(this.mock.$log2(8n, rounding)).to.eventually.equal(3n);
  547. await expect(this.mock.$log2(9n, rounding)).to.eventually.equal(4n);
  548. await expect(this.mock.$log2(ethers.MaxUint256, rounding)).to.eventually.equal(256n);
  549. }
  550. });
  551. });
  552. describe('log10', function () {
  553. it('rounds down', async function () {
  554. for (const rounding of RoundingDown) {
  555. await expect(this.mock.$log10(0n, rounding)).to.eventually.equal(0n);
  556. await expect(this.mock.$log10(1n, rounding)).to.eventually.equal(0n);
  557. await expect(this.mock.$log10(2n, rounding)).to.eventually.equal(0n);
  558. await expect(this.mock.$log10(9n, rounding)).to.eventually.equal(0n);
  559. await expect(this.mock.$log10(10n, rounding)).to.eventually.equal(1n);
  560. await expect(this.mock.$log10(11n, rounding)).to.eventually.equal(1n);
  561. await expect(this.mock.$log10(99n, rounding)).to.eventually.equal(1n);
  562. await expect(this.mock.$log10(100n, rounding)).to.eventually.equal(2n);
  563. await expect(this.mock.$log10(101n, rounding)).to.eventually.equal(2n);
  564. await expect(this.mock.$log10(999n, rounding)).to.eventually.equal(2n);
  565. await expect(this.mock.$log10(1000n, rounding)).to.eventually.equal(3n);
  566. await expect(this.mock.$log10(1001n, rounding)).to.eventually.equal(3n);
  567. await expect(this.mock.$log10(ethers.MaxUint256, rounding)).to.eventually.equal(77n);
  568. }
  569. });
  570. it('rounds up', async function () {
  571. for (const rounding of RoundingUp) {
  572. await expect(this.mock.$log10(0n, rounding)).to.eventually.equal(0n);
  573. await expect(this.mock.$log10(1n, rounding)).to.eventually.equal(0n);
  574. await expect(this.mock.$log10(2n, rounding)).to.eventually.equal(1n);
  575. await expect(this.mock.$log10(9n, rounding)).to.eventually.equal(1n);
  576. await expect(this.mock.$log10(10n, rounding)).to.eventually.equal(1n);
  577. await expect(this.mock.$log10(11n, rounding)).to.eventually.equal(2n);
  578. await expect(this.mock.$log10(99n, rounding)).to.eventually.equal(2n);
  579. await expect(this.mock.$log10(100n, rounding)).to.eventually.equal(2n);
  580. await expect(this.mock.$log10(101n, rounding)).to.eventually.equal(3n);
  581. await expect(this.mock.$log10(999n, rounding)).to.eventually.equal(3n);
  582. await expect(this.mock.$log10(1000n, rounding)).to.eventually.equal(3n);
  583. await expect(this.mock.$log10(1001n, rounding)).to.eventually.equal(4n);
  584. await expect(this.mock.$log10(ethers.MaxUint256, rounding)).to.eventually.equal(78n);
  585. }
  586. });
  587. });
  588. describe('log256', function () {
  589. it('rounds down', async function () {
  590. for (const rounding of RoundingDown) {
  591. await expect(this.mock.$log256(0n, rounding)).to.eventually.equal(0n);
  592. await expect(this.mock.$log256(1n, rounding)).to.eventually.equal(0n);
  593. await expect(this.mock.$log256(2n, rounding)).to.eventually.equal(0n);
  594. await expect(this.mock.$log256(255n, rounding)).to.eventually.equal(0n);
  595. await expect(this.mock.$log256(256n, rounding)).to.eventually.equal(1n);
  596. await expect(this.mock.$log256(257n, rounding)).to.eventually.equal(1n);
  597. await expect(this.mock.$log256(65535n, rounding)).to.eventually.equal(1n);
  598. await expect(this.mock.$log256(65536n, rounding)).to.eventually.equal(2n);
  599. await expect(this.mock.$log256(65537n, rounding)).to.eventually.equal(2n);
  600. await expect(this.mock.$log256(ethers.MaxUint256, rounding)).to.eventually.equal(31n);
  601. }
  602. });
  603. it('rounds up', async function () {
  604. for (const rounding of RoundingUp) {
  605. await expect(this.mock.$log256(0n, rounding)).to.eventually.equal(0n);
  606. await expect(this.mock.$log256(1n, rounding)).to.eventually.equal(0n);
  607. await expect(this.mock.$log256(2n, rounding)).to.eventually.equal(1n);
  608. await expect(this.mock.$log256(255n, rounding)).to.eventually.equal(1n);
  609. await expect(this.mock.$log256(256n, rounding)).to.eventually.equal(1n);
  610. await expect(this.mock.$log256(257n, rounding)).to.eventually.equal(2n);
  611. await expect(this.mock.$log256(65535n, rounding)).to.eventually.equal(2n);
  612. await expect(this.mock.$log256(65536n, rounding)).to.eventually.equal(2n);
  613. await expect(this.mock.$log256(65537n, rounding)).to.eventually.equal(3n);
  614. await expect(this.mock.$log256(ethers.MaxUint256, rounding)).to.eventually.equal(32n);
  615. }
  616. });
  617. });
  618. });
  619. });