Add Math.modExp and a Panic library (#3298)

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
Co-authored-by: ernestognw <ernestognw@gmail.com>
This commit is contained in:
Mihir Wadekar
2024-02-02 09:40:00 -08:00
committed by GitHub
parent cc431f53e0
commit 192e873fcb
11 changed files with 300 additions and 33 deletions

View File

@ -141,6 +141,24 @@ describe('Math', function () {
});
});
describe('tryModExp', function () {
it('is correctly returning true and calculating modulus', async function () {
const base = 3n;
const exponent = 200n;
const modulus = 50n;
expect(await this.mock.$tryModExp(base, exponent, modulus)).to.deep.equal([true, base ** exponent % modulus]);
});
it('is correctly returning false when modulus is 0', async function () {
const base = 3n;
const exponent = 200n;
const modulus = 0n;
expect(await this.mock.$tryModExp(base, exponent, modulus)).to.deep.equal([false, 0n]);
});
});
describe('max', function () {
it('is correctly detected in both position', async function () {
await testCommutative(this.mock.$max, 1234n, 5678n, max(1234n, 5678n));
@ -222,7 +240,7 @@ describe('Math', function () {
});
});
describe('muldiv', function () {
describe('mulDiv', function () {
it('divide by 0', async function () {
const a = 1n;
const b = 1n;
@ -234,9 +252,8 @@ describe('Math', function () {
const a = 5n;
const b = ethers.MaxUint256;
const c = 2n;
await expect(this.mock.$mulDiv(a, b, c, Rounding.Floor)).to.be.revertedWithCustomError(
this.mock,
'MathOverflowedMulDiv',
await expect(this.mock.$mulDiv(a, b, c, Rounding.Floor)).to.be.revertedWithPanic(
PANIC_CODES.ARITHMETIC_UNDER_OR_OVERFLOW,
);
});
@ -336,6 +353,24 @@ describe('Math', function () {
}
});
describe('modExp', function () {
it('is correctly calculating modulus', async function () {
const base = 3n;
const exponent = 200n;
const modulus = 50n;
expect(await this.mock.$modExp(base, exponent, modulus)).to.equal(base ** exponent % modulus);
});
it('is correctly reverting when modulus is zero', async function () {
const base = 3n;
const exponent = 200n;
const modulus = 0n;
await expect(this.mock.$modExp(base, exponent, modulus)).to.be.revertedWithPanic(PANIC_CODES.DIVISION_BY_ZERO);
});
});
describe('sqrt', function () {
it('rounds down', async function () {
for (const rounding of RoundingDown) {